Advertisement
sajid006

Opengl Project

Jun 7th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.23 KB | None | 0 0
  1.  
  2. #include<GL/gl.h>
  3. #include <GL/glu.h>
  4. #include <GL/glut.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <windows.h>
  8. #include<math.h>
  9. #include "BmpLoader.h"
  10. const int width = 1000;
  11. const int height = 1000;
  12.  
  13. GLfloat eyeX = 0;
  14. GLfloat eyeY = 6;
  15. GLfloat eyeZ = -13;
  16.  
  17. GLfloat lookX = 0;
  18. GLfloat lookY = 5;
  19. GLfloat lookZ = 0;
  20.  
  21. GLfloat alpha = 0.0, theta = 0.0, axis_x=0.0, axis_y=0.0 , f_spin=0.0;
  22. GLboolean bRotate = false, uRotate = false;
  23.  
  24. bool press1 = false;
  25. bool press2 = false;
  26. bool press4 = false;
  27. bool flagRot = false;
  28. float rot = 0;
  29. unsigned int ID;
  30.  
  31. float px=0,py=30,pz=30,spot_cutoff = 30;
  32.  
  33. static GLfloat v_cube[8][3] =
  34. {
  35. {0,0,0},
  36. {0,0,1},
  37. {0,1,0},
  38. {0,1,1},
  39.  
  40. {1,0,0},
  41. {1,0,1},
  42. {1,1,0},
  43. {1,1,1}
  44. };
  45.  
  46. static GLubyte c_ind[6][4] =
  47. {
  48. {3,1,5,7}, //front
  49. {6,4,0,2}, //back
  50. {2,3,7,6}, //top
  51. {1,0,4,5}, //bottom
  52. {7,5,4,6}, //right
  53. {2,0,1,3} //left
  54. };
  55.  
  56. static void getNormal3p(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3)
  57. {
  58. GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
  59.  
  60. Ux = x2-x1;
  61. Uy = y2-y1;
  62. Uz = z2-z1;
  63.  
  64. Vx = x3-x1;
  65. Vy = y3-y1;
  66. Vz = z3-z1;
  67.  
  68. Nx = Uy*Vz - Uz*Vy;
  69. Ny = Uz*Vx - Ux*Vz;
  70. Nz = Ux*Vy - Uy*Vx;
  71.  
  72. glNormal3f(Nx,Ny,Nz);
  73. }
  74.  
  75. void set_material(float R=0.5, float G=0.5, float B=0.5, bool e=false , bool l = false)
  76. {
  77. GLfloat mat_no[] = {0,0,0, 1.0};
  78. GLfloat mat_amb[] = {R,G,B,1};
  79. GLfloat mat_diff[] = {R,G,B,1};
  80. GLfloat mat_spec[] = {1,1,1,1};
  81. GLfloat mat_sh[] = {30};
  82.  
  83. GLfloat mat_em[] = {1,1,1,1};
  84.  
  85. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_amb);
  86. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diff);
  87. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_spec);
  88. glMaterialfv(GL_FRONT, GL_SHININESS, mat_sh);
  89.  
  90. if(e && l)
  91. {
  92. glMaterialfv(GL_FRONT, GL_EMISSION, mat_em);
  93. }
  94. else
  95. {
  96. glMaterialfv(GL_FRONT, GL_EMISSION, mat_no);
  97. }
  98. }
  99.  
  100. void cube(float R=0.5, float G=0.5, float B=0.5, bool e=false, bool l = false)
  101. {
  102.  
  103. set_material(R,G,B,l);
  104.  
  105. glBegin(GL_QUADS);
  106. for (GLint i = 0; i <6; i++)
  107. {
  108. getNormal3p(v_cube[c_ind[i][0]][0], v_cube[c_ind[i][0]][1], v_cube[c_ind[i][0]][2],
  109. v_cube[c_ind[i][1]][0], v_cube[c_ind[i][1]][1], v_cube[c_ind[i][1]][2],
  110. v_cube[c_ind[i][2]][0], v_cube[c_ind[i][2]][1], v_cube[c_ind[i][2]][2]);
  111.  
  112. glTexCoord2f(0,1);
  113. glVertex3fv(&v_cube[c_ind[i][0]][0]);
  114. glTexCoord2f(0,0);
  115. glVertex3fv(&v_cube[c_ind[i][1]][0]);
  116. glTexCoord2f(1,0);
  117. glVertex3fv(&v_cube[c_ind[i][2]][0]);
  118. glTexCoord2f(1,1);
  119. glVertex3fv(&v_cube[c_ind[i][3]][0]);
  120. }
  121. glEnd();
  122. }
  123. void axes()
  124. {
  125. float length = 10;
  126. float width = 0.3;
  127.  
  128. // X-axis
  129. glPushMatrix();
  130. glTranslatef(length/2,0,0);
  131. glScalef(length,width,width);
  132. glTranslatef(-0.5,-0.5,-0.5);
  133. cube(0.8,0.1,0.1);
  134. glPopMatrix();
  135.  
  136. // Y-axis
  137. glPushMatrix();
  138. glTranslatef(0,length/2,0);
  139. glScalef(width,length,width);
  140. glTranslatef(-0.5,-0.5,-0.5);
  141. cube(0.1,0.8,0.1);
  142. glPopMatrix();
  143.  
  144. // Z-axis
  145. glPushMatrix();
  146. glTranslatef(0,0,length/2);
  147. glScalef(width,width,length);
  148. glTranslatef(-0.5,-0.5,-0.5);
  149. cube(0.1,0.1,0.8);
  150. glPopMatrix();
  151. }
  152.  
  153. void rodshape1()
  154. {
  155. float t_length = 6;
  156. float t_width = 0.5;
  157.  
  158. glPushMatrix();
  159. //glTranslatef(-14.5,7,0);
  160. glTranslatef(-14.5,7,0);
  161.  
  162. glPushMatrix();
  163. glScalef(t_width,t_width,t_length);
  164. glTranslatef(-0.5,0,-0.5);
  165. cube(1,1,1,true,press1);
  166. glPopMatrix();
  167.  
  168. glPushMatrix();
  169. glTranslatef(0,0,t_length/2+t_width/2);
  170. glScalef(t_width,t_width,t_width);
  171. glTranslatef(-0.5,0,-0.5);
  172. cube(0.2,0.2,0.2);
  173. glPopMatrix();
  174.  
  175. glPushMatrix();
  176. glTranslatef(0,0,-t_length/2-t_width/2);
  177. glScalef(t_width,t_width,t_width);
  178. glTranslatef(-0.5,0,-0.5);
  179. cube(0.2,0.2,0.2);
  180. glPopMatrix();
  181.  
  182. glPopMatrix();
  183.  
  184. }
  185.  
  186. void rodshape2()
  187. {
  188. float t_length = 6;
  189. float t_width = 0.5;
  190.  
  191. glPushMatrix();
  192. //glTranslatef(-14.5,7,0);
  193. glTranslatef(14,7,10);
  194.  
  195. glPushMatrix();
  196. glScalef(t_width,t_width,t_length);
  197. glTranslatef(-0.5,0,-0.5);
  198. cube(1,1,1,true,press2);
  199. glPopMatrix();
  200.  
  201. glPushMatrix();
  202. glTranslatef(0,0,t_length/2+t_width/2);
  203. glScalef(t_width,t_width,t_width);
  204. glTranslatef(-0.5,0,-0.5);
  205. cube(0.2,0.2,0.2);
  206. glPopMatrix();
  207.  
  208. glPushMatrix();
  209. glTranslatef(0,0,-t_length/2-t_width/2);
  210. glScalef(t_width,t_width,t_width);
  211. glTranslatef(-0.5,0,-0.5);
  212. cube(0.2,0.2,0.2);
  213. glPopMatrix();
  214.  
  215. glPopMatrix();
  216.  
  217. }
  218.  
  219. void spotlightshape()
  220. {
  221. float s_height = 0.25;
  222. float s_width = 2;
  223. float s_length = 2;
  224.  
  225. glPushMatrix();
  226. glTranslatef(7,9,7);
  227. /*
  228. glPushMatrix();
  229. glScalef(s_width,s_height,s_length);
  230. glTranslatef(-0.5,0,-0.5);
  231. cube(1,1,1);
  232. glPopMatrix();
  233. */
  234. glPushMatrix();
  235. glScalef(s_width,s_height,s_length);
  236. glTranslatef(-0.5,0,-0.5);
  237. cube(1,1,1,true,press4);
  238. glPopMatrix();
  239.  
  240. glPopMatrix();
  241.  
  242. }
  243.  
  244.  
  245. void light1()
  246. {
  247. //light
  248. GLfloat l1_no[] = {0, 0, 0, 1.0};
  249. GLfloat l1_amb[] = {0.1, 0.1, 0.1, 1.0};
  250. GLfloat l1_dif[] = {1,1,1,1};
  251. GLfloat l1_spec[] = {1,1,1,1};
  252. GLfloat l1_pos[] = {-14.5,7,0,1.0};
  253. //GLfloat l_pos[] = {-14.5,-7,10,1.0};
  254.  
  255. glEnable(GL_LIGHT0);
  256.  
  257. if(press1)
  258. {
  259. glLightfv(GL_LIGHT0, GL_AMBIENT, l1_amb);
  260. }
  261. else
  262. {
  263. glLightfv(GL_LIGHT0, GL_AMBIENT, l1_no);
  264. }
  265. if(press1)
  266. {
  267. glLightfv(GL_LIGHT0, GL_DIFFUSE, l1_dif);
  268. }
  269. else
  270. {
  271. glLightfv(GL_LIGHT0, GL_DIFFUSE, l1_no);
  272. }
  273. if(press1)
  274. {
  275. glLightfv(GL_LIGHT0, GL_SPECULAR, l1_spec);
  276. }
  277. else
  278. {
  279. glLightfv(GL_LIGHT0, GL_SPECULAR, l1_no);
  280. }
  281.  
  282.  
  283. glLightfv(GL_LIGHT0, GL_POSITION, l1_pos);
  284. }
  285.  
  286. void light2()
  287. {
  288. //light
  289. GLfloat l2_no[] = {0, 0, 0, 1.0};
  290. GLfloat l2_amb[] = {0.1, 0.1, 0.1, 1.0};
  291. GLfloat l2_dif[] = {1,1,1,1};
  292. GLfloat l2_spec[] = {1,1,1,1};
  293. //GLfloat l_pos[] = {-14.5,7,0,1.0};
  294. GLfloat l2_pos[] = {14,-7,10,1.0};
  295.  
  296. glEnable(GL_LIGHT1);
  297.  
  298. if(press2)
  299. {
  300. glLightfv(GL_LIGHT1, GL_AMBIENT, l2_amb);
  301. }
  302. else
  303. {
  304. glLightfv(GL_LIGHT1, GL_AMBIENT, l2_no);
  305. }
  306. if(press2)
  307. {
  308. glLightfv(GL_LIGHT1, GL_DIFFUSE, l2_dif);
  309. }
  310. else
  311. {
  312. glLightfv(GL_LIGHT1, GL_DIFFUSE, l2_no);
  313. }
  314. if(press2)
  315. {
  316. glLightfv(GL_LIGHT1, GL_SPECULAR, l2_spec);
  317. }
  318. else
  319. {
  320. glLightfv(GL_LIGHT1, GL_SPECULAR, l2_no);
  321. }
  322.  
  323. glLightfv(GL_LIGHT1, GL_POSITION, l2_pos);
  324. }
  325.  
  326. void light3()
  327. {
  328. //light
  329. GLfloat l3_no[] = {0, 0, 0, 1.0};
  330. GLfloat l3_amb[] = {0.5, 0.5, 0.5, 1.0};
  331. GLfloat l3_dif[] = {1,1,1,1};
  332. GLfloat l3_spec[] = {1,1,1,1};
  333. GLfloat l3_pos[] = {px,py,pz,1.0};
  334. //GLfloat l_pos[] = {14,-7,10,1.0};
  335.  
  336. glEnable(GL_LIGHT2);
  337.  
  338. if(press4)
  339. {
  340. glLightfv(GL_LIGHT2, GL_AMBIENT, l3_amb);
  341. }
  342. else
  343. {
  344. glLightfv(GL_LIGHT2, GL_AMBIENT, l3_no);
  345. }
  346. if(press4)
  347. {
  348. glLightfv(GL_LIGHT2, GL_DIFFUSE, l3_dif);
  349. }
  350. else
  351. {
  352. glLightfv(GL_LIGHT2, GL_DIFFUSE, l3_no);
  353. }
  354. if(press4)
  355. {
  356. glLightfv(GL_LIGHT2, GL_SPECULAR, l3_spec);
  357. }
  358. else
  359. {
  360. glLightfv(GL_LIGHT2, GL_SPECULAR, l3_no);
  361. }
  362.  
  363. glLightfv(GL_LIGHT2, GL_POSITION, l3_pos);
  364.  
  365. // spot light extra
  366. GLfloat l3_spt[] = {0,0,-1,1};
  367. GLfloat spt_ct[] = {spot_cutoff};
  368. glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, l3_spt);
  369. glLightfv(GL_LIGHT2, GL_SPOT_CUTOFF, spt_ct);
  370.  
  371. }
  372.  
  373. void LoadTexture(const char*filename)
  374. {
  375. glGenTextures(1, &ID);
  376. glBindTexture(GL_TEXTURE_2D, ID);
  377. glPixelStorei(GL_UNPACK_ALIGNMENT, ID);
  378. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  379. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  380. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  381. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  382. BmpLoader bl(filename);
  383. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bl.iWidth, bl.iHeight, GL_RGB, GL_UNSIGNED_BYTE, bl.textureData );
  384. }
  385.  
  386.  
  387. static void display(void)
  388. {
  389. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  390. glMatrixMode(GL_PROJECTION);
  391. glLoadIdentity();
  392. glFrustum(-3, 3, -3, 3, 2.0, 100.0);
  393.  
  394. glMatrixMode(GL_MODELVIEW);
  395. glLoadIdentity();
  396. gluLookAt(eyeX,eyeY,eyeZ, lookX,lookY,lookZ, 0,1,0);
  397. //glViewport(0, 0, width, height);
  398.  
  399. glRotatef(rot, 0,1,0);
  400. axes();
  401.  
  402. ///Main Road
  403. glEnable(GL_TEXTURE_2D);
  404. glBindTexture(GL_TEXTURE_2D,1);
  405. glPushMatrix();
  406. glScalef(10,0.2,50);
  407. glTranslatef(-0.5,-0.5,-0.5);
  408. cube(1,1,1);
  409. glPopMatrix();
  410. glDisable(GL_TEXTURE_2D);
  411.  
  412. ///Grass
  413. glEnable(GL_TEXTURE_2D);
  414. glBindTexture(GL_TEXTURE_2D,2);
  415. glPushMatrix();
  416. glScalef(1000,0.1,1000);
  417. glTranslatef(-0.5,-0.5,-0.5);
  418. cube(1,1,1);
  419. glPopMatrix();
  420. glDisable(GL_TEXTURE_2D);
  421.  
  422. ///Fence Left
  423. glEnable(GL_TEXTURE_2D);
  424. glBindTexture(GL_TEXTURE_2D,3);
  425. glPushMatrix();
  426. glTranslatef(30,0,25);
  427. glScalef(50,5,0.1);
  428. glTranslatef(-0.5,-0.5,-0.5);
  429. cube(1,1,1);
  430. glPopMatrix();
  431. glDisable(GL_TEXTURE_2D);
  432.  
  433. ///Fence Right
  434. glEnable(GL_TEXTURE_2D);
  435. glBindTexture(GL_TEXTURE_2D,3);
  436. glPushMatrix();
  437. glTranslatef(-30,0,25);
  438. glScalef(50,5,0.1);
  439. glTranslatef(-0.5,-0.5,-0.5);
  440. cube(1,1,1);
  441. glPopMatrix();
  442. glDisable(GL_TEXTURE_2D);
  443.  
  444. ///Building Front Right
  445. glEnable(GL_TEXTURE_2D);
  446. glBindTexture(GL_TEXTURE_2D,4);
  447. glPushMatrix();
  448. glTranslatef(-25,0,35);
  449. glScalef(40,40,0.1);
  450. glTranslatef(-0.5,-0.5,-0.5);
  451. cube(1,1,1);
  452. glPopMatrix();
  453. glDisable(GL_TEXTURE_2D);
  454.  
  455. ///Building Front Left
  456. glEnable(GL_TEXTURE_2D);
  457. glBindTexture(GL_TEXTURE_2D,4);
  458. glPushMatrix();
  459. glTranslatef(25,0,35);
  460. glScalef(40,40,0.1);
  461. glTranslatef(-0.5,-0.5,-0.5);
  462. cube(1,1,1);
  463. glPopMatrix();
  464. glDisable(GL_TEXTURE_2D);
  465.  
  466. ///Building Right
  467. glEnable(GL_TEXTURE_2D);
  468. glBindTexture(GL_TEXTURE_2D,4);
  469. glPushMatrix();
  470. glTranslatef(-45,0,65);
  471. glScalef(0.1,40,60);
  472. glTranslatef(-0.5,-0.5,-0.5);
  473. cube(1,1,1);
  474. glPopMatrix();
  475. glDisable(GL_TEXTURE_2D);
  476.  
  477. ///Building Left
  478. glEnable(GL_TEXTURE_2D);
  479. glBindTexture(GL_TEXTURE_2D,4);
  480. glPushMatrix();
  481. glTranslatef(45,0,65);
  482. glScalef(0.1,40,60);
  483. glTranslatef(-0.5,-0.5,-0.5);
  484. cube(1,1,1);
  485. glPopMatrix();
  486. glDisable(GL_TEXTURE_2D);
  487.  
  488. ///Building Back Left
  489. glEnable(GL_TEXTURE_2D);
  490. glBindTexture(GL_TEXTURE_2D,4);
  491. glPushMatrix();
  492. glTranslatef(25,0,95);
  493. glScalef(40,40,0.1);
  494. glTranslatef(-0.5,-0.5,-0.5);
  495. cube(1,1,1);
  496. glPopMatrix();
  497. glDisable(GL_TEXTURE_2D);
  498.  
  499. ///Building Back Right
  500. glEnable(GL_TEXTURE_2D);
  501. glBindTexture(GL_TEXTURE_2D,4);
  502. glPushMatrix();
  503. glTranslatef(-25,0,95);
  504. glScalef(40,40,0.1);
  505. glTranslatef(-0.5,-0.5,-0.5);
  506. cube(1,1,1);
  507. glPopMatrix();
  508. glDisable(GL_TEXTURE_2D);
  509.  
  510. glutSwapBuffers();
  511. }
  512.  
  513. void animate()
  514. {
  515.  
  516.  
  517. glutPostRedisplay();
  518. }
  519.  
  520.  
  521. static void key(unsigned char key, int x, int y)
  522. {
  523. switch (key)
  524. {
  525. case 27 :
  526. case 'q':
  527. exit(0);
  528. break;
  529. case 'w':
  530. eyeZ++;
  531. lookZ++;
  532. break;
  533. case 's':
  534. eyeZ--;
  535. lookZ--;
  536. break;
  537. case 'a':
  538. eyeX++;
  539. lookX++;
  540. break;
  541. case 'd':
  542. eyeX--;
  543. lookX--;
  544. break;
  545. case 'u':
  546. eyeY++;
  547. lookY++;
  548. break;
  549. case 'j':
  550. eyeY--;
  551. lookY--;
  552. break;
  553. case '.':
  554. rot--;
  555. break;
  556. case ',':
  557. rot++;
  558. break;
  559. }
  560.  
  561. glutPostRedisplay();
  562. }
  563.  
  564. static void idle(void)
  565. {
  566. glutPostRedisplay();
  567. }
  568.  
  569.  
  570.  
  571.  
  572. int main(int argc, char *argv[])
  573. {
  574. glutInit(&argc, argv);
  575. glutInitWindowSize(width,height);
  576. glutInitWindowPosition(10,10);
  577. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  578. glEnable(GL_LIGHTING);
  579. glutCreateWindow("1607006");
  580.  
  581. glutDisplayFunc(display);
  582. glutKeyboardFunc(key);
  583. glutIdleFunc(animate);
  584. //glutSpecialFunc(collision);
  585. //glutIdleFunc(idle);
  586. //glutReshapeFunc(res);
  587.  
  588. // glClearColor(1,1,1,1);
  589.  
  590. glEnable(GL_DEPTH_TEST);
  591. glShadeModel( GL_SMOOTH );
  592. glEnable(GL_NORMALIZE);
  593. glEnable(GL_BLEND);
  594.  
  595.  
  596. printf(" Use 'w' to look up, 's' to look down, 'd' to look right, and 'a' to look left.\n");
  597. printf(" Press '1' for right light turn on,\n Press '2' for left light turn on, \n Press '3' to turn on both light, \n Press '4' to turn on spotlight.\n");
  598.  
  599. LoadTexture("C:\\Users\\road.bmp");
  600. LoadTexture("C:\\Users\\grass.bmp");
  601. LoadTexture("C:\\Users\\fence.bmp");
  602. LoadTexture("C:\\Users\\window.bmp");
  603.  
  604. glutMainLoop();
  605.  
  606. return EXIT_SUCCESS;
  607. }
  608.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement