0% found this document useful (0 votes)
714 views

Glut Opengl Cheatsheet

The document provides an overview of OpenGL functions for creating windows and contexts, establishing views and projections, drawing basic and complex graphics primitives, applying transformations, textures, lighting, and other effects. It also includes formulas and examples for common projection matrices used to map 3D scenes to 2D views.

Uploaded by

Ohforf Ucksake
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
714 views

Glut Opengl Cheatsheet

The document provides an overview of OpenGL functions for creating windows and contexts, establishing views and projections, drawing basic and complex graphics primitives, applying transformations, textures, lighting, and other effects. It also includes formulas and examples for common projection matrices used to map 3D scenes to 2D views.

Uploaded by

Ohforf Ucksake
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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); 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); Callback and Related Functions glutReshapeFunc (myReshape); glutDisplayFunc (myDisplay); glutIdleFunc (spinDisplay); glutMouseFunc (mouse); glutKeyboardFunc (keyboard); glutMainLoop ( ); glutPostRedisplay ( ); 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); Double Buffering glutInitDisplayMode (GLUT_DOUBLE | . . .); glutSwapBuffers( );

Depth Testing glutInitDisplayMode (GLUT_DEPTH | . . .); glEnable (GL_DEPTH_TEST); glClear (GL_DEPTH_BUFFER_BIT | . . .);

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); 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); 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)


1 0 0 0 0 1 0 0 0 0 1 0 xt yt zt 1

Rotatef( , 0.0, 0.0, 1.0)


Scalef(sx, sy, sz)


0 0 0 1 sx 0 0 0 0 sy 0 0 0 0 sz 0 0 0 0 1

cos( )

-sin( )

0 0 1 0

sin( ) 0 0

cos( ) 0 0

There are similar rotation matrices about the X and Y axes.

Projection Matrices Simple Orthogonal


1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0

Simple Perspective
1 0 0 0 0 1 0 0 0 0 1 1/d 0 0 0 0

Orthogonal Projection to Canonical View Volume


2/(Xmax-Xmin) 0 0 0 0 2/(Ymax-Ymin) 0 0 0 0 2/(Zmax-Zmin) 0 -(Xmax+Xmin)/(Xmax-Xmin) -(Ymax+Ymin)/(Ymax-Ymin) -(Zmax+Zmin)/(Zmax-Zmin) 1

Perspective Normalization Matrix (assumes right projection and fovy of 90 degrees)


1 0 0 0 0 1 0 0 0 0 -1 0 0 0 = (Zmax + Zmin)/(Zmax - Zmin) = (2*Zmax*Zmin)/(Zmax - Zmin)

General Perspective Projection Matrix


2*Zmin /(Xmax-Xmin) 0 0 0 0 2*Zmin/ (Ymax-Ymin) 0 0 (Xmax+Xmin)/(Xmax-Xmin) (Ymax+Ymin)/(Ymax-Ymin) -(Zmax+Zmin)/(Zmax-Zmin) -1 0 0 -2*Zmax*Zmin/(Zmax-Zmin) 0

You might also like