- first edition 2018-8-28
OpenGL API
Drawing Primitives
- glBegin(Glenum mode)
- mode: GL_TRIANGLES, GL_QUADS, etc
- glVertex3f(GLfloat x,GLfloat y,GLfloat z)
- Draw one vertex; must be after glBegin();
- glEnd()
- End of a group of primitives
- glColor3f(GLbyte red, GLbyte blue,GLbyte green)
- Set the color for vertices drawn after the call
- glNormal3f(GLfloat nx,GL float ny, GLfloat nz)
- Set the normal vector
- glTexCoord2f(GLfloat s,GLfloat v)
- Set the texture coordinates
- glMatrixMode(GLenum mode)
- Set the current matrix
- mode: GL_MODELVIEW, GL_PROJECTION
- glLoadIdentity()
- Loads the identity matrix into the current matrix
- glLoadMatrixf(Glfloat *matrix)
- Load and arbitrary matrix
- matrix: An array of values in column-major form; elements from same column are contiguous
- glGetFloatv(GL_MODELVIEW_MATRIX,matrix)
- glGetFloatv(GL_PROJECTION_MATRIX,matrix)
- Gets a matrix- use sparingly
Transformations
- glFrustum(GLfloat left,GLfloat right,GLfloat bottom,GLfloat top,GLfloat near,GLfloat far)
- Multiplies the current matrix by a perspective matrix
- Parameters set up a viewing frustum( clipped pyramid ) representing the visible space
- glOrtho(Glfloat left,Glfloat right,GLfloat bottom,GLfloat top,GLfloat near,Glfloat far)
- Similar to glFrustum(), but sets up an orthographic projection instead
Matrix Stack
- glPushMatix()
- Pushes a copy of the current matrix down on the matrix stack
- There is one matrix stack per mode; GL_PROJECTION and GL_MODEVIEW each have their own stack
- glPopMatrix()
- Replaces the current matrix with the top of the stack, and then pops the stack
Lighting
- glEnable(GL_LIGHTING)
- Enable lighting
- glEnable(GL_LIGHTX)
- Enable lightX
- glLightfv(GL_LIGHo,GL_AMBIENT,GLfloat *params)
- glLightfv(GL_LIGHo,GL_SPECULAR,GLfloat *params)
- glLightfv(GL_LIGHo,GL_DIFFUSE,GLfloat *params)
- sets the ambient,diffuse,specular light for the light #o
- params: 4-vector:[red,green,blue,alpha]
- glLightfv(GL_LIGHTo,GL_POSITION,GLfloat *params)
- Set the light position or direction
- params: 4-vector:[x,y,z,point?]; if last argument is non-zero the light is a point light.
- glMaterialfv(GL_FRONT,GL_AMBIENT,GLfloat *params)
- glMaterialfv(GL_FRONT,GL_SPECULAR,GLfloat *params)
- glMaterialfv(GL_FRONT,GL_DIFFUSE,GLfloat *params)
- sets the ambient, diffuse, and specular material colors
- params: 4-vector: [red,blue,green,alpha]
- glMaterialf(GL_FRONT,GL_SHININESS,Glfloat params)
- sets the hardness/shininess(size of specular highlights)
- param: shininess in the range[0,128]
Other
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
- Must call at the beginning of each frame
- glEnable(GL_DEPTH_TEST)
- Enable depth-testing using depth buffer
- These functions let you draw from arrays, rather than using glColor,glVertex,etc.
- glEnableClientState(GL_VERTEX_ARRAY)
- glEnableClientState(GL_NORMAL_ARRAY)
- glEnableClientState(GL_COLOR_ARRAY)
- glVertexPointer()
- glNormalPoint();
- glColorPointer();
- glDrawArrays();