/* * File: * cliographics.c * Summary: * Graphics utilities for clio. See cliographics.h for details. * Author: * Samuel A. Rebelsky * Version: * 0.1 of July 2003. */ /********************************************************************* * Headers * ***********/ #include "cliographics.h" #include "clio.h" float fNormalX, fNormalY, fNormalZ; /********************************************************************* * Local Function Predeclarations * **********************************/ static void drawBox(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax); static void frameBox(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax); /********************************************************************* * Exported Functions * **********************/ void clioBox(int color, float shade, float x, float y, float z, float w, float h, float d) { int xmin,ymin,zmin,xmax,ymax,zmax; float component; /* Compute various components. */ xmin = x - w/2.0; ymin = y - h/2.0; zmin = z - d/2.0; xmax = xmin + w; ymax = ymin + h; zmax = zmin + d; /* Set the color to something fun. */ component = shade/MAXC; switch (color) { case 0: glColor3f(component, 0.0, 0.0); break; case 1: glColor3f(0.0, component, 0.0); break; case 2: glColor3f(0.0, 0.0, component); break; case 3: glColor3f(component, component, 0.0); break; case 4: glColor3f(component, 0.0, component); break; } /* switch */ /* Draw the faces of the box. */ drawBox(xmin,ymin,zmin,xmax,ymax,zmax); /* Frame the box afterwards. */ glColor3f(1.0,1.0,1.0); //frameBox(xmin,ymin,zmin,xmax,ymax,zmax); } /* clioBox() */ /********************************************************************* * Local Functions * *******************/ void drawBox(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax) { float v1[]= {xmax, ymax, zmax}; float v2[]= {xmin, ymax, zmax}; float v3[]= {xmin, ymin, zmax}; float v4[]= {xmax, ymin, zmax}; float v5[]= {xmax, ymax, zmin}; float v6[]= {xmax, ymin, zmin}; float v7[]= {xmin, ymax, zmin}; float v8[]= {xmin, ymin, zmin}; glBegin(GL_QUADS); CalculateVectorNormal(v1, v2, v3, &fNormalX, &fNormalY, &fNormalZ); // Set the normal vector for the polygon glNormal3f(fNormalX, fNormalY, fNormalZ); /* "Front" Face */ glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glEnd(); glBegin(GL_QUADS); CalculateVectorNormal(v7, v5, v6, &fNormalX, &fNormalY, &fNormalZ); // Set the normal vector for the polygon glNormal3f(fNormalX, fNormalY, fNormalZ); /* "Rear" Face */ glVertex3fv(v7); glVertex3fv(v5); glVertex3fv(v6); glVertex3fv(v8); glEnd(); glBegin(GL_QUADS); CalculateVectorNormal(v5, v1, v4, &fNormalX, &fNormalY, &fNormalZ); // Set the normal vector for the polygon glNormal3f(fNormalX, fNormalY, fNormalZ); /* "Right" Face */ glVertex3fv(v5); glVertex3fv(v1); glVertex3fv(v4); glVertex3fv(v6); glEnd(); glBegin(GL_QUADS); CalculateVectorNormal(v2, v7, v8, &fNormalX, &fNormalY, &fNormalZ); // Set the normal vector for the polygon glNormal3f(fNormalX, fNormalY, fNormalZ); /* "Left" Face */ glVertex3fv(v2); glVertex3fv(v7); glVertex3fv(v8); glVertex3fv(v3); glEnd(); glBegin(GL_QUADS); CalculateVectorNormal(v5, v7, v2, &fNormalX, &fNormalY, &fNormalZ); // Set the normal vector for the polygon glNormal3f(fNormalX, fNormalY, fNormalZ); /* "Top" Face */ glVertex3fv(v5); glVertex3fv(v7); glVertex3fv(v2); glVertex3fv(v1); glEnd(); glBegin(GL_QUADS); CalculateVectorNormal(v4, v3, v8, &fNormalX, &fNormalY, &fNormalZ); // Set the normal vector for the polygon glNormal3f(fNormalX, fNormalY, fNormalZ); /* "Bottom" Face */ glVertex3fv(v4); glVertex3fv(v3); glVertex3fv(v8); glVertex3fv(v6); glEnd(); } /* drawBox() */ void frameBox(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax) { glBegin(GL_LINES); /* "Front" face */ glVertex3f(xmin,ymin,zmin); glVertex3f(xmin,ymin,zmax); glVertex3f(xmin,ymin,zmax); glVertex3f(xmin,ymax,zmax); glVertex3f(xmin,ymax,zmax); glVertex3f(xmin,ymax,zmin); glVertex3f(xmin,ymax,zmin); glVertex3f(xmin,ymin,zmin); /* "Rear" Face */ glVertex3f(xmax,ymin,zmin); glVertex3f(xmax,ymin,zmax); glVertex3f(xmax,ymin,zmax); glVertex3f(xmax,ymax,zmax); glVertex3f(xmax,ymax,zmax); glVertex3f(xmax,ymax,zmin); glVertex3f(xmax,ymax,zmin); glVertex3f(xmax,ymin,zmin); /* The Rest */ glVertex3f(xmin,ymin,zmin); glVertex3f(xmax,ymin,zmin); glVertex3f(xmin,ymin,zmax); glVertex3f(xmax,ymin,zmax); glVertex3f(xmin,ymax,zmax); glVertex3f(xmax,ymax,zmax); glVertex3f(xmin,ymax,zmin); glVertex3f(xmax,ymax,zmin); /* That's it. */ glEnd(); } /* frameBox() */ void CalculateVectorNormal( float fVert1[], float fVert2[], float fVert3[], float *fNormalX, float *fNormalY, float *fNormalZ) { float Qx, Qy, Qz, Px, Py, Pz; Qx = fVert2[0]-fVert1[0]; Qy = fVert2[1]-fVert1[1]; Qz = fVert2[2]-fVert1[2]; Px = fVert3[0]-fVert1[0]; Py = fVert3[1]-fVert1[1]; Pz = fVert3[2]-fVert1[2]; *fNormalX = -1*(Py*Qz - Pz*Qy); *fNormalY = -1*(Pz*Qx - Px*Qz); *fNormalZ = -1*(Px*Qy - Py*Qx); }