Introduction to OpenGL - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Introduction to OpenGL

Description:

hide the complexities of differing window system APIs. ... Nate Robins (for demonstration) http://www.xmission.com/~nate/tutors.html ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 35
Provided by: graphicsC
Category:

less

Transcript and Presenter's Notes

Title: Introduction to OpenGL


1
Introduction to OpenGL
2
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
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
OpenGL 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.

5
A 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()

6
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
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()

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()

9
Primitives
10
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
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
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
Command Syntax
glVertex3f( 1.0, 1.0, 0.0 )
OpenGL API
Name
The number and types of arguments
14
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
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
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
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
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
ModelView Matrix
  • Viewing Transformation
  • To positioning and aiming the camera
  • Modeling Transformation
  • Perform rotate, translate, scale and combinations
    of these transformations to the object.

20
Viewing Transformations
  • gluLookAt (eyex, eyey, eyez, atx, aty, atz, upx,
    upy, upz )

21
Modeling Transformations
  • glTranslatefd(x, y, z)
  • Multiplies current matrix by a matrix that moves
    an object by x,y,z

glTranslatef( 0, 0, -1 )
22
Modeling 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)
23
Modeling Transformations
  • glScalefd (x, y, z)
  • Multiplies current matrix by a matrix that scales
    an object along axes.

glScalef( 2.0, -0.5, 1.0 )
24
Matrix in OpenGL
  • Matrix multiplications always apply to the top of
    matrix stack.

Top matrix In the stack
X
Translation matrix
(glTranslatef)
25
Matrix 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()
26
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
Projection Transformations
  • Perspective Projection
  • gluPerspective( GLdouble fovy, GLdouble aspect,
    GLdouble near, GLdouble far )

28
Projection Transformations
  • Perspective Projection
  • glFrustum( GLdouble left, GLdouble right,
    GLdouble bottom, GLdouble top, GLdouble near,
    GLdouble far )

29
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
OpenGL 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
31
Matrix in OpenGL
  • Mantain matrix stack
  • glPushMatrix() used to save current stack
  • glPopMatrix() used to restore previous
    stack

x
glScalef
glPopMatrix()
glPushMatirx()
32
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()
  • glPopMatrix()
  • glPopMatrix()

33
Matrix in OpenGL
  • Reference Object Hierarchy
  • http//www.gamedev.net/reference/articles/article1
    267.asp

34
References
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com