Advertisement
FaisalAhemdBijoy

City

May 21st, 2021
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.58 KB | None | 0 0
  1. #include<GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include<math.h>
  8. #include<bits/stdc++.h>
  9. #include "BmpLoader.h"
  10. #include <fstream>
  11. #include <iostream>
  12. using namespace std;
  13.  
  14.  
  15. const int window_width = 1200;
  16. const int window_height = 750;
  17. int mx,my;
  18.  
  19. GLfloat eye[] = {0,40,50};
  20. GLfloat look[] = {0,40,-500};
  21. //GLfloat eye[] = {0,5,20};
  22. //GLfloat look[] = {0,5,-100};
  23.  
  24. float nipa_z = 10,nipa_x = 0;
  25. float theta_right = 0, theta_left=0, theta =0;
  26. bool leg_rot_x = 0,leg_rot_y = 0,leg_rot_z = 0;
  27.  
  28. int b_rot = 0;
  29.  
  30.  
  31. float rot = 0;
  32.  
  33. unsigned int ID;
  34. #define GL_CLAMP_TO_EDGE 0x812F
  35.  
  36. static GLfloat v_cube[8][3] =
  37. {
  38.     {0,0,0},
  39.     {0,0,1},
  40.     {0,1,0},
  41.     {0,1,1},
  42.  
  43.     {1,0,0},
  44.     {1,0,1},
  45.     {1,1,0},
  46.     {1,1,1}
  47. };
  48.  
  49. static GLubyte c_ind[6][4] =
  50. {
  51. //    {0,2,6,4},
  52. //    {5,7,3,1},
  53. //    {0,4,5,1},
  54. //    {3,7,6,2},
  55. //    {0,1,3,2},
  56. //    {6,7,5,4}
  57.  
  58. //    {2,3,7,6}, //top
  59. //    {1,0,4,5}, //bottom
  60. //    {2,0,1,3}, //left
  61. //    {7,5,4,6}, //right
  62. //    {3,1,5,7}, //front
  63. //    {6,4,0,2} //back
  64.  
  65.  
  66. // {0,2,6,4},
  67. // {3,1,5,7},
  68. // {2,3,7,6},
  69. // {1,0,4,5},
  70. // {2,0,1,3},
  71. // {7,5,4,6}
  72.  
  73.     {3,1,5,7}, //front
  74.     {2,0,1,3}, //left
  75.     {7,5,4,6}, //r
  76.     {2,3,7,6}, //top
  77.     {1,0,5,4}, //bot
  78.     {6,4,0,2}, //back
  79.  
  80.  
  81. };
  82. static void getNormal3p(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3)
  83. {
  84.     GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
  85.  
  86.     Ux = x2-x1;
  87.     Uy = y2-y1;
  88.     Uz = z2-z1;
  89.  
  90.     Vx = x3-x1;
  91.     Vy = y3-y1;
  92.     Vz = z3-z1;
  93.  
  94.     Nx = Uy*Vz - Uz*Vy;
  95.     Ny = Uz*Vx - Ux*Vz;
  96.     Nz = Ux*Vy - Uy*Vx;
  97.  
  98.     glNormal3f(Nx,Ny,Nz);
  99. }
  100.  
  101. void material_property(float R, float G, float B)
  102. {
  103.     GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  104.     GLfloat mat_ambient[] = { R, G, B, 1.0 };
  105.     //GLfloat mat_ambient[] = { 0, 0, 0, 1.0 };
  106.     GLfloat mat_diffuse[] = { R, G, B, 1.0 };
  107.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  108.     GLfloat mat_shininess[] = {255};
  109.  
  110.     glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient);
  111.     glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse);
  112.     glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular);
  113.     glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess);
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120. void cube(float R=0.5, float G=0.5, float B=0.5 )
  121. {
  122.     material_property(R,G,B);
  123.     glBegin(GL_QUADS);
  124.     for (GLint i = 0; i <6; i++)
  125.     {
  126.         //        glColor3f(R,G,B);
  127.         //
  128.         //        R += 0.05;
  129.         //        G += 0.05;
  130.         //        B += 0.05;
  131.         getNormal3p(v_cube[c_ind[i][0]][0], v_cube[c_ind[i][0]][1], v_cube[c_ind[i][0]][2],
  132.                     v_cube[c_ind[i][1]][0], v_cube[c_ind[i][1]][1], v_cube[c_ind[i][1]][2],
  133.                     v_cube[c_ind[i][2]][0], v_cube[c_ind[i][2]][1], v_cube[c_ind[i][2]][2]);
  134.  
  135. //        for (GLint j=0; j<4; j++)
  136. //        {
  137. //            glVertex3fv(&v_cube[c_ind[i][j]][0]) ;
  138. //            glTexCoord2f(1,1);
  139. //        }
  140.         glTexCoord2f(1,1);
  141.         glVertex3fv(&v_cube[c_ind[i][0]][0]);
  142.         glTexCoord2f(1,0);
  143.         glVertex3fv(&v_cube[c_ind[i][1]][0]);
  144.         glTexCoord2f(0,0);
  145.         glVertex3fv(&v_cube[c_ind[i][2]][0]);
  146.         glTexCoord2f(0,1);
  147.         glVertex3fv(&v_cube[c_ind[i][3]][0]);
  148.  
  149.     }
  150.     glEnd();
  151. }
  152.  
  153.  
  154.  
  155. void LoadTexture(const char*filename, int rep = 1)
  156. {
  157.     glGenTextures(1, &ID);
  158.     glBindTexture(GL_TEXTURE_2D, ID);
  159.     glPixelStorei(GL_UNPACK_ALIGNMENT, ID);
  160.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  161.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  162.     if(rep)
  163.     {
  164.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  165.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  166.     }
  167.     else
  168.     {
  169.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  170.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  171.     }
  172.  
  173.     BmpLoader bl(filename);
  174.     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bl.iWidth, bl.iHeight, GL_RGB, GL_UNSIGNED_BYTE, bl.textureData );
  175. }
  176.  
  177. void buiding()
  178. {
  179.     glEnable(GL_TEXTURE_2D);
  180.     glBindTexture(GL_TEXTURE_2D,1);
  181.  
  182.     glPushMatrix();
  183.     glTranslatef(0,30,0);
  184.     glScalef(20,60,20);
  185.     glTranslatef(-0.5,-0.5,-0.5);
  186.     cube();
  187.     glPopMatrix();
  188.  
  189.     glDisable(GL_TEXTURE_2D);
  190.  
  191.  
  192.     glEnable(GL_TEXTURE_2D);
  193.     glBindTexture(GL_TEXTURE_2D,2);
  194.  
  195.     glPushMatrix();
  196.     glTranslatef(50,30,0);
  197.     glScalef(20,60,20);
  198.     glTranslatef(-0.5,-0.5,-0.5);
  199.     cube();
  200.     glPopMatrix();
  201.  
  202.     glDisable(GL_TEXTURE_2D);
  203.  
  204.     //roof
  205.     glPushMatrix();
  206.     glTranslatef(50,60,0);
  207.     glScalef(20,1,20);
  208.     glTranslatef(-0.5,-0.5,-0.5);
  209.     cube();
  210.     glPopMatrix();
  211.  
  212.  
  213. }
  214. void road()
  215. {
  216.      //main road
  217.     glEnable(GL_TEXTURE_2D);
  218.     glBindTexture(GL_TEXTURE_2D,3);
  219.  
  220.     glPushMatrix();
  221.     glTranslatef(0,0.5,20);
  222.     glScalef(100,1,10);
  223.     glTranslatef(-0.5,-0.5,-0.5);
  224.     cube();
  225.     glPopMatrix();
  226.  
  227.     glDisable(GL_TEXTURE_2D);
  228.  
  229.     //side road
  230.     glEnable(GL_TEXTURE_2D);
  231.     glBindTexture(GL_TEXTURE_2D,3);
  232.  
  233.     glPushMatrix();
  234.     glTranslatef(-50,0.5,0);
  235.     glRotatef(90,0,1,0);
  236.     glScalef(100,1,10);
  237.     glTranslatef(-0.5,-0.5,-0.5);
  238.     cube();
  239.     glPopMatrix();
  240.  
  241.     glDisable(GL_TEXTURE_2D);
  242. }
  243. void _light()
  244. {
  245.     //GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
  246.     GLfloat light_ambient[]  = {1.0, 1.0, 1.0, 1.0};
  247.     GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
  248.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  249.     GLfloat light_position[] = { 0.0, 50.0, 0, 1.0 };
  250.  
  251.     glEnable( GL_LIGHT7);
  252.  
  253.     glLightfv( GL_LIGHT7, GL_AMBIENT, light_ambient);
  254.     glLightfv( GL_LIGHT7, GL_DIFFUSE, light_diffuse);
  255.     glLightfv( GL_LIGHT7, GL_SPECULAR, light_specular);
  256.  
  257.     glLightfv( GL_LIGHT7, GL_POSITION, light_position);
  258.  
  259.  
  260.     /*GLfloat spot_direction[] = { 0.0, -1.0, 0.0 };
  261.     glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
  262.     glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 10.0);*/
  263. }
  264. void axes()
  265. {
  266.     float length = 10;
  267.     float width = 0.3;
  268.  
  269.     // X-axis
  270.     glPushMatrix();
  271.     glTranslatef(length/2,0,0);
  272.     glScalef(length,width,width);
  273.     glTranslatef(-0.5,-0.5,-0.5);
  274.     cube(1,0,0); //RED Line
  275.     glPopMatrix();
  276.  
  277.     // Y-axis
  278.     glPushMatrix();
  279.     glTranslatef(0,length/2,0);
  280.     glScalef(width,length,width);
  281.     glTranslatef(-0.5,-0.5,-0.5);
  282.     cube(0,1,0); //GREEN Line
  283.     glPopMatrix();
  284.  
  285.     // Z-axis
  286.     glPushMatrix();
  287.     glTranslatef(0,0,length/2);
  288.     glScalef(width,width,length);
  289.     glTranslatef(-0.5,-0.5,-0.5);
  290.     cube(0,0,1); //BLUE Line
  291.     glPopMatrix();
  292. }
  293. static void key(unsigned char key, int x, int y)
  294. {
  295.     switch (key)
  296.     {
  297.     //for light
  298.  
  299.  
  300.     case 't':
  301.  
  302.         eye[1]++;
  303.         look[1]++;
  304.  
  305.         break;
  306.     case 'g':
  307.  
  308.         eye[1]--;
  309.         look[1]--;
  310.         break;
  311.     case 'h':
  312.         look[0]++;
  313.         break;
  314.     case 'f':
  315.         look[0]--;
  316.         break;
  317.  
  318.     case 'e':
  319.         rot--;
  320.         break;
  321.     case 'r':
  322.         rot++;
  323.         break;
  324.     case 'l':
  325.  
  326.             eye[0]++;
  327.             look[0]++;
  328.  
  329.         break;
  330.     case 'j':
  331.  
  332.             eye[0]--;
  333.             look[0]--;
  334.  
  335.         break;
  336.     case '+':
  337.         eye[2]--;
  338.         break;
  339.     case 'i':
  340.  
  341.             eye[2]--;
  342.  
  343.         break;
  344.     case '-':
  345.         eye[2]++;
  346.         break;
  347.     case 'k':
  348.  
  349.             eye[2]++;
  350.  
  351.  
  352.         break;
  353.     case ' ':
  354.         b_rot += 20;
  355.         break;
  356.     case '5':
  357.         nipa_z +=  2;
  358.         leg_rot_x = 1;
  359.         leg_rot_y = 0;
  360.         leg_rot_z = 0;
  361.         eye[2] +=1;
  362.         look[2] +=1;
  363.     if(theta>0)
  364.         theta -=10;
  365.     else
  366.         theta +=10;
  367. //        if(theta_left>0)
  368. //            theta_left -=10;
  369. //        else
  370. //            theta_left +=10;
  371. //        if(theta_right>0)
  372. //            theta_right +=10;
  373. //        else
  374. //            theta_right -=10;
  375.  
  376.         break;
  377.     case '8':
  378.         nipa_z -= 2;
  379.         leg_rot_x = 1;
  380.         leg_rot_y = 0;
  381.         leg_rot_z = 0;
  382.         eye[2] -=1;
  383.         look[2] -=1;
  384.         if(theta<0)
  385.         theta +=10;
  386.     else
  387.         theta -=10;
  388.  
  389.  
  390. //        if(theta_left<0)
  391. //            theta_left +=10;
  392. //        else
  393. //            theta_left -=10;
  394. //        if(theta_right<0)
  395. //            theta_right -=10;
  396. //        else
  397. //            theta_right +=10;
  398.         break;
  399.     case '7':
  400.         nipa_x -=1;
  401.         leg_rot_x = 0;
  402.         leg_rot_y = 0;
  403.         leg_rot_z = 1;
  404.         if(theta<0)
  405.         theta +=10;
  406.     else
  407.         theta -=10;
  408.  
  409.         eye[0] -=1;
  410.         look[0] -=1;
  411.         break;
  412.     case '9':
  413.         nipa_x +=1;
  414.         leg_rot_x = 0;
  415.         leg_rot_y = 0;
  416.         leg_rot_z = 1;
  417.         eye[0] +=1;
  418.         look[0] +=1;
  419.          if(theta>0)
  420.         theta -=10;
  421.     else
  422.         theta +=10;
  423.         break;
  424.     }
  425.  
  426.     glutPostRedisplay();
  427. }
  428.  
  429.  
  430.  
  431.  
  432. static void display(void)
  433. {
  434.  
  435.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  436.     glMatrixMode(GL_PROJECTION);
  437.     glLoadIdentity();
  438.     int lim = 8;
  439.     glFrustum(-lim, lim, -lim, lim, 4, 200);
  440.  
  441.     glMatrixMode(GL_MODELVIEW);
  442.     glLoadIdentity();
  443.  
  444.  
  445.  
  446.     glMatrixMode(GL_MODELVIEW);
  447.     glLoadIdentity() ;
  448.  
  449.     //gluLookAt(eyeX,eyeY,eyeZ, lookX,lookY,lookZ, 0,1,0);
  450.     gluLookAt(eye[0],eye[1],eye[2], look[0],look[1],look[2], 0,1,0);
  451.     //glViewport(0, 0, window_width, window_height);
  452.  
  453.     glRotatef(rot, 0,1,0);
  454.  
  455. //    axes();
  456. //    wall();
  457. //    robot();
  458. buiding();
  459. road();
  460.  
  461.     glFlush();
  462.     glutSwapBuffers();
  463. }
  464.  
  465. int main(int argc, char *argv[])
  466. {
  467.  
  468.     glutInit(&argc, argv);
  469.     glutInitWindowSize(window_width,window_height);
  470.     glutInitWindowPosition(300,10);
  471.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  472.     glutCreateWindow("3D Maze");
  473.  
  474.      LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\graphics\\maze\\images\\building1.bmp",1);
  475.       LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\graphics\\maze\\images\\building2.bmp",2);
  476.        LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\graphics\\maze\\images\\road.bmp",3);
  477.      //LoadTexture("C:\\Users\\Admin\\Desktop\\nipa\\project\\maze\\images\\building2.bmp",2);
  478.      //LoadTexture("C:\\Users\\Admin\\Desktop\\nipa\\project\\maze\\images\\road.bmp",3);
  479.  
  480.  
  481.     glutDisplayFunc(display);
  482.     glutKeyboardFunc(key);
  483.  
  484.     _light();
  485.     glShadeModel( GL_SMOOTH );
  486.     glEnable(GL_DEPTH_TEST);
  487.     glEnable(GL_NORMALIZE);
  488.     glEnable(GL_LIGHTING);
  489.     glutMainLoop();
  490.  
  491. }
  492.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement