Draw a Simple Object
Example 1/4 #include “ glut.h ” void display(); void reshape(GLsizei w, GLsizei h); void main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(250, 250); glutInitWindowPosition(100, 100); glutCreateWindow("Drawing sample"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); }
Example 2/4 void reshape(GLsizei w, GLsizei h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
Example 3/4 void display(){ glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3d(1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glColor3d(1.0f, 0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glColor3d(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glColor3d(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); glFlush(); }
Example 4/4
Data Type
Clear the buffers 1/2 void glClearColor(GLclampf, GLclampf, GLclampf, GLclampf ) Set the current values for use in cleaning color buffers in RGBA mode. void glClearDepth(GLclampd) Set the current values for use in cleaning depth buffer.
Clear the buffers 2/2 void glClear(GLbitfield) Clear the specified buffers to their current clearing values. GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT GL_ACCUM_BUFFER_BIT GL_STENCIL_BUFFER_BIT
Color representation RGBA: red, green, blue, alpha Each channel has intensity from 0.0~1.0 Values outside this interval will be clamp to 0.0 or 1.0 Alpha is used in blending and transparency Specify a color: glColor{34}{sifd}[v]( … )
Points, Lines and Polygons 1/4 Describe points, lines, polygons void glBegin(GLenum) Marks the beginning of a vertex-data list The mode can be any of the values in next page void glEnd() Marks the end of a vertex-data list
Points, Lines and Polygons 2/4
Points, Lines and Polygons 3/4
Points, Lines and Polygons 4/4 Specifying the vertices glVertex{234}{sifd}[v]( … ) Specifies a vertex for use in describing a geometric object Can only effective between a glBegin() and glEnd() pair
Completion of drawing 1/2 void glFlush() Forces previously issued OpenGL commands to begin execution. void glFinish() Forces previous issued OpenGL commands to complete.
Completion of drawing 2/2 void glutSwapBuffers() Swap front and back buffers.