Chap 2 Write a Simple OpenGL Program - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Chap 2 Write a Simple OpenGL Program

Description:

OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL SuperBible ... Set the current values for use in cleaning color buffers in RGBA mode. ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 38
Provided by: cggmcsie
Category:
Tags: opengl | chap | program | simple | write

less

Transcript and Presenter's Notes

Title: Chap 2 Write a Simple OpenGL Program


1
Chap 2 Write a Simple OpenGL Program
2
Preparing 1/2
  • environmentMicrosoft Visual C 6.0?Microsoft
    Visual C .Net
  • Also needGLUT
  • http//www.xmission.com/nate/glut.html

3
Preparing 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/

4
Books
  • OpenGL Programming Guide (Red-book)
  • OpenGL Reference Manual (Blue-book)
  • OpenGL SuperBible

5
OpenGL 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)

6
Write an OpenGL Program 1/7
  • Microsoft Visual C 6.0
  • Step 1 create a Win32 Console Application project

7
Write an OpenGL Program 2/7
  • Step 2Press Alt-F7,brings Project
    Settings,select Link

8
Write an OpenGL Program 3/7
  • Step 3add opengl32.lib glu32.lib glut32.lib
    into Object/library modules
  • Step 4write your code
  • Step 5compile

9
Write an OpenGL Program 4/7
  • Microsoft Visual C .Net
  • Step 1create Win32 console project

10
Write an OpenGL Program 5/7
  • Step 2 check empty project

11
Write an OpenGL Program 6/7
  • Step 3select project / property / linker /
    input
  • Step 4 add opengl32.lib glu32.lib glut32.lib

12
Write an OpenGL Program 7/7
  • Step 5 add a new C source file
  • Step 6write your code
  • Step 7compile

13
A 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()

14
A 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()

15
OpenGL 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

16
OpenGL command Syntax 2/2
17
State 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.

18
State 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

19
Program 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

20
Program 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

21
Program 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

22
Program 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.

23
Program detail 5/5
  • Running the program
  • void glutMainLoop(void)
  • Enter the GLUT processing loop and never return.

24
GLUT 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

25
OpenGL 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()

26
OpenGL 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()

27
Data type
28
Clear 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

29
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
  • http//msdn.microsoft.com/library/default.asp?url
    /library/en-us/opengl/glfunc01_8koi.asp

30
Color 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

31
Points, 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

32
Points, Lines and Polygons 2/5
33
Points, Lines and Polygons 3/5
34
Points, 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

35
Points, Lines and Polygons 5/5
  • Specifying others
  • glNormal() , glColor(), glIndex(),
    glTexCoord(), glMaterial()
  • These commands should always be called before
    glVertex()

36
Completion 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

37
Completion of drawing 2/2
  • void glutSwapBuffers()
  • Swap front and back buffers.
  • http//www.opengl.org/resources/libraries/glut/spe
    c3/node21.html
Write a Comment
User Comments (0)
About PowerShow.com