📄 fakeraytrace.c
字号:
glTexCoord2f(0,1); glVertex2f(0,1); glEnd(); } else { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat) W / (GLfloat) H, 0.5, 40.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eye[0], eye[1], eye[2], /* eye at eye */ 0, 0, 0, /* looking at object */ up[0], up[1], up[2]); positionLights(-1, NULL); drawView(-1, NULL); if (multipass) { doSphereMap = 1; } drawObject(&sphere[0]); if (multipass) { doSphereMap = 0; glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_EQUAL); glEnable(GL_BLEND); drawObject(&sphere[0]); glDisable(GL_BLEND); glDepthFunc(GL_LESS); } if (multipass) { doSphereMap = 1; } drawObject(&sphere[1]); } if (doubleBuffer) { glutSwapBuffers(); }}int angle = 0;int downx, downy;voidmotion(int x, int y){ if (moving) { angle += (x - downx); angle = angle % 360; eye[0] = sin(angle * 3.14159/180.0) * -15; eye[2] = cos(angle * 3.14159/180.0) * -15; eye[1] = eye[1] + 0.1 * (y - downy); if (eye[1] > +25) eye[1] = +25; if (eye[1] < -25) eye[1] = -25; smapSetEyeVector(smap, eye); smapSetEyeVector(smap2, eye); glutPostRedisplay(); downx = x; downy = y; }}voidmouse(int button, int state, int x, int y){ if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_DOWN) { moving = 1; downx = x; downy = y; } else { moving = 0; } }}voididle(void){ timeCount = timeCount + 0.1; obj2[Y] = -5 + cos(timeCount) * 1.0; smapSetObjectVector(smap, obj); smapSetObjectVector(smap2, obj2); smapSetObjectVector(smap3, obj); glutPostRedisplay();}/* When not visible, stop animating. Restart when visible again. */static void visible(int vis){ if (vis == GLUT_VISIBLE) { if (animation) glutIdleFunc(idle); } else { if (!animation) glutIdleFunc(NULL); }}void initGraphicsState(void);void initCallbacks(int ifFullscreen);voidmenu(int value){ switch (value) { case 0: showSphereMap = 0; break; case 1: showSphereMap = TO_SPHERE_MAP; break; case -1: showSphereMap = TO_SPHERE_MAP2; break; case -2: showSphereMap = TO_SPHERE_MAP3; break; case 4: object = (object + 1) % 3; break; case 5: smapSetSphereMapTexDim(smap, 64); smapSetViewTexDim(smap, 64); smapSetSphereMapTexDim(smap2, 64); smapSetViewTexDim(smap2, 64); smapSetSphereMapTexDim(smap3, 64); smapSetViewTexDim(smap3, 64); break; case 6: smapSetSphereMapTexDim(smap, 128); smapSetViewTexDim(smap, 128); smapSetSphereMapTexDim(smap2, 128); smapSetViewTexDim(smap2, 128); smapSetSphereMapTexDim(smap3, 128); smapSetViewTexDim(smap3, 128); break; case 7: smapSetSphereMapTexDim(smap, 256); smapSetViewTexDim(smap, 256); smapSetSphereMapTexDim(smap2, 256); smapSetViewTexDim(smap2, 256); smapSetSphereMapTexDim(smap3, 256); smapSetViewTexDim(smap3, 256); break; case 8: glDrawBuffer(GL_FRONT); glReadBuffer(GL_FRONT); doubleBuffer = 0; break; case 9: glDrawBuffer(GL_BACK); glReadBuffer(GL_BACK); doubleBuffer = 1; break; case 10: dynamicSmap = 1; break; case 11: dynamicSmap = 0; break; case 12: cullBackFaces = 1; break; case 13: cullBackFaces = 0; break; case 14: doSphereMap = 1; multipass = 0; break; case 15: doSphereMap = 0; multipass = 0; break; case 16: multipass = 1; break; case 17: animation = !animation; if (animation) { glutIdleFunc(idle); } else { glutIdleFunc(NULL); } break; case 666: exit(0); break; } glutPostRedisplay();}voidkeyboard(unsigned char c, int x, int y){ switch (c) { case 27: exit(0); break; case '1': menu(5); break; case '2': menu(6); break; case '3': menu(7); break; case 's': menu(4); break; case 'f': glutGameModeString("400x300:16@60"); glutEnterGameMode(); initGraphicsState(); initCallbacks(0); break; case 'l': glutLeaveGameMode(); glutSetWindow(win); break; case 'a': sphereTess += 5; glutPostRedisplay(); break; case 'z': sphereTess -= 5; glutPostRedisplay(); break; case 'b': case 'B': multibounce = !multibounce; printf("multibounce = %d\n", multibounce); break; case ' ': menu(17); break; }}voidinitGraphicsState(void){ GLfloat lightColor[4] = { 0.6, 0.6, 0.6, 1.0 }; /* white */ glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor); glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor); glLightfv(GL_LIGHT2, GL_DIFFUSE, lightColor); glLightfv(GL_LIGHT3, GL_DIFFUSE, lightColor); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glEnable(GL_LIGHT3); glEnable(GL_DEPTH_TEST); glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_NORMALIZE); glBindTexture(GL_TEXTURE_2D, 100); makePatternTexture();}voidinitCallbacks(int ifFullscreen){ glutReshapeFunc(reshape); glutDisplayFunc(display); glutVisibilityFunc(visible); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); glutMotionFunc(motion); if (ifFullscreen) { glutCreateMenu(menu); glutAddMenuEntry("eye view", 0); glutAddMenuEntry("eye to obj 1 smap", 1); glutAddMenuEntry("eye to obj 2 smap", -1); glutAddMenuEntry("obj 1 to obj 2 smap", -2); glutAddMenuEntry("switch object", 4); glutAddMenuEntry("texdim = 64", 5); glutAddMenuEntry("texdim = 128", 6); glutAddMenuEntry("texdim = 256", 7); glutAddMenuEntry("single buffer", 8); glutAddMenuEntry("double buffer", 9); glutAddMenuEntry("dynamic smap", 10); glutAddMenuEntry("stop smap building", 11); glutAddMenuEntry("cull back faces", 12); glutAddMenuEntry("cull front faces", 13); glutAddMenuEntry("sphere map", 14); glutAddMenuEntry("texture", 15); glutAddMenuEntry("multipass", 16); glutAddMenuEntry("toggle animate", 17); glutAddMenuEntry("quit", 666); glutAttachMenu(GLUT_RIGHT_BUTTON); }}intmain(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); win = glutCreateWindow("fakeraytrace"); initGraphicsState(); initCallbacks(1); smap = smapCreateSphereMap(NULL); smapSetSphereMapTexObj (smap, TO_SPHERE_MAP); smapSetViewTexObjs (smap, texobjs); smapSetEyeVector (smap, eye); smapSetUpVector (smap, up); smapSetObjectVector (smap, obj); smapSetNearFar (smap, 0.5, 40.0); smapSetPositionLightsFunc (smap, positionLights); smapSetDrawViewFunc (smap, drawView); smapSetViewTexDim (smap, 64); smapSetSphereMapTexDim (smap, 64); smapSetContextData(smap, &sphere[0]); smap2 = smapCreateSphereMap(NULL); smapSetSphereMapTexObj (smap2, TO_SPHERE_MAP2); smapSetViewTexObjs (smap2, texobjs2); smapSetEyeVector (smap2, eye); smapSetUpVector (smap2, up); smapSetObjectVector (smap2, obj2); smapSetNearFar (smap2, 0.5, 40.0); smapSetPositionLightsFunc (smap2, positionLights); smapSetDrawViewFunc (smap2, drawView); smapSetViewTexDim (smap2, 64); smapSetSphereMapTexDim (smap2, 64); smapSetContextData(smap2, &sphere[1]); smap3 = smapCreateSphereMap(NULL); smapSetSphereMapTexObj (smap3, TO_SPHERE_MAP3); smapSetViewTexObjs (smap3, texobjs3); smapSetEyeVector (smap3, obj2); smapSetUpVector (smap3, up2); smapSetObjectVector (smap3, obj); smapSetNearFar (smap3, 0.1, 40.0); smapSetPositionLightsFunc (smap3, positionLights); smapSetDrawViewFunc (smap3, drawView); smapSetViewTexDim (smap3, 64); smapSetSphereMapTexDim (smap3, 64); smapSetContextData(smap3, &sphere[2]); glutMainLoop(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -