OpenGL - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

OpenGL

Description:

Raw OpenGL calls #include glu.h Includes commonly requested things ... Create a window. glutInitWindowSize (500, 500); glutCreateWindow ('Title' ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 32
Provided by: jeffch8
Category:
Tags: opengl

less

Transcript and Presenter's Notes

Title: OpenGL


1
OpenGL
  • Basics

2
Header files
  • include ltgl.hgt
  • Raw OpenGL calls
  • include ltglu.hgt
  • Includes commonly requested things
  • Shapes (teapots, cubes, spheres)
  • include ltglut.h gt
  • Includes windowing capabilities
  • glut.h includes glu.h and gl.h

3
Creating a Window
  • int main (int argc, char argv)
  • // Initialize OpenGL
  • glutInit (argc, argv)
  • // Create a window
  • glutInitWindowSize (500, 500)
  • glutCreateWindow (Title)
  • //Note still need a display function

4
Other things you may want(when initializing)
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glInitWindowPosition( )
  • glInitDisplayMode ( )
  • GLUT_RGB
  • GLUT_RGBA
  • GLUT_SINGLE
  • GLUT_DOUBLE (for animation)
  • GLUT_DEPTH (for 3D stuff)

5
Basic OpenGL Architecture
  • There is a main
  • There is an even t loop
  • There are a series of callbacks
  • Draw/paint
  • Idle function (glutIdleFunc)
  • Key pressed (glutKeyboardFunc)
  • Mouse (glutMouseFunc)
  • Mouse movement (glutPassiveMotionFunc)

6
Event Loop
  • void display ( )
  • // Draw stuff
  • int main (int argc, char argv)
  • // Initialize OpenGL
  • glutInit (argc, argv)
  • // Create a window
  • glutCreateWindow (Title)
  • // Tell OpenGL which is the drawing function
  • glutDisplayFunc (display)
  • // Start the event loop
  • glutMainLoop()

7
2D Drawing
  • Typically uses glBegin and glEnd
  • Usually clear the previous drawing first
  • Example
  • glClear (GL_COLOR_BUFFER_BIT)
  • glBegin(GL_POLYGON)
  • glVertex2f(-0.5, -0.5)
  • glVertex2f(-0.5, 0.5)
  • glVertex2f(0.5, 0.5)
  • glVertex2f(0.5, -0.5)
  • glEnd( )
  • glFlush( )

8
glBegin
  • Takes an enumeration type
  • GL_POINTS (points)
  • GL_LINES (two points per line)
  • GL_LINE_STRIP (connecting points)
  • GL_LINE_LOOP (enclosed points)
  • GL_POLYGON (as seen before)
  • glLineWidth (2.0)
  • glLineStipple (0xcccc)

9
Enable/Disabling Features
  • Use glEnable (type)
  • glEnable (GL_LINE_STIPPLE)
  • glEnable (GL_DEPTH_TEST)
  • glEnable (GL_LIGHTING)

10
Note About gl Calls
  • Usually specify the number and type of arguments
  • glVertex2f (2 arguments of type float)
  • glVertex3f (3 arguments of type float)
  • glVertex3i (3 arguments of type int)
  • General format glVertex234sifd

11
PolygonMode(not super-important)
  • What weve been drawing has 2 faces (front and
    back)
  • Drawing counter-clockwise is front
  • Drawing clockwise is back
  • glPolygonMode (GL_FRONT_AND_BACK, GL_FILL)
  • Have option to not draw the back faces
  • glCullFace ( )
  • GL_FRONT
  • GL_BACK
  • GL_FRONT_AND_BACK

12
Setting Colors
  • void glColor3bifd(r, g, b)
  • Example
  • void display ()
  • glClear (GL_COLOR_BUFFER_BIT)
  • glBegin(GL_POLYGON)
  • glColor3f (1.0, 0.0, 0.0)
  • glVertex2f(-0.5, -0.5)
  • glColor3f (0.0, 1.0, 0.0)
  • glVertex2f(-0.5, 0.5)
  • glColor3f (0.0, 0.0, 1.0)
  • glVertex2f(0.5, 0.5)
  • glColor3f (1.0, 1.0, 1.0)
  • glVertex2f(0.5, -0.5)
  • glEnd( )
  • glFlush( )

13
gluOrtho2D(not super-important)
  • Before, we drew with a window size of -1 to 1
  • gluOrtho2D redefines the coordinate system in 2D
  • Scale youre working in
  • Example
  • gluOrtho2D(-5, 5, -5, 5)

14
The Viewport(again, not super-important)
  • Restrict OpenGL to drawing in only part of the
    window (divides the window)
  • void glViewport (int x, int y, int w, int h)
  • Default viewport is entire window

15
Intro to the Matrix Stack
  • Matrices define
  • Translations
  • Rotations
  • Scaling
  • Skewing
  • We can build a matrix stack that contains a
    series of matrix operations
  • A clear matrix is called the identity matrix

16
Pushing and Popping
  • To add a matrix to the stack, glPushMatrix()
  • To remove a matrix from the stack, glPopMatrix()

17
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

18
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Identity
19
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Identity
20
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Translate
Identity
21
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Translate
Identity
22
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Identity
Translate
Identity
23
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Translate
Identity
Translate
Identity
24
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Translate
Identity
25
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Translate
Identity
26
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Rotate
Translate
Identity
27
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Translate
Identity
28
Example
  • glLoadIdentity()
  • glPushMatrix()
  • glTranslatef ()
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslate ()
  • glPopMatrix()
  • glPushMatrix()
  • glRotatef ()
  • glPopMatrix()
  • glPopMatrix()

Identity
29
Model-View vs Projection
  • We have two coordinate systems
  • Model-view
  • Rotation and translation of the world
  • Projection
  • How we are viewing this world
  • Changing
  • Set the matrix mode (glMatrixMode)
  • Load the identity matrix (glLoadIdentity)
  • Alter the matrix (gluOrtho2D)

30
Simple 2D Animation
  • include ltGL/glut.hgt
  • float degree 0.5f
  • void display ()
  • glClear (GL_COLOR_BUFFER_BIT)
  • glMatrixMode (GL_MODELVIEW)
  • glPushMatrix()
  • glRotatef (degree, 1.0f, 0.0f, 0.0f)
  • glRotatef (degree2, 0.0, 1.0f, 0.0f)
  • glBegin(GL_POLYGON)
  • glColor3f (1.0, 0.0, 0.0)
  • glVertex3f(-0.5, -0.5, 0.0)
  • glColor3f (0.0, 1.0, 0.0)
  • glVertex3f(-0.5, 0.5, 0.0)
  • glColor3f (0.0, 0.0, 1.0)
  • glVertex3f(0.5, 0.5, 0.0)
  • glColor3f (1.0, 1.0, 1.0)
  • glVertex3f(0.5, -0.5, 0.0)
  • glEnd( )

31
A Peek at 3D
  • include ltGL/glut.hgt
  • GLfloat light_position 100.0f, 100.0f,
    100.0f, 0.0f
  • GLfloat diffuse_light 0.9f, 0.9, 0.9f,
    0.0f
  • void display ()
  • glClear (GL_COLOR_BUFFER_BIT
    GL_DEPTH_BUFFER_BIT)
  • glPushMatrix()
  • glTranslatef (0.0f, 0.0f, -10.f)
  • glutSolidTeapot(2.0)
  • glPopMatrix()
  • glFlush()
  • glutSwapBuffers()
  • int main (int argv, char argc)
  • glutInit (argv, argc)
  • glutInitDisplayMode(GLUT_DOUBLE GLUT_RGBA
    GLUT_DEPTH)
  • glutCreateWindow ("Bob")
  • glutDisplayFunc (display)
Write a Comment
User Comments (0)
About PowerShow.com