Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi cs4160 Many slides...
-
date post
21-Dec-2015 -
Category
Documents
-
view
215 -
download
1
Transcript of Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi cs4160 Many slides...
Computer Graphics (Fall 2003)
COMS 4160, Lecture 6: OpenGL 2
Ravi Ramamoorthi
http://www.cs.columbia.edu/~cs4160
Many slides courtesy Greg Humphreys
State
• OpenGL is a big state machine• State encapsulates control for operations like:
– Lighting
– Shading
– Texture Mapping
• Boolean state settings can be turned on and off with glEnable and glDisable
• Anything that can be set can be queried using glGet
State Management
• glEnable, glDisable, glIsEnabled
• glGet
• Attributes– glPointSize, glFrontFace, glCullFace,
• Other advanced topics (later)– Vertex Arrays– Display Lists– Curved surfaces
Immediate vs. Retained Mode
• Two ways of specifying what is to be drawn– Immediate Mode
• Primitives are sent to the display as soon as they are specified
• Graphics system has no memory of drawn graphics primitives
– Retained Mode• Primitives placed in display lists
• Display lists can be kept on the graphics server
• Can be redisplayed with different graphics state
• Almost always a performance win (if you can get away with it)
Event Driven Interaction
• OpenGL does not dictate any particular model of interaction
• Applications respond to events generated by devices (i.e., mice) and window system events (i.e., window resized)
• Events are usually placed in a queue awaiting action
• Callbacks let you associate a function with a particular type of event– Mouse callback
Usual Interactions
• Initialization– Open / place / resize window
• Expose/resize/hide window• Mouse
– Button click– Button release– Mouse motion– Mouse drag (motion + button)
• Keyboard– What key was pressed (or released)?– Where was the mouse?– Were any modifier keys pressed? (control, alt, shift)
GLUT
• OpenGL Utility Toolkit
• Written by Mark Kilgard
• Provides basic window system interaction:– Open/close window– Mouse/keyboard callbacks– “Idle” callback for animation– Menus
Typical “main” Function
int main( int argc, char *argv[] ){
glutInit( &argc, argv );glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );glutCreateWindow( “A window” );glutDisplayFunc( display );glutReshapeFunc( reshape );glutMouseFunc( mouse );glutIdleFunc( idle );glutMainLoop();
}
Viewing (Chapter 3)
• Two parts– Object positioning (GL_MODELVIEW)
– Projection (GL_PROJECTION)
• Transformation stages (pp 98)• Perspective, Orthographic transformations• Camera always at origin, pointing –z direction• Transforms applied to objects
– Equivalent to moving camera by inverse…
– More details next…
Transformations
• Object in world coordinates (modelview)– glTranslatef(x,y,z) ; glRotatef(θ,x,y,z) ; glScalef(x,y,z)
– Right-multiply current matrix (last is first applied)
• Matrix Stacks– glPushMatrix, glPopMatrix, glLoad
– Useful for Hierarchically defined figures
• gluLookAt (fromv,atv,upv)– Usually in projection matrix, sometimes in modelview
– Concatenate with perspective (orthographic) projection
Sample Code
glMatrixMode( GL_MODELVIEW );glLoadIdentity();glTranslatef( 1, 1, 1 );glRotatef( 90, 1, 0, 0 );DrawObject();
Positioning the Camera
• Use gluLookAt to specify:– Eye location– “Look-at point”– “up” vector
• gluLookAt( 10, 10, 10, 1, 2, 3, 0, 0, 1 )– Eye point is (10, 10, 10)– Look at point is (1,2,3)– Up vector is (0,0,1)
• This is usually done in the GL_PROJECTION matrix, and combined with a perspective matrix
Viewing Volumes
• Orthographic projection– glOrtho( left, right, bottom, top, front, back ) specifies
the boundaries of the parallel viewing volume– Objects are clipped to the specified viewing cube
• Perspective Projection– glFrustum, gluPerspective– Clipping volume is a frustum
• Make sure the near and far clipping planes aren’t too far apart
• Make sure the near plane isn’t too close to the eye
Complete Viewing Example
//Projection firstglMatrixMode( GL_PROJECTION );glLoadIdentity();gluPerspective( 60, 1, 1, 100 );gluLookAt( 10, 10, 10, 1, 2, 3, 0, 0, 1 )
//Now object transformationsglMatrixMode( GL_MODELVIEW );glLoadIdentity();glTranslatef( 1, 1, 1 );glRotatef( 90, 1, 0, 0 );DrawObject();
Matrix Stacks
• OpenGL has multiple matrix “stacks”
• glPushMatrix pushes a copy of the top-of-stack matrix
• glPopMatrix throws away the top of the stack
• Very useful for hierarchichally defined figures
Color (Chapter 4)
• RGBA– 8 (or whatever number) bits/color channel
– Alpha A important for transparency
– 32 bits, 16 million colors
• Color Index– Index into color table, small number of colors (256)
• Shading model glShadeModel– GL_FLAT, GL_SMOOTH (Goraud)
– Issues in smooth shading with color index?
Drawing Primitives
• Specify primitives using glBegin and glEnd:
glBegin( primitive_type );…specify vertex attributes…draw vertices
glEnd();
• primitive_type specifies points, lines, triangles, quads, polygons, etc…
• GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, GL_POLYGON,…
Specifying Vertices
• glVertex{size}{type}{vector}e.g. glVertex3fv
glVertex2f( 1.0f, 1.0f );glVertex3i( 20, 20, 20 );
float verts[4] = { 1.0, 2.0, 3.0, 1.0 };glVertex4fv( verts );
Coordinates are passed in an arrayCoordinate types are float3 coordinates passed (x,y,z)
Example: A Wireframe Cube
GLfloat vertices[][3] = {{-1,-1,-1},{1,-1,1},{1,1,-1},{-1,1,-1},{-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1}
};
void cube(void){
polygon( 1,0,3,2 ); polygon( 3,7,6,2 );polygon( 7,3,0,4 ); polygon( 2,6,5,1 );polygon( 4,5,6,7 ); polygon( 5,4,0,1 );
}
Example: A Wireframe Cube
void polygon( int a, int b, int c, int d ){
glColor3f( 1,1,1 );glBegin( GL_LINE_LOOP ); glVertex3fv( vertices[a] ); glVertex3fv( vertices[b] ); glVertex3fv( vertices[c] ); glVertex3fv( vertices[d] );glEnd();
}
Specifying Vertex Attributes
• Vertex attributes are state settings that are usually applied between a glBegin/glEnd pair
• Vertex attributes are set using:
glColor, glNormal, glTexCoord, glEdgeFlag
• Vertex attribute routines take a form similar to glVertex:
glName{size}{type}{vector}e.g. glColor3us takes 3 unsigned short parameters
Pixel Primitives
• Provide a way to manipulate rectangles of pixels• glDrawPixels, glReadPixels, glCopyPixels move
pixel rectangles to and from the framebuffer• glBitmap takes a binary image and renders the
current color in framebuffer positions corresponding to 1’s in the image. This might be useful for drawing fonts
• glRasterPos defines where the pixels go in the framebuffer
• The interaction between glRasterPos and glBitmap is subtle and confusing
Hidden Surface Removal
• When we draw a fragment, record the z (distance to the eye) in the depth buffer
• If the z stored in the depth buffer is greater than the z for the fragment about to be drawn, draw it
• Otherwise, the fragment is behind something that has already been drawn, so throw it away
Hidden Surface Removal
• When setting up your window, specify a depth buffer:
glutInitDisplayMode( GLUT_DEPTH );
• When clearing, make sure to:glClear( GL_DEPTH_BUFFER_BIT );
• glEnable( GL_DEPTH_TEST );
• Set the depth test comparison operation:glDepthFunc( GL_LESS ); (this is the default)