Advertisement
FaisalAhemdBijoy

Lighting 43

Apr 25th, 2021
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.15 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.  
  9. const int width = 500;
  10. const int height = 500;
  11. const float rat = 1.0 * width / height;
  12.  
  13. GLfloat eX = 0;
  14. GLfloat eY = 15;
  15. GLfloat eZ = 20;
  16.  
  17. GLfloat lX = 0;
  18. GLfloat lY = 0;
  19. GLfloat lZ = 0;
  20.  
  21. float l_height = 15;
  22. float spt_cutoff = 30;
  23.  
  24. float rot = 0;
  25.  
  26. bool l_on = true;
  27.  
  28. static GLfloat v_cube[8][3] =
  29. {
  30.     {0,0,0},
  31.     {0,0,1},
  32.     {0,1,0},
  33.     {0,1,1},
  34.  
  35.     {1,0,0},
  36.     {1,0,1},
  37.     {1,1,0},
  38.     {1,1,1}
  39. };
  40.  
  41. static GLubyte c_ind[6][4] =
  42. {
  43.     {0,2,6,4},
  44.     {1,5,7,3},
  45.     {0,4,5,1},
  46.     {2,3,7,6},
  47.     {0,1,3,2},
  48.     {4,6,7,5}
  49. };
  50.  
  51. static void getNormal3p(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3)
  52. {
  53.     GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
  54.  
  55.     Ux = x2-x1;
  56.     Uy = y2-y1;
  57.     Uz = z2-z1;
  58.  
  59.     Vx = x3-x1;
  60.     Vy = y3-y1;
  61.     Vz = z3-z1;
  62.  
  63.     Nx = Uy*Vz - Uz*Vy;
  64.     Ny = Uz*Vx - Ux*Vz;
  65.     Nz = Ux*Vy - Uy*Vx;
  66.  
  67.     glNormal3f(Nx,Ny,Nz);
  68. }
  69.  
  70. void cube(float R=0.5, float G=0.5, float B=0.5, bool e=false, float alpha=1)
  71. {
  72.  
  73.     GLfloat m_no[] = {0, 0, 0, 1.0};
  74.     GLfloat m_amb[] = {R,G,B,1};
  75.     GLfloat m_diff[] = {R,G,B,1};
  76.     GLfloat m_spec[] = {1,1,1,1};
  77.     GLfloat m_sh[] = {30};
  78.  
  79.     GLfloat m_em[] = {1,1,1,1};
  80.  
  81.     glMaterialfv(GL_FRONT, GL_AMBIENT, m_amb);
  82.     glMaterialfv(GL_FRONT, GL_DIFFUSE, m_diff);
  83.     glMaterialfv(GL_FRONT, GL_SPECULAR, m_spec);
  84.     glMaterialfv(GL_FRONT, GL_SHININESS, m_sh);
  85.  
  86.     if(e & l_on) glMaterialfv(GL_FRONT, GL_EMISSION, m_em);
  87.     else glMaterialfv(GL_FRONT, GL_EMISSION, m_no);
  88.  
  89.     glBegin(GL_QUADS);
  90.     for (GLint i = 0; i <6; i++)
  91.     {
  92.         getNormal3p(v_cube[c_ind[i][0]][0], v_cube[c_ind[i][0]][1], v_cube[c_ind[i][0]][2],
  93.                     v_cube[c_ind[i][1]][0], v_cube[c_ind[i][1]][1], v_cube[c_ind[i][1]][2],
  94.                     v_cube[c_ind[i][2]][0], v_cube[c_ind[i][2]][1], v_cube[c_ind[i][2]][2]);
  95.  
  96.         for (GLint j=0; j<4; j++)
  97.         {
  98.             glVertex3fv(&v_cube[c_ind[i][j]][0]);
  99.         }
  100.     }
  101.     glEnd();
  102. }
  103.  
  104. void axes()
  105. {
  106.     float length = 10;
  107.     float width = 0.3;
  108.  
  109.     // X-axis
  110.     glPushMatrix();
  111.     glTranslatef(length/2,0,0);
  112.     glScalef(length,width,width);
  113.     glTranslatef(-0.5,-0.5,-0.5);
  114.     cube(0.8,0.1,0.1);
  115.     glPopMatrix();
  116.  
  117.     // Y-axis
  118.     glPushMatrix();
  119.     glTranslatef(0,length/2,0);
  120.     glScalef(width,length,width);
  121.     glTranslatef(-0.5,-0.5,-0.5);
  122.     cube(0.1,0.8,0.1);
  123.     glPopMatrix();
  124.  
  125.     // Z-axis
  126.     glPushMatrix();
  127.     glTranslatef(0,0,length/2);
  128.     glScalef(width,width,length);
  129.     glTranslatef(-0.5,-0.5,-0.5);
  130.     cube(0.1,0.1,0.8);
  131.     glPopMatrix();
  132. }
  133.  
  134. void flr()
  135. {
  136.     glPushMatrix();
  137. //    glTranslatef(0,-0.5,0);
  138.     glScalef(60,1,60);
  139.     glTranslatef(-0.5,-1,-0.5);
  140.     cube();
  141.     glPopMatrix();
  142. }
  143.  
  144. void table()
  145. {
  146.     float height=6;
  147.     float width=10;
  148.     float length=5;
  149.  
  150.     float base_height=1;
  151.     float leg_height=height-base_height;
  152.     float leg_width=0.5;
  153.  
  154.     // whole table
  155.     glPushMatrix();
  156.     glTranslatef(0,leg_height,0);
  157.  
  158.     // base
  159.     glPushMatrix();
  160.     glScalef(width,base_height,length);
  161.     glTranslatef(-0.5,0,-0.5);
  162.     cube(0.53,0.39,0.28);
  163.     glPopMatrix();
  164.  
  165.     // legs
  166.     glPushMatrix();
  167.     glTranslatef((width/2-leg_width/2),0,(length/2-leg_width/2));
  168.     glScalef(leg_width,leg_height,leg_width);
  169.     glTranslatef(-0.5,-1,-0.5);
  170.     cube(0.53,0.39,0.28);
  171.     glPopMatrix();
  172.  
  173.     glPushMatrix();
  174.     glTranslatef((width/2-leg_width/2),0,-(length/2-leg_width/2));
  175.     glScalef(leg_width,leg_height,leg_width);
  176.     glTranslatef(-0.5,-1,-0.5);
  177.     cube(0.53,0.39,0.28);
  178.     glPopMatrix();
  179.  
  180.     glPushMatrix();
  181.     glTranslatef(-(width/2-leg_width/2),0,(length/2-leg_width/2));
  182.     glScalef(leg_width,leg_height,leg_width);
  183.     glTranslatef(-0.5,-1,-0.5);
  184.     cube(0.53,0.39,0.28);
  185.     glPopMatrix();
  186.  
  187.     glPushMatrix();
  188.     glTranslatef(-(width/2-leg_width/2),0,-(length/2-leg_width/2));
  189.     glScalef(leg_width,leg_height,leg_width);
  190.     glTranslatef(-0.5,-1,-0.5);
  191.     cube(0.53,0.39,0.28);
  192.     glPopMatrix();
  193.  
  194.     glPopMatrix();
  195. }
  196.  
  197. static void res(int width, int height)
  198. {
  199.     glViewport(0, 0, width, width/rat);
  200. }
  201.  
  202. void light()
  203. {
  204.     //light
  205.     GLfloat l_no[] = {0, 0, 0, 1.0};
  206.     GLfloat l_amb[] = {0.5, 0.5, 0.5, 1.0};
  207.     GLfloat l_dif[] = {1,1,1,1};
  208.     GLfloat l_spec[] = {1,1,1,1};
  209.     GLfloat l_pos[] = {0,l_height,0,1.0};
  210.  
  211.     glEnable(GL_LIGHT0);
  212.  
  213.     if(l_on) glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb);
  214.     else glLightfv(GL_LIGHT0, GL_AMBIENT, l_no);
  215.     if(l_on) glLightfv(GL_LIGHT0, GL_DIFFUSE, l_dif);
  216.     else glLightfv(GL_LIGHT0, GL_DIFFUSE, l_no);
  217.     if(l_on) glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec);
  218.     else glLightfv(GL_LIGHT0, GL_SPECULAR, l_no);
  219.  
  220.     glLightfv(GL_LIGHT0, GL_POSITION, l_pos);
  221.  
  222.     // spot light extra
  223.     GLfloat l_spt[] = {0,-1,0,1};
  224.     GLfloat spt_ct[] = {spt_cutoff};
  225.     glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, l_spt);
  226.     glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, spt_ct);
  227. }
  228.  
  229. static void display(void)
  230. {
  231.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  232.     glMatrixMode(GL_PROJECTION);
  233.     glLoadIdentity();
  234.     glFrustum(-3, 3, -3, 3, 3.0, 100.0);
  235.  
  236.     glMatrixMode(GL_MODELVIEW);
  237.     glLoadIdentity();
  238.     gluLookAt(eX,eY,eZ, lX,lY,lZ, 0,1,0);
  239.  
  240.     glRotatef(rot, 0,1,0);
  241.     axes();
  242.  
  243.     table();
  244.     flr();
  245.  
  246.     glPushMatrix();
  247.     glRotatef(45, 0,1,0);
  248.     light();
  249.     glPopMatrix();
  250.  
  251.     glTranslatef(0,l_height+1,0);
  252.     glScalef(5,1,1);
  253.     glTranslatef(-0.5,-0.5,-0.5);
  254.     cube(1,0,0,true);
  255.  
  256.     glutSwapBuffers();
  257. }
  258.  
  259.  
  260. static void key(unsigned char key, int x, int y)
  261. {
  262.     switch (key)
  263.     {
  264.     case 27 :
  265.     case 'q':
  266.         exit(0);
  267.         break;
  268.  
  269.     case 't':
  270.         l_on=1-l_on;
  271.         break;
  272.  
  273.     case ',':
  274.         rot+=2;
  275.         break;
  276.     case '.':
  277.         rot-=2;
  278.         break;
  279.  
  280.     case 's':
  281.         eZ--;
  282.         lZ--;
  283.         break;
  284.     case 'p':
  285.         eZ++;
  286.         lZ++;
  287.         break;
  288.  
  289.     case 'n':
  290.         eY--;
  291.         break;
  292.     case 'u':
  293.         eY++;
  294.         break;
  295.  
  296.     case '1':
  297.         l_height++;
  298.         break;
  299.     case '2':
  300.         l_height--;
  301.         break;
  302.  
  303.     case '3':
  304.         spt_cutoff++;
  305.         break;
  306.     case '4':
  307.         spt_cutoff--;
  308.         break;
  309.     }
  310.  
  311.     glutPostRedisplay();
  312. }
  313.  
  314. static void idle(void)
  315. {
  316.     glutPostRedisplay();
  317. }
  318.  
  319. /* Program entry point */
  320.  
  321. int main(int argc, char *argv[])
  322. {
  323.     glutInit(&argc, argv);
  324.     glutInitWindowSize(width,height);
  325.     glutInitWindowPosition(10,10);
  326.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  327.  
  328.     glutCreateWindow("GLUT Shapes");
  329.  
  330.     glutDisplayFunc(display);
  331.     glutKeyboardFunc(key);
  332.     glutIdleFunc(idle);
  333.     glutReshapeFunc(res);
  334.  
  335. //    glClearColor(1,1,1,1);
  336.  
  337.     glEnable(GL_DEPTH_TEST);
  338.     glShadeModel( GL_SMOOTH );
  339.     glEnable(GL_NORMALIZE);
  340.     glEnable(GL_BLEND);
  341.  
  342.     glEnable(GL_LIGHTING);
  343.  
  344.     printf("########################################################################################\n");
  345.     printf("########################################################################################\n");
  346.     printf("##############                                                         #################\n");
  347.     printf("##############           PLEASE FOLLOW THE BELOW INSTRUCTIONS          #################\n");
  348.     printf("##############                                                         #################\n");
  349.     printf("########################################################################################\n");
  350.     printf("########################################################################################\n\n\n");
  351.     printf("Use 'w' to look up, 's' to look down, 'd' to look right, and 'a' to look left.\n");
  352.     printf("Use 'i' to move camera up, 'k' to move camera down, 'l' to move camera right, and 'j' to move camera left with the look at point fixed.\n");
  353.     printf("Use '+' to zoom in and '-' to zoom out.\n\n\n");
  354.  
  355.     glutMainLoop();
  356.  
  357.     return EXIT_SUCCESS;
  358. }
  359.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement