Xom9ik

Lab_10/15var (IV semester) CG

May 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include "glut.h"
  4. #include <math.h>
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <malloc.h>
  8. //  Подключает сами библиотеки: opengl32.dll, glu32.dll и glut32.dll
  9. #pragma comment(lib, "opengl32.lib")
  10. #pragma comment(lib, "glu32.lib")
  11. #pragma comment(lib, "glut32.lib")
  12.  
  13. #define GL_BGR 0x80E0
  14. #define coord1 glTexCoord2d(0,0);
  15. #define coord2 glTexCoord2d(0,1);
  16. #define coord3 glTexCoord2d(1,1);
  17. #define coord4 glTexCoord2d(1,0);
  18.  
  19.  
  20. static int w = 0, h = 0;
  21. static float Angle = 0.0f;
  22. float scaleX = 150, scaleY = 125, scaleZ = 150;
  23. int x = 0, y = 0, z = 0;
  24. GLuint  textura_id;
  25. int sample = 1;
  26. int angle = 0;
  27. double Width = 800, Height = 800;
  28. struct textura_struct{
  29.     int W;
  30.     int H;
  31.     unsigned char *Image;
  32. }get_textura;
  33.  
  34. int LoadTexture(char *FileName){
  35.     FILE *F;
  36.     if ((F = fopen(FileName, "rb")) == NULL)
  37.         return 0;
  38.     fseek(F, 18, SEEK_SET);
  39.     fread(&(get_textura.W), 2, 1, F);
  40.     fseek(F, 2, SEEK_CUR);
  41.     fread(&(get_textura.H), 2, 1, F);
  42.     printf("%d x %d\n", get_textura.W, get_textura.H);
  43.     if ((get_textura.Image = (unsigned char *)malloc(sizeof(unsigned char)* 3 * get_textura.W * get_textura.H)) == NULL) {
  44.         fclose(F);
  45.         return 0;
  46.     }
  47.     fseek(F, 30, SEEK_CUR);
  48.     fread(get_textura.Image, 3, get_textura.W * get_textura.H, F);
  49.     glGenTextures(1, &textura_id);
  50.     glBindTexture(GL_TEXTURE_2D, textura_id);
  51.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  52.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  53.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, get_textura.W, get_textura.H, GL_BGR, GL_UNSIGNED_BYTE, get_textura.Image);
  54.     free(get_textura.Image);
  55.     fclose(F);
  56.     return 1;
  57. }
  58. void Init(void) {
  59.     glClearColor(0.8f, 0.9f, 0.9f, 1.0f);
  60. }
  61.  
  62. void Display(void) {
  63.     Angle += 0.05f;
  64.     glClear(GL_COLOR_BUFFER_BIT);
  65.     glLoadIdentity();
  66.     gluLookAt(scaleX, scaleY, scaleZ, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  67.     glRotatef((GLfloat)Angle, 0.0f, 1.0f, 0.0f);
  68.     glTranslatef(x, y, z);
  69.     ///////////////////////////
  70.     glEnable(GL_TEXTURE_2D);
  71.     glBindTexture(GL_TEXTURE_2D, textura_id);
  72.  
  73.     glNormal3f(0.0f, 0.0f, 1.0f);
  74.     coord1 glVertex3f(0.0f, -1.0f, 1.0f); // Точка 1 (Перед)
  75.     coord2 glVertex3f(-1.0f, 1.0f, 1.0f); // Точка 2 (Перед)
  76.     coord3 glVertex3f(1.0f, 1.0f, 1.0f); // Точка 3 (Перед)
  77.     coord4 glVertex3f(0.0f, -1.0f, -2.0f);
  78.     glEnd();
  79.  
  80.     glBegin(GL_POLYGON);
  81.     coord1 glVertex3f(30, 0, 30);
  82.     coord2 glVertex3f(30, 60, 30);
  83.     coord3 glVertex3f(0, 60, 30);
  84.     coord4 glVertex3f(0, 0, 30);
  85.     glEnd();
  86.  
  87.     glBegin(GL_POLYGON);
  88.     coord1 glVertex3f(30, 0, 0);
  89.     coord2 glVertex3f(30, 60, 0);
  90.     coord3 glVertex3f(0, 60, 0);
  91.     coord4 glVertex3f(0, 0, 0);
  92.     glEnd();
  93.  
  94.     glBegin(GL_POLYGON);
  95.     coord1; glVertex3f(30, 0, 0);
  96.     coord2 glVertex3f(30, 30, 0);
  97.     coord3 glVertex3f(30, 30, 30);
  98.     coord4 glVertex3f(30, 0, 30);
  99.     glEnd();
  100.  
  101.     glBegin(GL_POLYGON);
  102.     coord1 glVertex3f(0, 0, 30);
  103.     coord2 glVertex3f(0, 60, 30);
  104.     coord3 glVertex3f(0, 60, 0);
  105.     coord4 glVertex3f(0, 0, 0);
  106.     glEnd();
  107.  
  108.     glBegin(GL_POLYGON);
  109.     coord1 glVertex3f(90, 60, 30);
  110.     coord2 glVertex3f(90, 60, 0);
  111.     coord3 glVertex3f(0, 60, 0);
  112.     coord4 glVertex3f(0, 60, 30);
  113.     glEnd();
  114.  
  115.     glBegin(GL_POLYGON);
  116.     coord1 glVertex3f(90, 30, 30);
  117.     coord2 glVertex3f(90, 30, 0);
  118.     coord3 glVertex3f(0, 30, 0);
  119.     coord4 glVertex3f(0, 30, 30);
  120.     glEnd();
  121.  
  122.     glBegin(GL_POLYGON);
  123.     coord1 glVertex3f(90, 30, 30);
  124.     coord2 glVertex3f(90, 60, 30);
  125.     coord3 glVertex3f(90, 60, 0);
  126.     coord4 glVertex3f(90, 30, 0);
  127.     glEnd();
  128.  
  129.     glBegin(GL_POLYGON);
  130.     coord1 glVertex3f(90, 30, 0);
  131.     coord2 glVertex3f(90, 60, 0);
  132.     coord3 glVertex3f(0, 60, 0);
  133.     coord4 glVertex3f(0, 30, 0);
  134.     glEnd();
  135.  
  136.     glBegin(GL_POLYGON);
  137.     coord1 glVertex3f(90, 30, 30);
  138.     coord2 glVertex3f(90, 60, 30);
  139.     coord3 glVertex3f(0, 60, 30);
  140.     coord4 glVertex3f(0, 30, 30);
  141.     glEnd();
  142.  
  143.     glBegin(GL_POLYGON);
  144.     coord1 glVertex3f(30, 0, 0);
  145.     coord2 glVertex3f(30, 0, 30);
  146.     coord3 glVertex3f(0, 0, 30);
  147.     coord4 glVertex3f(0, 0, 0);
  148.     glEnd();
  149.  
  150.     glDisable(GL_TEXTURE_2D);
  151.     glFlush();
  152.     glutSwapBuffers();
  153. }
  154.  
  155.  
  156. void Reshape(int width, int height) {
  157.     w = width;
  158.     h = height;
  159.     glViewport(0, 0, w, h);
  160.     glMatrixMode(GL_PROJECTION);
  161.     glLoadIdentity();
  162.     gluPerspective(65.0f, w / h, 1.0f, 1000.0f);
  163.     glMatrixMode(GL_MODELVIEW);
  164.     glLoadIdentity();
  165. }
  166.  
  167.  
  168. void keyboard(unsigned char key, int xx, int yy)
  169. {
  170.     std::cout << "Key pressed: " << key << std::endl;
  171.     switch (key) {
  172.     case 'a':
  173.         std::cout << "!Rotate counter-clockwise" << std::endl;
  174.         Angle += 5.0f;
  175.         glutPostRedisplay();
  176.         break;
  177.     case 'd':
  178.         std::cout << "Rotate Clockwise" << std::endl;
  179.         Angle -= 5.0f;
  180.         glutPostRedisplay();
  181.         break;
  182.     case 'w':
  183.         std::cout << "Offset Left" << std::endl;
  184.         x += 10;
  185.         glutPostRedisplay();
  186.         break;
  187.     case 's':
  188.         std::cout << "Offset Down" << std::endl;
  189.         y += 10;
  190.         glutPostRedisplay();
  191.         break;
  192.     case 'r':
  193.         std::cout << "Zoom in X in 2 times" << std::endl;
  194.         scaleX = 200;
  195.         scaleY = 100;
  196.         scaleZ = 100;
  197.         glutPostRedisplay();
  198.         break;
  199.     case 'f':
  200.         std::cout << "Y-scale reduction in 2 times" << std::endl;
  201.         scaleX = 100;
  202.         scaleY = 50;
  203.         scaleZ = 100;
  204.         glutPostRedisplay();
  205.         break;
  206.     case 'e':
  207.         std::cout << "Normal" << std::endl;
  208.         scaleX = 100;
  209.         scaleY = 100;
  210.         scaleZ = 100;
  211.         glutPostRedisplay();
  212.         break;
  213.     default:
  214.         break;
  215.     }
  216. }
  217.  
  218. int main(int argc, char *argv[]) {
  219.         glutInit(&argc, argv);
  220.         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  221.         glutInitWindowSize(Width, Height);
  222.         glutInitWindowPosition(200, 200);
  223.         glutCreateWindow("Lab 10");
  224.         glClearColor(0.0, 0.0, 0.0, 1.0);
  225.         glutDisplayFunc(Display);
  226.         glutReshapeFunc(Reshape);
  227.         if (LoadTexture((char *)"images5.bmp") != 1)
  228.             std::cout << "Images not load\n";
  229.         else
  230.             std::cout << "Images load\n";
  231.         glutKeyboardFunc(keyboard);
  232.         glutMainLoop();
  233.         return 0;
  234. }
Add Comment
Please, Sign In to add comment