# include "sgl_shapes.h" # ifndef QUAD_OBJ_INIT # define QUAD_OBJ_INIT() { if(!quadObj) initQuadObj(); } # endif static GLUquadricObj *quadObj; static void initQuadObj(void) { quadObj = gluNewQuadric(); if (!quadObj) { cerr<<"out of memory."< 0) { glNewList(lindex, GL_COMPILE); paint(); glEndList(); } else { cout << " Could not generate list." << endl; } } /******************************************************* * solid box :: paint *******************************************************/ void SolidBox :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glScalef(width,height,depth); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glutSolidCube(1.0); glPopMatrix(); } /******************************************************* * wire box :: paint *******************************************************/ void WireBox :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glScalef(width,height,depth); glutWireCube(1.0); glPopMatrix(); } /******************************************************* * rounded box :: paint *******************************************************/ void RoundedBox :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); GLfloat h=height,w=width,d=depth; h-=2*heightr; w-=2*widthr; d-=2*depthr; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); SolidCylinder c1(h,1,0,0,0); c1.setColor(color[0],color[1],color[2],color[3]); c1.setScale(widthr,depthr,1.0); c1.setRotation(-90,0,0); SolidCylinder c2(d,1.0,0,0,0); c2.setColor(color[0],color[1],color[2],color[3]); c2.setScale(widthr,heightr,1.0); SolidCylinder c3(w,1,0,0,0); c3.setColor(color[0],color[1],color[2],color[3]); c3.setScale(depthr,heightr,1.0); c3.setRotation(0,90,0); SolidSphere s; s.setColor(color[0],color[1],color[2],color[3]); s.setScale(widthr,heightr,depthr); glPushMatrix(); glTranslatef(widthr+w,heightr,depthr+d); c1.paint(); s.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr,heightr,depthr+d); c1.paint(); c3.paint(); s.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr+w,heightr,depthr); c1.paint(); c2.paint(); s.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr,heightr,depthr); c1.paint(); c2.paint(); c3.paint(); s.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr+w,heightr+h,depthr); c2.paint(); s.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr,heightr+h,depthr); c2.paint(); c3.paint(); s.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr,heightr+h,depthr+d); s.paint(); c3.paint(); glPopMatrix(); glPushMatrix(); glTranslatef(widthr+w,heightr+h,depthr+d); s.paint(); glPopMatrix(); glTranslatef(w/2.0+widthr,h/2.0+heightr,d/2.0+depthr); if (!hollow) { glPushMatrix(); glScalef(w,h,depth); glutSolidCube(1.0); glPopMatrix(); glPushMatrix(); glScalef(w,height,d); glutSolidCube(1.0); glPopMatrix(); glPushMatrix(); glScalef(width,h,d); glutSolidCube(1.0); glPopMatrix(); } glPopMatrix(); } /******************************************************* * box :: print *******************************************************/ void Box :: print () { cout << "Box" << endl; cout << " (" << x << "," << y << "," << z << ")" << endl; cout << " width : " << width << endl; cout << " height : " << height << endl; cout << " depth : " << depth << endl; } /******************************************************* * solid sphere :: paint *******************************************************/ void SolidSphere :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glutSolidSphere(r,15,10); glPopMatrix(); } /******************************************************* * wire sphere :: paint *******************************************************/ void WireSphere :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glutWireSphere(r,15,10); glPopMatrix(); } /******************************************************* * sphere :: print *******************************************************/ void Sphere :: print () { cout << "Sphere" << endl; cout << " (" << x << "," << y << "," << z << ")" << endl; cout << " radius : " << r << endl; } /******************************************************* * solid torus :: paint *******************************************************/ void SolidTorus :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glutSolidTorus(innerr,outerr,15,10); glPopMatrix(); } /******************************************************* * wire torus :: paint *******************************************************/ void WireTorus :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glutWireTorus(innerr,outerr,15,10); glPopMatrix(); } /******************************************************* * torus :: print *******************************************************/ void Torus :: print () { cout << "Torus" << endl; cout << " (" << x << "," << y << "," << z << ")" << endl; cout << " inner radius : " << innerr << endl; cout << " outer radius : " << outerr << endl; } /******************************************************* * cone :: print *******************************************************/ void Cone :: print () { cout << "Cone" << endl; cout << " (" << x << "," << y << "," << z << ")" << endl; cout << " base radius : " << baser << endl; cout << " top radius : " << topr << endl; cout << " height : " << height << endl; } /******************************************************* * wire cone :: paint *******************************************************/ void WireCone :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); // ******* glutWireCone(r,height,15,10); QUAD_OBJ_INIT(); gluQuadricDrawStyle(quadObj, GLU_LINE); gluQuadricNormals(quadObj, GLU_SMOOTH); gluDisk(quadObj,0,baser,15,5); glPushMatrix(); glTranslatef(0,0,height); gluDisk(quadObj,0,topr,15,5); glPopMatrix(); gluCylinder(quadObj, baser, topr, height, 15, 10); glPopMatrix(); } /******************************************************* * solid cone :: paint *******************************************************/ void SolidCone :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); // ******* glutSolidCone(baser,height,15,10); QUAD_OBJ_INIT(); gluQuadricDrawStyle(quadObj, GLU_FILL); gluQuadricNormals(quadObj, GLU_SMOOTH); glPushMatrix(); glRotatef(180, 0.0, 1.0, 0.0); gluDisk(quadObj,0,baser,15,6); glPopMatrix(); glPushMatrix(); glTranslatef(0,0,height); gluDisk(quadObj,0,topr,15,5); glPopMatrix(); gluCylinder(quadObj, baser, topr, height, 15, 10); glPopMatrix(); } /******************************************************* * gear :: paint *******************************************************/ void Gear :: paint () { glPushMatrix(); glTranslatef(x,y,z); glRotatef(rot_x, 1.0, 0.0, 0.0); glRotatef(rot_y, 0.0, 1.0, 0.0); glRotatef(rot_z, 0.0, 0.0, 1.0); glScalef(sfx,sfy,sfz); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); // from demo ******************************************** GLint i; GLfloat r0, r1, r2; GLfloat angle, da; GLfloat u, v, len; r0 = ir; r1 = or - tsize / 2.0; r2 = or + tsize / 2.0; da = 2.0 * PI / teeth / 4.0; glShadeModel(GL_FLAT); glNormal3f(0.0, 0.0, 1.0); /* draw front face */ glBegin(GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0 * PI / teeth; glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); } glEnd(); /* draw front sides of teeth */ glBegin(GL_QUADS); da = 2.0 * PI / teeth / 4.0; for (i = 0; i < teeth; i++) { angle = i * 2.0 * PI / teeth; glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); } glEnd(); glNormal3f(0.0, 0.0, -1.0); /* draw back face */ glBegin(GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0 * PI / teeth; glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); } glEnd(); /* draw back sides of teeth */ glBegin(GL_QUADS); da = 2.0 * PI / teeth / 4.0; for (i = 0; i < teeth; i++) { angle = i * 2.0 * PI / teeth; glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); } glEnd(); /* draw outward faces of teeth */ glBegin(GL_QUAD_STRIP); for (i = 0; i < teeth; i++) { angle = i * 2.0 * PI / teeth; glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); u = r2 * cos(angle + da) - r1 * cos(angle); v = r2 * sin(angle + da) - r1 * sin(angle); len = sqrt(u * u + v * v); u /= len; v /= len; glNormal3f(v, -u, 0.0); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); glNormal3f(cos(angle), sin(angle), 0.0); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5); u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da); v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da); glNormal3f(v, -u, 0.0); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); glNormal3f(cos(angle), sin(angle), 0.0); } glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5); glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5); glEnd(); glShadeModel(GL_SMOOTH); /* draw inside radius cylinder */ glBegin(GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0 * PI / teeth; glNormal3f(-cos(angle), -sin(angle), 0.0); glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); } glEnd(); // end demo ******************************************** glPopMatrix(); } /******************************************************* * gear :: print *******************************************************/ void Gear :: print () { cout << "Gear" << endl; cout << " (" << x << "," << y << "," << z << ")" << endl; cout << " teeth : " << teeth << endl; cout << " teeth size: " << tsize << endl; cout << " inner radius: " << ir << endl; cout << " outer radius: " << or << endl; cout << " width: " << width << endl; }