Guest User

Untitled

a guest
Oct 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. //final project Miguel y Emmanuel
  2. #include <windows.h>
  3. #include <GL/gl.h>
  4. #include <glut.h>
  5. #include <math.h>
  6. #include "Camera.h"
  7. #include "Vector3.h"
  8. #include <iostream>
  9. #include <assert.h>
  10. #include <fstream>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "textures.h"
  15. #include "terrain.h"
  16. #include <gl/GLU.h>
  17.  
  18.  
  19. using namespace std;
  20.  
  21. /*********************************/
  22. /* NAVIGATION VARIABLES */
  23. /*********************************/
  24. /* 0.- Terrain texture
  25. * 1.- Military Texture for Tents
  26. */
  27. static GLuint textures[3];
  28.  
  29. /*********************************/
  30. /* NAVIGATION VARIABLES */
  31. /*********************************/
  32. Camera *camera;
  33. float mouse_x, mouse_y;
  34. bool mouse_updated = false;
  35.  
  36. /*********************************/
  37. /* TERRAIN VARIABLES */
  38. /*********************************/
  39. RGBpixmap myTexs[3];
  40.  
  41.  
  42. void init(void)
  43. {
  44. // Set clear (background) color
  45. glClearColor(0.62, 0.90, 0.98, 0.0);
  46.  
  47. // Set light properties...
  48. GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
  49. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  50.  
  51. // Finish setting up the two lights (position, and component values (specular and diffuse))
  52. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  53. glLightfv(GL_LIGHT0, GL_AMBIENT, white_light);
  54. //glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
  55.  
  56. // Define material properties
  57. GLfloat mat_specular[] = { 0.8, 0.9, 1.0, 1.0 };
  58. GLfloat mat_shininess[] = { 30.0 };
  59. GLfloat mat_surface[] = { 0.7, 0.7, 0.7, 1.0 };
  60.  
  61. // Set material properties, as defined above
  62. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  63. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  64. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_surface);
  65. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_surface);
  66.  
  67. // Set shading model to use
  68. glShadeModel (GL_SMOOTH);
  69. // Enable lighting
  70. glEnable(GL_LIGHTING);
  71. // Activate (enable) lights
  72. glEnable(GL_LIGHT0);
  73. // Enable depth testing (for hidden surface removal)
  74. glEnable(GL_DEPTH_TEST);
  75. // Enable texture
  76. glEnable(GL_TEXTURE_2D);
  77. glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
  78.  
  79. // Initialize textures
  80. glGenTextures(2, textures);
  81. // Initialize terrain texture
  82. myTexs[0].SetTexColor(1.0, 1.0, 1.0);
  83. myTexs[0].readBMPFile("escaleras2.bmp");
  84. myTexs[0].SetTexture(textures[0]);
  85. // Initializse military texture
  86. myTexs[1].SetTexColor(1.0, 1.0, 1.0);
  87. myTexs[1].readBMPFile("escaleras1.bmp");
  88. myTexs[1].SetTexture(textures[1]);
  89. // Initialize textures
  90. //glGenTextures(2, textures);
  91. // Initialize terrain texture
  92. myTexs[2].SetTexColor(1.0, 1.0, 1.0);
  93. myTexs[2].readBMPFile("grass.bmp");
  94. myTexs[2].SetTexture(textures[2]);
  95.  
  96.  
  97.  
  98. // Initialize the camera for navigation
  99. camera = new Camera();
  100. camera->set( 5, 5, 5, 0, 5, 5, 0, 1, 0 );
  101. }
  102.  
  103.  
  104. void escalera(float peldanos){
  105. float ancho=10.0;
  106. float alto=1.0;
  107. float profundo=2.0;
  108. float peldanos=4;
  109.  
  110. for(int k=0;k<peldanos;k++){
  111.  
  112.  
  113. cout<<"---------------k vale --------------"<<k<<"\n";
  114.  
  115. //parte frontal del peldaño
  116. //con la textura 0 de escalera
  117. glBindTexture(GL_TEXTURE_2D, textures[0]);
  118. //inicia dibujando el primer peldaño
  119.  
  120. cout<<"**************imprime parte frontal del peldaño**********"<<"\n";
  121.  
  122. glBegin(GL_QUADS);
  123. glTexCoord2f(0.0, 0.0);
  124. glVertex3f(0.0, alto*k, profundo*(-k));
  125. cout<<"+++++++++++++++++ vertex 1 ++++++++++++++++++++++"<<"\n";
  126. cout<<"x: "<<0.0<<" y: "<<alto*k<<" z: "<<profundo*(-k)<<"\n";
  127.  
  128.  
  129. glTexCoord2f(1.0, 0.0);
  130. glVertex3f(ancho, alto*k, profundo*(-k));
  131. cout<<"+++++++++++++++++ vertex 2 ++++++++++++++++++++++"<<"\n";
  132. cout<<"x: "<<ancho<<" y: "<<alto*k<<" z: "<<profundo*(-k)<<"\n";
  133.  
  134. glTexCoord2f(1.0, 1.0);
  135. glVertex3f(ancho, alto*(k+1), profundo*(-k));
  136. cout<<"+++++++++++++++++ vertex 3 ++++++++++++++++++++++"<<"\n";
  137. cout<<"x: "<<ancho<<" y: "<<alto*(k+1)<<" z: "<<profundo*(-k)<<"\n";
  138.  
  139. glTexCoord2f(0.0, 1.0);
  140. glVertex3f(0.0, alto*(k+1),profundo*(-k));
  141. cout<<"+++++++++++++++++ vertex 4 ++++++++++++++++++++++"<<"\n";
  142. cout<<"x: "<<0.0<<" y: "<<alto*(k+1)<<" z: "<<profundo*(-k)<<"\n";
  143.  
  144. glEnd();
  145.  
  146.  
  147. //parte superior del peldaño
  148. //con la textura 1 de escalera
  149. glBindTexture(GL_TEXTURE_2D, textures[1]);
  150. //inicia dibujando el primer peldaño
  151. cout<<"**************imprime parte superior del peldaño**********"<<"\n";
  152. glBegin(GL_QUADS);
  153.  
  154. glTexCoord2f(0.0, 0.0);
  155. glVertex3f(0.0, alto*(k+1), profundo*(-k));
  156. cout<<"+++++++++++++++++ vertex 1 ++++++++++++++++++++++"<<"\n";
  157. cout<<"x: "<<0.0<<" y: "<<alto*(k+1)<<" z: "<<profundo*(-k)<<"\n";
  158.  
  159.  
  160. glTexCoord2f(1.0, 0.0);
  161. glVertex3f(ancho, alto*(k+1), profundo*(-k));
  162. cout<<"+++++++++++++++++ vertex 2 ++++++++++++++++++++++"<<"\n";
  163. cout<<"x: "<<ancho<<" y: "<<alto*(k+1)<<" z: "<<profundo*(-k)<<"\n";
  164.  
  165. glTexCoord2f(1.0, 1.0);
  166. glVertex3f(ancho, alto*(k+1), profundo*(k+1)*(-1));
  167. cout<<"+++++++++++++++++ vertex 3 ++++++++++++++++++++++"<<"\n";
  168. cout<<"x: "<<ancho<<" y: "<<alto*(k+1)<<" z: "<<profundo*(k+1)*(-1)<<"\n";
  169.  
  170. glTexCoord2f(0.0, 1.0);
  171. glVertex3f(0.0, alto*(k+1), profundo*(k+1)*(-1));
  172. cout<<"+++++++++++++++++ vertex 4 ++++++++++++++++++++++"<<"\n";
  173. cout<<"x: "<<0.0<<" y: "<<alto*(k+1)<<" z: "<<profundo*(k+1)*(-1)<<"\n";
  174.  
  175. glEnd();
  176.  
  177. }
  178. }
  179.  
  180. void display(void)
  181. {
  182.  
  183.  
  184. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  185. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  186.  
  187. glPushMatrix();
  188. // Draw terrain
  189. glBindTexture(GL_TEXTURE_2D, textures[2]);
  190. drawTerrain();
  191. glPopMatrix();
  192.  
  193. ///dibuja escaleras
  194.  
  195. glPushMatrix();
  196.  
  197. escalera(5.0);
  198.  
  199. glPopMatrix();
  200.  
  201. glFlush();
  202. glutSwapBuffers();
  203. }
  204.  
  205.  
  206. void mouse_motion( int x, int y ) {
  207. float dx, dy;
  208. if( ! mouse_updated) {
  209. mouse_x = x;
  210. mouse_y = y;
  211. mouse_updated = true;
  212. } else {
  213. dx = x - mouse_x;
  214. dy = y - mouse_y;
  215. mouse_x = x;
  216. mouse_y = y;
  217. //camera->pitch( dy / 30.0 );
  218. camera->yaw( dx );
  219. }
  220. }
  221.  
  222. void keyboard(unsigned char key, int x, int y)
  223. {
  224. switch (key) {
  225. case 0x1B:
  226. case 'q':
  227. case 'Q': exit(0); break;
  228. default: break;
  229. }
  230. }
  231.  
  232. void specialKeyboard(int key, int x, int y) {
  233. switch(key) {
  234. case GLUT_KEY_UP : camera->slide( 0.0, 0.0, -2.0 ); break;
  235. case GLUT_KEY_DOWN : camera->slide( 0.0, 0.0, 2.0 ); break;
  236. case GLUT_KEY_LEFT : camera->slide( -2.0, 0.0, 0.0 ); break;
  237. case GLUT_KEY_RIGHT : camera->slide( 2.0, 0.0, 0.0 ); break;
  238. }
  239. }
  240.  
  241. int main (int argc, char** argv)
  242. {
  243. glutInit(&argc, argv);
  244. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  245. glutInitWindowSize(1024, 800);
  246. glutInitWindowPosition(100, 100);
  247. glutCreateWindow("Final Project");
  248. init();
  249.  
  250. glutKeyboardFunc(keyboard);
  251. glutDisplayFunc(display);
  252. glutIdleFunc(display);
  253. glutSpecialFunc(specialKeyboard);
  254. glutPassiveMotionFunc(mouse_motion);
  255.  
  256. glutMainLoop();
  257. return 0;
  258. }
Add Comment
Please, Sign In to add comment