Advertisement
Xom9ik

Lab_7/15var (IV semester) CG

Apr 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <iostream>
  4. #include "glut.h"
  5.  
  6. static int w = 0, h = 0;
  7. static float Angle = 0.0f;
  8. float scaleX = 100, scaleY = 100, scaleZ = 100;
  9. int x = 0, y = 0, z = 0;
  10. void Init(void) {
  11.     glClearColor(0.8f, 0.9f, 0.9f, 1.0f);
  12. }
  13.  
  14. void Update(void) {
  15.     Angle += 0.05f;
  16.     glClear(GL_COLOR_BUFFER_BIT);
  17.     glLoadIdentity();
  18.  
  19.     gluLookAt(scaleX, scaleY, scaleZ, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  20.  
  21.     glRotatef((GLfloat)Angle, 0.0f, 1.0f, 0.0f);
  22.     glTranslatef(x, y, z);
  23.     glBegin(GL_TRIANGLES);
  24.     //1
  25.     glColor3f(0.9f, 0.2f, 0.5f);
  26.     glVertex3f(-75.0f, 0.0f, -50.0f);
  27.     glVertex3f(-75.0f, 0.0f, 50.0f);
  28.     glVertex3f(75.0f, 0.0f, 50.0f);
  29.     //2
  30.     glColor3f(0.5f, 0.2f, 0.9f);
  31.     glVertex3f(-75.0f, 0.0f, -50.0f);
  32.     glVertex3f(75.0f, 0.0f, -50.0f);
  33.     glVertex3f(75.0f, 0.0f, 50.0f);
  34.     glEnd();
  35.  
  36.    
  37.     glLineWidth(3.0f);
  38.  
  39.     //Green y
  40.     glBegin(GL_LINE_STRIP);
  41.     glColor3ub(0, 255, 0);
  42.  
  43.     glVertex3f(-75, 0, -50);
  44.     glVertex3f(-75, 3000, -50);
  45.     glEnd();
  46.  
  47.     //Red x
  48.     glBegin(GL_LINE_STRIP);
  49.     glColor3ub(255, 0, 0);
  50.  
  51.     glVertex3f(-75, 0, -50);
  52.     glVertex3f(3000, 0, -50);
  53.     glEnd();
  54.  
  55.     //Blue z
  56.     glBegin(GL_LINE_STRIP);
  57.     glColor3ub(0, 0, 255);
  58.  
  59.     glVertex3f(-75, 0, -50);
  60.     glVertex3f(-75, 0, 300);
  61.     glEnd();
  62.  
  63.     glLineWidth(10.0f);
  64.  
  65.     //Black
  66.     glBegin(GL_LINE_STRIP);
  67.     glColor3ub(0, 0, 0);
  68.     glVertex3f(-75, 50, -30);
  69.     glVertex3f(90, 50, -30);
  70.     glEnd();
  71.  
  72.     //Blue
  73.     glEnable(GL_LINE_STIPPLE);
  74.     glLineStipple(2, 0x00FF);
  75.     glColor3ub(0, 0, 255);
  76.     glBegin(GL_LINE_LOOP);
  77.     glVertex3f(-75, 70, -30);
  78.     glVertex3f(90, 0, -30);
  79.     glEnd();
  80.     glDisable(GL_LINE_STIPPLE);
  81.     ///////////////////////////
  82.  
  83.     glBegin(GL_POLYGON);
  84.     glColor3f(255, 0, 0);
  85.     glVertex3f(30, 0, 30);
  86.     glVertex3f(30, 60, 30);
  87.     glVertex3f(0, 60, 30);
  88.     glVertex3f(0, 0, 30);
  89.     glEnd();
  90.  
  91.     glBegin(GL_POLYGON);
  92.     glColor3f(255, 0, 0);
  93.     glVertex3f(30, 0, 0);
  94.     glVertex3f(30, 60, 0);
  95.     glVertex3f(0, 60, 0);
  96.     glVertex3f(0, 0, 0);
  97.     glEnd();
  98.  
  99.     glBegin(GL_POLYGON);
  100.     glColor3f(255, 0, 0);
  101.     glVertex3f(30, 0, 0);
  102.     glVertex3f(30, 30, 0);
  103.     glVertex3f(30, 30, 30);
  104.     glVertex3f(30, 0, 30);
  105.     glEnd();
  106.  
  107.     glBegin(GL_POLYGON);
  108.     glColor3f(255, 0, 0);
  109.     glVertex3f(0, 0, 30);
  110.     glVertex3f(0, 60, 30);
  111.     glVertex3f(0, 60, 0);
  112.     glVertex3f(0, 0, 0);
  113.     glEnd();
  114.  
  115.     glBegin(GL_POLYGON);
  116.     glColor3f(255, 0, 0);
  117.     glVertex3f(90, 60, 30);
  118.     glVertex3f(90, 60, 0);
  119.     glVertex3f(0, 60, 0);
  120.     glVertex3f(0, 60, 30);
  121.     glEnd();
  122.     //
  123.     glBegin(GL_POLYGON);
  124.     glColor3f(255, 0, 0);
  125.     glVertex3f(90, 30, 30);
  126.     glVertex3f(90, 30, 0);
  127.     glVertex3f(0, 30, 0);
  128.     glVertex3f(0, 30, 30);
  129.     glEnd();
  130.  
  131.     glBegin(GL_POLYGON);
  132.     glColor3f(255, 0, 0);
  133.     glVertex3f(90, 30, 30);
  134.     glVertex3f(90, 60, 30);
  135.     glVertex3f(90, 60, 0);
  136.     glVertex3f(90, 30, 0);
  137.     glEnd();
  138.  
  139.     glBegin(GL_POLYGON);
  140.     glColor3f(255, 0, 0);
  141.     glVertex3f(90, 30, 0);
  142.     glVertex3f(90, 60, 0);
  143.     glVertex3f(0, 60, 0);
  144.     glVertex3f(0, 30, 0);
  145.     glEnd();
  146.  
  147.     glBegin(GL_POLYGON);
  148.     glColor3f(255, 0, 0);
  149.     glVertex3f(90, 30, 30);
  150.     glVertex3f(90, 60, 30);
  151.     glVertex3f(0, 60, 30);
  152.     glVertex3f(0, 30, 30);
  153.     glEnd();
  154.  
  155.     glBegin(GL_POLYGON);
  156.     glColor3f(255, 0, 0);
  157.     glVertex3f(30, 0, 0);
  158.     glVertex3f(30, 0, 30);
  159.     glVertex3f(0, 0, 30);
  160.     glVertex3f(0, 0, 0);
  161.     glEnd();
  162.  
  163.     glFlush();
  164.     glutSwapBuffers();
  165. }
  166.  
  167.    
  168. void Reshape(int width, int height) {
  169.     w = width;
  170.     h = height;
  171.     glViewport(0, 0, w, h);
  172.     glMatrixMode(GL_PROJECTION);
  173.     glLoadIdentity();
  174.     gluPerspective(65.0f, w / h, 1.0f, 1000.0f);
  175.     glMatrixMode(GL_MODELVIEW);
  176.     glLoadIdentity();
  177.  
  178. }
  179.  
  180.  
  181. void keyboard(unsigned char key, int xx, int yy)
  182. {
  183.     std::cout << "Key pressed: " << key << std::endl;
  184.     switch (key) {
  185.     case 'a':
  186.         std::cout << "!Rotate counter-clockwise" << std::endl;
  187.         Angle += 5.0f;
  188.         glutPostRedisplay();
  189.         break;
  190.     case 'd':
  191.         std::cout << "Rotate Clockwise" << std::endl;
  192.         Angle -= 5.0f;
  193.         glutPostRedisplay();
  194.         break;
  195.     case 'w':  
  196.         std::cout << "Offset Left" << std::endl;
  197.         x += 10;
  198.         glutPostRedisplay();
  199.         break;
  200.     case 's':
  201.         std::cout << "Offset Down" << std::endl;
  202.         y += 10;
  203.         glutPostRedisplay();
  204.         break;
  205.     case 'r':
  206.         std::cout << "Zoom in X in 2 times" << std::endl;
  207.         scaleX = 200;
  208.         scaleY = 100;
  209.         scaleZ = 100;
  210.         glutPostRedisplay();
  211.         break;
  212.     case 'f':
  213.         std::cout << "Y-scale reduction in 2 times" << std::endl;
  214.         scaleX = 100;
  215.         scaleY = 50;
  216.         scaleZ = 100;
  217.         glutPostRedisplay();
  218.         break;
  219.     case 'e':
  220.         std::cout << "Normal" << std::endl;
  221.         scaleX = 100;
  222.         scaleY = 100;
  223.         scaleZ = 100;
  224.         glutPostRedisplay();
  225.         break;
  226.     default:
  227.         break;
  228.     }
  229. }
  230.  
  231. int main(int argc, char *argv[]) {
  232.     glutInit(&argc, argv);
  233.     glutInitWindowSize(800, 600);
  234.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  235.     glutCreateWindow("Computer Graphics #7");
  236.     glutDisplayFunc(Update);
  237.     glutReshapeFunc(Reshape);
  238.     glutKeyboardFunc(keyboard);
  239.     Init();
  240.     glutMainLoop();
  241.     return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement