Upload
buihuong
View
238
Download
0
Embed Size (px)
Citation preview
UT DALLAS Erik Jonsson School of Engineering & Computer Science
FEARLESS engineering
OpenGL Programming
CS 4392 Computer Animation
Spring 2010
FEARLESS engineering
What is OpenGL?
• A low level graphics API for 2D and 3D
interactive graphics. OS independent.
• Descendent of GL (from SGI)
FEARLESS engineering
OpenGL
• Contains a library of over 200 functions
• Portable– Implementations available for nearly all hardware and
operating systems
– Input or windowing are not included in OpenGL• Options for Windows: GLUT, GLUI, FLTK, or MFC
• GLUT = OpenGL Utility Toolkit
• Implementations of GLUT exist for most computing environment
FEARLESS engineering
What OpenGL isn’t:
• A windowing program or input driver, since those couldn’t be OS independent.
GL: core graphics capability
GLU: utilities on top of GL
GLUT: input and windowing functions
FEARLESS engineering
Reading materials
• Red bible
• Blue bible
• Super bible
• http://www.opengl.org/sdk/docs/man/
• http://nehe.gamedev.net/
• http://www.xmission.com/~nate/tutors.html
• ……..
• ……….
• ………….
FEARLESS engineering
Computer Graphics Pipeline
Geometric Model Rendered Image
OpenGL
Rendering
Algorithm
FEARLESS engineering
The Visualization Problem
FEARLESS engineering
How does OpenGL work?
• From the programmer’s point of view:
– Specify geometric objects
– Describe object properties
– Define how they should be viewed
– Move camera or objects around for navigation
FEARLESS engineering
How does OpenGL work?
• OpenGL pipeline:
Model Transformation Projection Rasterization Display
FEARLESS engineering
Basic OpenGL Syntax
• Prefix: gl
glBegin, glEnd, glClear, glPolygonMode, …
• Constant symbols: GL_
GL_RGB, GL_POLYGON, GL_LINES, …
• Data types: GL
GLbyte, GLshort, GLint, GLfloat, GLdouble, …
FEARLESS engineering Copyright @
Xiaohu Guo
OpenGL Commands: a Quick Look
Just function calls:
glColor3f (1.0, 1.0, 1.0);
Same command, different arguments:
glColor3ub(255,255,255); -- same result
GL prefix
Command name
Type suffix (if variable),
can also end with “v”
Number of arguments (if variable)
FEARLESS engineering
OpenGL Data Types
b 8 GLbyte signed char
s 16 GLshort short
i 32 GLint, GLsizei int / long
f 32 GLfloat, GLclampf float
d 64 GLdouble, GLclampd double
ub 8 GLubyte, Glboolean unsigned char
us 16 GLushort unsigned short
ui 32 GLuint, GLenum,
GLbitfield
unsigned int
FEARLESS engineering
OpenGL as a State Machine
• OpenGL has a state
– And associated state variables (line width, color, … etc.)
• eg. GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL
– Some states are binary states that are either GL_FALSE or GL_TRUE
– Each state variable has a default value.
• Changing states
– For binary state variables, use glEnable/glDisable (eg. GL_LIGHTING)
– Mode state variables require specific commands (eg. glShadeModel)
– Value state variables require specific commands too. (eg. glColor3f)
• Remains in effect until changed!
FEARLESS engineering
What is to be covered?
• Drawing
• Viewing
• ..
• ….
• …..
• …….
FEARLESS engineering Copyright @
Xiaohu Guo
Drawing
• Geometric object is described by the type of the primitive to be drawn and a set of vertices.
– GL_POINTS
– GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
– GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
– GL_QUADS, GL_QUAD_STRIP
– GL_POLYGON
FEARLESS engineering
Drawing
• Put GL into draw-polygon state:glBegin (GL_POLYGON);
• Send it the points making up the polygon:glVertex2f (x0, y0);
glVertex2f (x1, y1);
glVertex2f (x2, y2); …
• Tell it we’re finishedglEnd();
FEARLESS engineering
Viewing
• Set the object
• Set the camera
• Set the projection
FEARLESS engineering
Transformation
• glTranslate
• glRotate
• glScale
• glPushMatrix(); glPopMatrix();
FEARLESS engineering
Projection
• glPerspective(……)
• glOrtho(…..)
FEARLESS engineering
A real OpenGL program
• int main(int argc, char** argv)
• {
• glutInit(&argc, argv);
• glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
• glutInitWindowSize (500, 500);
• glutInitWindowPosition (100, 100);
• glutCreateWindow (argv[0]);
• init ();
• glutDisplayFunc(display);
• glutReshapeFunc(reshape);
• glutMainLoop();
• return 0;
• }
FEARLESS engineering
glutDisplayFunc(display)
• void display(void)
• {
• glClear (GL_COLOR_BUFFER_BIT);
• glColor3f (1.0, 1.0, 1.0);
• glLoadIdentity (); /* clear the matrix */
• /* viewing transformation */
• gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
• glScalef (1.0, 2.0, 1.0); /* modeling transformation */
• glutWireCube (1.0);
• glFlush ();
• }
FEARLESS engineering
glutReshapeFunc(reshape)
• void reshape (int w, int h)
• {
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}