CSCI 420. Computer Graphics. OpenGL Cheat Sheet .... Perspective Normalization Matrix (assumes right projection and fovy
CSCI 420 Computer Graphics OpenGL Cheat Sheet Create a Window glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition (0, 0); glutCreateWindow (win_name); Clear a Window glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT);
Double Buffering glutInitDisplayMode (GLUT_DOUBLE | . . .); glutSwapBuffers( );
Depth Testing glutInitDisplayMode (GLUT_DEPTH | . . .); glEnable (GL_DEPTH_TEST); glClear (GL_DEPTH_BUFFER_BIT | . . .);
Establish a Viewport and a Clipping Region glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity( ); glOrtho (-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW);
Projections and Locating the Camera glOrtho (-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glFrustum ( left, right, bottom, top, near, far); gluPerspective(fovy, (GLfloat) w/(GLfloat) h, near, far); gluLookAt (eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
Callback and Related Functions glutReshapeFunc (myReshape); glutDisplayFunc (myDisplay); glutIdleFunc (spinDisplay); glutMouseFunc (mouse); glutKeyboardFunc (keyboard); glutMainLoop ( ); glutPostRedisplay ( );
Lighting and Materials glLightf (source, parameter, value); glLightfv (source, parameter, pointer to array); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
Move Around glPushMatrix( ); glPopMatrix( ); glLoadIdentity ( ); glRotatef(spin, 0.0, 0.0, 1.0); glTranslatef(3.0, 2.0, 0.0); glScalef(1.0, 2.0, 1.0); Menus void color_menu(int id) { if(id == 1) {r = 1.0; g = 0.0; b = 0.0;}. . . c_menu = glutCreateMenu(color_menu); glutAddMenuEntry("Red",1); glutAttachMenu(GLUT_RIGHT_BUTTON); glutAddSubMenu("Colors", c_menu); Text glRasterPos2i(textX, textY); glutBitmapCharacter (GLUT_BITMAP_8_BY_13, ch); Functions to Draw glColor3f (red, green, blue); glBegin (GL_TRIANGLES); glVertex3f(3.0, 4.0, 0.0); glEnd(); // Alternatively glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer (3, GL_FLOAT, 0, vertices); glColorPointer (3, GL_FLOAT, 0, carray); glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndex);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
Blending and Antialiasing glEnable(GL_BLEND); glBlendFunc (source_factor, destination_factor); glDepthMask (GL_FALSE); // sets the depth buffer to read // only glEnable (GL_POINT_SMOOTH); glEnable (GL_LINE_SMOOTH); glEnable (GL_SMOOTHING); Bezier Curves and Surfaces glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]); glEnable(GL_MAP1_VERTEX_3); glEvalCoord1f((GLfloat) i/30.0); glMapGrid1f (100, 0.0, 1.0); glEvalMesh1(GL_LINE, 0, 100); Textures glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, 3, checkImageWidth, checkImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, &checkImage[0][0][0]); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glEnable(GL_TEXTURE_2D); glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
3D Transformations Translatef (xt, yt, zt)
Rotatef( , 0.0, 0.0, 1.0)
Scalef(sx, sy, sz)
1
0
0
xt
cos( )
-sin( )
0
0
sx
0
0
0
0
1
0
yt
sin( )
cos( )
0
0
0
sy
0
0
0
0
1
zt
0
0
1
0
0
0
sz
0
0
0
0
1
0
0
0
1
0
0
0
1
There are similar rotation matrices about the X and Y axes.
Projection Matrices Simple Orthogonal
Simple Perspective
1
0
0
0
1
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1/d
0
Orthogonal Projection to Canonical View Volume 2/(Xmax-Xmin)
0
0
-(Xmax+Xmin)/(Xmax-Xmin)
0
2/(Ymax-Ymin)
0
-(Ymax+Ymin)/(Ymax-Ymin)
0
0
2/(Zmax-Zmin)
-(Zmax+Zmin)/(Zmax-Zmin)
0
0
0
1
Perspective Normalization Matrix (assumes right projection and fovy of 90 degrees) 1
0
0
0
0
1
0
0
α = (Zmax + Zmin)/(Zmax - Zmin)
0
0
α
β
β = (2*Zmax*Zmin)/(Zmax - Zmin)
0
0
-1
0
General Perspective Projection Matrix 2*Zmin /(Xmax-Xmin)
0
(Xmax+Xmin)/(Xmax-Xmin)
0
0
2*Zmin/ (Ymax-Ymin)
(Ymax+Ymin)/(Ymax-Ymin)
0
0
0
-(Zmax+Zmin)/(Zmax-Zmin)
-2*Zmax*Zmin/(Zmax-Zmin)
0
0
-1
0