Advertisement
FaisalAhemdBijoy

Classroom 3D Design

Apr 18th, 2021
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.77 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<iostream>
  10. using namespace std;
  11.  
  12. double Txval=0,Tyval=0,Tzval=0;
  13. double windowHeight=700, windowWidth=700;
  14. GLfloat alpha = 0.0, theta = 0.0, axis_x=0.0, axis_y=0.0, f_spin=0.0;
  15. GLboolean bRotate = false, uRotate = false;
  16.  
  17. bool flagRot = false;
  18.  
  19.  
  20. static GLfloat v_cube[8][3] =
  21. {
  22.     {0,0,0},
  23.     {0,0,1},
  24.     {0,1,0},
  25.     {0,1,1},
  26.     {1,0,0},
  27.     {1,0,1},
  28.     {1,1,0},
  29.     {1,1,1}
  30. };
  31.  
  32. static GLubyte quadIndices[6][4] =
  33. {
  34.     {3, 1, 5, 7},
  35.     {2, 0, 4, 6},
  36.     {2, 0, 1, 3},
  37.     {6, 4, 5, 7},
  38.     {2, 3, 7, 6},
  39.     {0, 1, 5, 4}
  40. };
  41.  
  42. void cube(float cr = 0.5, float cg = 0.5, float cb = 0.5)
  43. {
  44.     glBegin(GL_QUADS);
  45.     for(GLint i = 0; i<6; i++)
  46.     {
  47.         glColor3f(cr,cg,cb);
  48.         //cr+=0.6;
  49.         //cg+=0.4;
  50.         //cb+=0.2;
  51.  
  52.         for(GLint j=0; j<4; j++)
  53.         {
  54.             glVertex3fv(&v_cube[quadIndices[i][j]][0]);
  55.         }
  56.     }
  57.     glEnd();
  58. }
  59.  
  60. void axes()
  61. {
  62.     float length = 10;
  63.     float width = 0.3;
  64.  
  65.     // X-axis
  66.     glPushMatrix();
  67.     glTranslatef(length/2,0,0);
  68.     glScalef(length,width,width);
  69.     glTranslatef(-0.5,-0.5,-0.5);
  70.     cube(0.8,0.1,0.1);
  71.     glPopMatrix();
  72.  
  73.     // Y-axis
  74.     glPushMatrix();
  75.     glTranslatef(0,length/2,0);
  76.     glScalef(width,length,width);
  77.     glTranslatef(-0.5,-0.5,-0.5);
  78.     cube(0.1,0.8,0.1);
  79.     glPopMatrix();
  80.  
  81.     // Z-axis
  82.     glPushMatrix();
  83.     glTranslatef(0,0,length/2);
  84.     glScalef(width,width,length);
  85.     glTranslatef(-0.5,-0.5,-0.5);
  86.     cube(0.1,0.1,0.8);
  87.     glPopMatrix();
  88. }
  89.  
  90. void wall()
  91. {
  92.     float length = 40;
  93.     float width = 1;
  94.     //floor
  95.     glPushMatrix();
  96.     glScalef(length,width,length);
  97.     glTranslatef(-0.5,-1,-0.5);
  98.     cube(0.9,0.9,0.9);
  99.     glPopMatrix();
  100.     //left
  101.     glPushMatrix();
  102.     glTranslatef(length/2,0,0);
  103.     glScalef(width,length,length);
  104.     glTranslatef(0,0,-0.5);
  105.     cube(0.3,0.7,0.7);
  106.     glPopMatrix();
  107.     // right
  108.     glPushMatrix();
  109.     glTranslatef(-length/2,0,0);
  110.     glScalef(width,length,length);
  111.     glTranslatef(0,0,-0.5);
  112.     cube(0.3,0.7,0.7);
  113.     glPopMatrix();
  114.     // Up
  115.     glPushMatrix();
  116.     glTranslatef(0,length,0);
  117.     glScalef(length,width,length);
  118.     glTranslatef(-0.5,0,-0.5);
  119.     cube(1,0,0);
  120.     glPopMatrix();
  121.     // Front
  122.     glPushMatrix();
  123.     glTranslatef(0,0,-length/2);
  124.     glScalef(length,length,width);
  125.     glTranslatef(-0.5,0,0);
  126.     cube(0,1.0,0);
  127.     glPopMatrix();
  128.  
  129.     // while board
  130.     glPushMatrix();
  131.     glTranslatef(0,length/3,-(length/2 -1));
  132.     glScalef(length/2,length/3,width);
  133.     glTranslatef(-0.5,0,0);
  134.     cube(1.0,1.0,1.0);
  135.     glPopMatrix();
  136. }
  137.  
  138. void fan()
  139. {
  140.     float height = 43;
  141.  
  142.     float m_width = 4;
  143.     float m_height = 2;
  144.     float angle = 0;
  145.  
  146.     float s_width = 2.0;
  147.     float s_height = 20.0;
  148.  
  149.     glPushMatrix();
  150.     glTranslatef(0,0,10);
  151.     for(int i=0; i<=360 ; i=i+10)
  152.     {
  153.  
  154.         glPushMatrix();
  155.         glTranslatef(0,height,0);
  156.         glRotatef(i,0,1,0);
  157.         glScalef(m_width,m_height/8,m_height);
  158.         glTranslatef(-0.5,0,-0.5);
  159.         cube(0.753, 0.753, 0.753);
  160.         glPopMatrix();
  161.  
  162.     }
  163.  
  164.     glPushMatrix();
  165.     glTranslatef(0,height-m_width-m_height/8,0);
  166.     glScalef(1,m_width,1);
  167.     glTranslatef(-0.5,0,-0.5);
  168.     cube(1,0,0);
  169.     glPopMatrix();
  170.  
  171.     glPushMatrix();
  172.     glRotatef(f_spin,0,1,0);
  173.     for(int i=0; i<=360 ; i=i+10)
  174.     {
  175.  
  176.         glPushMatrix();
  177.         glTranslatef(0,height-m_width-m_height/8-m_height,0);
  178.         glRotatef(i,0,1,0);
  179.         glScalef(m_width,m_height,m_height);
  180.         glTranslatef(-0.5,0,-0.5);
  181.         cube(0.863, 0.078, 0.235);
  182.         glPopMatrix();
  183.  
  184.     }
  185.  
  186.     glPushMatrix();
  187.     glTranslatef(0,height-m_width-m_height/8-m_height+1,0);
  188.     glScalef(s_height,0.5,s_width);
  189.     glTranslatef(-0.5,0,-0.5);
  190.     cube(0.000, 0.749, 1.000);
  191.     glPopMatrix();
  192.  
  193.     glPushMatrix();
  194.     glTranslatef(0,height-m_width-m_height/8-m_height+1,0);
  195.     glScalef(s_width,0.5,s_height);
  196.     glTranslatef(-0.5,0,-0.5);
  197.     cube(0.000, 0.749, 1.000);
  198.     glPopMatrix();
  199.  
  200.     glPopMatrix();
  201.  
  202.     glPopMatrix();
  203. }
  204.  
  205. void chair()
  206. {
  207.  
  208.     float length=20;
  209.     float width=1;
  210.  
  211.     //base seat
  212.     glPushMatrix();
  213.     glTranslatef(0,length/2,0);
  214.     glScalef(length,width,length);
  215.     glTranslatef(-0.5,-0.5,-0.5);
  216.     cube();
  217.     glPopMatrix();
  218.  
  219.     // leg base 1
  220.     glPushMatrix();
  221.     glTranslatef(length/2 -width/2,0,length/2-width/2);
  222.     glScalef(width,length,width);
  223.     glTranslatef(-0.5,-0.5,-0.5);
  224.     cube(1,0,0);
  225.     glPopMatrix();
  226.     // leg base 2
  227.     glPushMatrix();
  228.     glTranslatef(length/2 -width/2,0,- length/2 +width/2);
  229.     glScalef(width,length,width);
  230.     glTranslatef(-0.5,-0.5,-0.5);
  231.     cube(1,0,0);
  232.     glPopMatrix();
  233.     // leg base 3
  234.     glPushMatrix();
  235.     glTranslatef(-length/2 +width/2,0,+ length/2 -width/2);
  236.     glScalef(width,length,width);
  237.     glTranslatef(-0.5,-0.5,-0.5);
  238.     cube(1,0,0);
  239.     glPopMatrix();
  240.     // leg  base 4
  241.     glPushMatrix();
  242.     glTranslatef(-length/2 +width/2,0,- length/2 +width/2);
  243.     glScalef(width,length,width);
  244.     glTranslatef(-0.5,-0.5,-0.5);
  245.     cube(1,0,0);
  246.     glPopMatrix();
  247.  
  248.     // upper  1
  249.     glPushMatrix();
  250.     glTranslatef(length/2 -width/2,length,length/2-width/2);
  251.     glScalef(width,length,width);
  252.     glTranslatef(-0.5,-0.5,-0.5);
  253.     cube(0,1,0);
  254.     glPopMatrix();
  255.     // upper 2
  256.     glPushMatrix();
  257.     glTranslatef(-length/2 -width/2,length,length/2+width/2);
  258.     glScalef(width,length,width);
  259.     glTranslatef(-0.5,-0.5,-0.5);
  260.     cube(0,1,0);
  261.     glPopMatrix();
  262.  
  263.     // upper close 1
  264.     glPushMatrix();
  265.     glTranslatef(0,length,length/2);
  266.     glScalef(length,length/6,0);
  267.     glTranslatef(-0.5,-0.5,-0.5);
  268.     cube(0,0,1);
  269.     glPopMatrix();
  270.     // upper close 2
  271.     glPushMatrix();
  272.     glTranslatef(0,length+5,length/2);
  273.     glScalef(length,length/6,0);
  274.     glTranslatef(-0.5,-0.5,-0.5);
  275.     cube(1,0,1);
  276.     glPopMatrix();
  277.  
  278.     // upper close 3
  279.     glPushMatrix();
  280.     glTranslatef(0,length-5,length/2);
  281.     glScalef(length,length/6,0);
  282.     glTranslatef(-0.5,-0.5,-0.5);
  283.     cube(1,0,1);
  284.     glPopMatrix();
  285.  
  286.  
  287.  
  288.  
  289. }
  290.  
  291.  
  292.  
  293.  
  294. void table()
  295. {
  296.     float length=20;
  297.     float width=1;
  298.  
  299.     //base
  300.     glPushMatrix();
  301.     glTranslatef(0,length/2,0);
  302.     glScalef(length,width+1,length);
  303.     glTranslatef(-0.5,-0.5,-0.5);
  304.     cube();
  305.     glPopMatrix();
  306.  
  307.     // base 1
  308.     glPushMatrix();
  309.     glTranslatef(length/2 -width/2,0,length/2-width/2);
  310.     glScalef(width,length,width);
  311.     glTranslatef(-0.5,-0.5,-0.5);
  312.     cube(1,0,0);
  313.     glPopMatrix();
  314.     // base 2
  315.     glPushMatrix();
  316.     glTranslatef(length/2 -width/2,0,- length/2 +width/2);
  317.     glScalef(width,length,width);
  318.     glTranslatef(-0.5,-0.5,-0.5);
  319.     cube(1,0,0);
  320.     glPopMatrix();
  321.     // base 3
  322.     glPushMatrix();
  323.     glTranslatef(-length/2 +width/2,0,+ length/2 -width/2);
  324.     glScalef(width,length,width);
  325.     glTranslatef(-0.5,-0.5,-0.5);
  326.     cube(1,0,0);
  327.     glPopMatrix();
  328.     // base 4
  329.     glPushMatrix();
  330.     glTranslatef(-length/2 +width/2,0,- length/2 +width/2);
  331.     glScalef(width,length,width);
  332.     glTranslatef(-0.5,-0.5,-0.5);
  333.     cube(1,0,0);
  334.     glPopMatrix();
  335.  
  336.  
  337. }
  338.  
  339. void display(void)
  340. {
  341.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  342.  
  343.     glMatrixMode( GL_PROJECTION );
  344.     glLoadIdentity();
  345.     glFrustum(-5,5,-5,5, 4, 70);
  346.  
  347.     glMatrixMode( GL_MODELVIEW );
  348.     glLoadIdentity();
  349.     gluLookAt(10,10,40, 1,5,1, 0,1,0);
  350.  
  351.     glViewport(0, 0, windowHeight, windowWidth);
  352.     glRotatef(theta,axis_x,axis_y,0);
  353.     glTranslatef(0,0,Tzval);
  354.  
  355.     //axes();
  356.     //wall();
  357.     //wardrobe();
  358.     //chair_table();
  359.     //fan();
  360.     axes();
  361.     wall();
  362.     //table();
  363.     //chair();
  364.     for(int i=-20; i<=20; i+=15)
  365.     {
  366.         glPushMatrix();
  367.         glTranslatef(i,3,10);
  368.         //glRotatef(180,0,1,0);
  369.         glScalef(0.3,0.3,0.3);
  370.         chair();
  371.         glPopMatrix();
  372.  
  373.         glPushMatrix();
  374.         glTranslatef(i,3,0);
  375.         glScalef(0.5,0.5,0.5);
  376.         table();
  377.         glPopMatrix();
  378.     }
  379.  
  380.  
  381.     glFlush();
  382.     glutSwapBuffers();
  383.  
  384. }
  385.  
  386. static void key(unsigned char key, int x, int y)
  387. {
  388.     switch (key)
  389.     {
  390.     case 27 :
  391.     case 'q':
  392.         exit(0);
  393.         break;
  394.  
  395.     case 'l': // left
  396.         theta++;
  397.         axis_y=1;
  398.         axis_x=0;
  399.         break;
  400.     case 'r': // right
  401.         theta--;
  402.         axis_y=1;
  403.         axis_x=0;
  404.         break;
  405.     case 'd': // down
  406.         theta++;
  407.         axis_x=1;
  408.         axis_y=0;
  409.         break;
  410.     case 'u': // up
  411.         theta--;
  412.         axis_x=1;
  413.         axis_y=0;
  414.     case 'z':
  415.         flagRot=true;
  416.         break;
  417.     case 'x':
  418.         flagRot=false;
  419.         break;
  420.     case '+':
  421.         Tzval+=0.2;
  422.         break;
  423.     case '-':
  424.         Tzval-=0.2;
  425.         break;
  426.     }
  427.  
  428.     glutPostRedisplay();
  429. }
  430.  
  431. static void idle(void)
  432. {
  433.     if (flagRot == true)
  434.     {
  435.         f_spin = f_spin + 0.1;
  436.         if(f_spin > 360)
  437.             f_spin = 0;
  438.     }
  439.     glutPostRedisplay();
  440. }
  441.  
  442. int main(int argc, char**argv)
  443. {
  444.     glutInit(&argc,argv);
  445.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  446.     glutInitWindowPosition(200,200);
  447.     glutInitWindowSize(700,700);
  448.     glutCreateWindow("Classroom 3D");
  449.     cout<<"Up: u"<<endl;
  450.     cout<<"Down: d"<<endl;
  451.     cout<<"Left: l"<<endl;
  452.     cout<<"Right: r"<<endl;
  453.     cout<<"Zoom In: +"<<endl;
  454.     cout<<"Zoom Out: -"<<endl;
  455.     cout<<"Fan Rotate start: z"<<endl;
  456.     cout<<"Fan Rotate stop: x"<<endl;
  457.     cout<<"Quit: q"<<endl;
  458.     glShadeModel(GL_SMOOTH);
  459.     glEnable(GL_DEPTH_TEST);
  460.     glutDisplayFunc(display);
  461.     glutKeyboardFunc(key);
  462.     glutIdleFunc(idle);
  463.     glutMainLoop();
  464. }
  465.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement