#include #include #include #include #include #define facenum 14 #define rebrnum 15 #define vertnum 9 /*typedef struct { int face1; int face2; int face3; double normal[3]; } ADJ; */ GLfloat bigvalue = 5.0; short int backculled[facenum]; int adjacency[facenum][3]; GLfloat /*normals[facenum][3],*/ normalsm[facenum][3]; GLfloat volume[rebrnum][4][3]; int volumesize; GLfloat vertexm[vertnum][3], vertexes[vertnum][3] = { 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.0, -1.0, 0.0 }; GLfloat vectors_tl[vertnum][3]; int faces[facenum][3] = { 0, 3, 2, 1, 0, 2, 4, 7, 3, 0, 4, 3, 5, 6, 7, 4, 5, 7, 1, 2, 6, 5, 1, 6, 5, 4, 0, 1, 5, 0, 3, 8, 2, 7, 8, 3, 6, 8, 7, 2, 8, 6 }; void CalcAdjacency(int adjacency[][3], int facecount) { int i, j, k, l; short int s, a[2], b[2]; for (i=0; i0) { for (j=0; j<3; j++) { s=0; for (k=0; k<3; k++) s+=normals[adjacency[i][j]][k]*vectors_tl[faces[adjacency[i][j]][0]][k]; if (s<=0) { backculled[adjacency[i][j]]=1; switch (j) { case 0: for (k=0; k<3; k++) { volume[vs][0][k]=vertexes[faces[i][0]][k]; volume[vs][1][k]=vertexes[faces[i][1]][k]; volume[vs][2][k]=vertexes[faces[i][0]][k]-bigvalue* vectors_tl[faces[i][0]][k]; volume[vs][3][k]=vertexes[faces[i][1]][k]-bigvalue* vectors_tl[faces[i][1]][k]; }; break; case 1: for (k=0; k<3; k++) { volume[vs][0][k]=vertexes[faces[i][2]][k]; volume[vs][1][k]=vertexes[faces[i][0]][k]; volume[vs][2][k]=vertexes[faces[i][2]][k]-bigvalue* vectors_tl[faces[i][2]][k]; volume[vs][3][k]=vertexes[faces[i][0]][k]-bigvalue* vectors_tl[faces[i][0]][k]; }; break; case 2: for (k=0; k<3; k++) { volume[vs][0][k]=vertexes[faces[i][1]][k]; volume[vs][1][k]=vertexes[faces[i][2]][k]; volume[vs][2][k]=vertexes[faces[i][1]][k]-bigvalue* vectors_tl[faces[i][1]][k]; volume[vs][3][k]=vertexes[faces[i][2]][k]-bigvalue* vectors_tl[faces[i][2]][k]; }; break; }; vs++; }; }; }; }; volumesize=vs; }; GLfloat anglecube = 0.0, angle = 0.0; GLfloat slag = 0.2; GLfloat lp[3], lightpos[4] = {0.0, 0.0, 2.0, 1.0}; GLUquadricObj *qo; void TransformLP(GLfloat vertexes[][3], GLfloat vertexm[][3], const GLfloat angle, const GLfloat x, const GLfloat y, const GLfloat z) { GLfloat m[9], mt[16], buf[16], c, s; int i; /* c=cos(angle); s=sin(angle); m[0]=x*x*(1-c)+c; m[1]=y*x*(1-c)+z*s; m[2]=x*z*(1-c)-y*s; m[3]=x*y*(1-c)-z*s; m[4]=y*y*(1-c)+c; m[5]=y*z*(1-c)+x*s; m[6]=x*z*(1-c)+y*s; m[7]=y*z*(1-c)-x*s; m[8]=z*z*(1-c)+c; lp[0]=lightpos[0]*m[0]+lightpos[1]*m[1]+lightpos[2]*m[2]; lp[1]=lightpos[0]*m[3]+lightpos[1]*m[4]+lightpos[2]*m[5]; lp[2]=lightpos[0]*m[6]+lightpos[1]*m[7]+lightpos[2]*m[8]; */ for (i=0; i<16; buf[i++]=0.0); glPushMatrix(); glLoadIdentity(); glRotatef(angle, x, y, z); glGetFloatv(GL_MODELVIEW_MATRIX, mt); for (i=0; i 1) { glVertex3f(s1[0], s1[1], s1[2]); glVertex3f(s2[0], s2[1], s2[2]); glVertex3f(s3[0], s3[1], s3[2]); return; }; s12[0] = (s1[0]+s2[0])/2; s12[1] = (s1[1]+s2[1])/2; s12[2] = (s1[2]+s2[2])/2; s23[0] = (s2[0]+s3[0])/2; s23[1] = (s2[1]+s3[1])/2; s23[2] = (s2[2]+s3[2])/2; s31[0] = (s1[0]+s3[0])/2; s31[1] = (s1[1]+s3[1])/2; s31[2] = (s1[2]+s3[2])/2; DrawTriangle(s1, s12, s31, a+1); DrawTriangle(s12, s2, s23, a+1); DrawTriangle(s31, s23, s3, a+1); DrawTriangle(s12, s23, s31, a+1); } /*void DrawTriangle(float s[][3], int a) {8 // const float size = 0.2; // float s12[3], s23[3], s31[3]; float sx[3][3]; if (a > 4) { glVertex3fv(s[0]); glVertex3fv(s[1]); glVertex3fv(s[2]); return; }; sx[0][0] = (s[0][0]+s[1][0])/2; sx[0][1] = (s[0][1]+s[1][1])/2; sx[0][2] = (s[0][2]+s[1][2])/2; sx[1][0] = (s[1][0]+s[2][0])/2; sx[1][1] = (s[1][1]+s[2][1])/2; sx[1][2] = (s[1][2]+s[2][2])/2; sx[2][0] = (s[0][0]+s[2][0])/2; sx[2][1] = (s[0][1]+s[2][1])/2; sx[2][2] = (s[0][2]+s[2][2])/2; DrawTriangle(s[0], sx[0], sx[2], a+1); DrawTriangle(sx[0], s[1], sx[1], a+1); DrawTriangle(sx[2], sx[1], s[2], a+1); DrawTriangle(sx[0], sx[1], sx[2], a+1); } */ void Display() { int i,j; GLfloat lp[4], sd[3]; GLfloat mt[16] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glLoadIdentity(); glRotatef(angle, 0.0, 1.0, 0.0); mt[12]=lightpos[0]; mt[13]=lightpos[1]; mt[14]=lightpos[2]; mt[15]=lightpos[3]; glMultMatrixf(mt); glGetFloatv(GL_MODELVIEW_MATRIX, mt); glPopMatrix(); glPushMatrix(); glRotatef(angle, 0.0, 1.0, 0.0); glPushMatrix(); glDisable(GL_LIGHTING); glTranslatef(lightpos[0], lightpos[1], lightpos[2]); glColor3f(0.0, 1.0, 0.0); glutWireCube(0.2); glPopMatrix(); glLightfv(GL_LIGHT0, GL_POSITION, lightpos); // sd[0]=-lightpos[0]; sd[1]=-lightpos[1]; sd[2]=-lightpos[2]; // glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, sd); glEnable(GL_LIGHTING); glPopMatrix(); glPushMatrix(); glColor3f(0.6, 0.3, 0.7); glBegin(GL_QUADS); glNormal3f(0.0, 0.0, 1.0); glVertex3f(3.0, 3.0, -2.5); glVertex3f(-3.0, 3.0, -2.5); glVertex3f(-3.0, -3.0, -2.5); glVertex3f(3.0, -3.0, -2.5); glNormal3f(1.0, 0.0, 0.0); glVertex3f(-3.0, 3.0, -2.5); glVertex3f(-3.0, 3.0, 2.5); glVertex3f(-3.0, -3.0, 2.5); glVertex3f(-3.0, -3.0, -2.5); glEnd(); TransformLP(vertexes, vertexm, anglecube, 1.0, 0.5, 0.8); CalcNormals(normalsm, vertexm, faces, facenum); // glPushMatrix(); // glRotatef(anglecube, 1.0, 0.5, 0.8); glColor3f(0.8, 0.2, 0.15); glBegin(GL_TRIANGLES); for (i=0; i 360.0) anglecube-=360.0; if ((angle>=100.0)||(angle<=-10.0)) slag=-slag; glutPostRedisplay(); FPS++; }; void MyInit() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 640.0/480.0, 2.0, 13.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -8.5); glRotatef(-30.0, 1.0, 0.5, 0.0); glEnable(GL_DEPTH_TEST); // glEnable(GL_NORMALIZE); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); // glPolygonMode(GL_FRONT, GL_LINE); glShadeModel(GL_SMOOTH); glLightfv(GL_LIGHT0, GL_POSITION, lightpos); glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0); glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.15); glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 100.0); // glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20.0); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); qo=gluNewQuadric(); alarm(1); }; void printFPS(int i) { printf("%s%d%s", "FPS: ", FPS, "\n"); FPS=0; alarm(1); }; int main(int argc, char **argv) { int i; struct sigaction act; act.sa_handler = &printFPS; act.sa_flags = 0; sigaction(SIGALRM, &act, NULL); for (i=0; i