220 likes | 380 Views
Quadrics and Display Lists. Jason Goffeney 3/20/2006. Quadrics. The OpenGL utility library contains a set of functions for constructing polygonal quadric surfaces. The primitive shapes: Sphere Cylinder Disk Partial Disk. Quadrics: Construction.
E N D
Quadrics and Display Lists Jason Goffeney 3/20/2006
Quadrics • The OpenGL utility library contains a set of functions for constructing polygonal quadric surfaces. • The primitive shapes: • Sphere • Cylinder • Disk • Partial Disk
Quadrics: Construction • A series of steps are followed for the construction of a quadrics. • A generic quadric object is created • The quadric object’s properties are set • The quadric object is passed to one of the quadric primitive functions to create the final object
Quadrics: Construction • The generic quadric object is created by the following call. GLUquadricObj * quadObj = gluNewQuadric(); • It is destroyed by the corresponding call. gluDeleteQuadric(quadObj);
Quadrics: Construction • A single quadric object can be used in the creation of lots of primitive objects. • You don’t need to create a new one for each shape you want to make
Quadric Properties • Several properties can be set in the generic quadric object before it is used to create a primitive shape. • Drawing Style • Normal Type • Normal Orientation • Texture Coordinates
gluQuadricDrawStyle • void gluQuadricDrawStyle (GLUquadricObj * qobj, GLenum drawStyle) The quadric object GLU_POINT GLU_LINE GLU_SILHOUETTE GLU_FILL
gluQuadricNormals • void gluQuadricNormals (GLUquadricObj * qobj, GLenum normals); The quadric object GLU_NONE GLU_FLAT GLU_SMOOTH
Quadric Primitives: Sphere • void gluSphere(GLUquadricObj * qobj, GLdouble radius, GLint slices, GLint stacks)
Quadric Primitives: Cylinder • void gluCylinder(GLUquadricObj * qobj, GLdouble baseRadius, GLdouble topRadius, GLdouble height, GLint slices, GLint stacks) Note: the resulting cylinder will have an open top and bottom.
Quadric Primitives: Disk • void gluDisk(GLUquadricObj * qobj, GLdouble innerRadius, GLdouble outerRadius, GLint slices, GLint rings) inner outer
Quadric Primitives: Partial Disk • void gluPartialDisk(GLUquadricObj * qobj, GLdouble innerRadius, GLdouble outerRadius, GLint slices, GLint rings, GLdouble startAngle, GLdouble sweepAngle) The start angle is the distance from +y axis the disk will start at. The sweep angle is the distance the disk will proceed through. The stop angle is the start angle + sweep angle. This example starts at 45 degrees and sweeps 135 degrees.
Quadric Primitives • Each primitive has a default starting location with the “top” pointing in the +z axis direction (toward the screen). • Each primitive is also created at the origin (0,0,0). You will need to translate it to place it in your scene.
Display Lists • What is it? • A display list is a way of performing a set of OpenGL operations and then saving them to be called at a later time. • Why? • Mainly it is more efficient as some calls are very expensive to perform from scratch (like building a quadric or performing a group of matrix multiplications). • It basically pre-compiles the OpenGL calls and stores them • Also it provides you with an almost database like approach to calling your objects to be used.
Display Lists • int glGenLists(int numLists) • This call takes the number of lists you are going to build and returns an index id number for the first object. • This number is used as a base index for looking up a list. • Example: listIdx = glGenLists(4); //Setup up 4 contiguous list ids … glCallList(listIdx + 2); //Calls the 3rd list
Display Lists • void glNewList(int listIndex, GLenum mode) The display list id number • GL_COMPILE • compile and save for later • GL_COMPILE_AND_EXECUTE • compile and run now • Example: • glNewList(listIdx + 1, GL_COMPILE); //Create 2nd list • //make OpenGL calls here • glEndList(); //Ends the list
Display Lists • void glCallList(int listIndex) • This call invokes the OpenGL commands stored in list listIndex. • If the list is for an object then it will be called during the paint step of your code • Example: listIdx = glGenLists(4); //Setup up 4 contiguous list ids … glCallList(listIdx + 2); //Calls the 3rd list
Display List Usage //Called via initializeGL //Create a new quadric object GLUquadricObj * quadObj = gluNewQuadric(); //Allocate 2 contiguous list id numbers displayIdx = glGenLists(2); //Setup object properties gluQuadricDrawStyle(quadObj, GLU_FILL); gluQuadricNormals(quadObj, GLU_FLAT); //Start List glNewList(displayIdx, GL_COMPILE); glTranslatef(100, 100, -400.0); gluSphere(quadObj, 50, 15, 15); glEndList(); //Setup object properties gluQuadricDrawStyle(quadObj, GLU_FILL); gluQuadricNormals(quadObj, GLU_SMOOTH); //Start List glNewList(displayIdx + 1, GL_COMPILE); glTranslatef(-100, -100, -400.0); gluSphere(quadObj, 50, 15, 15); glEndList(); //Delete the quadric object gluDeleteQuadric(quadObj); • I want to display 2 spheres objects side by side. • Since I am setting these up before any rendering happens I can put them in initializeGL.
Display List Usage • I want to display 2 spheres objects side by side. • Now I can call them in paintGL glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Clear Model View Matrix and call 1st list glLoadIdentity(); glCallList(displayIdx); //Clear Model View Matrix and call 2nd list glLoadIdentity(); glCallList(displayIdx + 1);
Other Display List Uses • Load an entire model and perform the drawing as a list. • Rather than allocating a set of list indices using glGenLists with a number of lists as a parameter, you can call it for each object with a value of 1 as the parameter. If you store the returned value in some data structure then you can have as many lists as you want.