SlideShare a Scribd company logo
Introduction to OpenGL
Jian Huang
This set of slides are extracted from the Interactive OpenGL
Programming course given by Dave Shreine, Ed Angel and
Vicki Shreiner on SIGGRAPH 2001.
OpenGL and GLUT Overview
• What is OpenGL & what can it do for
me?
• OpenGL in windowing systems
• Why GLUT
• GLUT program template
What Is OpenGL?
• Graphics rendering API
– high-quality color images composed of
geometric and image primitives
– window system independent
– operating system independent
OpenGL Architecture
OpenGL as a Renderer
• Geometric primitives
– points, lines and polygons
– Image Primitives
– images and bitmaps
• separate pipeline for images and geometry
– linked through texture mapping
• Rendering depends on state
– colors, materials, light sources, etc.
Relate APIs
• AGL, GLX, WGL
– glue between OpenGL and windowing systems
• GLU (OpenGL Utility Library)
– part of OpenGL
– NURBS, tessellators, quadric shapes, etc
• GLUT (OpenGL Utility Toolkit)
– portable windowing API
– not officially part of OpenGL
OpenGL and Related APIs
Preliminaries
• Header Files
– #include <GL gl.h>
– #include <GL glu.h>
– #include <GL glut.h>
• Libraries
• Enumerated types
• OpenGL defines numerous types for
compatibility
– GLfloat, GLint, GLenum, etc.
GLUT Basics
• Application Structure
• Configure and open window
• Initialize OpenGL state
• Register input callback functions
– render
– resize
– input: keyboard, mouse, etc.
• Enter event processing loop
Sample Program
void main( int argc, char** argv )
{
int mode = GLUT_RGB|GLUT_DOUBLE;
glutInitDisplayMode( mode );
glutCreateWindow( argv[0] );
init(); // initiate OpenGL states, program variables
glutDisplayFunc( display ); // register callback routines
glutReshapeFunc( resize );
glutKeyboardFunc( key );
glutIdleFunc( idle );
glutMainLoop(); // enter the event-driven loop
}
OpenGL Initialization
• Set up whatever state you’re going to use
void init( void )
{
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glClearDepth( 1.0 );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST );
}
GLUT Callback Functions
• Routine to call when something happens
– window resize or redraw
– user input
– animation
• “Register” callbacks with GLU
– glutDisplayFunc( display );
– glutIdleFunc( idle );
– glutKeyboardFunc( keyboard );
Rendering Callback
• Do all of our drawing here
glutDisplayFunc( display );
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_TRIANGLE );
glVertex3fv( v[0] );
glVertex3fv( v[1] );
glVertex3fv( v[2] );
glEnd();
glutSwapBuffers();
}
Idle Callbacks
• Use for animation and continuous
update
glutIdleFunc( idle );
void idle( void )
{
t +=dt;
glutPostRedisplay();
}
User Input Callbacks
• Process user input
glutKeyboardFunc( keyboard );
void keyboard( char key, int x, int y )
{
switch( key ) {
case ‘q’ : case ‘Q’ :
exit( EXIT_SUCCESS );
break;
case ‘r’ : case ‘R’ :
rotate = GL_TRUE;
break;
}
}
Elementary Rendering
• Geometric Primitives
• Managing OpenGL State
• OpenGL Buffers
OpenGL Geometric Primitives
• All geometric primitives are specified by
vertices
Simple Example
void drawRhombus( GLfloat color[] )
{
glBegin( GL_QUADS );
glColor3fv( color );
glVertex2f( 0.0, 0.0 );
glVertex2f( 1.0, 0.0 );
glVertex2f( 1.5, 1.118 );
glVertex2f( 0.5, 1.118 );
glEnd();
}
OpenGL Command Formats
Specifying Geometric
Primitives
• Primitives are specified using
glBegin( primType );
glEnd();
• primType determines how vertices are combined
GLfloat red, greed, blue;
Glfloat coords[3];
glBegin( primType );
for (i =0;i <nVerts; ++i ) {
glColor3f( red, green, blue );
glVertex3fv( coords );
}
glEnd();
OpenGL Color Model
• Both RGBA (true color) and Color Index
Controlling Rendering
• Appearance
• From Wireframe to Texture mapped
OpenGL’s State Machine
• All rendering attributes are
encapsulated in the OpenGL State
– rendering styles
– shading
– lighting
– texture mapping
Manipulating OpenGL State
• Appearance is controlled by current state
for each ( primitive to render ) {
update OpenGL state
render primitive
}
• manipulating vertex attributes is most
common way to manipulate state
– glColor*() / glIndex*()
– glNormal*()
– glTexCoord*()
Controlling current state
• Setting State
glPointSize( size );
glLineStipple( repeat, pattern );
glShadeModel( GL_ SMOOTH );
• Enabling Features
glEnable( GL_ LIGHTING );
glDisable( GL_TEXTURE_2D );
Transformations in OpenGL
• Modeling
• Viewing
– orient camera
– projection
• Animation
• Map to screen
Coordinate Systems and
Transformations
• Steps in Forming an Image
– specify geometry (world coordinates)
– specify camera (camera coordinates)
– project (window coordinates)
– map to viewport (screen coordinates)
• Each step uses transformations
• Every transformation is equivalent to a
change in coordinate systems
3D Transformations
• A vertex is transformed by 4 x 4 matrices
• all affine operation are matrix multiplication
• matrices are stored column-major in OGL
• matrices are always post-multiplied
• v
• Mv
Except in perspective projection, the
fourth row = (0,0,0,1), w left unchanged.
OpenGL uses stacks of matrices, the
programmer must remember that the last
matrix specified is the first applied.
Specifying Transformations
• Programmer has two styles of specifying
transformations
– specify matrices glLoadMatrix, glMultMatrix
– specify operation glRotate, glOrtho
• Programmer does not have to remember the
exact matrices
• check appendix of Red Book
Programming Transformations
• Prior to rendering, view, locate, and orient:
– eye/camera position
– 3D geometry
• Manage the matrices
– including matrix stack
• Combine (composite) transformations
• Transformation matrices are part of the state, they must be
defined prior to any vertices to which they are to apply.
• OpenGL provides matrix stacks for each type of supported
matrix (ModelView, projection, texture) to store matrices.
Transformation Pipeline
Matrix Operations
• Specify Current Matrix Stack
– glMatrixMode( GL_MODELVIEW or GL_PROJECTION )
• Other Matrix or Stack Operation
– glLoadIdentity() glPushMatrix() glPopMatrix()
• Viewport
– usually same as window size
– viewport aspect ratio should be same as projection
transformation or resulting image may be distorted
– glViewport( x, y, width, height )
Projection Transformation
• Perspective projection
– gluPerspective( fovy, aspect, zNear, zFar )
– glFrustum( left, right, bottom, top, zNear,
zFar ) (very rarely used)
• Orthographic parallel projection
– glOrtho( left, right, bottom, top, zNear, zFar)
– gluOrtho2D( left, right, bottom, top )
– calls glOrtho with z values near zero
• Warning: for gluPerspective() or
glFrustum(), don’t use zero for zNear!
Applying Projection
• Transformations
• Typical use ( orthographic projection)
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( left, right, bottom, top, zNear, zFar );
Viewing Transformations
• Position the camea/eye in the scene
• To “fly through” a scene
• change viewing transformation and redraw
scene
gluLookAt( eye x ,eye y ,eye z ,
aim x ,aim y ,aim z ,
up x ,up y ,up z )
• up vector determines unique orientation
• careful of degenerate positions
Modeling Transformations
• Move object
– glTranslate{fd}( x, y, z )
• Rotate object aro nd arbitrary axis
– glRotate{fd}( angle, x, y, z )
– angle is in degrees
• Dilate (stretch or shrink) object
– glScale{fd}( x, y, z )
y x
Projection is left handed
• Projection transformation (gluPerspective,
glOrtho) are left handed
– think of zNear and zFar as distance from view
point
• Everything else is right handed, including the
vertexes to be rendered
Common Transformation
Usage
• Example of resize() routine
– restate projection & viewing
transformations
• Usually called when window resized
• Registered a callback for
glutReshapeFunc()
resize(): Perspective & LookAt
void resize( int w, int h )
{
glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 65.0, (GLfloat) w / h,
1.0, 100.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0.0, 0.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
}

More Related Content

PPT
september11.ppt
PPT
Opengl (1)
PPTX
OpenGL_summer2012.ccccccccccccccccccpptx
PPT
openGL basics for sample program (1).ppt
PPT
openGL basics for sample program.ppt
PDF
OpenGL Interaction
PPTX
UNIT 1 OPENGL_UPDATED .pptx
september11.ppt
Opengl (1)
OpenGL_summer2012.ccccccccccccccccccpptx
openGL basics for sample program (1).ppt
openGL basics for sample program.ppt
OpenGL Interaction
UNIT 1 OPENGL_UPDATED .pptx

Similar to Introduction to OpenGL modern OpenGL program (20)

PDF
Open gl
PDF
Ujug07presentation
PPT
opengl.ppt
PPT
Intro to Computer Graphics.ppt
PPT
Open gl
PDF
18csl67 vtu lab manual
PDF
Open gl tips
PDF
Open gl basics
PDF
OpenGL ES on iOS
PPT
01.Opengl_intro-2.ppt
PDF
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
 
PPT
OpenGL Transformations
PPT
Programming with OpenGL
PPTX
OpenGL Introduction
PPTX
BYO3D 2011: Rendering
PPTX
Chapter02 graphics-programming
PPT
Working with Callbacks
PPTX
CGLabLec6.pptx
PPT
CS 354 Object Viewing and Representation
PDF
OpenGL Introduction.
Open gl
Ujug07presentation
opengl.ppt
Intro to Computer Graphics.ppt
Open gl
18csl67 vtu lab manual
Open gl tips
Open gl basics
OpenGL ES on iOS
01.Opengl_intro-2.ppt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
 
OpenGL Transformations
Programming with OpenGL
OpenGL Introduction
BYO3D 2011: Rendering
Chapter02 graphics-programming
Working with Callbacks
CGLabLec6.pptx
CS 354 Object Viewing and Representation
OpenGL Introduction.
Ad

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
web development for engineering and engineering
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
composite construction of structures.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Welding lecture in detail for understanding
PDF
Well-logging-methods_new................
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Geodesy 1.pptx...............................................
DOCX
573137875-Attendance-Management-System-original
PPTX
OOP with Java - Java Introduction (Basics)
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Arduino robotics embedded978-1-4302-3184-4.pdf
web development for engineering and engineering
UNIT 4 Total Quality Management .pptx
CH1 Production IntroductoryConcepts.pptx
composite construction of structures.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
additive manufacturing of ss316l using mig welding
Welding lecture in detail for understanding
Well-logging-methods_new................
Operating System & Kernel Study Guide-1 - converted.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Construction Project Organization Group 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Geodesy 1.pptx...............................................
573137875-Attendance-Management-System-original
OOP with Java - Java Introduction (Basics)
Ad

Introduction to OpenGL modern OpenGL program

  • 1. Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki Shreiner on SIGGRAPH 2001.
  • 2. OpenGL and GLUT Overview • What is OpenGL & what can it do for me? • OpenGL in windowing systems • Why GLUT • GLUT program template
  • 3. What Is OpenGL? • Graphics rendering API – high-quality color images composed of geometric and image primitives – window system independent – operating system independent
  • 5. OpenGL as a Renderer • Geometric primitives – points, lines and polygons – Image Primitives – images and bitmaps • separate pipeline for images and geometry – linked through texture mapping • Rendering depends on state – colors, materials, light sources, etc.
  • 6. Relate APIs • AGL, GLX, WGL – glue between OpenGL and windowing systems • GLU (OpenGL Utility Library) – part of OpenGL – NURBS, tessellators, quadric shapes, etc • GLUT (OpenGL Utility Toolkit) – portable windowing API – not officially part of OpenGL
  • 8. Preliminaries • Header Files – #include <GL gl.h> – #include <GL glu.h> – #include <GL glut.h> • Libraries • Enumerated types • OpenGL defines numerous types for compatibility – GLfloat, GLint, GLenum, etc.
  • 9. GLUT Basics • Application Structure • Configure and open window • Initialize OpenGL state • Register input callback functions – render – resize – input: keyboard, mouse, etc. • Enter event processing loop
  • 10. Sample Program void main( int argc, char** argv ) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutCreateWindow( argv[0] ); init(); // initiate OpenGL states, program variables glutDisplayFunc( display ); // register callback routines glutReshapeFunc( resize ); glutKeyboardFunc( key ); glutIdleFunc( idle ); glutMainLoop(); // enter the event-driven loop }
  • 11. OpenGL Initialization • Set up whatever state you’re going to use void init( void ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glClearDepth( 1.0 ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); }
  • 12. GLUT Callback Functions • Routine to call when something happens – window resize or redraw – user input – animation • “Register” callbacks with GLU – glutDisplayFunc( display ); – glutIdleFunc( idle ); – glutKeyboardFunc( keyboard );
  • 13. Rendering Callback • Do all of our drawing here glutDisplayFunc( display ); void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); glBegin( GL_TRIANGLE ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); glVertex3fv( v[2] ); glEnd(); glutSwapBuffers(); }
  • 14. Idle Callbacks • Use for animation and continuous update glutIdleFunc( idle ); void idle( void ) { t +=dt; glutPostRedisplay(); }
  • 15. User Input Callbacks • Process user input glutKeyboardFunc( keyboard ); void keyboard( char key, int x, int y ) { switch( key ) { case ‘q’ : case ‘Q’ : exit( EXIT_SUCCESS ); break; case ‘r’ : case ‘R’ : rotate = GL_TRUE; break; } }
  • 16. Elementary Rendering • Geometric Primitives • Managing OpenGL State • OpenGL Buffers
  • 17. OpenGL Geometric Primitives • All geometric primitives are specified by vertices
  • 18. Simple Example void drawRhombus( GLfloat color[] ) { glBegin( GL_QUADS ); glColor3fv( color ); glVertex2f( 0.0, 0.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 1.5, 1.118 ); glVertex2f( 0.5, 1.118 ); glEnd(); }
  • 20. Specifying Geometric Primitives • Primitives are specified using glBegin( primType ); glEnd(); • primType determines how vertices are combined GLfloat red, greed, blue; Glfloat coords[3]; glBegin( primType ); for (i =0;i <nVerts; ++i ) { glColor3f( red, green, blue ); glVertex3fv( coords ); } glEnd();
  • 21. OpenGL Color Model • Both RGBA (true color) and Color Index
  • 22. Controlling Rendering • Appearance • From Wireframe to Texture mapped
  • 23. OpenGL’s State Machine • All rendering attributes are encapsulated in the OpenGL State – rendering styles – shading – lighting – texture mapping
  • 24. Manipulating OpenGL State • Appearance is controlled by current state for each ( primitive to render ) { update OpenGL state render primitive } • manipulating vertex attributes is most common way to manipulate state – glColor*() / glIndex*() – glNormal*() – glTexCoord*()
  • 25. Controlling current state • Setting State glPointSize( size ); glLineStipple( repeat, pattern ); glShadeModel( GL_ SMOOTH ); • Enabling Features glEnable( GL_ LIGHTING ); glDisable( GL_TEXTURE_2D );
  • 26. Transformations in OpenGL • Modeling • Viewing – orient camera – projection • Animation • Map to screen
  • 27. Coordinate Systems and Transformations • Steps in Forming an Image – specify geometry (world coordinates) – specify camera (camera coordinates) – project (window coordinates) – map to viewport (screen coordinates) • Each step uses transformations • Every transformation is equivalent to a change in coordinate systems
  • 28. 3D Transformations • A vertex is transformed by 4 x 4 matrices • all affine operation are matrix multiplication • matrices are stored column-major in OGL • matrices are always post-multiplied • v • Mv Except in perspective projection, the fourth row = (0,0,0,1), w left unchanged. OpenGL uses stacks of matrices, the programmer must remember that the last matrix specified is the first applied.
  • 29. Specifying Transformations • Programmer has two styles of specifying transformations – specify matrices glLoadMatrix, glMultMatrix – specify operation glRotate, glOrtho • Programmer does not have to remember the exact matrices • check appendix of Red Book
  • 30. Programming Transformations • Prior to rendering, view, locate, and orient: – eye/camera position – 3D geometry • Manage the matrices – including matrix stack • Combine (composite) transformations • Transformation matrices are part of the state, they must be defined prior to any vertices to which they are to apply. • OpenGL provides matrix stacks for each type of supported matrix (ModelView, projection, texture) to store matrices.
  • 32. Matrix Operations • Specify Current Matrix Stack – glMatrixMode( GL_MODELVIEW or GL_PROJECTION ) • Other Matrix or Stack Operation – glLoadIdentity() glPushMatrix() glPopMatrix() • Viewport – usually same as window size – viewport aspect ratio should be same as projection transformation or resulting image may be distorted – glViewport( x, y, width, height )
  • 33. Projection Transformation • Perspective projection – gluPerspective( fovy, aspect, zNear, zFar ) – glFrustum( left, right, bottom, top, zNear, zFar ) (very rarely used) • Orthographic parallel projection – glOrtho( left, right, bottom, top, zNear, zFar) – gluOrtho2D( left, right, bottom, top ) – calls glOrtho with z values near zero • Warning: for gluPerspective() or glFrustum(), don’t use zero for zNear!
  • 34. Applying Projection • Transformations • Typical use ( orthographic projection) glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( left, right, bottom, top, zNear, zFar );
  • 35. Viewing Transformations • Position the camea/eye in the scene • To “fly through” a scene • change viewing transformation and redraw scene gluLookAt( eye x ,eye y ,eye z , aim x ,aim y ,aim z , up x ,up y ,up z ) • up vector determines unique orientation • careful of degenerate positions
  • 36. Modeling Transformations • Move object – glTranslate{fd}( x, y, z ) • Rotate object aro nd arbitrary axis – glRotate{fd}( angle, x, y, z ) – angle is in degrees • Dilate (stretch or shrink) object – glScale{fd}( x, y, z ) y x
  • 37. Projection is left handed • Projection transformation (gluPerspective, glOrtho) are left handed – think of zNear and zFar as distance from view point • Everything else is right handed, including the vertexes to be rendered
  • 38. Common Transformation Usage • Example of resize() routine – restate projection & viewing transformations • Usually called when window resized • Registered a callback for glutReshapeFunc()
  • 39. resize(): Perspective & LookAt void resize( int w, int h ) { glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 65.0, (GLfloat) w / h, 1.0, 100.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); }