5.25.2006

My Second OpenGL Program

/*

*/

#include <GLUT/glut.h>

void display(void);
void reshape(int width, int height);
void init(void);

int main(int argc, char **argv) {
glutInit(&argc, argv); // Initialize opengl
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(320, 240);
glutCreateWindow("Book Example 2");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

void display(void) {
// clear pixels with the clear color
glClear(GL_COLOR_BUFFER_BIT);
// draw the four points, in four different colors
glBegin(GL_POINTS); // We are using the GL_POINTS primitive
glColor3f(0.0,1.0,0.0); // green
glVertex2f(10,10); // Color3f: 3 arguments, floating point
glColor3f(1.0, 1.0, 0.0); // yellow
glVertex2f(10,110); // Vertex2f: 2 arguments, floating point
glColor3f(0.0,0.0,1.0); // blue
glVertex2f(150,110);
glColor3f(1.0,1.0,1.0); // white
glVertex2f(150,10);
// test below
glColor3f(1.0,0.6,0.7); // fleshy pink color
glVertex2i(25,25);
glEnd();

// begin flushing OpenGL calls to display buffer
glFlush();
}

void reshape(int width, int height) {
// on reshape AND startup, make the viewport the size of the entire window
glViewport(0,0,(GLsizei) width, (GLsizei) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// keep our world coordinate system constant, though
gluOrtho2D(0.0,160.0,0.0,120.0);
}

void init(void) {
glClearColor(0.0,0.6,0.5,1.0);
// set the points to be 5 pixels big
glPointSize(5.0);
}