Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• Numerical Data
- Actual coordinates of vertices, etc.
• Topological Data
- Details of what is connected to what, etc.
1
Data Structures Data Structures
n n type0
• Static Data Structure: x1, y1, z1 p1 offset0
- The point data is stored in arrays p2 type1
p3 offset1
- The topological data are arrays of array indices
Overview
2
Aims OpenGL and GLUT Overview
• Demonstrate enough OpenGL to write an interactive • What is OpenGL & what can it do for me?
graphics program with • OpenGL in windowing systems
- custom modeled 3D objects or imagery • Why GLUT
- lighting
• A GLUT program template
- texture mapping
• Introduce advanced topics for future investigation
Texture
Memory
Pixel
Operations
3
OpenGL as a Renderer Related APIs
• Headers Files
#include <GL/gl.h>
application program
#include <GL/glu.h>
#include <GL/glut.h>
OpenGL Motif
widget or similar GLUT • Libraries
GLX, AGL
or WGL GLU • Enumerated Types
- OpenGL defines numerous types for compatibility
X, Win32, Mac O/S GL
–GLfloat, GLint, GLenum, etc.
4
GLUT Basics Sample Program
• Set up whatever state you’re going to use • Routine to call when something happens
- window resize or redraw
void init( void ) - user input
{
glClearColor( 0.0, 0.0, 0.0, 1.0 ); - animation
glClearDepth( 1.0 );
• “Register” callbacks with GLUT
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST ); glutDisplayFunc( display );
} glutIdleFunc( idle );
glutKeyboardFunc( keyboard );
5
Rendering Callback Idle Callbacks
• Do all of your drawing here • Use for animation and continuous update
6
OpenGL Geometric Primitives Simple Example
void drawRhombus( GLfloat color[] )
{
glBegin( GL_QUADS );
GL_LINES glColor3fv( color );
GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP glVertex2f( 0.0, 0.0 );
GL_POINTS
glVertex2f( 1.0, 0.0 );
glVertex2f( 1.5, 1.118 );
glVertex2f( 0.5, 1.118 );
GL_TRIANGLES glEnd();
}
GL_QUADS
GL_QUAD_STRIP
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
7
Shapes Tutorial Controlling Rendering Appearance
• All rendering attributes are encapsulated in the • Appearance is controlled by current state
OpenGL State for each ( primitive to render ) {
- rendering styles update OpenGL state
render primitive
- shading }
- lighting
• Manipulating vertex attributes is most common way to
- texture mapping manipulate state
glColor*()
glIndex*()
glNormal*()
glTexCoord*()
8
Controlling current state Transformations in OpenGL
9
Coordinate Systems and Transformations Affine Transformations
- w is usually 1.0
- all operations are matrix multiplications
- directions (directed line segments) can be represented with w =
0.0
10
Transformation
Specifying Transformations Poly.
Per
Vertex
Pipeline CPU DL
Texture
Raster Frag FB
transformations
object eye clip normalized window
- specify matrices (glLoadMatrix, glMultMatrix) device
- specify operation (glRotate, glOrtho) v
e
• Prior to rendering, view, locate, and orient: r Modelview Projection Perspective Viewport
t
- eye/camera position e Matrix Matrix Division Transform
x
- 3D geometry
Modelview Projection
• Manage the matrices • other calculations here
- material color
- including matrix stack Modelview - shade model (flat)
- polygon rendering mode
• Combine (composite) transformations - polygon culling
- clipping
11
Viewing Transformations Projection Tutorial
12
Projection is left handed Common Transformation Usage
y
y z+
left handed right handed
x
x
z+
13
resize(): Ortho (part 1) resize(): Ortho (part 2)
Double
Compositing Modeling Transformations Poly.
Per
Vertex
Buffering CPU DL
Texture
Raster Frag FB
Pixel
• Problem 1: hierarchical objects
- one position depends upon a previous position
- robot arm or hand; sub-assemblies
• Solution 1: moving local coordinate system
- modeling transformations move coordinate system
- post-multiply column-major matrices 1
2
1
2
4 4
- OpenGL post-multiplies matrices Front 8
16
8
16
Back
Buffer Buffer
Display
14
Depth Buffering and
Animation Using Double Buffering
Hidden Surface Removal
• Request a double buffered color buffer
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
• Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );
• Render scene
• Request swap of front and back buffers 1
2
1
2
4 4
glutSwapBuffers(); Color 8 8 Depth
16 16
Buffer Buffer
• Repeat steps 2 - 4 for animation
Display
15
An Updated Program Template (cont.) An Updated Program Template (cont.)
void drawScene( void )
void init( void ) {
{ GLfloat vertices[] = { … };
glClearColor( 0.0, 0.0, 1.0, 1.0 ); GLfloat colors[] = { … };
} glClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
glBegin( GL_TRIANGLE_STRIP );
void idle( void )
{ /* calls to glColor*() and glVertex*() */
glutPostRedisplay();
} glEnd();
glutSwapBuffers();
}
Texture Poly.
Per
Vertex Texture Mapping
Mapping CPU DL
Texture
Raster Frag FB
Pixel
y
• Apply a 1D, 2D, or 3D image to geometric
primitives
z x
• Uses of Texturing geometry screen
- simulating materials
- reducing geometric complexity
- image warping t
image
- reflections
s
Graphics Lecture 6: Slide 63 Graphics Lecture 6: Slide 64
16
Texture Mapping and the OpenGL Pipeline Texture Example
• Images and geometry flow through separate pipelines • The texture (below) is a
that join at the rasterizer 256 x 256 image that has been
- “complex” textures do not affect geometric complexity mapped to a rectangular
polygon which is viewed in
perspective
vertices geometry pipeline
rasterizer
image pixel pipeline
17
Texture Objects Texture Objects (cont.)
• Like display lists for texture images • Create texture objects with texture data and state
- one image per texture object glBindTexture( target, id );
- may be shared by several graphics contexts • Bind textures before using
• Generate texture names glBindTexture( target, id );
glGenTextures( n, *texIds );
Image CPU DL
Texture
Raster Frag FB Texture CPU DL
Texture
Raster Frag FB
Pixel Pixel
18
Tutorial: Texture Texture Application Methods
• Filter Modes
- minification or magnification
- special mipmap minification filters
• Wrap Modes
- clamping or repeating
• Texture Functions
- how to mix primitive’s color with texture’s color
• blend, modulate or replace texels
19
Wrapping Mode Texture Functions
• Query largest dimension of texture image • Lighting simulates how objects reflect light
- typically largest square texture - material composition of object
- doesn’t consider internal format size
- light’s color and position
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &size )
- global lighting parameters
• Texture proxy • ambient light
- will memory accommodate requested texture size? • two sided lighting
- no image specified; placeholder
- available in both color index
- if texture won’t fit, texture state variables set to 0
and RGBA mode
• doesn’t know about other textures
• only considers whether this one texture will fit all of memory
20
Surface
How OpenGL Simulates Lights Poly.
Per
Vertex
Normals CPU DL
Texture
Raster Frag FB
Pixel
• Phong lighting model
- Computed at vertices
• Normals define how a surface reflects light
• Lighting contributors glNormal3f( x, y, z )
- Surface material properties
- Current normal is used to compute vertex’s color
- Light properties
- Use unit normals for proper lighting
- Lighting model properties • scaling affects a normal’s length
glEnable( GL_NORMALIZE )
or
glEnable( GL_RESCALE_NORMAL )
21
Light Sources (cont.) Types of Lights
22
Controlling a Light’s Position Light Position Tutorial
23
Light Model Properties Tips for Better Lighting
glLightModelfv( property, value ); • Recall lighting computed only at vertices
• Enabling two sided lighting - model tessellation heavily affects lighting results
GL_LIGHT_MODEL_TWO_SIDE • better results but more geometry to process
• Global ambient color • Use a single infinite light for fastest lighting
GL_LIGHT_MODEL_AMBIENT - minimal computation per vertex
• Local viewer mode
GL_LIGHT_MODEL_LOCAL_VIEWER
• Separate specular color
GL_LIGHT_MODEL_COLOR_CONTROL
24