#include #include #include #include #include #include #define ESCAPE 27 DFF *dff; float xpos = 0.0f, ypos = 0.0f, zpos = -6.8f; float xrot = -66.0f, yrot = 0.0f, zrot = -120.0f; int textured; void exitprog(int signal) { printf("Exiting...%d\n", signal); exit(signal); } void keypress(unsigned char key, int x, int y) { switch(key) { case 'q': yrot -= 0.7f; break; case 'Q': yrot -= 2.0f; break; case 'e': yrot += 0.7f; break; case 'E': yrot += 2.0f; break; case 'h': xpos += 0.1f; break; case 'H': xpos += 0.2f; break; case 'j': ypos += 0.1f; break; case 'J': ypos += 0.2f; break; case 'k': ypos -= 0.1f; break; case 'K': ypos -= 0.2f; break; case 'l': xpos -= 0.1f; break; case 'L': xpos -= 0.2f; break; case 't': case 'T': if (textured == 1) glDisable(GL_TEXTURE_2D); else glEnable(GL_TEXTURE_2D); textured = !textured; break; /* case 'O': if (whichobj == 0) whichobj = objscount; whichobj--; whichframe = 0; isvisible(); break; case 'o': whichobj++; if (whichobj == objscount) whichobj = 0; isvisible(); break; case 'F': if (whichframe == 0) whichframe = objs[whichobj].mdl->clp->frmcount; whichframe--; isvisible(); break; case 'f': whichframe++; if (whichframe == objs[whichobj].mdl->clp->frmcount) whichframe = 0; isvisible(); break; case 'V': case 'v': objs[whichobj].mdl->clp->frm[whichframe].display = !objs[whichobj].mdl->clp->frm[whichframe].display; isvisible(); break; */ case 'c': case 'C': printf("\nCamera position: %f %f %f\n", xpos, ypos, zpos); printf("Camera roatation: %f %f %f\n\n", xrot, yrot, zrot); break; /* case 'i': case 'I': objscount = 0; if (objs != NULL) { DestroyObjs(objs); free(objs); if ((objs = ReadIde(ide)) == NULL) { objscount = 0; exitprog(0); objs->mdl = LoadModel(objs->Model, objs->Texture, img, 0); objscount = 1; break; */ case ESCAPE: exitprog(0); default: break; } glutPostRedisplay(); } void spkeypress(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) xrot -= 2.0f; else xrot -= 0.7f; break; case GLUT_KEY_DOWN: if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) xrot += 2.0f; else xrot += 0.7f; break; case GLUT_KEY_LEFT: if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) zrot -= 2.0f; else zrot -= 0.7f; break; case GLUT_KEY_RIGHT: if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) zrot += 2.0f; else zrot += 0.7f; break; case GLUT_KEY_PAGE_UP: if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) zpos += 0.7f; else zpos += 0.2f; break; case GLUT_KEY_PAGE_DOWN: if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) zpos -= 0.7f; else zpos -= 0.2f; break; default: break; } glutPostRedisplay(); } void resize(int w, int h) { if(h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat) w/(GLfloat)h, 0.1f, 2000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); glTranslatef(0.0f, 0.0f, 0.0f); */ } void drawclump(_clump *clp) { int i, j, k, g, f; _split *split; vector3f *vert; for (i = 0; i < clp->natomic; i++) { g = clp->atm[i].geoindex; f = clp->atm[i].frmindex; /* glPushMatrix(); */ for (j = 0; j < clp->geo[g].nsplit; j++) { split = &clp->geo[g].split[j]; vert = clp->geo[g].vertex; glBegin(GL_TRIANGLE_STRIP); for (k = 0; k < split->nindex; k++) { glVertex3fv(vert[split->indices[k]]); } glEnd(); } } } void renderscene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(xpos, ypos, zpos); glRotatef(xrot, 1.0f, 0.0f, 0.0f); glRotatef(yrot, 0.0f, 1.0f, 0.0f); glRotatef(zrot, 0.0f, 0.0f, 1.0f); drawclump(dff[0].clp); glutSwapBuffers(); } int main(int argc, char *argv[]) { int w, h; FILE *fdff; w = 800; h = 600; glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); glutInitWindowSize(w, h); glutCreateWindow("DFF Viewer"); glutReshapeFunc(resize); glutSpecialFunc(&spkeypress); glutKeyboardFunc(&keypress); glutDisplayFunc(renderscene); if ((fdff = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Couldn't open file %s\n", argv[1]); exitprog(1); } fseek(fdff, 0, SEEK_END); dff = dffread(fdff, 0, ftell(fdff)); glClearColor(0.6f, 0.6f, 0.6f, 1.0f); glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_ALPHA_TEST); glEnable(GL_NORMALIZE); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glShadeModel(GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat) w/(GLfloat)h, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glutMainLoop(); return 0; }