Herowaree

opengl

Mar 4th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.21 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glut.h>
  3.  
  4. //Other libraries
  5. #include <iostream>
  6. #include <fstream>
  7. #include <math.h>
  8. #include <string.h>
  9. //---------------------------------------------------------------
  10.  
  11. #define PI 3.141592654
  12.  
  13. //Global
  14. float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0;
  15. float lastx, lasty;
  16. float xrotrad, yrotrad;
  17.  
  18. const unsigned int MAP_SIZE = 30;
  19. const float MAX_DISTANCE = 275;
  20. const float MOVEMENT_SPEED = 0.05;
  21.  
  22. GLuint Texture, paper;
  23. GLuint skybox[5];
  24.  
  25. bool state[255];
  26.  
  27. enum{ SKY_LEFT = 0,
  28. SKY_BACK = 1,
  29. SKY_RIGHT = 2,
  30. SKY_FRONT = 3,
  31. SKY_TOP = 4
  32. };
  33.  
  34. typedef struct vertex{
  35. float x[8000];
  36. float y[8000];
  37. float z[8000];
  38. int count;
  39. }tVertex;
  40.  
  41. typedef struct normal{
  42. float x[8000];
  43. float y[8000];
  44. float z[8000];
  45. int count;
  46. }tNormal;
  47.  
  48. typedef struct texture{
  49. float s[8000];
  50. float t[8000];
  51. int count;
  52. }tTexture;
  53.  
  54. typedef struct face{
  55. int v1, v2, v3;
  56. int n1, n2, n3;
  57. int t1, t2, t3;
  58. face *next;
  59. }tFace;
  60.  
  61. typedef struct faceList{
  62. int count;
  63. face *head;
  64. }tFaceList;
  65.  
  66. typedef struct object{
  67. tFaceList faces;
  68. tTexture textureCoordinates;
  69. tVertex vertexs;
  70. tNormal normals;
  71. GLuint textureID;
  72. }tObject;
  73.  
  74. tObject slender, rock, tree, crate, house;
  75.  
  76. //Alusion
  77. void keyboard_down(unsigned char key, int x, int y);
  78. void drawText(float x, float y, const char *string, void *font);
  79. void initskybox();
  80. void drawSkybox(float size);
  81. void moveWorld();
  82. void mouse(int x, int y);
  83. void free_mem(void);
  84. void drawGround(int w, int h);
  85. void getMouseAction(int button, int state, int x, int y);
  86. void displayScreen(void);
  87. void displayComands(void);
  88. void keyboard_up( unsigned char key, int x, int y);
  89. void light(void);
  90.  
  91. void insertVertex(tObject *obj, float x, float y, float z);
  92. void insertNormal(tObject *obj, float x, float y, float z);
  93. void insertTexture(tObject *obj, float s, float t);
  94. void insertNormal(tObject *obj, float x, float y, float z);
  95. void insertFaceCount(tObject *obj, int v1, int t1, int n1, int v2, int t2, int n2, int v3, int t3, int n3);
  96. void loadObject(tObject *obj, const char *object, const char *texture);
  97.  
  98. GLuint loadBMP_custom(const char * imagepath);
  99.  
  100. unsigned int loadTexture(const char* imagepath);
  101.  
  102. void free_mem(void){
  103. std::clog << "Exiting...\n";
  104. }
  105.  
  106. void loadObject(tObject *obj, const char *object, const char *texture){
  107. obj->vertexs.count = 0;
  108. obj->normals.count = 0;
  109. obj->textureCoordinates.count = 0;
  110. obj->faces.count = 0;
  111. obj->faces.head = NULL;
  112.  
  113. FILE *file = fopen(object, "r");
  114. if( file == NULL){
  115. printf("Impossible to open the file !\n");
  116. return ;
  117. }
  118.  
  119. while(1){
  120. char line[128];
  121. int res = fscanf(file, "%s", line);
  122. if(res == EOF) break;
  123.  
  124. if(strcmp(line, "v") == 0){
  125. float x, y, z;
  126. fscanf(file, "%f %f %f\n", &x, &y, &z);
  127. insertVertex(obj, x, y, z);
  128. }else if(strcmp(line, "vt") == 0){
  129. float s, t;
  130. fscanf(file, "%f %f\n", &s, &t);
  131. insertTexture(obj, s, t);
  132. }else if(strcmp(line, "f") == 0){
  133. int v1, t1, n1, v2, t2, n2, v3, t3, n3;
  134. fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &v1, &t1, &n1, &v2, &t2, &n2, &v3, &t3, &n3);
  135. insertFaceCount(obj, v1, t1, n1, v2, t2, n2, v3, t3, n3);
  136. }else if(strcmp(line, "vn") == 0){
  137. float nx, ny, nz;
  138. fscanf(file, "%f %f %f\n", &nx, &ny, &nz);
  139. insertNormal(obj, nx, ny, nz);
  140. }
  141. }
  142.  
  143. obj->textureID = loadBMP_custom(texture);
  144. }
  145.  
  146. void insertVertex(tObject *obj, float x, float y, float z){
  147. obj->vertexs.x[ obj->vertexs.count ] = x;
  148. obj->vertexs.y[ obj->vertexs.count ] = y;
  149. obj->vertexs.z[ obj->vertexs.count ] = z;
  150.  
  151. obj->vertexs.count = obj->vertexs.count + 1;
  152. }
  153.  
  154. void insertNormal(tObject *obj, float x, float y, float z){
  155. obj->normals.x[ obj->normals.count ] = x;
  156. obj->normals.y[ obj->normals.count ] = y;
  157. obj->normals.z[ obj->normals.count ] = z;
  158.  
  159. obj->normals.count = obj->normals.count + 1;
  160. }
  161.  
  162. void insertTexture(tObject *obj, float s, float t){
  163. obj->textureCoordinates.s[ obj->textureCoordinates.count ] = s;
  164. obj->textureCoordinates.t[ obj->textureCoordinates.count ] = t;
  165.  
  166. obj->textureCoordinates.count = obj->textureCoordinates.count + 1;
  167. }
  168.  
  169. void insertFaceCount(tObject *obj, int v1, int t1, int n1, int v2, int t2, int n2, int v3, int t3, int n3){
  170. face *newFace = new face;
  171. newFace->next = NULL;
  172.  
  173. newFace->v1 = v1;
  174. newFace->v2 = v2;
  175. newFace->v3 = v3;
  176.  
  177. newFace->n1 = n1;
  178. newFace->n2 = n2;
  179. newFace->n3 = n3;
  180.  
  181. newFace->t1 = t1;
  182. newFace->t2 = t2;
  183. newFace->t3 = t3;
  184.  
  185. if(obj->faces.head == NULL){
  186. obj->faces.head = newFace;
  187. }else{
  188. face *cur = obj->faces.head;
  189. while(cur->next != NULL){
  190. cur = cur->next;
  191. }
  192. cur->next = newFace;
  193. }
  194.  
  195. obj->faces.count = obj->faces.count + 1;
  196. }
  197.  
  198. //http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
  199. GLuint loadBMP_custom(const char * imagepath){
  200. unsigned char header[54];
  201. unsigned int dataPos;
  202. unsigned int width, height;
  203. unsigned int imageSize;
  204.  
  205. unsigned char *data;
  206.  
  207. FILE *file = fopen(imagepath, "rb");
  208. if(!file){
  209. std::clog << "Image could not be opened\n";
  210. return 0;
  211. }
  212.  
  213. if( fread(header, 1, 54, file) != 54){
  214. std::clog << "Not a correct BMP file\n";
  215.  
  216. return false;
  217. if(header[0] != 'B' || header[1]!= 'M'){
  218. std::clog << "Not a correct BMP file\n";
  219. return 0;
  220. }
  221. }
  222.  
  223. dataPos = *(int*)&(header[0x0A]);
  224. imageSize = *(int*)&(header[0x22]);
  225. width = *(int*)&(header[0x12]);
  226. height = *(int*)&(header[0x16]);
  227.  
  228. if(imageSize == 0) imageSize = width*height*3;
  229. if(dataPos == 0) dataPos = 54;
  230.  
  231. data = new unsigned char [imageSize];
  232.  
  233. fread(data, 1, imageSize, file);
  234.  
  235. fclose(file);
  236.  
  237. GLuint textureID;
  238. glGenTextures(1, &textureID);
  239. glBindTexture(GL_TEXTURE_2D, textureID);
  240. glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
  241.  
  242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  243. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  244.  
  245. return textureID;
  246. }
  247.  
  248. unsigned int loadTexture(const char* imagepath){
  249. unsigned char header[54];
  250. unsigned int dataPos;
  251. unsigned int width, height;
  252. unsigned int imageSize;
  253. unsigned char *data;
  254.  
  255. FILE *file = fopen(imagepath, "rb");
  256. if(!file){
  257. std::clog << "Image "<< imagepath <<" could not be opened\n";
  258. return 0;
  259. }
  260.  
  261. if( fread(header, 1, 54, file) != 54){
  262. std::clog << "Not a correct BMP file\n";
  263. return false;
  264.  
  265. if(header[0] != 'B' || header[1]!= 'M'){
  266. std::clog << "Not a correct BMP file\n";
  267. return 0;
  268. }
  269. }
  270.  
  271. dataPos = *(int*)&(header[0x0A]);
  272. imageSize = *(int*)&(header[0x22]);
  273. width = *(int*)&(header[0x12]);
  274. height = *(int*)&(header[0x16]);
  275.  
  276. if(imageSize == 0) imageSize = width * height * 3;
  277. if(dataPos == 0) dataPos = 54;
  278.  
  279. data = new unsigned char [imageSize];
  280.  
  281. fread(data, 1, imageSize, file);
  282.  
  283. fclose(file);
  284.  
  285. glEnable(GL_TEXTURE_2D);
  286.  
  287. unsigned int num;
  288. glGenTextures(1,&num);
  289. glBindTexture(GL_TEXTURE_2D,num);
  290. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  291. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  292. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  293. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  294. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  295. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  296.  
  297. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
  298.  
  299. glDisable(GL_TEXTURE_2D);
  300.  
  301. return num;
  302. }
  303.  
  304. void initskybox(){
  305. skybox[SKY_LEFT] = loadTexture("nightsky_left.bmp");
  306. skybox[SKY_BACK] = loadTexture("nightsky_back.bmp");
  307. skybox[SKY_RIGHT] = loadTexture("nightsky_right.bmp");
  308. skybox[SKY_FRONT] = loadTexture("nightsky_front.bmp");
  309. skybox[SKY_TOP] = loadTexture("nightsky_top.bmp");
  310. }
  311.  
  312. void drawSkybox(float size){
  313. bool L = glIsEnabled(GL_LIGHTING);
  314. bool T = glIsEnabled(GL_TEXTURE_2D);
  315. bool DT = glIsEnabled(GL_DEPTH_TEST);
  316.  
  317. if( L ) glDisable(GL_LIGHTING);
  318. if( !T ) glEnable(GL_TEXTURE_2D);
  319. if( DT ) glDisable(GL_DEPTH_TEST);
  320.  
  321. glBindTexture(GL_TEXTURE_2D, skybox[SKY_FRONT]);
  322. glBegin(GL_QUADS);
  323. glTexCoord2f(1,1); glVertex3f(size / 2, size - size / 2, size / 2);
  324. glTexCoord2f(0,1); glVertex3f(-size / 2, size - size / 2, size / 2);
  325. glTexCoord2f(0,0); glVertex3f(-size / 2, -size / 2, size / 2);
  326. glTexCoord2f(1,0); glVertex3f(size / 2, -size / 2, size / 2);
  327. glEnd();
  328.  
  329. glBindTexture(GL_TEXTURE_2D,skybox[SKY_LEFT]);
  330. glBegin(GL_QUADS);
  331. glTexCoord2f(1,1); glVertex3f(-size / 2, size - size / 2, size / 2);
  332. glTexCoord2f(0,1); glVertex3f(-size / 2, size - size / 2, -size / 2);
  333. glTexCoord2f(0,0); glVertex3f(-size / 2, -size / 2, -size / 2);
  334. glTexCoord2f(1,0); glVertex3f(-size / 2, -size / 2, size / 2);
  335. glEnd();
  336.  
  337. glBindTexture(GL_TEXTURE_2D,skybox[SKY_BACK]);
  338. glBegin(GL_QUADS);
  339. glTexCoord2f(0,1); glVertex3f(size / 2, size - size / 2, -size / 2);
  340. glTexCoord2f(1,1); glVertex3f(-size / 2, size - size / 2, -size / 2);
  341. glTexCoord2f(1,0); glVertex3f(-size / 2, -size / 2, -size / 2);
  342. glTexCoord2f(0,0); glVertex3f(size / 2, -size / 2, -size / 2);
  343. glEnd();
  344.  
  345. glBindTexture(GL_TEXTURE_2D,skybox[SKY_RIGHT]);
  346. glBegin(GL_QUADS);
  347. glTexCoord2f(1,1); glVertex3f(size / 2, size - size / 2, -size / 2);
  348. glTexCoord2f(0,1); glVertex3f(size / 2, size - size / 2, size / 2);
  349. glTexCoord2f(0,0); glVertex3f(size / 2, -size / 2, size / 2);
  350. glTexCoord2f(1,0); glVertex3f(size / 2, -size / 2, -size / 2);
  351. glEnd();
  352.  
  353. glBindTexture(GL_TEXTURE_2D,skybox[SKY_TOP]);
  354. glBegin(GL_QUADS);
  355. glTexCoord2f(1,0); glVertex3f(size / 2, size - size / 2, size / 2);
  356. glTexCoord2f(0,0); glVertex3f(-size / 2, size - size / 2, size / 2);
  357. glTexCoord2f(0,1); glVertex3f(-size / 2, size - size / 2, -size / 2);
  358. glTexCoord2f(1,1); glVertex3f(size / 2 , size - size / 2, -size / 2);
  359. glEnd();
  360.  
  361. if( L ) glEnable(GL_LIGHTING);
  362. if( !T ) glDisable(GL_TEXTURE_2D);
  363. if( DT ) glEnable(GL_DEPTH_TEST);
  364. }
  365.  
  366. void mouse(int x, int y) { //TODO: Keep the mouse on screen
  367. int diffx = x - lastx;
  368. int diffy = y - lasty;
  369. lastx = x;
  370. lasty = y;
  371.  
  372. if( (xrot + (float) diffy) < -30.0f){
  373. xrot = -30.0f;
  374. }else if( (xrot + (float) diffy) > 20.0f){
  375. xrot = 20.0f;
  376. }else{
  377. xrot += (float)diffy;
  378. }
  379.  
  380. yrot += (float) diffx;
  381. }
  382.  
  383. //http://stackoverflow.com/questions/14925521/how-to-make-action-on-keyboard-input-be-instant
  384. void keyboard_up(unsigned char key, int x, int y){
  385. state[ key ] = false;
  386. }
  387.  
  388. void keyboard_down(unsigned char key, int x, int y){
  389. state[ key ] = true;
  390. }
  391.  
  392. //http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/
  393. //http://www.swiftless.com/tutorials/opengl/camera2.html
  394. void keyboard(void){
  395.  
  396. if(state['w'] || state['W']){
  397. yrotrad = (yrot / 180 * PI);
  398. xrotrad = (xrot / 180 * PI);
  399. xpos += float(sin(yrotrad));
  400. zpos -= float(cos(yrotrad));
  401. }
  402.  
  403. if(state['s'] || state['S']){
  404. yrotrad = (yrot / 180 * PI);
  405. xrotrad = (xrot / 180 * PI);
  406. xpos -= float(sin(yrotrad));
  407. zpos += float(cos(yrotrad));
  408. }
  409.  
  410. if(state['d'] || state['D']){
  411. yrotrad = (yrot / 180 * PI);
  412. xpos += float(cos(yrotrad)) * 0.8;
  413. zpos += float(sin(yrotrad)) * 0.8;
  414. }
  415.  
  416. if(state['a'] || state['A']){
  417. yrotrad = (yrot / 180 * PI);
  418. xpos -= float(cos(yrotrad)) * 0.8;
  419. zpos -= float(sin(yrotrad)) * 0.8;
  420. }
  421.  
  422. if(state['q'] || state['Q']){
  423. exit(0);
  424. }
  425. }
  426.  
  427. void resize(int w, int h){
  428. glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  429. glMatrixMode (GL_PROJECTION);
  430. glLoadIdentity();
  431. gluPerspective(50.0f, (float)w/(float)h, 1.0f, 400.0f);
  432. glMatrixMode(GL_MODELVIEW);
  433. glLoadIdentity();
  434. }
  435. //---------------------------------------------------------------
  436. //Functions from end game
  437.  
  438. bool result;
  439. void displayEnd(void){
  440. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  441.  
  442. glColor3f(1.0f, 1.0f, 1.0f);
  443. if(result){
  444. drawText(-0.18, 0.15, "VOCE GANHOU", GLUT_BITMAP_9_BY_15);
  445. }else{
  446. drawText(-0.18, 0.15, "VOCE PERDEU", GLUT_BITMAP_9_BY_15);
  447. }
  448.  
  449. glColor3f(1.0f, 0.0f, 0.0f);
  450. drawText(-0.065, -0.15, "SAIR", GLUT_BITMAP_9_BY_15);
  451.  
  452. glutSwapBuffers();
  453. glutPostRedisplay();
  454. }
  455.  
  456. void mouseEnd(int button, int state, int x, int y){
  457. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  458. if( (x >= 230) && (x <= 282) && (y >= 286) && (y <= 306) ){
  459. exit(0);
  460. }
  461. }
  462. }
  463.  
  464. //---------------------------------------------------------------
  465. //Functions from the game screen
  466.  
  467. void mouseGame(int button, int state, int x, int y){
  468. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  469. if(xpos < -224 && xpos > -275 && zpos < -32 && zpos > -67){
  470.  
  471. glutDisplayFunc(displayEnd);
  472. glutMouseFunc(mouseEnd);
  473.  
  474. glMatrixMode(GL_PROJECTION) ;
  475. glPushMatrix() ;
  476. glLoadIdentity();
  477.  
  478. glMatrixMode(GL_MODELVIEW) ;
  479. glPushMatrix() ;
  480. glLoadIdentity() ;
  481.  
  482. glDisable(GL_LIGHTING);
  483. glDisable(GL_DEPTH_TEST);
  484. glDisable(GL_TEXTURE_2D);
  485.  
  486. glutKeyboardFunc(NULL);
  487. glutPassiveMotionFunc(NULL);
  488. glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
  489.  
  490. result = true;
  491. }
  492. }
  493. }
  494.  
  495. void moveWorld(){
  496. glMatrixMode(GL_MODELVIEW);
  497. glLoadIdentity();
  498.  
  499. glRotatef(xrot, 1.0, 0.0, 0.0);
  500. glRotatef(yrot, 0.0, 1.0, 0.0);
  501.  
  502. if(xpos > MAX_DISTANCE) xpos = MAX_DISTANCE;
  503. if(xpos < -MAX_DISTANCE) xpos = -MAX_DISTANCE;
  504.  
  505. if(zpos > MAX_DISTANCE) zpos = MAX_DISTANCE;
  506. if(zpos < -MAX_DISTANCE) zpos = -MAX_DISTANCE;
  507.  
  508. glTranslated(-xpos * MOVEMENT_SPEED, 0.0, -zpos * MOVEMENT_SPEED);
  509.  
  510. if(xpos < 22 && xpos > -21 && zpos > 102 && zpos < 150){
  511. glutDisplayFunc(displayEnd);
  512. glutMouseFunc(mouseEnd);
  513.  
  514. glMatrixMode(GL_PROJECTION) ;
  515. glPushMatrix() ;
  516. glLoadIdentity();
  517.  
  518. glMatrixMode(GL_MODELVIEW) ;
  519. glPushMatrix() ;
  520. glLoadIdentity() ;
  521.  
  522. glDisable(GL_LIGHTING);
  523. glDisable(GL_DEPTH_TEST);
  524. glDisable(GL_TEXTURE_2D);
  525.  
  526. glutKeyboardFunc(NULL);
  527. glutPassiveMotionFunc(NULL);
  528. glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
  529.  
  530. result = false;
  531. }
  532. }
  533.  
  534. void drawObjects(void){
  535. glEnable(GL_DEPTH_TEST);
  536. glEnable(GL_TEXTURE_2D);
  537.  
  538. glBindTexture(GL_TEXTURE_2D, slender.textureID);
  539. glBegin(GL_TRIANGLES);
  540. glColor3f(1.0f, 1.0f, 1.0f);
  541. face *temp = slender.faces.head;
  542. for(int i = 0; i < slender.faces.count; i++){
  543. glNormal3f(slender.normals.x[temp->n1 - 1], slender.normals.y[temp->n1 - 1], slender.normals.z[temp->n1 - 1]);
  544. glTexCoord2f(slender.textureCoordinates.s[temp->t1 - 1], slender.textureCoordinates.t[temp->t1 - 1]);
  545. glVertex3f(slender.vertexs.x[temp->v1 - 1], slender.vertexs.y[temp->v1 - 1], slender.vertexs.z[temp->v1 - 1]);
  546.  
  547. glNormal3f(slender.normals.x[temp->n2 - 1], slender.normals.y[temp->n2 - 1], slender.normals.z[temp->n2 - 1]);
  548. glTexCoord2f(slender.textureCoordinates.s[temp->t2 - 1], slender.textureCoordinates.t[temp->t2 - 1]);
  549. glVertex3f(slender.vertexs.x[temp->v2 - 1], slender.vertexs.y[temp->v2 - 1], slender.vertexs.z[temp->v2 - 1]);
  550.  
  551. glNormal3f(slender.normals.x[temp->n3 - 1], slender.normals.y[temp->n3 - 1], slender.normals.z[temp->n3 - 1]);
  552. glTexCoord2f(slender.textureCoordinates.s[temp->t3 - 1], slender.textureCoordinates.t[temp->t3 - 1]);
  553. glVertex3f(slender.vertexs.x[temp->v3 - 1], slender.vertexs.y[temp->v3 - 1], slender.vertexs.z[temp->v3 - 1]);
  554.  
  555. temp = temp->next;
  556. }
  557. glEnd();
  558.  
  559. glBindTexture(GL_TEXTURE_2D, rock.textureID);
  560. glBegin(GL_TRIANGLES);
  561. temp = rock.faces.head;
  562. for(int i = 0; i < rock.faces.count; i++){
  563. glNormal3f(rock.normals.x[temp->n1 - 1], rock.normals.y[temp->n1 - 1], rock.normals.z[temp->n1 - 1]);
  564. glTexCoord2f(rock.textureCoordinates.s[temp->t1 - 1], rock.textureCoordinates.t[temp->t1 - 1]);
  565. glVertex3f(rock.vertexs.x[temp->v1 - 1], rock.vertexs.y[temp->v1 - 1], rock.vertexs.z[temp->v1 - 1]);
  566.  
  567. glNormal3f(rock.normals.x[temp->n2 - 1], rock.normals.y[temp->n2 - 1], rock.normals.z[temp->n2 - 1]);
  568. glTexCoord2f(rock.textureCoordinates.s[temp->t2 - 1], rock.textureCoordinates.t[temp->t2 - 1]);
  569. glVertex3f(rock.vertexs.x[temp->v2 - 1], rock.vertexs.y[temp->v2 - 1], rock.vertexs.z[temp->v2 - 1]);
  570.  
  571. glNormal3f(rock.normals.x[temp->n3 - 1], rock.normals.y[temp->n3 - 1], rock.normals.z[temp->n3 - 1]);
  572. glTexCoord2f(rock.textureCoordinates.s[temp->t3 - 1], rock.textureCoordinates.t[temp->t3 - 1]);
  573. glVertex3f(rock.vertexs.x[temp->v3 - 1], rock.vertexs.y[temp->v3 - 1], rock.vertexs.z[temp->v3 - 1]);
  574.  
  575. temp = temp->next;
  576. }
  577. glEnd();
  578.  
  579. glBindTexture(GL_TEXTURE_2D, tree.textureID);
  580. glBegin(GL_TRIANGLES);
  581. temp = tree.faces.head;
  582. for(int i = 0; i < tree.faces.count; i++){
  583. glNormal3f(tree.normals.x[temp->n1 - 1], tree.normals.y[temp->n1 - 1], tree.normals.z[temp->n1 - 1]);
  584. glTexCoord2f(tree.textureCoordinates.s[temp->t1 - 1], tree.textureCoordinates.t[temp->t1 - 1]);
  585. glVertex3f(tree.vertexs.x[temp->v1 - 1], tree.vertexs.y[temp->v1 - 1], tree.vertexs.z[temp->v1 - 1]);
  586.  
  587. glNormal3f(tree.normals.x[temp->n2 - 1], tree.normals.y[temp->n2 - 1], tree.normals.z[temp->n2 - 1]);
  588. glTexCoord2f(tree.textureCoordinates.s[temp->t2 - 1], tree.textureCoordinates.t[temp->t2 - 1]);
  589. glVertex3f(tree.vertexs.x[temp->v2 - 1], tree.vertexs.y[temp->v2 - 1], tree.vertexs.z[temp->v2 - 1]);
  590.  
  591. glNormal3f(tree.normals.x[temp->n3 - 1], tree.normals.y[temp->n3 - 1], tree.normals.z[temp->n3 - 1]);
  592. glTexCoord2f(tree.textureCoordinates.s[temp->t3 - 1], tree.textureCoordinates.t[temp->t3 - 1]);
  593. glVertex3f(tree.vertexs.x[temp->v3 - 1], tree.vertexs.y[temp->v3 - 1], tree.vertexs.z[temp->v3 - 1]);
  594.  
  595. temp = temp->next;
  596. }
  597. glEnd();
  598.  
  599. glBindTexture(GL_TEXTURE_2D, crate.textureID);
  600. glBegin(GL_TRIANGLES);
  601. temp = crate.faces.head;
  602. for(int i = 0; i < crate.faces.count; i++){
  603. glNormal3f(crate.normals.x[temp->n1 - 1], crate.normals.y[temp->n1 - 1], crate.normals.z[temp->n1 - 1]);
  604. glTexCoord2f(crate.textureCoordinates.s[temp->t1 - 1], crate.textureCoordinates.t[temp->t1 - 1]);
  605. glVertex3f(crate.vertexs.x[temp->v1 - 1], crate.vertexs.y[temp->v1 - 1], crate.vertexs.z[temp->v1 - 1]);
  606.  
  607. glNormal3f(crate.normals.x[temp->n2 - 1], crate.normals.y[temp->n2 - 1], crate.normals.z[temp->n2 - 1]);
  608. glTexCoord2f(crate.textureCoordinates.s[temp->t2 - 1], crate.textureCoordinates.t[temp->t2 - 1]);
  609. glVertex3f(crate.vertexs.x[temp->v2 - 1], crate.vertexs.y[temp->v2 - 1], crate.vertexs.z[temp->v2 - 1]);
  610.  
  611. glNormal3f(crate.normals.x[temp->n3 - 1], crate.normals.y[temp->n3 - 1], crate.normals.z[temp->n3 - 1]);
  612. glTexCoord2f(crate.textureCoordinates.s[temp->t3 - 1], crate.textureCoordinates.t[temp->t3 - 1]);
  613. glVertex3f(crate.vertexs.x[temp->v3 - 1], crate.vertexs.y[temp->v3 - 1], crate.vertexs.z[temp->v3 - 1]);
  614.  
  615. temp = temp->next;
  616. }
  617. glEnd();
  618.  
  619. glBindTexture(GL_TEXTURE_2D, house.textureID);
  620. glBegin(GL_TRIANGLES);
  621. temp = house.faces.head;
  622. for(int i = 0; i < house.faces.count; i++){
  623. glNormal3f(house.normals.x[temp->n1 - 1], house.normals.y[temp->n1 - 1], house.normals.z[temp->n1 - 1]);
  624. glTexCoord2f(house.textureCoordinates.s[temp->t1 - 1], house.textureCoordinates.t[temp->t1 - 1]);
  625. glVertex3f(house.vertexs.x[temp->v1 - 1], house.vertexs.y[temp->v1 - 1], house.vertexs.z[temp->v1 - 1]);
  626.  
  627. glNormal3f(house.normals.x[temp->n2 - 1], house.normals.y[temp->n2 - 1], house.normals.z[temp->n2 - 1]);
  628. glTexCoord2f(house.textureCoordinates.s[temp->t2 - 1], house.textureCoordinates.t[temp->t2 - 1]);
  629. glVertex3f(house.vertexs.x[temp->v2 - 1], house.vertexs.y[temp->v2 - 1], house.vertexs.z[temp->v2 - 1]);
  630.  
  631. glNormal3f(house.normals.x[temp->n3 - 1], house.normals.y[temp->n3 - 1], house.normals.z[temp->n3 - 1]);
  632. glTexCoord2f(house.textureCoordinates.s[temp->t3 - 1], house.textureCoordinates.t[temp->t3 - 1]);
  633. glVertex3f(house.vertexs.x[temp->v3 - 1], house.vertexs.y[temp->v3 - 1], house.vertexs.z[temp->v3 - 1]);
  634.  
  635. temp = temp->next;
  636. }
  637. glEnd();
  638. }
  639.  
  640. void drawGround(int w, int h){
  641. int i, j, r;
  642.  
  643. glColor3f(1.0f, 1.0f, 1.0f);
  644.  
  645. glEnable(GL_TEXTURE_2D);
  646. glBindTexture(GL_TEXTURE_2D, Texture);
  647.  
  648. glNormal3f(0.0, 1.0, 0.0);
  649. for(i = -2*w; i < 2*w; i++){
  650. for(j = -2*h; j < 2*h; j++){
  651. glBegin(GL_QUADS);
  652. glTexCoord2f(0, 0); glVertex3f(0.5 * i, -1.0f, j*0.5);
  653. glTexCoord2f(1, 0); glVertex3f(i*0.5 + 0.5, -1.0f, j*0.5);
  654. glTexCoord2f(1, 1); glVertex3f(i*0.5 + 0.5, -1.0f, j*0.5 + 0.5);
  655. glTexCoord2f(0, 1); glVertex3f(i*0.5, -1.0f, j*0.5 + 0.5);
  656. glEnd();
  657. }
  658. }
  659. }
  660.  
  661. void drawText(float x, float y, const char *string, void *font){
  662. glRasterPos2f(x, y);
  663. int len, i;
  664. len = (int) strlen(string);
  665. for(i = 0; i < len; i++){
  666. glutBitmapCharacter(font, string[i]);
  667. }
  668. }
  669.  
  670. void putPages(void){
  671. glPushMatrix();
  672. glTranslatef(-12.35, -0.80, -0.72);
  673. glEnable(GL_TEXTURE_2D);
  674. glBindTexture(GL_TEXTURE_2D, paper);
  675. glNormal3f(0.0, 0.0, -1.0);
  676. glBegin(GL_QUADS);
  677. glTexCoord2f(0, 0); glVertex3f(0.5, 0.25, 0);
  678. glTexCoord2f(1, 0); glVertex3f(0, 0.25, 0);
  679. glTexCoord2f(1, 1); glVertex3f(0, 0.75, 0);
  680. glTexCoord2f(0, 1); glVertex3f(0.5, 0.75, 0);
  681. glEnd();
  682. glPopMatrix();
  683. }
  684.  
  685. //http://www.glprogramming.com/red/chapter05.html
  686. //https://www.opengl.org/sdk/docs/man2/xhtml/glLight.xml
  687. void light(void){
  688. glClearColor(0.0, 0.0, 0.0, 0.0);
  689.  
  690. glEnable(GL_LIGHTING);
  691.  
  692. float lightAmb[] = { 0.0, 0.0, 0.0, 1.0 };
  693. float lightDifAndSpec[] = { 20.0, 20.0, 20.0, 20.0 };
  694. float globAmb[] = { 0.6, 0.6, 0.6, 0.6 };
  695.  
  696. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
  697. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDifAndSpec);
  698. glLightfv(GL_LIGHT0, GL_SPECULAR, lightDifAndSpec);
  699.  
  700. //The intensity of light decreases as distance from the light increases.
  701. glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
  702. glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0);
  703. glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);
  704.  
  705. glEnable(GL_LIGHT0);
  706. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globAmb);
  707. }
  708.  
  709. void displayGame(void){
  710. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  711.  
  712. float light_position[] = { xpos * MOVEMENT_SPEED, 0.0, zpos * MOVEMENT_SPEED, 1.0 }; //Positional Light
  713. float spotDirection[] = {cos (yrot * PI/180 + 11.0) , -sin(xrot * PI/ 180), sin(yrot * PI / 180.0 + 11.0)};
  714.  
  715. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  716. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
  717. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20);
  718.  
  719. drawSkybox(4*MAP_SIZE);
  720.  
  721. drawGround(MAP_SIZE, MAP_SIZE);
  722.  
  723. putPages();
  724.  
  725. drawObjects();
  726.  
  727. //Show light0
  728. /*glPushMatrix();
  729. glDisable(GL_LIGHTING);
  730. glTranslatef(xpos * MOVEMENT_SPEED, 0.0, zpos * MOVEMENT_SPEED);
  731. glRotatef(-90.0, 1.0, 0.0, 0.0);
  732. glColor3f(1.0f, 1.0f, 1.0f);
  733. glutWireCone(3.0 * tan( 45.0/180.0 * PI ), 4.0, 20, 20);
  734. glEnable(GL_LIGHTING);
  735. glPopMatrix();*/
  736.  
  737. keyboard();
  738. moveWorld();
  739.  
  740. glutSwapBuffers();
  741. glutPostRedisplay();
  742. }
  743.  
  744. //-------------------------------------
  745. //Functions from second screen
  746.  
  747. void mouseComands(int button, int state, int x, int y){
  748. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  749. if( (x <= 275) && (x >= 257) && (y >= 423) && (y <= 437) ){
  750. glutKeyboardFunc(keyboard_down);
  751. glutKeyboardUpFunc(keyboard_up);
  752. glutIgnoreKeyRepeat(GL_TRUE);
  753.  
  754. light();
  755.  
  756. glutReshapeFunc(resize);
  757. glutPassiveMotionFunc(mouse);
  758. glutMouseFunc(mouseGame);
  759.  
  760. glutSetCursor(GLUT_CURSOR_NONE); //Hide cursor
  761.  
  762. glMatrixMode(GL_PROJECTION);
  763. glLoadIdentity();
  764. gluPerspective (50.0, 1.0f, 1.0f, 400.0f);
  765.  
  766. loadObject(&slender, "slenderman.obj", "toonB.bmp");
  767. loadObject(&rock, "rockn.obj", "rock.bmp");
  768. loadObject(&tree, "tree.obj", "tree.bmp");
  769. loadObject(&crate, "craten.obj", "crate.bmp");
  770. loadObject(&house, "house.obj", "farmhouse.bmp");
  771. paper = loadBMP_custom("page.bmp");
  772.  
  773. glutDisplayFunc(displayGame);
  774.  
  775.  
  776. //glutFullScreen();
  777. }
  778. }
  779. }
  780.  
  781. void displayComands(void){
  782. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  783. glColor3f(1.0f, 1.0f, 1.0f);
  784.  
  785. drawText(-0.90f, 0.70f, "Comandos basicos:", GLUT_BITMAP_9_BY_15);
  786. drawText(-0.75f, 0.50f, "W: Mover-se para frente", GLUT_BITMAP_9_BY_15);
  787. drawText(-0.75f, 0.40f, "A: Mover-se para esquerda", GLUT_BITMAP_9_BY_15);
  788. drawText(-0.75f, 0.30f, "S: Mover-se para tras", GLUT_BITMAP_9_BY_15);
  789. drawText(-0.75f, 0.20f, "D: Mover-se para direita", GLUT_BITMAP_9_BY_15);
  790. drawText(-0.75f, 0.10f, "Movimento do mouse: Olhar ao seu redor", GLUT_BITMAP_9_BY_15);
  791. drawText(-0.75f, 0.0f, "Botao esquerdo do mouse: Pegar pagina", GLUT_BITMAP_9_BY_15);
  792. drawText(-0.75f, -0.10f, "Q: Sair", GLUT_BITMAP_9_BY_15);
  793. drawText(-0.0f, -0.70f, "OK", GLUT_BITMAP_9_BY_15);
  794.  
  795. glutSwapBuffers();
  796. glutPostRedisplay();
  797. }
  798.  
  799. //------------------------------------------------------------------------
  800. //Functions from the first screen
  801.  
  802. void displayScreen(void){
  803. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  804. glColor3f(1.0f, 1.0f, 1.0f);
  805. drawText(-0.20, 0.15, "SLENDER", GLUT_BITMAP_TIMES_ROMAN_24);
  806.  
  807. glColor3f(0.0f, 1.0f, 0.0f);
  808. drawText(-0.08, -0.02, "JOGAR", GLUT_BITMAP_9_BY_15);
  809.  
  810. glColor3f(1.0f, 0.0f, 0.0f);
  811. drawText(-0.065, -0.16, "SAIR", GLUT_BITMAP_9_BY_15);
  812.  
  813. glColor3f(1.0f, 1.0f, 1.0f);
  814. drawText(-0.95, -0.95, "Desenvolvido por Thulio Guilherme - 2015", GLUT_BITMAP_8_BY_13);
  815.  
  816. glutSwapBuffers();
  817. glutPostRedisplay();
  818. }
  819.  
  820. void mouseScreen(int button, int state, int x, int y){
  821. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  822. if( (x >= 225) && (x <= 287) && (y >= 241) && (y <= 261) ){
  823. glutMouseFunc(mouseComands);
  824. glutDisplayFunc(displayComands);
  825. }
  826.  
  827. if( (x >= 230) && (x <= 282) && (y >= 286) && (y <= 306) ){
  828. exit(0);
  829. }
  830. }
  831. }
  832.  
  833. //---------------------------------------------------------------
  834.  
  835. int main(int argc, char **argv){
  836. std::clog << "Begin...\n";
  837.  
  838. glutInit(&argc,argv);
  839. glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  840. glutInitWindowSize(512, 512);
  841. glutInitWindowPosition(100, 100);
  842. glutCreateWindow("Slender Alpha");
  843.  
  844. glutDisplayFunc(displayScreen);
  845. glutMouseFunc(mouseScreen);
  846.  
  847. atexit(free_mem);
  848.  
  849. initskybox();
  850.  
  851. glClearColor(0.0, 0.0, 0.0, 1.0);
  852.  
  853. Texture = loadBMP_custom("texture4.bmp");
  854.  
  855. glutMainLoop();
  856.  
  857. return 0;
  858. }
Advertisement
Add Comment
Please, Sign In to add comment