opengl programming - utdallas.eduutdallas.edu/~praba/4392/yin-opengl.pdf · fearless engineering...
TRANSCRIPT
![Page 1: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/1.jpg)
UT DALLAS Erik Jonsson School of Engineering & Computer Science
FEARLESS engineering
OpenGL Programming
CS 4392 Computer Animation
Spring 2010
![Page 2: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/2.jpg)
FEARLESS engineering
What is OpenGL?
• A low level graphics API for 2D and 3D
interactive graphics. OS independent.
• Descendent of GL (from SGI)
![Page 3: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/3.jpg)
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
![Page 4: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/4.jpg)
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
![Page 5: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/5.jpg)
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
• ……..
• ……….
• ………….
![Page 6: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/6.jpg)
FEARLESS engineering
Computer Graphics Pipeline
Geometric Model Rendered Image
OpenGL
Rendering
Algorithm
![Page 7: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/7.jpg)
FEARLESS engineering
The Visualization Problem
![Page 8: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/8.jpg)
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
![Page 9: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/9.jpg)
FEARLESS engineering
How does OpenGL work?
• OpenGL pipeline:
Model Transformation Projection Rasterization Display
![Page 10: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/10.jpg)
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, …
![Page 11: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/11.jpg)
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)
![Page 12: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/12.jpg)
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
![Page 13: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/13.jpg)
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!
![Page 14: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/14.jpg)
FEARLESS engineering
What is to be covered?
• Drawing
• Viewing
• ..
• ….
• …..
• …….
![Page 15: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/15.jpg)
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
![Page 16: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/16.jpg)
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();
![Page 17: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/17.jpg)
FEARLESS engineering
Viewing
• Set the object
• Set the camera
• Set the projection
![Page 18: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/18.jpg)
FEARLESS engineering
Transformation
• glTranslate
• glRotate
• glScale
• glPushMatrix(); glPopMatrix();
![Page 19: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/19.jpg)
FEARLESS engineering
Projection
• glPerspective(……)
• glOrtho(…..)
![Page 20: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/20.jpg)
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;
• }
![Page 21: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/21.jpg)
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 ();
• }
![Page 22: OpenGL Programming - utdallas.eduutdallas.edu/~praba/4392/Yin-OpenGL.pdf · FEARLESS engineering OpenGL •Contains a library of over 200 functions •Portable –Implementations](https://reader031.vdocuments.us/reader031/viewer/2022021806/5bbf4cfc09d3f216668bba6c/html5/thumbnails/22.jpg)
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);
}