23.10.2014 Views

3D Objects in OpenGL

3D Objects in OpenGL

3D Objects in OpenGL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

College of Science<br />

Computer Department<br />

2011 - 2012<br />

<strong>3D</strong> <strong>Objects</strong> <strong>in</strong> <strong>OpenGL</strong><br />

Lecturer: Jamal A. Husse<strong>in</strong><br />

1


GLUT <strong>3D</strong> Shapes<br />

import com.sun.opengl.util.gl2.*;<br />

//Enable Light<strong>in</strong>g<br />

glEnable(GL_LIGHTING);<br />

glEnable(GL_LIGHT0);<br />

glEnable(GL_COLOR_MATERIAL);<br />

…<br />

GLUT glut = new GLUT();<br />

…<br />

glut.glutSolidTeapot(0.5);<br />

glut.glutWireCube(1.0f);<br />

glut.glutSolidTorus(0.2, 0.5, 10, 20);<br />

glut.glutWireIcosahedron();<br />

Creat<strong>in</strong>g a GLUT<br />

object usually<br />

<strong>in</strong>side display()<br />

2


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireSphere(double radius,<br />

<strong>in</strong>t slices, <strong>in</strong>t stacks);<br />

void glutSolidSphere(double radius,<br />

<strong>in</strong>t slices, <strong>in</strong>t stacks);<br />

For example: glutWireSphere(0.5, 20, 16);<br />

glutSolidSphere(0.5, 20, 16);<br />

3


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireCube(double size);<br />

void glutSolidCube(double size);<br />

For example:<br />

glutWireCube(1.0f);<br />

glutSolidCube(1.0f);<br />

4


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireTorus(double <strong>in</strong>nerRadius,<br />

double outerRadius, <strong>in</strong>t nsides, <strong>in</strong>t r<strong>in</strong>gs) ;<br />

void glutSolidTorus(double <strong>in</strong>nerRadius,<br />

double outerRadius, <strong>in</strong>tnsides, <strong>in</strong>t r<strong>in</strong>gs) ;<br />

For example: glutSolidTorus(0.2, 0.5, 10, 20);<br />

glutWireTorus(0.2, 0.5, 10, 20);<br />

5


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireIcosahedron(void);<br />

void glutSolidlcosahedron(void);<br />

For example: glutWireIcosahedron();<br />

glutSolidIcosahedron();<br />

6


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireOctahedron(void);<br />

void glutSolidOctahedron(void);<br />

For example:<br />

glutWireOctahedron();<br />

glutSolidOctahedron();<br />

7


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireTetrahedron(void);<br />

void glutSolidTetrahedron(void);<br />

For example: glutWireTetrahedron();<br />

glutSolidTetrahedron();<br />

8


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireDodecahedron();<br />

void glutSolidDodecahedron();<br />

For example: glutWireDodecahedron();<br />

glutSolidDodecahedron();<br />

9


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireCone(double radius, double height,<br />

<strong>in</strong>t slices, <strong>in</strong>t stacks);<br />

void glutSolidCone(double radius, double height,<br />

<strong>in</strong>t slices, <strong>in</strong>t stacks);<br />

For example: glutWireCone(1.0, 2.0, 15, 12);<br />

glutSolidCone(1.0, 2.0, 15, 12);<br />

10


Draw<strong>in</strong>g <strong>3D</strong> Object<br />

void glutWireTeapot(double size);<br />

void glutSolidTeapot(double size);<br />

For example:<br />

glutWireTeapot(0.6);<br />

glutSolidTeapot(0.6);<br />

11


GLU <strong>3D</strong> <strong>Objects</strong><br />

12


GLU <strong>3D</strong> <strong>Objects</strong><br />

GLUquadric gluNewQuadric(void);<br />

Creates a new quadrics object and returns a po<strong>in</strong>ter to it. A<br />

null po<strong>in</strong>ter is returned if the rout<strong>in</strong>e fails.<br />

void gluDeleteQuadric(GLUquadric qobj);<br />

Destroys the quadrics object qobj and frees up any memory<br />

used by it.<br />

GLUquadric qobj = gluNewQuadric();<br />

13


GLU <strong>3D</strong> <strong>Objects</strong><br />

Controll<strong>in</strong>g Quadrics Attributes<br />

void gluQuadricDrawStyle(<br />

GLUquadric qobj, GLenum drawStyle);<br />

<br />

For the quadrics object qobj, drawStyle controls the render<strong>in</strong>g<br />

style. Legal values for drawStyle are GLU_POINT, GLU_LINE,<br />

GLU_SILHOUETTE, and GLU_FILL.<br />

GLUquadric qobj = gluNewQuadric();<br />

gluQuadricDrawStyle(qobj, GLU_LINE);<br />

14


GLU <strong>3D</strong> <strong>Objects</strong><br />

void gluSphere(<br />

GLUquadric qobj, double radius,<br />

<strong>in</strong>t slices, <strong>in</strong>t stacks);<br />

Draws a sphere of the given radius, centered around the<br />

orig<strong>in</strong>, (0, 0, 0). The sphere is subdivided around the z-axis <strong>in</strong>to<br />

a number of slices (similar to longitude) and along the z-<br />

axis <strong>in</strong>to a number of stacks (latitude).<br />

GLUquadric qobj = gluNewQuadric();<br />

gluQuadricDrawStyle(qobj, GLU_FILL);<br />

gluSphere(qobj, 0.75, 15, 10);<br />

15


GLU <strong>3D</strong> <strong>Objects</strong><br />

void gluCyl<strong>in</strong>der( GLUquadric qobj,<br />

double baseRadius, double topRadius,<br />

double height, <strong>in</strong>t slices, <strong>in</strong>t stacks);<br />

Draws a cyl<strong>in</strong>der oriented along the z-axis, with the base of the cyl<strong>in</strong>der at z = 0<br />

and the top at z = height. Like a sphere, the cyl<strong>in</strong>der is subdivided around the z-axis<br />

<strong>in</strong>to a number of slices and along the z-axis <strong>in</strong>to a number of stacks.<br />

baseRadius is the radius of the cyl<strong>in</strong>der at z = 0. topRadius is the radius of<br />

the cyl<strong>in</strong>der at z = height. If topRadius is set to 0, then a cone is generated.<br />

GLUquadric qobj = gluNewQuadric();<br />

gluQuadricDrawStyle(qobj, GLU_FILL);<br />

gluCyl<strong>in</strong>der(qobj, 0.5, 0.3, 1.0, 15, 5);<br />

16


GLU <strong>3D</strong> <strong>Objects</strong><br />

void gluDisk( GLUquadric qobj,<br />

double <strong>in</strong>nerRadius, double outerRadius,<br />

<strong>in</strong>t slices, <strong>in</strong>t r<strong>in</strong>gs);<br />

<br />

Draws a disk on the z = 0 plane, with a radius of outerRadius and a<br />

concentric circular hole with a radius of <strong>in</strong>nerRadius. If<br />

<strong>in</strong>nerRadius is 0, then no hole is created. The disk is subdivided<br />

around the z-axis <strong>in</strong>to a number of slices and also about the z-axis <strong>in</strong>to a<br />

number of concentric r<strong>in</strong>gs.<br />

GLUquadric qobj = gluNewQuadric();<br />

gluQuadricDrawStyle(qobj, GLU_FILL);<br />

gluDisk(qobj, 0.25, 1.0, 20, 4);<br />

17


GLU <strong>3D</strong> <strong>Objects</strong><br />

void gluPartialDisk( GLUquadric qobj,<br />

<br />

double <strong>in</strong>nerRadius, double outerRadius,<br />

<strong>in</strong>t slices, <strong>in</strong>t r<strong>in</strong>gs,<br />

double startAngle, double sweepAngle);<br />

Draws a partial disk on the z = 0 plane. A partial disk is similar to a complete<br />

disk, <strong>in</strong> terms of outerRadius, <strong>in</strong>nerRadius, slices, and r<strong>in</strong>gs.<br />

The difference is that only a portion of a partial disk is drawn, start<strong>in</strong>g from<br />

startAngle through startAngle + sweepAngle.<br />

GLUquadric qobj = gluNewQuadric();<br />

gluQuadricDrawStyle(qobj, GLU_FILL);<br />

gluPartialDisk(qobj, 0.0, 1.0, 20, 4, 0.0, 225.0);<br />

18


References<br />

Donald Hearn, M. Paul<strong>in</strong>e Baker, Computer Graphics<br />

with <strong>OpenGL</strong>, 3 rd edition, Prentice Hall, 2004<br />

• Chapter 8: Three-Dimensional Object<br />

Representations<br />

The <strong>OpenGL</strong> Programmer's Guide (the<br />

Redbook), Addison-Wesley<br />

• Chapter 11: Tessellators and Quadrics<br />

19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!