Advertisement
FaisalAhemdBijoy

City 1106

May 22nd, 2021
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.11 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. GLfloat eyeX=0;
  18. GLfloat eyeY=40;
  19. GLfloat eyeZ=50;
  20.  
  21. GLfloat lookX=0;
  22. GLfloat lookY=40;
  23. GLfloat lookZ=-500;
  24.  
  25. bool light_switch_0=true;
  26. bool light_switch_1=true;
  27. bool spot_light_switch=true;
  28.  
  29.  
  30. float rot = 0;
  31.  
  32. unsigned int ID;
  33. #define GL_CLAMP_TO_EDGE 0x812F
  34.  
  35. static GLfloat v_cube[8][3] =
  36. {
  37.     {0,0,0},
  38.     {0,0,1},
  39.     {0,1,0},
  40.     {0,1,1},
  41.  
  42.     {1,0,0},
  43.     {1,0,1},
  44.     {1,1,0},
  45.     {1,1,1}
  46. };
  47.  
  48. static GLubyte c_ind[6][4] =
  49. {
  50.  
  51.  
  52.     {3,1,5,7}, //front
  53.     {2,0,1,3}, //left
  54.     {7,5,4,6}, //right
  55.     {2,3,7,6}, //top
  56.     {1,0,5,4}, //bottom
  57.     {6,4,0,2}, //back
  58.  
  59.  
  60. };
  61. static void getNormal3p(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3)
  62. {
  63.     GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
  64.  
  65.     Ux = x2-x1;
  66.     Uy = y2-y1;
  67.     Uz = z2-z1;
  68.  
  69.     Vx = x3-x1;
  70.     Vy = y3-y1;
  71.     Vz = z3-z1;
  72.  
  73.     Nx = Uy*Vz - Uz*Vy;
  74.     Ny = Uz*Vx - Ux*Vz;
  75.     Nz = Ux*Vy - Uy*Vx;
  76.  
  77.     glNormal3f(Nx,Ny,Nz);
  78. }
  79.  
  80. void material_property(float R, float G, float B)
  81. {
  82.     GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  83.     GLfloat mat_ambient[] = { R, G, B, 1.0 };
  84.     GLfloat mat_diffuse[] = { R, G, B, 1.0 };
  85.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  86.     GLfloat mat_shininess[] = {255};
  87.  
  88.     glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient);
  89.     glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse);
  90.     glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular);
  91.     glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess);
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98. void cube(float R=0.5, float G=0.5, float B=0.5 )
  99. {
  100.     material_property(R,G,B);
  101.     glBegin(GL_QUADS);
  102.     for (GLint i = 0; i <6; i++)
  103.     {
  104.  
  105.         getNormal3p(v_cube[c_ind[i][0]][0], v_cube[c_ind[i][0]][1], v_cube[c_ind[i][0]][2],
  106.                     v_cube[c_ind[i][1]][0], v_cube[c_ind[i][1]][1], v_cube[c_ind[i][1]][2],
  107.                     v_cube[c_ind[i][2]][0], v_cube[c_ind[i][2]][1], v_cube[c_ind[i][2]][2]);
  108.  
  109.  
  110.         glTexCoord2f(1,1);
  111.         glVertex3fv(&v_cube[c_ind[i][0]][0]);
  112.         glTexCoord2f(1,0);
  113.         glVertex3fv(&v_cube[c_ind[i][1]][0]);
  114.         glTexCoord2f(0,0);
  115.         glVertex3fv(&v_cube[c_ind[i][2]][0]);
  116.         glTexCoord2f(0,1);
  117.         glVertex3fv(&v_cube[c_ind[i][3]][0]);
  118.  
  119.     }
  120.     glEnd();
  121. }
  122.  
  123.  
  124.  
  125. void LoadTexture(const char*filename, int rep = 1)
  126. {
  127.     glGenTextures(1, &ID);
  128.     glBindTexture(GL_TEXTURE_2D, ID);
  129.     glPixelStorei(GL_UNPACK_ALIGNMENT, ID);
  130.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  131.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  132.     if(rep)
  133.     {
  134.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  135.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  136.     }
  137.     else
  138.     {
  139.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  140.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  141.     }
  142.  
  143.     BmpLoader bl(filename);
  144.     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bl.iWidth, bl.iHeight, GL_RGB, GL_UNSIGNED_BYTE, bl.textureData );
  145. }
  146.  
  147. void buiding()
  148. {
  149.     // building 1
  150.     glEnable(GL_TEXTURE_2D);
  151.     glBindTexture(GL_TEXTURE_2D,1);
  152.  
  153.     glPushMatrix();
  154.     glTranslatef(0,30,0);
  155.     glScalef(20,60,20);
  156.     glTranslatef(-0.5,-0.5,-0.5);
  157.     cube();
  158.     glPopMatrix();
  159.  
  160.     glDisable(GL_TEXTURE_2D);
  161.  
  162.     // building 2
  163.     glEnable(GL_TEXTURE_2D);
  164.     glBindTexture(GL_TEXTURE_2D,2);
  165.  
  166.     glPushMatrix();
  167.     glTranslatef(50,30,0);
  168.     glScalef(20,60,20);
  169.     glTranslatef(-0.5,-0.5,-0.5);
  170.     cube();
  171.     glPopMatrix();
  172.  
  173.     glDisable(GL_TEXTURE_2D);
  174.  
  175.     // building 3
  176.     glEnable(GL_TEXTURE_2D);
  177.     glBindTexture(GL_TEXTURE_2D,3);
  178.  
  179.     glPushMatrix();
  180.     glTranslatef(100,30,0);
  181.     glScalef(20,60,20);
  182.     glTranslatef(-0.5,-0.5,-0.5);
  183.     cube();
  184.     glPopMatrix();
  185.  
  186.     glDisable(GL_TEXTURE_2D);
  187.  
  188.     // building 4
  189.     glEnable(GL_TEXTURE_2D);
  190.     glBindTexture(GL_TEXTURE_2D,4);
  191.  
  192.     glPushMatrix();
  193.     glTranslatef(0,30,-30);
  194.     glScalef(20,60,20);
  195.     glTranslatef(-0.5,-0.5,-0.5);
  196.     cube();
  197.     glPopMatrix();
  198.     glDisable(GL_TEXTURE_2D);
  199.  
  200.     // building 5
  201.     glEnable(GL_TEXTURE_2D);
  202.     glBindTexture(GL_TEXTURE_2D,5);
  203.  
  204.     glPushMatrix();
  205.     glTranslatef(50,30,-30);
  206.     glScalef(20,60,20);
  207.     glTranslatef(-0.5,-0.5,-0.5);
  208.     cube();
  209.     glPopMatrix();
  210.     glDisable(GL_TEXTURE_2D);
  211.  
  212.     // building 6
  213.     glEnable(GL_TEXTURE_2D);
  214.     glBindTexture(GL_TEXTURE_2D,6);
  215.  
  216.     glPushMatrix();
  217.     glTranslatef(100,30,-30);
  218.     glScalef(20,60,20);
  219.     glTranslatef(-0.5,-0.5,-0.5);
  220.     cube();
  221.     glPopMatrix();
  222.     glDisable(GL_TEXTURE_2D);
  223.  
  224.     // building 7
  225.     glEnable(GL_TEXTURE_2D);
  226.     glBindTexture(GL_TEXTURE_2D,2);
  227.  
  228.     glPushMatrix();
  229.     glTranslatef(0,30,-60);
  230.     glScalef(20,60,20);
  231.     glTranslatef(-0.5,-0.5,-0.5);
  232.     cube();
  233.     glPopMatrix();
  234.     glDisable(GL_TEXTURE_2D);
  235.  
  236.     // building 8
  237.     glEnable(GL_TEXTURE_2D);
  238.     glBindTexture(GL_TEXTURE_2D,1);
  239.  
  240.     glPushMatrix();
  241.     glTranslatef(50,30,-60);
  242.     glScalef(20,60,20);
  243.     glTranslatef(-0.5,-0.5,-0.5);
  244.     cube();
  245.     glPopMatrix();
  246.     glDisable(GL_TEXTURE_2D);
  247.  
  248.     // building 9
  249.     glEnable(GL_TEXTURE_2D);
  250.     glBindTexture(GL_TEXTURE_2D,4);
  251.  
  252.     glPushMatrix();
  253.     glTranslatef(100,30,-60);
  254.     glScalef(20,60,20);
  255.     glTranslatef(-0.5,-0.5,-0.5);
  256.     cube();
  257.     glPopMatrix();
  258.     glDisable(GL_TEXTURE_2D);
  259.  
  260.  
  261.     //roof
  262.     glPushMatrix();
  263.     glTranslatef(50,60,0);
  264.     glScalef(20,1,20);
  265.     glTranslatef(-0.5,-0.5,-0.5);
  266.     cube();
  267.     glPopMatrix();
  268.  
  269.  
  270. }
  271. void road()
  272. {
  273.     //main road
  274.     glEnable(GL_TEXTURE_2D);
  275.     glBindTexture(GL_TEXTURE_2D,10);
  276.  
  277.     glPushMatrix();
  278.     glTranslatef(0,0.5,20);
  279.     glScalef(200,1,10);
  280.     glTranslatef(-0.5,-0.5,-0.5);
  281.     cube();
  282.     glPopMatrix();
  283.  
  284.     glDisable(GL_TEXTURE_2D);
  285.  
  286.     //side road
  287.     glEnable(GL_TEXTURE_2D);
  288.     glBindTexture(GL_TEXTURE_2D,10);
  289.  
  290.     glPushMatrix();
  291.     glTranslatef(-50,0.5,0);
  292.     glRotatef(90,0,1,0);
  293.     glScalef(100,1,10);
  294.     glTranslatef(-0.5,-0.5,-0.5);
  295.     cube();
  296.     glPopMatrix();
  297.  
  298.     glDisable(GL_TEXTURE_2D);
  299. }
  300. void trees()
  301. {
  302.     // tree 1
  303.  
  304.     glEnable(GL_TEXTURE_2D);
  305.     glBindTexture(GL_TEXTURE_2D,12);
  306.  
  307.     glPushMatrix();
  308.     glTranslatef(-20,30,0);
  309.     glScalef(20,60,20);
  310.     glTranslatef(-0.5,-0.5,-0.5);
  311.     cube();
  312.     glPopMatrix();
  313.  
  314.     glDisable(GL_TEXTURE_2D);
  315.  
  316.     // tree 2
  317.  
  318.     glEnable(GL_TEXTURE_2D);
  319.     glBindTexture(GL_TEXTURE_2D,12);
  320.  
  321.     glPushMatrix();
  322.     glTranslatef(-70,30,0);
  323.     glScalef(20,60,20);
  324.     glTranslatef(-0.5,-0.5,-0.5);
  325.     cube();
  326.     glPopMatrix();
  327.  
  328.     glDisable(GL_TEXTURE_2D);
  329.  
  330.  
  331.  
  332. }
  333. void wood_leaf()
  334. {
  335.  
  336.     // wood
  337.     glEnable(GL_TEXTURE_2D);
  338.     glBindTexture(GL_TEXTURE_2D,13);
  339.  
  340.     glPushMatrix();
  341.     glTranslatef(30,30,0);
  342.     glScalef(2,30,2);
  343.     glTranslatef(-0.5,-0.5,-0.5);
  344.     cube();
  345.     glPopMatrix();
  346.     glDisable(GL_TEXTURE_2D);
  347.  
  348.     //leaf
  349.  
  350.     glEnable(GL_TEXTURE_2D);
  351.     glBindTexture(GL_TEXTURE_2D,14);
  352.  
  353.     glPushMatrix();
  354.     glTranslatef(30,50,0);
  355.     glScalef(8,12,8);
  356.     glTranslatef(-0.5,-0.5,-0.5);
  357.     cube();
  358.     glPopMatrix();
  359.     glDisable(GL_TEXTURE_2D);
  360.  
  361.  
  362. }
  363.  
  364. void traffic_signal()
  365. {
  366.     // base
  367.     glEnable(GL_TEXTURE_2D);
  368.     glBindTexture(GL_TEXTURE_2D,13);
  369.  
  370.     glPushMatrix();
  371.     glTranslatef(-20,30,0);
  372.     glScalef(2,30,2);
  373.     glTranslatef(-0.5,-0.5,-0.5);
  374.     cube();
  375.     glPopMatrix();
  376.     glDisable(GL_TEXTURE_2D);
  377.  
  378.     // Signal light
  379.  
  380.     glEnable(GL_TEXTURE_2D);
  381.     glBindTexture(GL_TEXTURE_2D,8);
  382.  
  383.     glPushMatrix();
  384.     glTranslatef(-20,50,0);
  385.     glScalef(5,10,5);
  386.     glTranslatef(-0.5,-0.5,-0.5);
  387.     cube();
  388.     glPopMatrix();
  389.     glDisable(GL_TEXTURE_2D);
  390.  
  391.     // roof off
  392.     glPushMatrix();
  393.     glTranslatef(-20,55,0);
  394.     glScalef(5,2,5);
  395.     glTranslatef(-0.5,-0.5,-0.5);
  396.     cube();
  397.     glPopMatrix();
  398.  
  399. }
  400.  
  401. void signboard()
  402. {
  403.     // base
  404.     glEnable(GL_TEXTURE_2D);
  405.     glBindTexture(GL_TEXTURE_2D,13);
  406.  
  407.     glPushMatrix();
  408.     glTranslatef(-50,30,0);
  409.     glScalef(2,30,2);
  410.     glTranslatef(-0.5,-0.5,-0.5);
  411.     cube();
  412.     glPopMatrix();
  413.     glDisable(GL_TEXTURE_2D);
  414.  
  415.     // Signal light
  416.  
  417.     glEnable(GL_TEXTURE_2D);
  418.     glBindTexture(GL_TEXTURE_2D,9);
  419.  
  420.     glPushMatrix();
  421.     glTranslatef(-50,50,0);
  422.     glScalef(20,20,20);
  423.     glTranslatef(-0.5,-0.5,-0.5);
  424.     cube();
  425.     glPopMatrix();
  426.     glDisable(GL_TEXTURE_2D);
  427.  
  428.     // roof off
  429.     glPushMatrix();
  430.     glTranslatef(-50,55,0);
  431.     glScalef(5,2,5);
  432.     glTranslatef(-0.5,-0.5,-0.5);
  433.     cube();
  434.     glPopMatrix();
  435.  
  436. }
  437. void shop()
  438. {
  439.     // shop 1
  440.     glEnable(GL_TEXTURE_2D);
  441.     glBindTexture(GL_TEXTURE_2D,11);
  442.  
  443.     glPushMatrix();
  444.     glTranslatef(-100,30,0);
  445.     glScalef(20,40,20);
  446.     glTranslatef(-0.5,-0.5,-0.5);
  447.     cube();
  448.     glPopMatrix();
  449.  
  450.     glDisable(GL_TEXTURE_2D);
  451.  
  452.     // roof off
  453.     //roof
  454.     glPushMatrix();
  455.     glTranslatef(-100,50,0);
  456.     glScalef(20,2,20);
  457.     glTranslatef(-0.5,-0.5,-0.5);
  458.     cube();
  459.     glPopMatrix();
  460. }
  461.  
  462. void road_light()
  463. {
  464.     // base
  465.  
  466.     glPushMatrix();
  467.     glTranslatef(80,30,0);
  468.     glScalef(1,30,1);
  469.     glTranslatef(-0.5,-0.5,-0.5);
  470.     cube(0.1,0.1,0.1);
  471.     glPopMatrix();
  472.  
  473.     // light stand
  474.     glPushMatrix();
  475.     glTranslatef(85,42,0);
  476.     glScalef(10,1,1);
  477.     glTranslatef(-0.5,-0.5,-0.5);
  478.     cube(0.8,0.6,0.2);
  479.     glPopMatrix();
  480.  
  481.     // Bulb line
  482.     glPushMatrix();
  483.     glTranslatef(85,38,0);
  484.     glScalef(1,7,1);
  485.     glTranslatef(-0.5,-0.5,-0.5);
  486.     cube(0.9,0.9,0.9);
  487.     glPopMatrix();
  488.  
  489.     // texture light glutsolidshere
  490.  
  491.  
  492.     glPushMatrix();
  493.     glTranslatef(85,35,0);
  494.     glutSolidSphere(2,16,16);
  495.     glPopMatrix();
  496.  
  497.  
  498. }
  499.  
  500. void _light()
  501. {
  502.     //GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
  503.     GLfloat light_ambient[]  = {1.0, 1.0, 1.0, 1.0};
  504.     GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
  505.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  506.     GLfloat light_position[] = { 0.0, 50.0, 0, 1.0 };
  507.  
  508.     glEnable( GL_LIGHT7);
  509.  
  510.     glLightfv( GL_LIGHT7, GL_AMBIENT, light_ambient);
  511.     glLightfv( GL_LIGHT7, GL_DIFFUSE, light_diffuse);
  512.     glLightfv( GL_LIGHT7, GL_SPECULAR, light_specular);
  513.  
  514.     glLightfv( GL_LIGHT7, GL_POSITION, light_position);
  515. }
  516.  
  517. void light_function_0(float x, float y, float z)
  518. {
  519.     // Light Specification
  520.     GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
  521.     GLfloat light_ambient[] = {0.1, 0.1, 0.1, 1.0};
  522.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1 };
  523.     GLfloat light_specular[] = { 1, 1, 1, 1 };
  524.     GLfloat light_position[] = { x, y, z, 1.0 };
  525.  
  526.     glEnable( GL_LIGHT0);
  527.     if (light_switch_0)
  528.     {
  529.         glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient);
  530.         glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  531.         glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular);
  532.  
  533.     }
  534.     else
  535.     {
  536.         glLightfv( GL_LIGHT0, GL_AMBIENT, no_light);
  537.         glLightfv( GL_LIGHT0, GL_DIFFUSE, no_light);
  538.         glLightfv( GL_LIGHT0, GL_SPECULAR, no_light);
  539.  
  540.     }
  541.  
  542.  
  543.     glLightfv( GL_LIGHT0, GL_POSITION, light_position);
  544.  
  545. }
  546.  
  547. void light_function_1(float x, float y, float z)
  548. {
  549.     // Light Specification
  550.     GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
  551.     GLfloat light_ambient[] = {0.1, 0.1, 0.1, 1.0};
  552.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1 };
  553.     GLfloat light_specular[] = { 1, 1, 1, 1 };
  554.     GLfloat light_position[] = { x, y, z, 1.0 };
  555.  
  556.     glEnable( GL_LIGHT1);
  557.     if (light_switch_1)
  558.     {
  559.         glLightfv( GL_LIGHT1, GL_AMBIENT, light_ambient);
  560.         glLightfv( GL_LIGHT1, GL_DIFFUSE, light_diffuse);
  561.         glLightfv( GL_LIGHT1, GL_SPECULAR, light_specular);
  562.  
  563.     }
  564.     else
  565.     {
  566.         glLightfv( GL_LIGHT1, GL_AMBIENT, no_light);
  567.         glLightfv( GL_LIGHT1, GL_DIFFUSE, no_light);
  568.         glLightfv( GL_LIGHT1, GL_SPECULAR, no_light);
  569.  
  570.     }
  571.  
  572.  
  573.     glLightfv( GL_LIGHT1, GL_POSITION, light_position);
  574.  
  575. }
  576.  
  577.  
  578. void spot_light_function(float x, float y, float z)
  579. {
  580.     // Light Specification
  581.     GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
  582.     GLfloat light_ambient[] = {0.0, 0.5, 0.0, 1.0};
  583.     GLfloat light_diffuse[] = { 0.0, 1.0, 0.0, 1 };
  584.     GLfloat light_specular[] = { 1, 1, 1, 1 };
  585.     GLfloat light_position[] = { x, y, z, 1.0 };
  586.  
  587.     glEnable( GL_LIGHT2);
  588.     if (spot_light_switch)
  589.     {
  590.         glLightfv( GL_LIGHT2, GL_AMBIENT, light_ambient);
  591.         glLightfv( GL_LIGHT2, GL_DIFFUSE, light_diffuse);
  592.         glLightfv( GL_LIGHT2, GL_SPECULAR, light_specular);
  593.  
  594.     }
  595.     else
  596.     {
  597.         glLightfv( GL_LIGHT2, GL_AMBIENT, no_light);
  598.         glLightfv( GL_LIGHT2, GL_DIFFUSE, no_light);
  599.         glLightfv( GL_LIGHT2, GL_SPECULAR, no_light);
  600.  
  601.     }
  602.  
  603.  
  604.     glLightfv( GL_LIGHT2, GL_POSITION, light_position);
  605.     GLfloat direction[]= {0,-1,0,1};
  606.     GLfloat cut_off=60;
  607.     glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,direction);
  608.     glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,cut_off);
  609.  
  610. }
  611. void axes()
  612. {
  613.     float length = 10;
  614.     float width = 0.3;
  615.  
  616.     // X-axis
  617.     glPushMatrix();
  618.     glTranslatef(length/2,0,0);
  619.     glScalef(length,width,width);
  620.     glTranslatef(-0.5,-0.5,-0.5);
  621.     cube(1,0,0); //RED Line
  622.     glPopMatrix();
  623.  
  624.     // Y-axis
  625.     glPushMatrix();
  626.     glTranslatef(0,length/2,0);
  627.     glScalef(width,length,width);
  628.     glTranslatef(-0.5,-0.5,-0.5);
  629.     cube(0,1,0); //GREEN Line
  630.     glPopMatrix();
  631.  
  632.     // Z-axis
  633.     glPushMatrix();
  634.     glTranslatef(0,0,length/2);
  635.     glScalef(width,width,length);
  636.     glTranslatef(-0.5,-0.5,-0.5);
  637.     cube(0,0,1); //BLUE Line
  638.     glPopMatrix();
  639. }
  640. static void key(unsigned char key, int x, int y)
  641. {
  642.     switch (key)
  643.     {
  644.     case 'u': // up
  645.  
  646.  
  647.         eyeY++;
  648.         lookY++;
  649.  
  650.         break;
  651.     case 'd': // down
  652.  
  653.         eyeY--;
  654.         lookY--;
  655.         break;
  656.     case 'a': // look right
  657.         lookX++;
  658.  
  659.         break;
  660.     case 'b':
  661.         lookX--;
  662.         // look left
  663.         break;
  664.  
  665.     case 'p': // rotate left
  666.         rot--;
  667.         break;
  668.     case 'q': // rotate right
  669.         rot++;
  670.         break;
  671.     case 'l': // left
  672.  
  673.         eyeX++;
  674.         lookX++;
  675.  
  676.  
  677.         break;
  678.     case 'r': // right
  679.  
  680.         eyeX--;
  681.         lookX--;
  682.  
  683.         break;
  684.     case '+': // zoom in
  685.         eyeZ--;
  686.         break;
  687.  
  688.     case '-':
  689.         //zoom out
  690.         eyeZ++;
  691.         break;
  692.  
  693.     case '1':
  694.         light_switch_0 =! light_switch_0;
  695.         break;
  696.     case '2':
  697.         light_switch_1 =! light_switch_1;
  698.         break;
  699.     case '3':
  700.         spot_light_switch =! spot_light_switch;
  701.         break;
  702.     }
  703.  
  704.     glutPostRedisplay();
  705. }
  706.  
  707.  
  708. void display_settings()
  709. {
  710.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  711.     glMatrixMode(GL_PROJECTION);
  712.     glLoadIdentity();
  713.     int frustum_window = 8;
  714.     glFrustum(-frustum_window, frustum_window, -frustum_window, frustum_window, 4, 200);
  715.  
  716.     glMatrixMode(GL_MODELVIEW);
  717.     glLoadIdentity();
  718.  
  719.  
  720.  
  721.     glMatrixMode(GL_MODELVIEW);
  722.     glLoadIdentity() ;
  723.  
  724.     gluLookAt(eyeX,eyeY,eyeZ, lookX,lookY,lookZ, 0,1,0);
  725.     //glViewport(0, 0, window_width, window_height);
  726.  
  727.     glRotatef(rot, 0,1,0);
  728.  
  729. }
  730.  
  731. static void display(void)
  732. {
  733.  
  734.  
  735.     display_settings();
  736.     axes();
  737.     buiding();
  738.     road();
  739.     //trees();
  740.     wood_leaf();
  741.     traffic_signal();
  742.     signboard();
  743.     shop();
  744.  
  745.  
  746.     //light 1
  747.     glPushMatrix();
  748.     light_function_0(85,35,0);
  749.     glTranslatef(85,0,0);
  750.     road_light();
  751.     //cube();
  752.     glPopMatrix();
  753.  
  754.     // light 2
  755.     glPushMatrix();
  756.     light_function_1(-100,35,0);
  757.     glTranslatef(-100,0,0);
  758.     road_light();
  759.     //cube();
  760.     glPopMatrix();
  761.  
  762.    
  763.  
  764.     glFlush();
  765.     glutSwapBuffers();
  766. }
  767.  
  768. int main(int argc, char *argv[])
  769. {
  770.  
  771.     glutInit(&argc, argv);
  772.     glutInitWindowSize(window_width,window_height);
  773.     glutInitWindowPosition(300,10);
  774.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  775.     glutCreateWindow("3D City");
  776.  
  777.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\building1.bmp",1);
  778.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\building2.bmp",2);
  779.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\building3.bmp",3);
  780.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\building4.bmp",4);
  781.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\building5.bmp",5);
  782.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\building6.bmp",6);
  783.  
  784.     // Traffic Light
  785.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\traffic1.bmp",7);
  786.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\traffic2.bmp",8);
  787.  
  788.     // Signboard
  789.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\signboard2.bmp",9);
  790.  
  791.     // roads
  792.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\road3.bmp",10);
  793.  
  794.     // shop
  795.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\shop1.bmp",11);
  796.  
  797.     // Trees
  798.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\tree2.bmp",12);
  799.  
  800.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\wood1.bmp",13);
  801.     //LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\wood2.bmp",15);
  802.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\leaf1.bmp",14);
  803.  
  804.     // road light bulb texture
  805.     LoadTexture("C:\\Users\\Shimul\\Documents\\CSE 4208 Computer Graphics\\City 3D\\images\\bulb4.bmp",15);
  806.  
  807.  
  808.     cout<<"--------------------------"<<endl;
  809.     cout<<"--------------------------"<<endl;
  810.     cout<<"-- 3D City Design ---"<<endl;
  811.     cout<<"-- Faisal Ahmed ----------"<<endl;
  812.     cout<<"-- Roll: 1607048 ---------"<<endl;
  813.     cout<<"--------------------------"<<endl;
  814.     cout<<"--------------------------"<<endl;
  815.     cout<<"Press : u -> Move Up"<<endl;
  816.     cout<<"Press : d -> Move Down"<<endl;
  817.     cout<<"Press : l -> Move Left"<<endl;
  818.     cout<<"Press : r -> Move Right"<<endl;
  819.  
  820.     cout<<"Press : p -> Rotate Right"<<endl;
  821.     cout<<"Press : p -> Rotate Right"<<endl;
  822.  
  823.     cout<<"Press : a -> Look Right"<<endl;
  824.     cout<<"Press : b -> Look Left"<<endl;
  825.  
  826.     cout<<"Press : + -> Zoom In"<<endl;
  827.     cout<<"Press : - -> Zoom Out"<<endl;
  828.  
  829.     cout<<"Press : 1 -> ON/OFF Light 1"<<endl;
  830.     cout<<"Press : 2 -> ON/OFF Light 2"<<endl;
  831.     cout<<"Press : 3 -> ON/OFF Spot Light"<<endl;
  832.  
  833.  
  834.     glutDisplayFunc(display);
  835.     glutKeyboardFunc(key);
  836.  
  837.     _light();
  838.     glShadeModel( GL_SMOOTH );
  839.     glEnable(GL_DEPTH_TEST);
  840.     glEnable(GL_NORMALIZE);
  841.     glEnable(GL_LIGHTING);
  842.     glutMainLoop();
  843.  
  844. }
  845.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement