OpenGL Tutorial given by TA

Download Report

Transcript OpenGL Tutorial given by TA

OpenGL Introduction
By TA: Weng Hao
Index
•
•
•
•
•
What and Why OpenGL?
Contents
How to get started?
A sample application
References materials
What and Why OpenGL?
• Open Graphics Library
• APIs for rendering 2D and 3D scene
• specification, not software product  not
platform-specific/language-specific
• Windows/Mac OS/Linux/FreeBSD C/C++/Java
• OpenGL vs D3D
• CAD、virtual reality、computer vision、
Games
Contents
• OpenGL pipeline
Contents
• State machine
Color:red
Color:blue
Linewidth:1.0
Linewidth: 5.0
Matrix:modelview
Matrix:projection
Light pos1
Light pos2
…
…
Contents
• A metaphor for
transformation
• Coornidate system
- the world coordinate
- the camera coordinate
- the local coordinate
Contents
• v’ = M*v
[x,y,z,w]T
Transformation
matrix
Object
coordinates
Modelview
matrix
[x,y,z,w]T
vertex
eye
coordinates
Projection
matrix
Perspective
division
viewport
Transformation
clip
coordinates
window
coordinates
normalized device
coordinates
Contents-modelview transformation
• View transformation
- where the camera is located
- gluLookAt, glTranslate, glRotate
• Model transformation
- the position, scale and direction of model
- glTranslate, glScale,glRotate
• Why together?
- share the same effect
• One demo
Contents-projection transformation
• Define a viewing volume
- perspective projection
- glFrustum,gluPerspective
- animation,application
for realistic effect
- Orthographic Projection
- glOrtho
- CAD, architecture
• Viewing Volume Clipping
• One demo
Contents-viewport transformation
• viewport transformation
- the size of the image on the screen
- glViewport
notice: the aspect ratio of a viewport = that of the viewing
volume
Contents
• Render lighting
- set normal vector for each vertex of the object
- create, position and enable the light
source(glLightfv,GL_LIGHTING)
- select light model(glLightModel)
-define material attribute(glmaterialfv)
• one demo
• Texture , display list, fog … …
Contents
draw
animation
swap
Contents – related libraries
• Glu(OpenGL Utility)
- gluLookAt, gluPerspective … …
• Glut(OpenGL Utility Toolkit)
- window system-independent(window management,display
callback, handling input events,drawing 3D objects, backgroud
process management)
- glutWindow, glutMouseFunc, glutkeyboardFunc … …
• glX(OpenGL Extension to the X window System)
How to get started?
• Configure the enviroment
hardware-independent, update the drivers(gl)
AMD/ATI, Nvidia,Intel -> OpenGL
• (Windows OS)
- .h(windows.h gl.h glu.h)
-> the include directory of VS \Microsoft Visual Studio 9.0\VC\include\GL
- .lib(opengl32.lib glu32.lib)
- .dll(opengl32.dll glu32.dll)
-> the system directory \window\system(32)
Visual Studio: add the .h, .lib and .dll files into the related
directories
How to get started?
• Find the version of installed OpenGL
• OpenGL Reference Mannual
How to get started?
• Data types
• void glColor3f(GLfloat x, GLfloat y, GLfloat z);
• void glVertex3d(GLdouble x, GLdouble y, GLdouble z);
Glfloat color_array[ ] = {1.0, 0.0, 0.0};
• glColor3fv(color_array);
How to get started?
• Geometry primitive
- point, line, polygon(GL_POINTS,GL_LINES,GL_POLYGON…)
notice: polygon will fill its interior pixels
must be convex(easy to render)
complex polygon? -> combine the primitive ones(glu)
How to get started?
• curve and surface
simulated by short lines and small polygons
- normal vector(glNormal*)
- vertex array(non-interleaved/interleaved vertex
array)
large number of rendering
some points shared
How to get started?
• Function naming rules
gl-(glViewport, glColor*f, glMatrixMode)
glu-(gluLookAt, gluPerspective)
glut- glutCreateWindow, glutDisplayFunc, glutSolidSphere
glX-
• parameters
GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP,
GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIAN,
GL_QUADS, GL_QUAD_STRIP
A sample application
• Code and explanation
References
• web:
–
–
–
–
–
OpenGL official wbsite:http://www.opengl.org
OpenGL Reference Mannual:http://www.opengl.org/sdk/docs/
GLUT:http://www.opengl.org/resources/libraries/glut/
Nate Robin: http://user.xmission.com/~nate/tutors.html
NEHE:http://nehe.gamedev.net
• specification :
– The OpenGL Utility Toolkit (GLUT) Programming Interface (PDF/HTML) for
Windows
• book:
– OpenGL Programming Guide (seventh edition), Addison Wesley
– OpenGL SuperBible (third edition), Waite Group Press
– OpenGL Shading Language