Download presentation
Presentation is loading. Please wait.
Published byOswald Cook Modified over 9 years ago
1
Introduction to OpenGL 1
2
2
3
OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics hardware directly Cross-platform 3
4
Related API GLU (OpenGL Utility Library) –Part of OpenGL –Use the prefix of glu (ex: gluLookAt()) GLUT (OpenGL Utility Toolkit) –Not officially part of OpenGL –hide the complexities of differing window system APIs. –Use the prefix of glut (ex: glutDisplayFunc()) 4
5
OpenGL Utility Toolkit (GLUT) Visual C++ –http://www.xmission.com/~nate/glut.htmlhttp://www.xmission.com/~nate/glut.html –glut32.dll to %WinDir%\System, –glut32.lib to $(MSDevDir)\..\..\VC\lib –glut.h to $(MSDevDir)\..\..\VC\include\GL. 5
6
A Simple Example #include void GL_display(); void GL_reshape(GLsizei w, GLsizei h); void main(void) { glutCreateWindow("Sample"); glutDisplayFunc(GL_display); glutReshapeFunc(GL_reshape); glutMainLoop(); } 6
7
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); } 7
8
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); } Color BufferDepth Buffer 8
9
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); } 9
10
Primitives 10
11
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); } 11
12
OpenGL as a State Machine Put a value into various states, then it will remain in effect until being changed. –e.g. glColor*() Many state variables are enabled or disabled with glEnable(), glDisable() –e.g. glEnable(GL_LIGHT0) 12
13
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); } 13
14
Command Syntax glVertex3f( 1.0, 1.0, 0.0 ); OpenGL API Name The number and types of arguments Data TypeC-Language TypeOpenGL Type Definition b8-bit integersigned charGLbyte s16-bit integershortGLshort i32-bit integerlongGLint, GLsizei f32-bit floating-pointfloatGLfloat, GLclampf d64-bit floating-pointdoubleGLdouble, GLclampd ub8-bit unsigned integerunsigned charGLubyte, GLboolean us16-bit unsigned integerunsigned shortGLushort ui32-bit unsigned integerunsigned longGLuint, GLenum, GLbitfield 14
15
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); } 15
16
A Simple Example 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(); } 16
17
A Simple Example 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(); } 17
18
A Simple Example 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(); } 18
19
Matrix in OpenGL There are two matrix stacks. –ModelView matrix (GL_MODELVIEW) –Projection matrix (GL_PROJECTION) When we call functions of transformation, we should change to the appropriate matrix stack first. glMatrixMode(GL_MODELVIEW); //now we are in modelview matrix stack! //do modelview transformation here….. glMatrixMode(GL_PROJECTION); //now we are in projection matrix stack! //do projection transformation here…. 19
20
ModelView Matrix Modeling Transformation Perform rotate, translate, scale and combinations of these transformations to the object. Viewing Transformation To positioning and aiming the camera 20
21
Modeling Transformations glTranslate{fd}(x, y, z) –Multiplies current matrix by a matrix that moves an object by x,y,z glTranslatef( 0, 0, -1 ) 21
22
Modeling Transformations glRotate{fd}(angle, x, y, z ) –Multiplies current matrix by a matrix that rotates an object in a counterclockwise direction about the ray from origin to (x,y,z) with angle as the degrees glRotatef( 45.0, 0, 0, 1) 22
23
Modeling Transformations glScale{fd} (x, y, z) –Multiplies current matrix by a matrix that scales an object along axes. glScalef( 2.0, -0.5, 1.0 ) 23
24
Viewing Transformations gluLookAt (eyex, eyey, eyez, atx, aty, atz, upx, upy, upz ); 24
25
Matrix in OpenGL Matrix multiplications always apply to the top of matrix stack. Top matrix In the stack Translation matrix (glTranslatef) X 25
26
Matrix in OpenGL The order of transformations is critical. 26 glTranslatef( 1,0,0 ); glRotatef(45.0, 0,0,1 ); drawObject(); glRotatef(45.0, 0,0,1 ); glTranslatef( 1,0,0 ); drawObject();
27
Projection Transformations Orthographic Projection glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far ) gluOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) 27
28
Projection Transformations Perspective Projection –gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far ); 28
29
Projection Transformations Perspective Projection –glFrustum( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far ); 29
30
A Simple Example 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(); } 30
31
OpenGL Transformations 31 Modelview Matrix Modelview Matrix Viewport Transformation Viewport Transformation Projection Matrix Projection Matrix Perspective Division eye coordinates clip coordinates normalized device coordinates window coordinates XYZWXYZW Vertex XYXY glTranslatef glRotatef glScalf gluLookAt gluPerspective gluOrtho2D glFrustum glOrtho glViewport()
32
Matrix in OpenGL Mantain matrix stack –glPushMatrix() : used to save current stack –glPopMatrix() : used to restore previous stack glPushMatirx() x glScalef glPopMatrix() 32
33
Matrix in OpenGL glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix(); glPushMatrix(); glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix(); 33
34
Matrix in OpenGL Reference: Object Hierarchy –http://www.gamedev.net/reference/articles/articl e1267.asphttp://www.gamedev.net/reference/articles/articl e1267.asp 34
35
References OpenGL officially website: –http:/www.opengl.orghttp:/www.opengl.org NeHe (useful installation guides included) –http://nehe.gamedev.net/http://nehe.gamedev.net/ Nate Robins (for demonstration) –http://www.xmission.com/~nate/tutors.htmlhttp://www.xmission.com/~nate/tutors.html The Red Book (OpenGL Programming Guide) 35
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.