📄 demo.c
字号:
glNormal3fv(fnormals[face]); glBegin(GL_POLYGON); glColor3fv(colors[a]); /* glNormal3fv(normals[a]); */ glTexCoord2f(0.0,0.0); glVertex3fv(vertices[a]); glColor3fv(colors[b]); /* glNormal3fv(normals[b]); */ glTexCoord2f(0.0,1.0); glVertex3fv(vertices[b]); glColor3fv(colors[c]); /* glNormal3fv(normals[c]); */ glTexCoord2f(1.0,1.0); glVertex3fv(vertices[c]); glColor3fv(colors[d]); /* glNormal3fv(normals[d]); */ glTexCoord2f(1.0,0.0); glVertex3fv(vertices[d]); glEnd(); }}void colorcube(void){ /* map vertices to faces */ polygon(1,0,3,2,0); polygon(3,7,6,2,1); polygon(7,3,0,4,2); polygon(2,6,5,1,3); polygon(4,5,6,7,4); polygon(5,4,0,1,5);}/*----------------------------------------------------------------------*//* ** These functions implement a simple trackball-like motion control.*/float lastPos[3] = {0.0F, 0.0F, 0.0F};int curx, cury;int startX, startY;voidtrackball_ptov(int x, int y, int width, int height, float v[3]){ float d, a; /* project x,y onto a hemi-sphere centered within width, height */ v[0] = (2.0F*x - width) / width; v[1] = (height - 2.0F*y) / height; d = (float) sqrt(v[0]*v[0] + v[1]*v[1]); v[2] = (float) cos((M_PI/2.0F) * ((d < 1.0F) ? d : 1.0F)); a = 1.0F / (float) sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); v[0] *= a; v[1] *= a; v[2] *= a;}voidmouseMotion(int x, int y){ float curPos[3], dx, dy, dz; trackball_ptov(x, y, winWidth, winHeight, curPos); dx = curPos[0] - lastPos[0]; dy = curPos[1] - lastPos[1]; dz = curPos[2] - lastPos[2]; if (dx || dy || dz) { angle = 90.0F * sqrt(dx*dx + dy*dy + dz*dz); axis[0] = lastPos[1]*curPos[2] - lastPos[2]*curPos[1]; axis[1] = lastPos[2]*curPos[0] - lastPos[0]*curPos[2]; axis[2] = lastPos[0]*curPos[1] - lastPos[1]*curPos[0]; lastPos[0] = curPos[0]; lastPos[1] = curPos[1]; lastPos[2] = curPos[2]; } glutPostRedisplay();}voidstartMotion(long time, int button, int x, int y){ if (!trackballEnabled) return; trackingMouse = true; redrawContinue = false; startX = x; startY = y; curx = x; cury = y; trackball_ptov(x, y, winWidth, winHeight, lastPos); trackballMove = true;}voidstopMotion(long time, int button, int x, int y){ if (!trackballEnabled) return; trackingMouse = false; if (startX != x || startY != y) { redrawContinue = true; } else { angle = 0.0F; redrawContinue = false; trackballMove = false; }}/*----------------------------------------------------------------------*/void display(void){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /* view transform */ glLoadIdentity(); glTranslatef(0.0,0.0,viewxform_z); if (trackballMove) { glPushMatrix(); glLoadIdentity(); glRotatef(angle, axis[0], axis[1], axis[2]); glMultMatrixf((GLfloat *) trackballXform); glGetFloatv(GL_MODELVIEW_MATRIX, trackballXform); glPopMatrix(); } glPushMatrix(); glMultMatrixf((GLfloat*)lightXform); setLightPos(); glPopMatrix(); glPushMatrix(); glMultMatrixf((GLfloat *) objectXform); colorcube(); glPopMatrix(); glFlush(); glutSwapBuffers();}/*----------------------------------------------------------------------*/void mouseButton(int button, int state, int x, int y){ switch (button) { case GLUT_LEFT_BUTTON: trackballXform = (GLfloat*)objectXform; break; case GLUT_MIDDLE_BUTTON: trackballXform = (GLfloat*)lightXform; break; } switch(state) { case GLUT_DOWN: startMotion(0,1, x,y); break; case GLUT_UP: stopMotion(0,1, x,y); break; } }void myReshape(int w, int h){ glViewport(0, 0, w, h); winWidth = w; winHeight = h;}void spinCube(){ if (redrawContinue) glutPostRedisplay();}void userEventAction(int key) { switch(key) { case '0': /* wire frame/polygon */ drawLines = !drawLines; break; case '1': smoothEnabled = !smoothEnabled; break; case '2': /* lighting */ lightingEnabled = !lightingEnabled; break; case '3': /* texture */ texEnabled = !texEnabled; break; case '4': /* fog */ fogEnabled = !fogEnabled; break; case '5': /* HSR */ depthEnabled = !depthEnabled; break; case '6': /* line aa */ lineAAEnabled = !lineAAEnabled; break; case '7': /* perpective texture */ fastTexture = !fastTexture; break; case 'b': currentMaterials = &brassMaterials; break; case 'c': currentMaterials = &colorCubeMaterials; break; case 'C': currentLighting = &colorCubeLighting; break; case 'i': idleSpin = !idleSpin; break; case 'm': /* mipmapped texture */ mipmapEnabled = !mipmapEnabled; break; case 'p': /* perspective/ortho */ perspectiveXform = !perspectiveXform; break; case 'r': currentMaterials = &redPlasticMaterials; break; case 'w': currentLighting = &whiteLighting; break; case 27: exit(0); default: break; } userSettings(); glutPostRedisplay();}void keyboard(unsigned char key, int x, int y){ userEventAction(key);}/*----------------------------------------------------------------------*/typedef struct menuEntryStruct { char *label; char key;} menuEntryStruct;static menuEntryStruct mainMenu[] = { "lines/polygons", '0', "flat/smooth", '1', "lighting", '2', "texture", '3', "fog", '4', "HSR", '5', "line smooth", '6', "motion", 'i', "ortho/perspective", 'p', "quit", 27,};int mainMenuEntries = sizeof(mainMenu)/sizeof(menuEntryStruct);void selectMain(int choice){ userEventAction(mainMenu[choice].key);}static menuEntryStruct materialsMenu[] = { "brass", 'b', "white", 'c', "red plastic", 'r',};int materialsMenuEntries = sizeof(materialsMenu)/sizeof(menuEntryStruct);void selectMaterials(int choice){ userEventAction(materialsMenu[choice].key);}static menuEntryStruct lightingMenu[] = { "white", 'w', "color", 'C',};int lightingMenuEntries = sizeof(lightingMenu)/sizeof(menuEntryStruct);void selectLighting(int choice){ userEventAction(lightingMenu[choice].key);}void setMenuEntries(bool init){ int i, sub1, sub2; if (init) { sub1 = glutCreateMenu(selectMaterials); for (i=0; i < materialsMenuEntries; i++) { glutAddMenuEntry(materialsMenu[i].label, i); } sub2 = glutCreateMenu(selectLighting); for (i=0; i < lightingMenuEntries; i++) { glutAddMenuEntry(lightingMenu[i].label, i); } glutCreateMenu(selectMain); for (i=0; i < mainMenuEntries; i++) { glutAddMenuEntry(mainMenu[i].label, i); } glutAddSubMenu("materials", sub1); glutAddSubMenu("lighting", sub2); glutAttachMenu(GLUT_RIGHT_BUTTON); } else { }}/*----------------------------------------------------------------------*/voidmain(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouseButton); glutMotionFunc(mouseMotion); glutKeyboardFunc(keyboard); initSettings(); userSettings(); glutMainLoop();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -