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.

Slides:



Advertisements
Similar presentations
Computer Graphics - Graphics Programming -
Advertisements

Department of nskinfo i-education
02/17/05CISC640/440 OpenGL Tutorial1 OpenGL Tutorial CISC 640/440 Computer Graphics TA: Qi Li/Mani Thomas
Chapter 2: Graphics Programming
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Goals of the course: 1. to introduce real-time 3D graphics programming with openGL.
Computer Graphics CSCE 441
An Interactive Introduction to OpenGL Programming Dave Shreiner Ed Angel Vicki Shreiner.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner OpenGL, GLUT, Transformations.
CENG477 Introduction to Computer Graphics Introduction to OpenGL, GLUT and GLUI.
Teaching a Graphics Class with OpenGL: Two Approaches
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
CSE 494/598 Intro to Applied Computer Graphics Anshuman Razdan DCST AR's Web Page AR's Web Page
OpenGL (II). How to Draw a 3-D object on Screen?
Introduction to OpenGL Pipeline From Programmer View Tong-Yee Lee.
Today … The rendering pipeline in detail What is OpenGL
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
Computer Graphics Bing-Yu Chen National Taiwan University.
1 Computer Graphics Dr. Amy Zhang Assistant Professor of Computer Science United International College.
1 GLUT Callback functions Event-driven: Programs that use windows  Input/Output  Wait until an event happens and then execute some pre-defined functions.
Open GL Programming Speaker: 彭任右 Date: 2005/10/3.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2010 Tamara Munzner GLUT, Transformations.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Computer Graphics Bing-Yu Chen National Taiwan University.
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
OpenGL: Introduction Yanci Zhang Game Programming Practice.
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
Computing & Information Sciences Kansas State University CG Basics 3 of 8: OpenGL Primer 1 CIS 636/736: (Introduction to) Computer Graphics CIS 636 Introduction.
Chap 3 Viewing and Transformation
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
CS552: Computer Graphics Lecture 6: Viewing in 2D.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to Graphics Programming. Graphics API.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL LAB III.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
The Human Visual System vs The Pinhole camera
Reference1. [OpenGL course slides by Rasmus Stenholt]
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
An Interactive Introduction to OpenGL Programming
An Interactive Introduction to OpenGL Programming
גרפיקה ממוחשבת: מבוא ל-OpenGL
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
An Interactive Introduction to OpenGL Programming
Presentation transcript:

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 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, ); glVertex2f( 0.5, ); 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 zNea r!

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, ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); }