Overview Today: - 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
Vertex and Fragment Operations 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.
OpenGL environments Most OpenGL environments simplify the management of the OpenGL context within a windowed application. init (happens once) set up camera lens / projection matrix initialize lighting 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 camera position drawing cursor coordinates draw stuff listen for mouse, keyboard, etc
init projection matrix 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
init lighting 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 http://www.opengl.org/sdk/docs/man/xhtml/glLight.xml //enable lighting glEnable(GL_LIGHTING); // define a light glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_AMBIENT,ambient, 0); glLightfv(GL_LIGHT1, GL_DIFFUSE,diffuse, 0); glLightfv(GL_LIGHT1, GL_POSITION,lightPosition, 0);
load textures 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” glEnable(GL_TEXTURE_2D); glGenTextures(3, int, 0); //bind 3 textures to IDs glBindTexture(GL_TEXTURE_2D, textures); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(textures, 0, GL_RGBA, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgPixelData);
drawing textures 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. glBindTexture(GL_TEXTURE_2D, textureID); gl.glBegin(GL2.GL_QUADS); 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); glEnd();
Pushing and Popping the Modelview 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) • store this view glPushMatrix(); • do something which changes the modelview matrix glRotatef(45f, 0f, 1f, 0f); glScale3f(2f, .5f, 0f); glTranslate3f(1f, -2f, 3f); (draw stuff...) • return to nice simple view glPopMatrix(); You can nest a large number of these modelviews on the stack with no loss of performance. Used in scene graphs.