470 likes | 592 Vues
CS 470 Introduction to Computer Graphics. Basic 3D in OpenGL. OpenGL in 3D. Recall that in OpenGL 2D was a special case of 3D …so in some respects 3D is like 2D but we add a third coordinate (z) to represent the third dimension…
E N D
CS 470 Introduction toComputer Graphics Basic 3D in OpenGL
OpenGL in 3D • Recall that in OpenGL 2D was a special case of 3D • …so in some respects 3D is like 2D but we add a third coordinate (z) to represent the third dimension… • …this opens up a number of issues that were not present when we looked at 2D.
OpenGL in 3D • An import conceptual foundation behind OpenGL is the use of a “virtual” camera… • The camera is an independent object (form other objects) in 3D space… • We do not see the camera, but… • how we position, orient and adjust the virtual camera determines how we see our objects and how they are rendered.
OpenGL in 3D • The image of 3D objects are imposed on a 2D plane of the camera (which is a 3D object)… • This 2D plane, called a projection plane, can be thought of as the film plane of the camera. • This process is called “projection”
OpenGL in 3D • Lines, called projectors are drawn (projected) from each point on each 3D object to the camera… • …the lines pass through the center of the camera’s “lens” or center of projection… • … the place where the projectors intercept the camera’s projection-plane defines how the 3D image appears on a 2D display… • However, conceptually the film-plane in this camera sits in front of the camera.
OpenGL in 3D • Remember the 2D Clipping window?… • in 3D we have a clipping volume… • Just like a real camera – the clipping volume defines the left, right top and bottom boundaries of what the camera can see… • …unlike are a real camera, the clipping volume has a front and back boundary… • known as the near and far clipping planes.
OpenGL in 3D • The left, right, top, bottom, near and far clipping planes defines what, of the scene, can be seen and not seen. • objects that fall inside these planes can be seen in the rendered image… • object that fall outside of these planes can not be seen.
OpenGL in 3D • The clipping volumes define a truncated pyramid shape known as a frustrum. • Objects inside frustrum appear on the rendered image.
OpenGL in 3D • Imagine that the center of projection (camera’s lens) in very close to the near clipping plane… • … the angle of the projectors would be relatively sharp… • Imagine on the other hand that the center of projection (the lens) is placed infinitely far away from the projection plane… • The projectors would all be relatively parallel… • This is known as “orthographic” projection.
OpenGL in 3D void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); defines the clipping (viewing) volume and establishes an orthographic projection matrix that is a right parallelepiped -defined in world coordinates.
OpenGL in 3D • Normal used … glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); …
OpenGL in 3D void gluLookAt( GLdouble eyex, GLdouble eyey, GLDouble eyez, GLdouble atx, GLdouble aty, GLdouble atz, GLdouble upx, GLdouble upy, GLdouble upz); defines the camera location (eye) points the camera (at) orients the camera (up) – which way is up
OpenGL in 3D void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutWireCube(0.5); glutSwapBuffers(); }
OpenGL in 3D Note: gluLookAt performs the necessary rotations and translations to properly view the objects.
OpenGL in 3D void glutWireCube(GLdouble size); defines a wireframe cube centered on the origin with its sides of size size aligned with the coordinate axis. void glutSolidCube(GLdouble size); same as glutWireCube excepts defines a solid cube.
OpenGL in 3D void glVertex3{s|i|f|d}(TYPE x, TYPE y, TYPE z); defines the location of a vertex in 3D space. glVertex3f( x, y); void glVertex3{s|i|f|d}v(TYPE *coordvec); same as glVertex3* except that the vertex coordinates are in the array coordvec.
OpenGL in 3D glFrustrum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); Creates a perspective viewing/clipping volume and generates a perspective projection matrix.
OpenGL in 3D • GLU Quadrics
OpenGL in 3D GLUquadricObj* gluNewQuadric(); defines a new quadric object and returns a pointer to it.
OpenGL in 3D void gluDeleteQuadic(GLUquadricObj *obj); Deletes the quadric object obj
OpenGL in 3D void gluQuadricDrawStyle(GLUquadricObj *obj, GLenum style); Sets the drawing style for the quadric obj. Style can be GLU_POINT, GLU_LINE, GLU_FILL, or GLU_SILHOUETTE.
OpenGL in 3D void gluQuadricNormals(GLUquadricObj *obj, GLenum mode); defines the normal mode for quadric object obj. model can be GLU_NONE, GLU_FLAT, GLU_SMOOTH.
OpenGL – 3D gluSphere(GLUquadricObj *obj, GLdouble radius, GLint slides, GLint stacks); Creates a sphere centered at the origin with radius radius. It has slices longitude and stacks latitude.
OpenGL in 3D void gluCylinder(GLUquadricObj *obj, GLdouble base, GLdouble top, GLdouble height, GLdouble slices, GLdouble stacks); defines a quadric cylinder with height aligned with the z axis and base is at z=0.
OpenGL in 3D void gluDisk(GLUquadricObj *obj, GLdouble inner, GLdouble outer, GLint slices, GLint rings); defines a disk in the plane z=0 formed from concentric rings.
OpenGL in 3D void gluPartialDisk(GLUquadricObj *obj, GLdouble inner, GLdouble outer, GLint slices, GLint rings, GLdouble start, GLdouble angle); Creates a partial disk with a gap starting at start and continuing for angle degrees.
OpenGL in 3D GLUT Objects
OpenGL in 3D void glutWireSphere(GLdouble radius, GLint slices, GLint Stacks); void glutSolidSphere (GLdouble radius, GLint slices, GLint Stacks); defines an approximation to a sphere centered at the origin with the specified radius, number of slices and stacks. Former defines a wireframe sphere and the latter a solid sphere.
OpenGL in 3D void glutWireCone(GLdouble base, GLdouble height, GLint slices, GLint stacks); void glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks); defines a cone with a base in plane z=0 and height units high. It will have slices slices and stacks stacks. The former will create a wireframe cone and the latter will create a solid cone.
OpenGL in 3D void glutWireTorus(GLdouble inner, GLdouble outer, GLint sides, GLint slices); void glutWireTorus(GLdouble inner, GLdouble outer, GLint sides, GLint slices); defines a 3D torus with the inside (hole) diameter of inner and the outside diameter of outer. It will have sides sides (radial sections) and slices slices (around the torus). The former will create a wireframe torus and the latter will create a solid torus.
OpenGL in 3D void glutWireTetrahedron();void glutSolidTetrahedron(); defines a tetrahedron (four sides) object with a vertices on a sphere with a radius of 1. The former creates a wireframe tetrahedron, while the latter creates a solid tetrahedron.
OpenGL in 3D • Other solid glut objects glutWireOctahedron() -- 8 sides glutSolidOctahedron() -- 8 sides glutWireDodecahedron() – 12 sides glutSolidDodecahedron() – 12 sides glutWireIcosahedron() -- 20 sides glutSolidIcosahedron() -- 20 sides
OpenGL in 3D void glutWireTeapot(GLdouble size); void glutSolidTeapot(GLdouble size); defines a Utah teapot of size size centered at the origin. The former creates a wireframe teapot and the latter creates a solid teapot.
OpenGL in 3D – Perspective Projection (again) void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far); defines a perspective projection and cooresponding clipping volume. Also defines the projection matrix for a perspective projection.
OpenGL in 3D …gluPerspective()… fovy is the field of view in the yz plane. fovy can be in the range of 0.0 to 180.0 aspect if the aspect ratio of the frustrum. = width/height near, far same meaning as in glOrtho and glFrustrum. near, far in world coordinates relative to camera.
OpenGL in 3D • back to drawing objects…
OpenGL in 3D cube() { glColor3f(0.0, 1.0, 0.0); /* build one face of cube */ glBegin(GL_POLYGON); glVertex3f(-1.0, -1.0, -1.0); glVertex3f(-1.0, 1.0, -1.0); glVertex3f(-1.0, 1.0, 1.0); glVertex3f(-1.0, -1.0, 1.0); glEnd(); /* build second face of cube */ … } void display() { glClear(COLOR_BUFFER_BIT); cube(); }
OpenGL in 3D • using arrays…. GLFLoat vlist[][3] = {{-1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0}… } example from Angel, 2002
OpenGL in 3D 5 6 1 2 4 7 0 3 …example from Angel, 2002
OpenGL in 3D void polygon(int a, int b, int c, int d); { glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3fv(vlist[a]); glVertex3fv(vlist[b]); glVertex3fv(vlist[c]); glVertex3fv(vlist[d]); glEnd(); } …example from Angel, 2002
OpenGL in 3D void cube() { polygon(0, 3, 2, 1); polygon(2, 3, 7, 6); polygon(3, 0, 7, 6); polygon(1, 2, 6, 5); polygon(4, 5, 6, 7); polygon(5, 4, 0, 1); }
OpenGL in 3D • using Vertex arrays • can simplify the creation of 3D objects • must do three steps … • enable vertex arrays • define the format of the array • use the array to define/render object
OpenGL in 3D void glEnableClientState(GLenum array_type); void glDisableClientState(GLenum array_type); Enables/disables the functionality of certain array types. array_type can be of several types. Here we will only look at GL_VERTEX_ARRAY
OpenGL in 3D void glVertexPointer(GLint dim, GLenum type, GLsizei stride, GLvoid *array); associates properties of a vertex array with with the array array dim is the dimension of the data type is the GL data type (i.e. GLfloat) stride is the number of bytes between data values
OpenGL in 3D GLubyte indices[]={0,3,2,1, 2,3,7,6, 0,4,7,3, 1,2,6,5, 4,5,6,7, 0,1,5,4}; void glDrawElements(GLenum mode, GLsizei n, GLenum type, void *indices); draws object defined by the vertex array indices with n dimension data, of type type using mode mode.