You Like My Tutorials? Click The Banner To Keep 'Em Coming!

glGameDeveloper(): The Game Developer's Source for OpenGL Coding
Main Menu
Latest News
Archived News
Tutorials
Links
E-Mail Me

Other
Message Board
My Guestbook

Tutorials -- Drawing Triangles
 


Drawing Triangles in OpenGL Page 1 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

Drawing Polygons in OpenGL

This 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.

Drawing Triangles in OpenGL Page 1 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

This site best viewed with MS Internet Explorer 5.0 at 16-bit color and at least 800x600.

All Contents of this site are Copyright 1999, 2000 Joe Zimmerman. No part of this site (code, HTML, etc.) may be reproduced without my permission.

Click Here!