mirror of
https://github.com/SFML/SFML.git
synced 2024-11-25 04:41:05 +08:00
Improved OpenGL and X11 rotating cube examples.
This commit is contained in:
parent
e2420dfe76
commit
650e792350
@ -36,6 +36,10 @@ void initialize(sf::Window& window)
|
|||||||
static const double pi = 3.141592654;
|
static const double pi = 3.141592654;
|
||||||
GLdouble extent = std::tan(90.0 * pi / 360.0);
|
GLdouble extent = std::tan(90.0 * pi / 360.0);
|
||||||
glFrustum(-extent, extent, -extent, extent, 1.0, 500.0);
|
glFrustum(-extent, extent, -extent, extent, 1.0, 500.0);
|
||||||
|
|
||||||
|
// Enable position and texture coordinates vertex components
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
@ -62,46 +66,57 @@ void draw(sf::Window& window, float elapsedTime)
|
|||||||
glRotatef(elapsedTime * 6.f, 0.f, 1.f, 0.f);
|
glRotatef(elapsedTime * 6.f, 0.f, 1.f, 0.f);
|
||||||
glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f);
|
glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f);
|
||||||
|
|
||||||
// Draw a cube
|
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
||||||
glBegin(GL_QUADS);
|
static const GLfloat cube[] =
|
||||||
|
{
|
||||||
|
// positions // colors
|
||||||
|
-50, -50, -50, 1, 1, 0,
|
||||||
|
-50, 50, -50, 1, 1, 0,
|
||||||
|
-50, -50, 50, 1, 1, 0,
|
||||||
|
-50, -50, 50, 1, 1, 0,
|
||||||
|
-50, 50, -50, 1, 1, 0,
|
||||||
|
-50, 50, 50, 1, 1, 0,
|
||||||
|
|
||||||
glColor3f(1.f, 1.f, 0.f);
|
50, -50, -50, 1, 1, 0,
|
||||||
glVertex3f(-50.f, -50.f, -50.f);
|
50, 50, -50, 1, 1, 0,
|
||||||
glVertex3f(-50.f, 50.f, -50.f);
|
50, -50, 50, 1, 1, 0,
|
||||||
glVertex3f( 50.f, 50.f, -50.f);
|
50, -50, 50, 1, 1, 0,
|
||||||
glVertex3f( 50.f, -50.f, -50.f);
|
50, 50, -50, 1, 1, 0,
|
||||||
|
50, 50, 50, 1, 1, 0,
|
||||||
|
|
||||||
glColor3f(1.f, 1.f, 0.f);
|
-50, -50, -50, 1, 0, 1,
|
||||||
glVertex3f(-50.f, -50.f, 50.f);
|
50, -50, -50, 1, 0, 1,
|
||||||
glVertex3f(-50.f, 50.f, 50.f);
|
-50, -50, 50, 1, 0, 1,
|
||||||
glVertex3f( 50.f, 50.f, 50.f);
|
-50, -50, 50, 1, 0, 1,
|
||||||
glVertex3f( 50.f, -50.f, 50.f);
|
50, -50, -50, 1, 0, 1,
|
||||||
|
50, -50, 50, 1, 0, 1,
|
||||||
|
|
||||||
glColor3f(0.f, 1.f, 1.f);
|
-50, 50, -50, 1, 0, 1,
|
||||||
glVertex3f(-50.f, -50.f, -50.f);
|
50, 50, -50, 1, 0, 1,
|
||||||
glVertex3f(-50.f, 50.f, -50.f);
|
-50, 50, 50, 1, 0, 1,
|
||||||
glVertex3f(-50.f, 50.f, 50.f);
|
-50, 50, 50, 1, 0, 1,
|
||||||
glVertex3f(-50.f, -50.f, 50.f);
|
50, 50, -50, 1, 0, 1,
|
||||||
|
50, 50, 50, 1, 0, 1,
|
||||||
|
|
||||||
glColor3f(0.f, 1.f, 1.f);
|
-50, -50, -50, 0, 1, 1,
|
||||||
glVertex3f(50.f, -50.f, -50.f);
|
50, -50, -50, 0, 1, 1,
|
||||||
glVertex3f(50.f, 50.f, -50.f);
|
-50, 50, -50, 0, 1, 1,
|
||||||
glVertex3f(50.f, 50.f, 50.f);
|
-50, 50, -50, 0, 1, 1,
|
||||||
glVertex3f(50.f, -50.f, 50.f);
|
50, -50, -50, 0, 1, 1,
|
||||||
|
50, 50, -50, 0, 1, 1,
|
||||||
|
|
||||||
glColor3f(1.f, 0.f, 1.f);
|
-50, -50, 50, 0, 1, 1,
|
||||||
glVertex3f(-50.f, -50.f, 50.f);
|
50, -50, 50, 0, 1, 1,
|
||||||
glVertex3f(-50.f, -50.f, -50.f);
|
-50, 50, 50, 0, 1, 1,
|
||||||
glVertex3f( 50.f, -50.f, -50.f);
|
-50, 50, 50, 0, 1, 1,
|
||||||
glVertex3f( 50.f, -50.f, 50.f);
|
50, -50, 50, 0, 1, 1,
|
||||||
|
50, 50, 50, 0, 1, 1
|
||||||
|
};
|
||||||
|
|
||||||
glColor3f(1.f, 0.f, 1.f);
|
// Draw the cube
|
||||||
glVertex3f(-50.f, 50.f, 50.f);
|
glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube);
|
||||||
glVertex3f(-50.f, 50.f, -50.f);
|
glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube + 3);
|
||||||
glVertex3f( 50.f, 50.f, -50.f);
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
glVertex3f( 50.f, 50.f, 50.f);
|
|
||||||
|
|
||||||
glEnd();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ int main()
|
|||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
|
||||||
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
||||||
GLfloat cube[] =
|
static const GLfloat cube[] =
|
||||||
{
|
{
|
||||||
// positions // texture coordinates
|
// positions // texture coordinates
|
||||||
-20, -20, -20, 0, 0,
|
-20, -20, -20, 0, 0,
|
||||||
|
Loading…
Reference in New Issue
Block a user