GLART week 2: Geometry

Explore the OpenGL coordinate system by creating simple geometry and transforming the "model view" with the translate, rotate and scale commands. Use the OpenGL matrix stack to manage the additive nature of transform commands. Assigment: Translate, rotate and scale Create a simple animated "solar system". Reading: Redbook: in Chapter 2: "Describing Points, Lines, and Polygons", pages 37-47 (stop at "Basic State Management" or "Displaying Points, Lines, and Polygons) Redbook: in Chapter 3: "Viewing and Modelling Transformations", pages 106-113 (stop at "Viewing Transformations") Examples: translate, rotate, scale - use transformations to position an arrow pushMatrix and popMatrix - isolate the effect of transformations Basic solar system - use translate, rotate and scale to create orbits Images: Geometric paintings See also: nehe tutorials 2-5 http://nehe.gamedev.net/lesson.asp?index=01 nate robbins transformation tutorial an interactive tutorial you can run to see transformations in action download the full set of tutorials and run the "transformation" executable http://www.xmission.com/~nate/tutors.html opengl FAQ -- Transformations: http://www.opengl.org/resources/faq/technical/transformations.htm Matrix Transformation tutorials I found these online, they provide some more detail on matrix math and 3D programming. Explanation of matrix contents Example matrices for model transforms Matrix Transformations Tutorial with code 3D Vector Math Tutorial Understanding Flash Transformations (2D matrix use in Flash) NOTES: ----------------------------------------------------------------------- Transformations ------------------- OpenGL coordinate system can be altered: moved, rotated, stretched using the functions: glTranslate(), glRotate(), glScale() These functions change the position and nature of the coordinate system. All rendering that follows is affected by the change to the coordinate system. Translate, rotate, scale are additive. The order of the commands changes the resulting orientation and position of geometry. see Red book pages 106-113 Remember, transformations don't act on the geometry as if it's a persistent object. Transformations act on the coordinate system. Picture drawing a 4x4 grid on a sheet of rubber and drawing a dot at position 2,2. If you then stretch the rubber sheet to twice it's size (glScale()), the point is still at position 2,2, but now appears to be further from the center of the sheet. Similarly, when you translate or rotate, the coordinate system is shifted, and all following glVertex commands are shifted with it. The rendering commands that executed before the translate command are not affected. glMatrixMode( GL_MODELVIEW ) --------------------------------------------- Specify which matrix stack is current. All subsequent commands that perform matrix operations will affect the current matrix. For instance, to rotate a cube you need to select the modelview matrix (glMatrixMode(GL_MODELVIEW)) before calling glRotate() so that the rotate matrix operation will be performed on the correct matrix stack. glLoadIdentity() --------------------------- return a matrix to its default state. This "clears" the matrix. When used in the GL_MODELVIEW matrix mode, this will return the origin to the center of the screen, unscaled and unrotated. glTranslate(x,y,z) --------------------- shift the origin (the entire coordinate system) by the given units. All rendering after this command will be shifted. glRotate(degrees, x,y,z) ------------------------- Rotate the coordinate system, around the origin, by the given number of degrees. The rotation will take place around either the specified axis. Specify which axis to rotate by passing a 1 in either the X, Y, or Z parameter: glRotate(90, 0,1,0); // rotate around the vertical axis (Y) (as the earth rotates) glRotate(180, 1,0,0); // rotate around the horizontal axis (X) glRotate(-45, 0,0,1); // rotate around the Z axis The coordinate system will rotate around the specified axis. This can sometimes be confusing, especially if you're combining translations and other rotations. Just think of a wheel rotating on it's axle, then picture that the axle of the wheel is the axis you chose. For instance when rotating around the Y axis, the wheel is turned so it's axle is vertical (the Y axis), and the wheel is turning on it's side (like the earth turning on it's axis). glScale(x,y,z) ------------------- Increase or decrease the size of the coordinate system. This has a similar effect to moving the viewpoint forward closer to your geometry. glPushMatrix() and glPopMatrix() ----------------------------------- Use glPushMatrix() and glPopMatrix() to isolate transformations. The translate, rotate and scale commands are additive. Each transformation adds its effect to the current state of the coordinate system, which can create complex effects when many transformations are used in sequence. glPushMatrix() preserves the current state of the coordinate system. Several transformations can be executed, and geometry rendered, then the pop command returns the coordinate system to the previous state. glu.gluLookAt() -------------------- This command sets the position of the viewpoint in your scene. This also affects the ModelView matrix. It's called the "Model-View" matrix because it controls positioning of geometry (the model) AND positioning of the eye (the viewpoint). Typically you call gluLookAt() right after glLoadIdentity() and then draw the rest of your scene: gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); // set the eye position first glu.gluLookAt( 0, 2, 4, // eye position 0, 0f, 0, // target 0f, 1f, 0f); // which way is up // then draw scene NOTE about glBegin() and glEnd() ---------------------------------- Don't put glTranslate(), glRotate() and glScale() commands inside glBegin and glEnd blocks of code. Only geometry related commands (for example glVertex() and glColor()) will work inside Begin/End. Other commands are ignored. If you want to build geometry by rotating and translating shapes, then put the rotate, translate and scale commands OUTSIDE of the glBegin/glEnd code blocks.