Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////
  2. // main.cpp
  3. //
  4.  
  5. ///////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9.  
  10. #ifdef __APPLE__
  11. # include <GLUT/glut.h>
  12. #else
  13. # include <GL/glut.h>
  14. #endif
  15.  
  16. using namespace std;
  17. //Structures
  18. struct perspectives {
  19. float ex,ey,ez,cx,cy,cz;
  20. int x, y, z;
  21. };
  22.  
  23. struct coordinates {
  24. float x, y, z;
  25. };
  26.  
  27. // Globals.
  28. static float a = .1; // Blue ambient reflectance.
  29. static float d = 1.0; // Blue diffuse reflectance.
  30. static float s = 1.0; // White specular reflectance.
  31. static float h = 50.0; // Shininess.
  32. static float e = 0.0; // Blue emittance.
  33. static float t = 0.0; // Quadratic attenuation factor.
  34. static float zMove = 0.0; // z-direction component.
  35. static char theStringBuffer[10]; // String buffer.
  36. static long font = (long)GLUT_BITMAP_8_BY_13; // Font selection.
  37. static bool cameraRotate = false;
  38.  
  39. struct perspectives camera = {0,0,12,0,0,0,0,1,0};
  40. struct coordinates key = {0,0,11};
  41.  
  42. // Routine to draw a bitmap character string.
  43. void writeBitmapString(void *font, char *string)
  44. {
  45. char *c;
  46.  
  47. for (c = string; *c != '\0'; c++) glutBitmapCharacter(font, *c);
  48. }
  49.  
  50. // Routine to convert floating point to char string.
  51. void floatToString(char * destStr, int precision, float val)
  52. {
  53. sprintf(destStr,"%f",val);
  54. destStr[precision] = '\0';
  55. }
  56.  
  57. //Routine to draw a ball of light
  58. void drawMoon()
  59. {//TODO
  60. // Light position vectors.
  61. float lightPos[] = { 3, 4, -.2, 1.0 };
  62.  
  63. glDisable(GL_LIGHTING);
  64.  
  65. // Light0 and its sphere positioned.
  66. glPushMatrix();
  67. glLightfv(GL_LIGHT1, GL_POSITION, lightPos);
  68. glTranslatef(lightPos[0], lightPos[1], lightPos[2]);
  69. glColor3f(1.0, 1.0, 1.0);
  70. glutSolidSphere(2, 20, 20);
  71. glPopMatrix();
  72.  
  73.  
  74. glEnable(GL_LIGHTING);
  75. }
  76.  
  77. //Routine to draw starry sky
  78. void drawStars()
  79. {
  80. //TODO
  81. }
  82.  
  83. void drawPumpkin()
  84. {
  85. //TODO
  86. }
  87.  
  88. void drawKey()
  89. {
  90. //TODO
  91. }
  92.  
  93.  
  94. //Routine to draw walls
  95. void drawWalls()
  96. {
  97. //glDisable(GL_LIGHTING);
  98. glColor3f(51/255.0,25/255.0,0);
  99. glPushMatrix();
  100. //glRotatef(0,0,0,1);
  101. glScalef(1,1,3);
  102. glBegin(GL_QUADS);
  103. /* Floor */
  104. glVertex3f(-1,-1,-1);
  105. glVertex3f(-1,-1,1);
  106. glVertex3f(1,-1,1);
  107. glVertex3f(1,-1,-1);
  108. /* Ceiling */
  109. glVertex3f(-1,1,-1);
  110. glVertex3f(1,1,-1);
  111. glVertex3f(1,1,1);
  112. glVertex3f(-1,1,1);
  113. /* Walls */
  114.  
  115. /* front outer */
  116. /* left half */
  117. glVertex3f(-1,-1,1);
  118. glVertex3f(-.45,-1,1);
  119. glVertex3f(-.45,1,1);
  120. glVertex3f(-1,1,1);
  121.  
  122. /* right half */
  123. glVertex3f(.45,-1,1);
  124. glVertex3f(1,-1,1);
  125. glVertex3f(1,1,1);
  126. glVertex3f(.45,1,1);
  127.  
  128. /* top half */
  129.  
  130. glVertex3f(-1,.5,1);
  131. glVertex3f(1,.5,1);
  132. glVertex3f(1,1,1);
  133. glVertex3f(-1,1,1);
  134. /* end front outer */
  135.  
  136. /* front door */
  137. //TODO
  138.  
  139. /* end front door */
  140.  
  141. /* back inner */
  142. /* left half */
  143. glVertex3f(-1,-1,-1);
  144. glVertex3f(-.45,-1,-1);
  145. glVertex3f(-.45,1,-1);
  146. glVertex3f(-1,1,-1);
  147.  
  148. /* right half */
  149. glVertex3f(.45,-1,-1);
  150. glVertex3f(1,-1,-1);
  151. glVertex3f(1,1,-1);
  152. glVertex3f(.45,1,-1);
  153.  
  154. /* top half */
  155.  
  156. glVertex3f(-1,.5,-1);
  157. glVertex3f(1,.5,-1);
  158. glVertex3f(1,1,-1);
  159. glVertex3f(-1,1,-1);
  160. /* end inner */
  161.  
  162. glVertex3f(1,1,1);
  163. glVertex3f(1,1,-1);
  164. glVertex3f(1,-1,-1);
  165. glVertex3f(1,-1,1);
  166.  
  167.  
  168. glVertex3f(-1,1,1);
  169. glVertex3f(-1,-1,1);
  170. glVertex3f(-1,-1,-1);
  171. glVertex3f(-1,1,-1);
  172. glEnd();
  173.  
  174. glPopMatrix();
  175. //glEnable(GL_LIGHTING);
  176. }
  177.  
  178. void drawLight()
  179. {
  180. // Light position vectors.
  181. float lightPos0[] = { 0.0, .75, -.5, 1.0 };
  182.  
  183. glDisable(GL_LIGHTING);
  184.  
  185. // Light0 and its sphere positioned.
  186. glPushMatrix();
  187. glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  188. glTranslatef(lightPos0[0], lightPos0[1], lightPos0[2]);
  189. glColor3f(1.0, 1.0, 1.0);
  190. glutWireSphere(0.05, 8, 8);
  191. glPopMatrix();
  192.  
  193.  
  194. glEnable(GL_LIGHTING);
  195. }
  196.  
  197. // Initialization routine.
  198. void setup(void)
  199. {
  200. //Set globals
  201.  
  202. a = 1- abs(camera.ez/15.0);
  203.  
  204. glClearColor(0.0, 0.0, 0.0, 0.0);
  205. glEnable(GL_DEPTH_TEST); // Enable depth testing.
  206.  
  207. // Turn on OpenGL lighting.
  208. glEnable(GL_LIGHTING);
  209.  
  210. // Light property vectors.
  211. float lightAmb[] = { 0.0, 0.0, 0.0, 1.0 };
  212. float lightDifAndSpec0[] = { 1.0, 1.0, 1.0, 1.0 };
  213. float lightDifAndSpec1[] = { 1.0, 1.0, 0.0, 1.0 };
  214. float globAmb[] = { 0.2, 0.2, 0.2, 1.0 };
  215.  
  216. // Light0 properties.
  217. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
  218. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDifAndSpec0);
  219. glLightfv(GL_LIGHT0, GL_SPECULAR, lightDifAndSpec0);
  220.  
  221. // Light1 properties.
  222. glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmb);
  223. glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDifAndSpec1);
  224. glLightfv(GL_LIGHT1, GL_SPECULAR, lightDifAndSpec1);
  225.  
  226. glEnable(GL_LIGHT0); // Enable particular light source.
  227. glEnable(GL_LIGHT1); // Enable particular light source.
  228. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globAmb); // Global ambient light.
  229. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); // Enable local viewpoint
  230.  
  231. // Cull back faces.
  232. glEnable(GL_CULL_FACE);
  233. glCullFace(GL_BACK);
  234. }
  235.  
  236. // Drawing routine.
  237. void drawScene()
  238. {
  239.  
  240.  
  241. // Material property vectors.
  242. float matAmb[] = {0.0, 0.0, a, 1.0};
  243. float matDif[] = {0.0, 0.0, d, 1.0};
  244. float matSpec[] = { s, s, s, 1.0 };
  245. float matShine[] = { h };
  246. float matEmission[] = {0.0, 0.0, e, 1.0};
  247.  
  248. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  249. glLoadIdentity();
  250.  
  251. // Light quadratic attenuation factor.
  252. glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, t);
  253. glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, t);
  254.  
  255.  
  256. //gluLookAt(0.0, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  257.  
  258. gluLookAt(camera.ex,camera.ey, camera.ez,
  259. camera.cx,camera.cy,camera.cz,
  260. camera.x,camera.y,camera.z);
  261. //gluLookAt(0,0,0,0,0,0,0,1,0);
  262. // Draw light source spheres after disabling lighting.
  263. drawLight();
  264.  
  265. // Material properties of sphere.
  266. glMaterialfv(GL_FRONT, GL_AMBIENT, matAmb);
  267. glMaterialfv(GL_FRONT, GL_DIFFUSE, matDif);
  268. glMaterialfv(GL_FRONT, GL_SPECULAR, matSpec);
  269. glMaterialfv(GL_FRONT, GL_SHININESS, matShine);
  270. glMaterialfv(GL_FRONT, GL_EMISSION, matEmission);
  271. // Sphere.
  272. glTranslatef(0.0, 0.0, zMove); // Move the sphere.
  273.  
  274. //glutSolidCube(1.5);
  275. drawWalls();
  276. drawMoon();
  277.  
  278. glutSwapBuffers();
  279. }
  280.  
  281. // OpenGL window reshape routine.
  282. void resize (int w, int h)
  283. {
  284. glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  285. glMatrixMode (GL_PROJECTION);
  286. glLoadIdentity();
  287. gluPerspective(60.0, (float)w/(float)h, 1.0, 100.0);
  288. glMatrixMode(GL_MODELVIEW);
  289. }
  290.  
  291. // Keyboard input processing routine.
  292. void keyInput(unsigned char key, int x, int y)
  293. {
  294.  
  295. int mod = glutGetModifiers();
  296.  
  297. if (mod!=0)
  298. {
  299. switch(mod)
  300. {
  301. //Shift key
  302. case 1 : cameraRotate=true; break;
  303. //ALT key
  304. case 4 : ; break;
  305. mod = 0;
  306. }
  307.  
  308. }
  309.  
  310.  
  311. switch (key)
  312. {
  313. case 27:
  314. exit(0);
  315. break;
  316. case 'a':
  317. if (a > 0.0) a -= 0.05;
  318. glutPostRedisplay();
  319. break;
  320. case 'A':
  321. if (a < 1.0) a += 0.05;
  322. glutPostRedisplay();
  323. break;
  324. case 'd':
  325. if (d > 0.0) d -= 0.05;
  326. glutPostRedisplay();
  327. break;
  328. case 'D':
  329. if (d < 1.0) d += 0.05;
  330. glutPostRedisplay();
  331. break;
  332. case 's':
  333. if (s > 0.0) s -= 0.05;
  334. glutPostRedisplay();
  335. break;
  336. case 'S':
  337. if (s < 1.0) s += 0.05;
  338. glutPostRedisplay();
  339. break;
  340. case 'e':
  341. if (e > 0.0) e -= 0.05;
  342. glutPostRedisplay();
  343. break;
  344. case 'E':
  345. if (e < 1.0) e += 0.05;
  346. glutPostRedisplay();
  347. break;
  348. case 'h':
  349. if (h > 0.0) h -= 1.0;
  350. glutPostRedisplay();
  351. break;
  352. case 'H':
  353. if (h < 128.0) h += 1.0;
  354. glutPostRedisplay();
  355. break;
  356. case 't':
  357. if (t > 0.0) t -= 0.005;
  358. glutPostRedisplay();
  359. break;
  360. case 'T':
  361. t += 0.005;
  362. glutPostRedisplay();
  363. break;
  364. default:
  365. break;
  366. }
  367. }
  368.  
  369. // Callback routine for non-ASCII key entry.
  370. void specialKeyInput(int key, int x, int y)
  371. {
  372.  
  373. if (key == GLUT_KEY_UP){
  374. if(camera.ez - .1 >= 0)
  375. {camera.ez += -0.1;
  376. camera.ex += 0;}
  377. }
  378. if (key == GLUT_KEY_DOWN){
  379. camera.ez += 0.1;
  380. camera.ex += 0;
  381. }
  382. if (key == GLUT_KEY_LEFT){
  383. camera.cx -= .1;
  384. }
  385. if (key == GLUT_KEY_RIGHT){
  386. camera.cx += .1;
  387. }
  388. printf("This is my ez %f\n", camera.ez);
  389.  
  390.  
  391. a = 1- abs(camera.ez/5.0);
  392. glutPostRedisplay();
  393. }
  394.  
  395. // Routine to output interaction instructions to the C++ window.
  396. void printInteraction(void)
  397. {
  398. }
  399.  
  400. // Main routine.
  401. int main(int argc, char **argv)
  402. {
  403. printInteraction();
  404. glutInit(&argc, argv);
  405. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  406. glutInitWindowSize (600, 600);
  407. glutInitWindowPosition (100, 100);
  408. glutCreateWindow ("scaryRoom.cpp");
  409. setup();
  410. glutDisplayFunc(drawScene);
  411. glutReshapeFunc(resize);
  412. glutKeyboardFunc(keyInput);
  413. glutSpecialFunc(specialKeyInput);
  414. glutMainLoop();
  415.  
  416. return 0;
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement