OpenGL:Tutorials:Tutorial Framework:First Polygon
From GPWiki(Redirected from OpenGL Tutorial Framework:First Polygon)
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello. Here's a quick primer for OpenGL, how to setup a perspective view and render your first polygon.
[edit] Setting up the screenWe use glViewport() to tell OpenGL how much of the current screen to use. Generally we want to use the whole screen so we use something like: glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
GLfloat view_height = 600.f / 800.f; glFrustum(.5f, -.5f, -.5f * view_height, .5f * view_height, 1.f, 500.f); Or, if you do not have the GLU Library: you can mimic the gluPerspective function alltogether: #include <cmath> // header for math calculations GLfloat view_height = 600; // perhaps pulled from OS screen resolution GLfloat view_width = 800; // these would indicate aspect void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zmin, GLfloat zmax) { GLfloat xmin, xmax, ymin, ymax; ymax = zMin * tan(fovy * M_PI / 360.0); ymin = -ymax; xmin = ymin * aspect; xmax = ymax * aspect; glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); } gluPerspective(45.0f, GLfloat(view_width/view_height), 1.0f, 500.0f); If you do have the GLU Library you will only need this: gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f); In any event, we have selected the projection matrix, and reset it. The call to gluPerspective() has 4 parameters.
glMatrixMode(GL_MODELVIEW); [edit] Drawing our polygonBefore we do anything, we'll clear away the screen and depth buffer: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); You can change the color that the screen is cleared to (defaults to black) with glClearColor. This will give a pretty pink background (the arguments are red, green, blue, alpha, use 1 for alpha if you don't know what it does yet): glClearColor(1.f, 0.f, 1.f, 1.f); Now we can reset the modelview matrix: glLoadIdentity(); // Reset current matrix (Modelview)
glTranslatef(0.0f,0.0f,-5.0f); glRotatef(Rotate,0.0f,0.0f,1.0f); The parameters to glTranslatef() are pretty straight forward. They are an x,y and z offset from the current position.
glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.5f,0.0f); glVertex3f(-1.0f,-1.0f,0.0f); glVertex3f( 1.0f,-1.0f,0.0f); glEnd(); glBegin(GL_TRIANGLES) treats each set of three vertices specified by glVertex() as a separate triangle. There are many modes for glBegin() to draw points, lines, triangle strips or fans, quads and irregular polygons. The parameters of glVertex() are simply x, y and z coordinates specifying a point in our 3D world.
Rotate+=0.05f; FlipBuffers();
When you run this example, you should see a rotating white triangle on a black background. If you can stand the excitement, stay tuned and we'll show you how to add colour! [edit] Source CodeThe source to Render.cpp, compile this demo using the OpenGL Tutorial Framework #include "Framework.h" void Render(void) { float Rotate=0.0f; // A rotation value to be used to spin our polygon glClearColor(0,0,0,0); // Setup our screen glViewport(0,0,800,600); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,800.0f/600.0f,1.0f,500.0f); glMatrixMode(GL_MODELVIEW); // This loop will run until Esc is pressed while(RunLevel) { if(Keys[VK_ESCAPE]) // Esc Key RunLevel=0; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // Reset current matrix (Modelview) glTranslatef(0.0f,0.0f,-5.0f); glRotatef(Rotate,0.0f,0.0f,1.0f); glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.5f,0.0f); glVertex3f(-1.0f,-1.0f,0.0f); glVertex3f( 1.0f,-1.0f,0.0f); glEnd(); Rotate+=0.05f; FlipBuffers(); } } |


