Title: Introduction to OpenGL
1Introduction to OpenGL
2OpenGL
- A Graphics rendering API introduced in 1992 by
Silicon Graphics Inc - Provide the low-level functions to access
graphics hardware directly - Cross-platform
3Related 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())
4OpenGL Utility Toolkit (GLUT)
- Visual C
- http//www.xmission.com/nate/glut.html
- glut32.dll to WinDir\System,
- glut32.lib to (MSDevDir)\..\..\VC\lib
- glut.h to (MSDevDir)\..\..\VC\include\GL.
5A Simple Example
- include ltGL/glut.hgt
- void GL_display()
- void GL_reshape(GLsizei w, GLsizei h)
- void main(void)
-
- glutInitDisplayMode(GLUT_SINGLE GLUT_RGB)
- glutCreateWindow("Sample")
- glutDisplayFunc(GL_display)
- glutReshapeFunc(GL_reshape)
- glutMainLoop()
6A 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()
7A 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()
8A 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()
9Primitives
10A 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()
11OpenGL 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)
12A 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()
13Command Syntax
glVertex3f( 1.0, 1.0, 0.0 )
OpenGL API
Name
The number and types of arguments
14A 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()
15A 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()
16A 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()
17A 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()
18Matrix 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.
19ModelView Matrix
- Viewing Transformation
- To positioning and aiming the camera
- Modeling Transformation
- Perform rotate, translate, scale and combinations
of these transformations to the object.
20Viewing Transformations
- gluLookAt (eyex, eyey, eyez, atx, aty, atz, upx,
upy, upz )
21Modeling Transformations
- glTranslatefd(x, y, z)
- Multiplies current matrix by a matrix that moves
an object by x,y,z
glTranslatef( 0, 0, -1 )
22Modeling Transformations
- glRotatefd(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)
23Modeling Transformations
- glScalefd (x, y, z)
- Multiplies current matrix by a matrix that scales
an object along axes.
glScalef( 2.0, -0.5, 1.0 )
24Matrix in OpenGL
- Matrix multiplications always apply to the top of
matrix stack.
Top matrix In the stack
X
Translation matrix
(glTranslatef)
25Matrix in OpenGL
- The order of transformations is critical.
glTranslatef( 1,0,0 ) glRotatef(45.0, 0,0,1
) drawObject()
glRotatef(45.0, 0,0,1 ) glTranslatef( 1,0,0
) drawObject()
26Projection 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)
27Projection Transformations
- Perspective Projection
- gluPerspective( GLdouble fovy, GLdouble aspect,
GLdouble near, GLdouble far )
28Projection Transformations
- Perspective Projection
- glFrustum( GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble near,
GLdouble far )
29A 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()
30OpenGL Transformations
glTranslatef glRotatef glScalf gluLookAt
gluPerspective gluOrtho2D glFrustum glOrtho
glViewport()
Modelview Matrix
Viewport Transformation
Projection Matrix
Perspective Division
Vertex
X Y Z W
X Y
eye coordinates
clip coordinates
normalized device coordinates
window coordinates
31Matrix in OpenGL
- Mantain matrix stack
- glPushMatrix() used to save current stack
- glPopMatrix() used to restore previous
stack
x
glScalef
glPopMatrix()
glPushMatirx()
32Matrix 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()
- glPopMatrix()
- glPopMatrix()
33Matrix in OpenGL
- Reference Object Hierarchy
- http//www.gamedev.net/reference/articles/article1
267.asp
34References
- OpenGL officially website
- http/www.opengl.org
- NeHe (useful installation guides included)
- http//nehe.gamedev.net/
- Nate Robins (for demonstration)
- http//www.xmission.com/nate/tutors.html
- The Red Book (OpenGL Programming Guide)