Advertisement
raieed

Graphics Lab 1 assignment

Apr 11th, 2021
1,584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 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.  
  8. int windowWidth = 800;
  9. int windowHeight = 600;
  10.  
  11. int rot = 0;
  12.  
  13. float Txval = 0;
  14. float Tyval = 0;
  15.  
  16. float sval = 1.00;
  17. float rval = 1.00;
  18.  
  19. bool flagScale = false;
  20. bool firstFlag = true;
  21. bool lastFlag = false;
  22.  
  23. void triangle()
  24. {
  25.     glBegin(GL_TRIANGLES);
  26.     // Define a group of vertices that define one or more primitives
  27.         glColor3f(1.0, 1.0, 1.0);
  28.         glVertex2f(2.0, 2.0);
  29.         glColor3f(0.0, 1.0, 1.0);
  30.         glVertex2f(2.0, 0.0);
  31.         glColor3f(0.0, 1.0, 0.0);
  32.         glVertex2f(0.0, 0.0);
  33.     glEnd();
  34.     // Terminates a list of vertices that specifies a primitive initiated by glBegin
  35. }
  36.  
  37. void rectangle()
  38. {
  39.     glBegin(GL_QUADS);
  40.  
  41.         glColor3f(0.0, 1.0, 0.0);
  42.         glVertex2f(1.0, 2.0);
  43.         glColor3f(0.0, 1.0, 0.0);
  44.         glVertex2f(0.0, 2.0);
  45.         glColor3f(1.0, 0.0, 0.0);
  46.         glVertex2f(0.0, 0.0);
  47.         glColor3f(1.0, 0.0, 0.0);
  48.         glVertex2f(1.0, 0.0);
  49.     glEnd();
  50. }
  51.  
  52. void cube()
  53. {
  54.     glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
  55.     // Top face (y = 1.0f)
  56.     // Define vertices in counter-clockwise (CCW) order with normal pointing out
  57.  
  58.   glColor3f(0.0f, 1.0f, 0.0f);     // Green
  59.       glVertex3f( 1.0f, 1.0f, -1.0f);
  60.       glVertex3f(-1.0f, 1.0f, -1.0f);
  61.       glVertex3f(-1.0f, 1.0f,  1.0f);
  62.       glVertex3f( 1.0f, 1.0f,  1.0f);
  63.  
  64.       // Bottom face (y = -1.0f)
  65.       glColor3f(1.0f, 0.5f, 0.0f);     // Orange
  66.       glVertex3f( 1.0f, -1.0f,  1.0f);
  67.       glVertex3f(-1.0f, -1.0f,  1.0f);
  68.       glVertex3f(-1.0f, -1.0f, -1.0f);
  69.       glVertex3f( 1.0f, -1.0f, -1.0f);
  70.  
  71.       // Front face  (z = 1.0f)
  72.       glColor3f(1.0f, 0.0f, 0.0f);     // Red
  73.       glVertex3f( 1.0f,  1.0f, 1.0f);
  74.       glVertex3f(-1.0f,  1.0f, 1.0f);
  75.       glVertex3f(-1.0f, -1.0f, 1.0f);
  76.       glVertex3f( 1.0f, -1.0f, 1.0f);
  77.  
  78.       // Back face (z = -1.0f)
  79.       glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
  80.       glVertex3f( 1.0f, -1.0f, -1.0f);
  81.       glVertex3f(-1.0f, -1.0f, -1.0f);
  82.       glVertex3f(-1.0f,  1.0f, -1.0f);
  83.       glVertex3f( 1.0f,  1.0f, -1.0f);
  84.  
  85.       // Left face (x = -1.0f)
  86.       glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  87.       glVertex3f(-1.0f,  1.0f,  1.0f);
  88.       glVertex3f(-1.0f,  1.0f, -1.0f);
  89.       glVertex3f(-1.0f, -1.0f, -1.0f);
  90.       glVertex3f(-1.0f, -1.0f,  1.0f);
  91.  
  92.       // Right face (x = 1.0f)
  93.       glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
  94.       glVertex3f(1.0f,  1.0f, -1.0f);
  95.       glVertex3f(1.0f,  1.0f,  1.0f);
  96.       glVertex3f(1.0f, -1.0f,  1.0f);
  97.       glVertex3f(1.0f, -1.0f, -1.0f);
  98.  
  99.  
  100.  
  101.     glEnd();
  102.  
  103. }
  104.  
  105. void display_first(void)
  106. {
  107.  
  108.         glMatrixMode(GL_MODELVIEW);
  109.         glLoadIdentity();
  110.  
  111.         glViewport(0, 0, windowWidth/2, windowHeight/2);
  112.  
  113.         rectangle();
  114. }
  115.  
  116. void myKeyboardFunc( unsigned char key, int x, int y)
  117. {
  118.     switch(key)
  119.     {
  120.     case '2':
  121.         flagScale=true;
  122.         firstFlag=false;
  123.         lastFlag=false;
  124.         break;
  125.     case '1':
  126.         flagScale=false;
  127.         firstFlag=true;
  128.         lastFlag=false;
  129.         break;
  130.     case '3':
  131.         flagScale=false;
  132.         firstFlag=false;
  133.         lastFlag=true;
  134.         break;
  135.     case 27:
  136.         exit(1);
  137.     }
  138.     glutPostRedisplay();
  139. }
  140.  
  141. void animate()
  142. {
  143.     if(flagScale)
  144.     {
  145.         rval += 0.5;
  146.         if(rval >= 359)
  147.             rval = 1.0;
  148.     }
  149.  
  150.     glutPostRedisplay();
  151. }
  152.  
  153. void translate()
  154. {
  155.     glMatrixMode(GL_MODELVIEW);
  156.  
  157.     glTranslatef(0.0, 0.0, 0.0);
  158. }
  159.  
  160. void display(void)
  161. {
  162.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  163.  
  164.         glMatrixMode(GL_PROJECTION);
  165.         glLoadIdentity();
  166.         glOrtho(-3, 3, -3, 3, -3, 10);
  167.  
  168.         glMatrixMode(GL_MODELVIEW);
  169.         glLoadIdentity();
  170.  
  171.     if(flagScale)
  172.     {
  173.         glPushMatrix();
  174.         glRotatef(rval, 0, 0, 1);
  175.         triangle();
  176.         glPopMatrix();
  177.     }
  178.  
  179.     if(firstFlag)
  180.     {
  181.         glPushMatrix();
  182.         rectangle();
  183.         glPopMatrix();
  184.     }
  185.  
  186.     if(lastFlag)
  187.     {
  188.         glPushMatrix();
  189.         glRotatef(45,0,1,0);
  190.         glRotatef(45,1,0,0);
  191.         glRotatef(rot,1,0,1);
  192.         cube();
  193.         glPopMatrix();
  194.     }
  195.  
  196.     glFlush();
  197.     glutSwapBuffers();
  198. }
  199.  
  200. int main(int argc, char **argv)
  201. {
  202.     glutInit(&argc, argv);
  203.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  204.  
  205.     glutInitWindowPosition(100, 100);
  206.     glutInitWindowSize(windowWidth/2, windowHeight/2);
  207.     glutCreateWindow("Lab-1 assignment");
  208.  
  209.     glShadeModel(GL_SMOOTH);
  210.     glEnable(GL_DEPTH_TEST);
  211.  
  212.     glutDisplayFunc(display);
  213.  
  214.     glutIdleFunc(animate);
  215.  
  216.     glutKeyboardFunc(myKeyboardFunc);
  217.  
  218.     glutMainLoop();
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement