OpenGL:Tutorials:Tutorial Framework:First Polygon

From GPWiki
Jump to: navigation, search

Here's a quick primer for OpenGL, how to setup a perspective view and render your first polygon.


Setting up the screen

We 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);


Next we need to specify the perspective projection.

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();


If you do not have the GLU Library: You can specify a perspective projection by directly using the glFrustum function:

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.

  • The field of view angle, here set to 45 degrees.
  • The aspect ratio of the view, this will normally be ScreenResX / ScreenResY.
  • The near and far clipping planes. Both these values should be positive.


A frustum is kind of like a trapezoid shape that indicates what falls within the view of the camera. We are specifying the same frustum here as the one in the above gluPerspective function. The first four parameters indicate how pointy the trapizoid is. They give the left, right, bottom and top of the rectangle that you would get if you chopped off the top of a pyramid at the near clipping plane (5th parameter to the function). Here the width of the pyramid is 1 at that point, and the height is 3/4 - based on the aspect ratio of the screen (here 800x600). The last two parameters give the near and far clip planes, just like in gluPerspective.


Now the screen is set up, we select the modelview matrix which we'll manipulate to display our renderings.

  glMatrixMode(GL_MODELVIEW);

Drawing our polygon

Before 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)


We call glTranslatef() to move the view 'out' a little, away from our soon to be drawn polygon. Then we apply a rotation using glRotatef():

  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.
The parameters to glRotatef() are a rotation value and an axis vector.


Finally, we can draw our polygon:

  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.


Last of all, we increase the rotation angle for the next frame and swap the buffers so that we can see our work.

  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!

Source Code

The 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();
    }
 
 }