Overview. Today: - Make sure everyone is set up with an O penGL environment - OpenGL basics: shapes, lighting, textures - P ushing/ Popping modelview stacks Proble m Set 1, due April 13th : 1. create a Camera class which allows users to navigate 3D space interactively
- Make sure everyone is set up with an OpenGL environment
- OpenGL basics: shapes, lighting, textures
- Pushing/Poppingmodelview stacks
Problem Set 1, due April 13th:
1. create a Camera class which allows users to navigate 3D space interactively
2. create a scene graph data structure
3a. create an interactive visual space that emulates existing work
3b. create an animated version of the above
Last week we talked about how a vertex is transformed from 3D space to a 2D projection onto a display by multiplying it through the graphics pipeline.
The pipeline is split into two operations: vertex operations and pixel (or fragment) operations.
Vertex operations control the position of geometry.
Fragment operations control the way the geometry looks (ie, lighting, blending, texturing), ultimately specifying a color for each pixel.
OpenGL gives you limited control over how your GPU executes these operations. GLSL shader programs give you more control.
Most OpenGL environments simplify the management of the OpenGL context within a windowed application.
init (happens once)
set up camera lens / projection matrix
load textures from disk
reshape (happens when screen is resized)
re-set projection matrix based on new size of screen
display (happens 60 times per second)
position drawing cursor coordinates
listen for mouse, keyboard, etc
Define camera attributes and set Projection matrix
- glViewport, usually just the screen bounds
- glPerspective, usually near,far = .1, 100; fovy=45, aspect ratio = w/h
or use glFrustum
or create an array of floats and load it using glLoadMatrix
glLightfv defines one of the characteristics for one of the lights
ambient = general background light
diffuse = light from a source that scatters uniformly when it bounces off an object
specular = light from a source that scatters in a particular direction
// define a light
glLightfv(GL_LIGHT1, GL_AMBIENT,ambient, 0);
glLightfv(GL_LIGHT1, GL_DIFFUSE,diffuse, 0);
glLightfv(GL_LIGHT1, GL_POSITION,lightPosition, 0);
Loading textures by hand is kind of a pain. OpenGL environments generally provide helper methods.
a texture is just an array of data, can be used for images, depth maps, luminance maps, etc
enable textures and generate texture ids
bind a specific texture id
load image from disk
put it into a texture object – usually 2D, RGBA format
set texture attributes (eg, linear filtering, clamping)
textures are copied directly onto the video card, so drawing them is “hardware-accelerated”
glGenTextures(3, int, 0); //bind 3 textures to IDs
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(textures, 0, GL_RGBA, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgPixelData);
To draw a texture you “bind” it to your geometry and then position it in relation to vertices.
Texture coordinates are always normalized to between 0 and 1.
glTexCoord2f(0.0f, 0.0f);glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-1.0f, 1.0f, 1.0f);
These are convenience methods to let you save state during drawing.
- set up camera view by moving the cursor 10 units away
glTranslate3f(0f, 0f, -10f)
glRotatef(45f, 0f, 1f, 0f); glScale3f(2f, .5f, 0f); glTranslate3f(1f, -2f, 3f);
You can nest a large number of these modelviews on the stack with no loss of performance. Used in scene graphs.