Title: Chap 2 Write a Simple OpenGL Program
1Chap 2 Write a Simple OpenGL Program
2Preparing 1/2
- environmentMicrosoft Visual C 6.0?Microsoft
Visual C .Net - Also needGLUT
- http//www.xmission.com/nate/glut.html
3Preparing 2/2
- On Microsoft Visual C 6.0
- Put glut.h into ltMSVCgt/include/GL/
- Put glut.lib into ltMSVCgt/lib/
- Put glut32.dll into ltwindowgt/System32/
- On Microsoft Visual C .Net
- Put glut.h into ltMSVCgt/platformSDK/include/GL/
- Put glut.lib into ltMSVCgt/platformSDK/lib/
- Put glut32.dll into ltwindowgt/System32/
4Books
- OpenGL Programming Guide (Red-book)
- OpenGL Reference Manual (Blue-book)
- OpenGL SuperBible
5OpenGL Utility Toolkit (GLUT)
- A window system-independent toolkit to hide the
complexity of differing window system APIs. - Providing following operations
- Initializing and creating window
- Handling window and input events
- Drawing basic 3D objects
- Running the program
- Use the prefix of glut (ex glutCreateWindow)
6Write an OpenGL Program 1/7
- Microsoft Visual C 6.0
- Step 1 create a Win32 Console Application project
7Write an OpenGL Program 2/7
- Step 2Press Alt-F7,brings Project
Settings,select Link
8Write an OpenGL Program 3/7
- Step 3add opengl32.lib glu32.lib glut32.lib
into Object/library modules - Step 4write your code
- Step 5compile
9Write an OpenGL Program 4/7
- Microsoft Visual C .Net
- Step 1create Win32 console project
10Write an OpenGL Program 5/7
- Step 2 check empty project
11Write an OpenGL Program 6/7
- Step 3select project / property / linker /
input - Step 4 add opengl32.lib glu32.lib glut32.lib
12Write an OpenGL Program 7/7
- Step 5 add a new C source file
- Step 6write your code
- Step 7compile
13A simple OpenGL program 1/2
- includeltGL/glut.hgt
- void GL_display()
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
- glClear(GL_COLOR_BUFFER_BIT)
- glColor3f(1.0f, 1.0f, 1.0f)
- glutSolidCube(1.0)
- glFlush()
-
- void GL_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()
-
14A simple OpenGL program 2/2
- void main(int argc, char argv)
- glutInit(argc, argv)
- glutInitDisplayMode(GLUT_SINGLE GLUT_RGB)
- glutCreateWindow("sample")
- glutDisplayFunc(GL_display)
- glutReshapeFunc(GL_reshape)
- glutMainLoop()
15OpenGL command Syntax 1/2
- OpenGL commands use the prefix gl and initial
capital letters for each word ex glClearColor - OpenGL defined constants begin with GL_ , use all
capital letters and underscores to separate words
ex GL_COLOR_BUFFER_BIT
16OpenGL command Syntax 2/2
17State management 1/2
- OpenGL is a state machine
- You put it into various states (or modes) that
then remain in effect until you change them. - Each state variable or mode has a default value,
and at any point can query the system for each
variables current value.
18State management 2/2
- glEnable(GLenum), glDisable(GLenum)
- Enable and disable some state.
- http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc01_3l5x.asp - glIsEnable(GLenum)
- Query if the specific state is enabled.
- http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc03_7pgk.asp - glGetBooleanv(), glGetIntegerv(), glGetFloatv(),
glGetDoublev(), glGetPointerv() - Query the specific state value
- http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc02_5ub8.asp
19Program detail 1/5
- Initializing and creating a window
- void glutInit(int , char)
- Initializing the GLUT library.
- Should be called before any other GLUT routine.
- http//www.opengl.org/resources/libraries/glut/spe
c3/node10.html - void glutInitDisplayMode(unsigned int)
- Specify a display mode for windows created.
- GLUT_RGB / GLUT_RGBA / GLUT_INDEX
- GLUT_SINGLE / GLUT_DOUBLE
- GLUT_DEPTH / GLUT_STENCIL / GLUT_ACCUM
- http//www.opengl.org/resources/libraries/glut/spe
c3/node12.html
20Program detail 2/5
- void glutInitWindowPosition(int, int)
- void glutInitWindowSize(int, int)
- Initializing the window position and size when
created. - http//www.opengl.org/resources/libraries/glut/spe
c3/node11.html - int glutCreateWindow(char)
- Open a window with previous settings.
- http//www.opengl.org/resources/libraries/glut/spe
c3/node16.html383
21Program detail 3/5
- Handling window and input events
- void glutDisplayFunc(void (func)(void))
- Called whenever the contents of the windows need
to be redrawn. - Put whatever you wish to draw on screen here.
- Use glutPostRedisplay() to manually ask GLUT to
recall this display function. - http//www.opengl.org/resources/libraries/glut/spe
c3/node46.html - void glutReshapeFunc(void (func)(void))
- Called whenever the window is resized or moved.
- You should always call glViewport() here to
resize your viewport. - http//www.opengl.org/resources/libraries/glut/spe
c3/node48.html
22Program detail 4/5
- Other call back functions
- void glutKeyboardFunc(void (func)(unsigned char
key, int x, int y)) - http//www.opengl.org/resources/libraries/glut/spe
c3/node49.html - void glutMouseFunc(void (func)(int button, int
state, int x, int y)) - http//www.opengl.org/resources/libraries/glut/spe
c3/node50.html - void glutMotionFunc(void (func)(int x, int y))
- http//www.opengl.org/resources/libraries/glut/spe
c3/node51.html - void glutIdleFunc(void (func)(void))
- http//www.opengl.org/resources/libraries/glut/spe
c3/node63.html - See OpenGL Programming Guide Appendix D for more
detail.
23Program detail 5/5
- Running the program
- void glutMainLoop(void)
- Enter the GLUT processing loop and never return.
24GLUT objects
- GLUT provides the follow objects
- Sphere, cube, torus, icosahedron, ocrtahedron,
tetrahedron, teapot, dodecahedron, cone. - Both wireframe and solid
- Ex glutSolidSphere(1.0, 24, 24)
- Ex glutWireCube(1.0)
- http//www.opengl.org/resources/libraries/glut/spe
c3/node80.htmlSECTION000120000000000000000
25OpenGL program -2 1/2
- include ltGL/glut.hgt
- void GL_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()
26OpenGL program -2 2/2
- void GL_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()
-
- void main(int argc, char argv)
- glutInit(argc, argv)
- glutInitDisplayMode(GLUT_SINGLE GLUT_RGB)
- glutInitWindowSize(250, 250)
- glutInitWindowPosition(100, 100)
- glutCreateWindow("Drawing sample")
- glutDisplayFunc(GL_display)
- glutReshapeFunc(GL_reshape)
- glutMainLoop()
-
27Data type
28Clear the buffers 1/2
- void glClearColor(GLclampf, GLclampf, GLclampf,
GLclampf ) - Set the current values for use in cleaning color
buffers in RGBA mode. - http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc01_0rhu.asp - void glClearDepth(GLclampd)
- Set the current values for use in cleaning depth
buffer. - http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc01_4j1k.asp
29Clear 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
- http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc01_8koi.asp
30Color representation
- RGBA red, green, blue, alpha
- Each channel has intensity from 0.01.0
- Values outside this interval will be clamp to 0.0
or 1.0 - Alpha is used in blending and transparency
- Specify a color
- glColor34sifdv()
- http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc01_62b6.asp
31Points, Lines and Polygons 1/5
- 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
- http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc01_9u3y.asp
32Points, Lines and Polygons 2/5
33Points, Lines and Polygons 3/5
34Points, Lines and Polygons 4/5
- Specifying the vertices
- glVertex234sifdv()
- Specifies a vertex for use in describing a
geometric object - Can only effective between a glBegin() and
glEnd() pair - http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc03_2kag.asp
35Points, Lines and Polygons 5/5
- Specifying others
- glNormal() , glColor(), glIndex(),
glTexCoord(), glMaterial() - These commands should always be called before
glVertex()
36Completion of drawing 1/2
- void glFlush()
- Forces previously issued OpenGL commands to begin
execution. - http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc02_8sa0.asp - void glFinish()
- Forces previous issued OpenGL commands to
complete. - http//msdn.microsoft.com/library/default.asp?url
/library/en-us/opengl/glfunc02_3aqw.asp
37Completion of drawing 2/2
- void glutSwapBuffers()
- Swap front and back buffers.
- http//www.opengl.org/resources/libraries/glut/spe
c3/node21.html