Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction CE325: 3D Computer Graphics 2. Coordinate Systems Adrian F. Clark alien@essex.ac.uk VASE Laboratory, School of Computer Science and Electronic Engineering University of Essex 2011–12 /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Outline 1 Coordinate systems 2 Defining the camera 3 Pre-defined objects 4 Camera motion 5 Interaction /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction The viewport The rectangular window in which OpenGL draws is known as the viewport. When OpenGL creates a window, the viewport is set to fill it. The bottom left corner of the viewport is given in window coordinates, along with the viewport’s width and height. glViewport (x, y, width, height) This allows you to create several displays in the same window. If the camera’s aspect ratio does not match the viewport, the displayed image will appear distorted. This is why the viewport is normally set in the reshape callback, which we shall discuss when we look at transformations. /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction World coordinates and handedness In 3D, coordinate systems may be either left- or right-handed Maths and most computer graphics use right-handed coordinates: if x runs along the screen and y up is, then z is out of the screen It is important that you do not confuse the coordinate systems of the viewport (pixels in the window) with /home/alien/environment/tex/cesthe x, y , z of the world Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Setting the camera The easiest way to set the position and orientation of the OpenGL camera is via gluLookAt: gluLookAt (camx, camy, camz, px, py, pz, upx, upy, upz); This sets the camera to be at position (camx, camy, camz), looking at (px, py, pz) and with the up direction being given by (upx, upy, upz) If gluLookAt isn’t called, the camera is positioned at the origin, looks down the negative z-axis, and has its up direction parallel to the y -axis — equivalent to gluLookAt (0.0, 0.0, 0.0, // camera position 0.0, 0.0, -1.0, // where we’re looking 0.0 1.0 0.0) // up direction /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Pre-defined objects [see opengl-04.c] Rectangles are so common that there is a dedicated primitive for drawing them void glRectf (GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) which draws a rectangle in the plane z = 0 with sides parallel to the x and y axes. To draw a wireframe or solid cube, you can use void glutWireCube (GLdouble size) void glutSolidCube (GLdouble size) to produce a cube of size size centred at the origin /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction To draw a wireframe or solid sphere, you can use void glutWireSphere (GLdouble radius, GLint slices, GLint stacks) void glutSolidSphere (GLdouble radius, GLint slices, GLint stacks) to produce a sphere centred at the origin. There are also GLUT primitives for drawing the cone, torus, tetrahedron, octahedron, dedecahedron, and icosahedron. One of the classic computer graphic objects is Newell’s teapot, and GLUT provides an easy way to draw it: void glutWireTeapot (GLdouble scale) void glutSolidTeapot (GLdouble scale) to produce a teapot scaled by scale centred at the origin. /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Looking around an object [see opengl-05.c] We can use gluLookAt to move the viewpoint around an object There’s a little geometry in this: x = r cos θ z = r sin θ As θ changes from frame to frame, our viewpoint of the object changes too The code to manipulate θ lies in the idle callback /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Double-buffering The program also introduces the idea of double-buffered: it draws into an off-screen frame buffer and then swaps the off-screen one for the on-screen one as the screen refreshes This involves setting the display mode using glutInitDisplayMode and invoking glutPostRedisplay whenever the buffer-swapping is to occur Why do we need double-buffering? If we don’t, updates will be jerky and the model will appear to flash or flicker /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Keyboard interaction [see opengl-06.c] We have already used the keyboard callback to handle keypresses for interaction. In fact, OpenGL calls this routine only for keystrokes that have ASCII representations. Other keystrokes such as the arrow keys found on most keyboards (which typically generate sequences of ASCII codes) result in the invocation of the special character callback. /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Mouse interaction Most programs will actually involve interaction with the mouse rather than the keyboard. This is achieved by the mouse callbacks, registered with: glutMouseFunc for mouse clicks glutMotionFunc for mouse movements with a button pressed glutPassiveFunc for mouse movements with a button not pressed OpenGL also provides functions for defining menus, both attached and pop-up; but this is not the appropriate place to go into details of their use. /home/alien/environment/tex/ces- Coordinate systems Defining the camera Pre-defined objects Camera motion Interaction Where do we go from here? We’ve got about as far as we can without knowing some geometry For example, we cannot explore the shading of 3D objects without knowing how to work out normal vectors So the next thing we’ll do is go over some trigonometry, then we’ll move on to think about vectors, and then transformations /home/alien/environment/tex/ces-