From 650e792350769bd311142147b777e5b9c316c326 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Mon, 12 Jan 2015 18:15:46 +0100 Subject: [PATCH] Improved OpenGL and X11 rotating cube examples. --- examples/X11/X11.cpp | 83 ++++++++++++++++++++++---------------- examples/opengl/OpenGL.cpp | 2 +- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/examples/X11/X11.cpp b/examples/X11/X11.cpp index 2c2b3f986..2c2419b75 100644 --- a/examples/X11/X11.cpp +++ b/examples/X11/X11.cpp @@ -36,6 +36,10 @@ void initialize(sf::Window& window) static const double pi = 3.141592654; GLdouble extent = std::tan(90.0 * pi / 360.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 * 18.f, 0.f, 0.f, 1.f); - // Draw a cube - glBegin(GL_QUADS); + // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) + 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); - glVertex3f(-50.f, -50.f, -50.f); - glVertex3f(-50.f, 50.f, -50.f); - glVertex3f( 50.f, 50.f, -50.f); - glVertex3f( 50.f, -50.f, -50.f); + 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); - glVertex3f(-50.f, -50.f, 50.f); - glVertex3f(-50.f, 50.f, 50.f); - glVertex3f( 50.f, 50.f, 50.f); - glVertex3f( 50.f, -50.f, 50.f); + -50, -50, -50, 1, 0, 1, + 50, -50, -50, 1, 0, 1, + -50, -50, 50, 1, 0, 1, + -50, -50, 50, 1, 0, 1, + 50, -50, -50, 1, 0, 1, + 50, -50, 50, 1, 0, 1, - glColor3f(0.f, 1.f, 1.f); - glVertex3f(-50.f, -50.f, -50.f); - glVertex3f(-50.f, 50.f, -50.f); - glVertex3f(-50.f, 50.f, 50.f); - glVertex3f(-50.f, -50.f, 50.f); + -50, 50, -50, 1, 0, 1, + 50, 50, -50, 1, 0, 1, + -50, 50, 50, 1, 0, 1, + -50, 50, 50, 1, 0, 1, + 50, 50, -50, 1, 0, 1, + 50, 50, 50, 1, 0, 1, - glColor3f(0.f, 1.f, 1.f); - glVertex3f(50.f, -50.f, -50.f); - glVertex3f(50.f, 50.f, -50.f); - glVertex3f(50.f, 50.f, 50.f); - glVertex3f(50.f, -50.f, 50.f); + -50, -50, -50, 0, 1, 1, + 50, -50, -50, 0, 1, 1, + -50, 50, -50, 0, 1, 1, + -50, 50, -50, 0, 1, 1, + 50, -50, -50, 0, 1, 1, + 50, 50, -50, 0, 1, 1, - glColor3f(1.f, 0.f, 1.f); - glVertex3f(-50.f, -50.f, 50.f); - glVertex3f(-50.f, -50.f, -50.f); - glVertex3f( 50.f, -50.f, -50.f); - glVertex3f( 50.f, -50.f, 50.f); + -50, -50, 50, 0, 1, 1, + 50, -50, 50, 0, 1, 1, + -50, 50, 50, 0, 1, 1, + -50, 50, 50, 0, 1, 1, + 50, -50, 50, 0, 1, 1, + 50, 50, 50, 0, 1, 1 + }; - glColor3f(1.f, 0.f, 1.f); - glVertex3f(-50.f, 50.f, 50.f); - glVertex3f(-50.f, 50.f, -50.f); - glVertex3f( 50.f, 50.f, -50.f); - glVertex3f( 50.f, 50.f, 50.f); - - glEnd(); + // Draw the cube + glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube); + glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube + 3); + glDrawArrays(GL_TRIANGLES, 0, 36); } diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index 2df59386c..c2d16978f 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -80,7 +80,7 @@ int main() glBindTexture(GL_TEXTURE_2D, texture); // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) - GLfloat cube[] = + static const GLfloat cube[] = { // positions // texture coordinates -20, -20, -20, 0, 0,