Getting Started With Opengl: Woo, Neider Et Al., Chapter 1
Getting Started With Opengl: Woo, Neider Et Al., Chapter 1
Example:
glutInitDisplayMode(GLUT_RGB);
glColor3f(1.0, 1.0, 1.0);
void display(void) {
glClear (GL_COLOR_BUFFER_BIT); /* clear all pixels */
/* draw white polygon with corners at (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
/* Declare initial window size, position, and display mode. Open window with
"hello in its title bar. Call initialization routines. Register callback function
to display graphics. Enter main loop and process events. */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Handling Input Events
glutReshapeFunction()
To register the function that should be called when
resizing the window
glutKeyboardFunc(), glutMouseFunc()
To register the functions called to handle keyboard
and mouse events
glutMotionFunc()
To register the function that handles the mouse
movement when a button pressed
Handling Input Events - Example
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: /* ASCII code of the ESC key */
exit(0);
break;
}
}
/* Main Loop */
int main(int argc, char** argv) {
..
glutKeyboardFunc (keyboard);
..
}
Double Buffering
#include <GL/glut.h>
#include <stdlib.h>
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
Double Buffering
void spinDisplay(void) {
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}