Monday, February 07, 2005

Steps to Write a Simple OpenGL Program - 2

RenderScene function is called whenever our drawing scene is to be drawn or updated.

The code for RenderScene()

// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color

glClear(GL_COLOR_BUFFER_BIT);

// Enable smooth shading

glShadeModel(GL_SMOOTH);

// Draw the triangle

glBegin(GL_TRIANGLES);

// Red Apex
glColor3ub(255, 0, 0);
glVertex3f(0.0f,200.0f,0.0f);

// Green on the right bottom
corner

glColor3ub(0,255,0);
glVertex3f(200.0f,-70.0f,0.0f);

// Blue on the left bottom corner
glColor3ub(0,0,255);
glVertex3f(-200.0f, -70.0f, 0.0f);

glEnd();

// Flush drawing commands
glutSwapBuffers();
}

Explanation:

(1) The glClear(GL_COLOR_BUFFER_BIT) will clear the color buffer with the color specified with the call to glClearColor(), e.g. glClearColor(0.0, 0.0, 0.0, 1.0) . The color buffer is screen pixel value, clearing it will replace all pixels by the spefied color.

(2) glShadeModel(GL_SMOOTH) determines the shadding model for the objects drawn. A flat shadding model (GL_FLAT) means the polygons, triangles and other shapes take on a single solid color. Whereas a smooth shadding model (GL_SMOOTH) will interpolate the different colors from the RGB color cube from one vertex to the other.

(3) All rendering of primitives are done within the glBegin(GL_TRIANGLES) and glEnd() block. The glBegin() call signals that all the subsequent functions are to used to render an object. The argument of glBegin() tells OpenGL how to interprete the individual vertex. In our case, we are drawing a triangle and 3 vertice will be group as a triangle.

(4) glColor3ub() takes in 3 unsigned byte (8 bit - max value of 255) of Red, Green and Blue component and assign it to the vertex following it.

(5) glVertex3f() sets the vertex in 3D space, x, y, z respectively.

(6) glutSwapBuffers() will set the display to the current buffer and switch the drawing buffer.


0 Comments:

Post a Comment

<< Home