| |||||||||||
|
Main Menu Other |
Tutorials -- Drawing Triangles | ||||||||||
|
Drawing Polygons in OpenGLThis tutorial follows the previous tutorial on how to make a basic OpenGL program. If you haven't done that tutorial yet and are just starting with OpenGL, it's best to read that one first. Now we're ready to make our program draw something. Open up your previous project from the last tutorial and go to our Display() function in "main.cpp". First go back up to the top where you have the line '#include "main.h"'. Put a few empty lines after it, and add the following line to it. float rotate; This variable we'll use for rotating our triangle. Now go back down to the Display function and put these lines in it.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (GLfloat) ww/(GLfloat) wh, 1, 700);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat LightAmbient[] = { 0.75f, 0.75f, 0.75f, 1.5f};
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 0.9f};
GLfloat LightSpecular[] = { 0.8f, 0.8f, 0.8f, 1.0f};
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
glColor4f(1, 1, 1, 1);
rotate++;
glTranslatef(0, 0, -10);
glRotatef(rotate, 0, 1, 0);
glBegin(GL_POLYGON);
glNormal3f( 1, 0, 0);
glVertex3f( 0, 1, 0);
glVertex3f(-1, -1, 0);
glVertex3f( 1, -1, 0);
glEnd();
Click the Run button (the exclaimation mark) or hit Ctrl+F5 and click Yes. Once the program runs, you should see a triangle rotating incessantly. If not, check the errors that occur. If you can't figure it out, let me know. Now lets get into each line of code, and why/how it works.
|
|||||||||||