Advertisement
Guest User

Lab6 save

a guest
May 26th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.56 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  GLFW Lab 3.1
  4. //
  5. //  Created by Кирилл Снегур on 19.03.2019.
  6. //  Copyright © 2019 ALLMIGHT DEVELOPMENT. All rights reserved.
  7. //
  8. //
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <iostream>
  13. #include <sstream>
  14. #include <fstream>
  15. #include <GL/glew.h>
  16. #include <GLFW/glfw3.h>
  17. #include <cmath>
  18. #include <math.h>
  19.  
  20. using namespace std;
  21.  
  22. int WIDTH = 800, HEIGHT = 800;
  23.  
  24. float x = 0, y = 0, z = 0;
  25. float RotateX = 0.0f, RotateY = 0.0f, RotateZ = 0.0f;
  26. float PositionX = 1.0f, PositionY = 1.0f, PositionZ = 1.0f;
  27.  
  28. int circleVelue = 30, triangleValue =20;
  29.  
  30. GLfloat PologonModeValue = GL_LINE;
  31.  
  32. GLfloat phi = asin(0.25 / sqrt(2.0));
  33. GLfloat teta = asin(0.25 / sqrt(2.0 - 0.25 * 0.25));
  34.  
  35. GLfloat TransformationMatrix[4][4] = {
  36.     {1, 0, 0, 0},
  37.     {0, 1, 0, 0},
  38.     {0, 0, 1, 0},
  39.     {0, 0, 0, 1},
  40. };
  41.  
  42.  
  43. static double PI = 3.1415926535897932384626433832795;
  44.  
  45. struct RGBA {
  46.     float r, g, b, a;
  47. };
  48.  
  49. bool lighting = true;
  50. RGBA globalAmbient { 0.5f, 0.5f, 0.5f, 1 };
  51. RGBA Ambient = { 0.5, 0.5, 0.5, 1 };
  52. RGBA Diffuse = { 0.5, 0.5, 0.5, 1 };
  53. RGBA Specular = { 0.5, 0.5, 0.5, 1 };
  54. float Shininess = 4.f;
  55.  
  56. bool texture = true;
  57. int textureValue = 0;
  58.  
  59. int getTextureValue(const char *path) {
  60.     ifstream texture(path);
  61.     ostringstream bufferStream;
  62.     bufferStream << texture.rdbuf();
  63.     const char* binareTexture = bufferStream.str().c_str();
  64.     texture.close();
  65.    
  66.     // расположенние данных в файле
  67.     int width = *(int*)&(binareTexture[0x12]);
  68.     int height = *(int*)&(binareTexture[0x16]);
  69.     //int dataPos = *(int*)&(binareTexture[0x0A]);
  70.     //dataPos = dataPos == 0 ? 16 : dataPos;
  71.    
  72.     GLuint textureVal;
  73.     glGenTextures(1, &textureVal);
  74.     glBindTexture(GL_TEXTURE_2D, textureVal);
  75.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  76.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  77.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  78.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  79.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, binareTexture /*+ dataPos*/);
  80.     glBindTexture(GL_TEXTURE_2D, 0);
  81.     return textureVal;
  82. }
  83.  
  84.  
  85.  
  86. void framebuffer_size_callback(GLFWwindow* window, int width, int height){
  87.     if(width>height)
  88.     {
  89.         glViewport((width-height)/2, 0, std::min(width,height), std::min(width,height));
  90.     }
  91.     else
  92.         glViewport(0, (height-width)/2, std::min(width,height), std::min(width,height));
  93. };
  94.  
  95. void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mode){
  96.     GLfloat step = 0.05f;
  97.     GLfloat rotateStep = 5.0f;
  98.     GLfloat valueStep = 1.0f;
  99.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  100.         glfwSetWindowShouldClose(window, GL_TRUE);
  101.    
  102.     if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  103.         switch (key) {
  104.             case GLFW_KEY_D:
  105.                 RotateX -= rotateStep;
  106.                 break;
  107.             case GLFW_KEY_A:
  108.                 RotateX += rotateStep;
  109.                 break;
  110.             case GLFW_KEY_S:
  111.                 RotateY -= rotateStep;
  112.                 break;
  113.             case GLFW_KEY_W:
  114.                 RotateY += rotateStep;
  115.                 break;
  116.             case GLFW_KEY_Q:
  117.                 RotateZ += rotateStep;
  118.                 break;
  119.             case GLFW_KEY_E:
  120.                 RotateZ -= rotateStep;
  121.                 break;
  122.             case GLFW_KEY_LEFT:
  123.                 x -= step;
  124.                 break;
  125.             case GLFW_KEY_RIGHT:
  126.                 x += step;
  127.                 break;
  128.             case GLFW_KEY_UP:
  129.                 y += step;
  130.                 break;
  131.             case GLFW_KEY_DOWN:
  132.                 y -= step;
  133.                 break;
  134.             case GLFW_KEY_C:
  135.                 circleVelue -= valueStep;
  136.                 break;
  137.             case GLFW_KEY_V:
  138.                 circleVelue += valueStep;
  139.                 break;
  140.             case GLFW_KEY_R:
  141.                 triangleValue -= valueStep;
  142.                 break;
  143.             case GLFW_KEY_F:
  144.                 triangleValue += valueStep;
  145.                 break;
  146.             case GLFW_KEY_1:
  147.                 PologonModeValue = GL_FILL;
  148.                 break;
  149.             case GLFW_KEY_2:
  150.                 PologonModeValue = GL_LINE;
  151.                 break;
  152.             case GLFW_KEY_T:
  153.                 texture = !texture;
  154.                 break;
  155.             case GLFW_KEY_P:
  156.                 globalAmbient.r += .1f;
  157.                 break;
  158.             case GLFW_KEY_L:
  159.                 globalAmbient.r -= .1f;
  160.                 break;
  161.             case GLFW_KEY_LEFT_BRACKET:
  162.                 globalAmbient.g += .1f;
  163.                 break;
  164.             case GLFW_KEY_SEMICOLON:
  165.                 globalAmbient.g -= .1f;
  166.                 break;
  167.             case GLFW_KEY_RIGHT_BRACKET:
  168.                 globalAmbient.b += .1f;
  169.                 break;
  170.             case GLFW_KEY_APOSTROPHE:
  171.                 globalAmbient.b -= .1f;
  172.                 break;
  173.         }
  174.     }
  175. }
  176.  
  177. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){
  178.     PositionX += 0.5 * yoffset;
  179.     PositionY += 0.5 * yoffset;
  180.     PositionZ += 0.5 * yoffset;
  181. }
  182.  
  183. void Draw_Cube(GLfloat size){
  184.     glBegin(GL_QUADS);
  185.  
  186.     glVertex3f( -size / 2, -size / 2, -size / 2);
  187.     glVertex3f( -size / 2,  size / 2, -size / 2);
  188.     glVertex3f( -size / 2,  size / 2,  size / 2);
  189.     glVertex3f( -size / 2, -size / 2,  size / 2);
  190.  
  191.     glVertex3f(  size / 2, -size / 2, -size / 2);
  192.     glVertex3f(  size / 2, -size / 2,  size / 2);
  193.     glVertex3f(  size / 2,  size / 2,  size / 2);
  194.     glVertex3f(  size / 2,  size / 2, -size / 2);
  195.  
  196.     glVertex3f( -size / 2, -size / 2, -size / 2);
  197.     glVertex3f( -size / 2, -size / 2,  size / 2);
  198.     glVertex3f(  size / 2, -size / 2,  size / 2);
  199.     glVertex3f(  size / 2, -size / 2, -size / 2);
  200.  
  201.     glVertex3f( -size / 2, size / 2, -size / 2);
  202.     glVertex3f( -size / 2, size / 2,  size / 2);
  203.     glVertex3f(  size / 2, size / 2,  size / 2);
  204.     glVertex3f(  size / 2, size / 2, -size / 2);
  205.  
  206.     glVertex3f( -size / 2, -size / 2, -size / 2);
  207.     glVertex3f(  size / 2, -size / 2, -size / 2);
  208.     glVertex3f(  size / 2,  size / 2, -size / 2);
  209.     glVertex3f( -size / 2,  size / 2, -size / 2);
  210.  
  211.     glVertex3f( -size / 2, -size / 2,  size / 2);
  212.     glVertex3f(  size / 2, -size / 2,  size / 2);
  213.     glVertex3f(  size / 2,  size / 2,  size / 2);
  214.     glVertex3f( -size / 2,  size / 2,  size / 2);
  215.     glEnd();
  216. }
  217.  
  218.  
  219. void Draw_Tor(int roundValue, int shapesValue, float externalRadDelta, float interiorRadDelta) {
  220.  
  221.     glMatrixMode(GL_MODELVIEW);
  222.     glLoadIdentity();
  223.     glTranslatef(x, y, z);
  224.  
  225.     glRotatef(RotateX, 0, 1, 0);
  226.     glRotatef(RotateY, 1, 0, 0);
  227.     glRotatef(RotateZ, 0, 0, 1);
  228.  
  229.     glScaled(PositionX, PositionY, PositionZ);
  230.  
  231.     glPolygonMode(GL_FRONT_AND_BACK, PologonModeValue);
  232.  
  233.  
  234.     glMaterialfv(GL_FRONT, GL_AMBIENT, (float*)&Ambient);
  235.     glMaterialfv(GL_FRONT, GL_DIFFUSE, (float*)&Diffuse);
  236.     glMaterialfv(GL_FRONT, GL_SPECULAR, (float*)&Specular);
  237.     glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
  238.     glEnable(GL_NORMALIZE);
  239.    
  240.     glEnable(GL_DEPTH_TEST);
  241.     glEnable(GL_LIGHT0);
  242.     glEnable(GL_BLEND);
  243.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  244.  
  245.  
  246.     double externalRad = 2.0 * PI / roundValue;
  247.     double interiorRad = 2.0 * PI / shapesValue;
  248.  
  249.  
  250.     for (int i = 0; i < roundValue; i++) {
  251.         double stepOne = i * externalRad;
  252.         double stepTwo = stepOne + externalRad;
  253.  
  254.         GLdouble HorizontalStepOne = cos(stepOne);
  255.         GLdouble VerticalStepOne = sin(stepOne);
  256.         GLdouble HorizontalStepTwo = cos(stepTwo);
  257.         GLdouble VerticalStepTwo = sin(stepTwo);
  258.        
  259.  
  260.         glBegin(GL_TRIANGLE_STRIP);
  261.  
  262.         for (int j = 0; j <= shapesValue; j++) {
  263.             double b = j * interiorRad;
  264.  
  265.             //GLdouble c = cos(b);
  266.            // GLdouble r = interiorRadDelta * c + externalRadDelta;
  267.             //GLdouble z = interiorRadDelta * sin(b);
  268.  
  269.             GLdouble externalRadValue = interiorRadDelta * cos(b) + externalRadDelta;
  270.             GLdouble interiorRadValue = interiorRadDelta * sin(b);
  271.  
  272.             glNormal3d(-HorizontalStepOne * cos(b), -VerticalStepOne * cos(b), -interiorRadValue / interiorRadDelta);
  273.             glTexCoord2d(i / (GLdouble) roundValue, j / (GLdouble) shapesValue);
  274.             glVertex3d(HorizontalStepTwo * externalRadValue, VerticalStepTwo * externalRadValue, interiorRadValue);
  275.  
  276.             glNormal3d(-HorizontalStepTwo * cos(b), -VerticalStepTwo * cos(b), -interiorRadValue / interiorRadDelta);
  277.             glTexCoord2d((i + 1) / (GLdouble) roundValue, j / (GLdouble) shapesValue);
  278.             glVertex3d(HorizontalStepOne * externalRadValue, VerticalStepOne * externalRadValue, interiorRadValue);
  279.  
  280.         }
  281.         glEnd();
  282.     }
  283.    
  284.  
  285. }
  286.  
  287.  
  288.  
  289. void Draw_dimetrialProection() {
  290.    
  291.     glMatrixMode(GL_MODELVIEW);
  292.     glLoadIdentity();
  293.     glRotatef(-phi*(180/3.14) , 1.0, 0.0, 0.0);
  294.     glRotatef(-teta*(180/3.14) , 0.0, 1.0, 0.0);
  295.     glTranslatef(-0.9, -0.7, -0.5);
  296.    
  297.     glScaled(0.2, 0.2, 0.2);
  298.     Draw_Cube(1);
  299. }
  300.  
  301. int main(void) {
  302.     if (!glfwInit()) {
  303.         return -1;
  304.     }
  305.    
  306.     GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Lab6", NULL, NULL);
  307.     glfwMakeContextCurrent(window);
  308.    
  309.     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  310.     glfwSetKeyCallback(window, keyCallback);
  311.     glfwSetScrollCallback(window, scroll_callback);
  312.    
  313.     textureValue = getTextureValue("/Users/kir_snegir/Desktop/Graphics Labs/Lab 3/Lab 3/Red_Bricks.bmp");
  314.  
  315.     while (!glfwWindowShouldClose(window)) {
  316.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  317.        
  318.         glMatrixMode(GL_PROJECTION);
  319.         glLoadIdentity();
  320.         glMultMatrixf(*TransformationMatrix);
  321.        
  322.         if (lighting) {
  323.             glEnable(GL_LIGHTING);
  324.             glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (float*)&globalAmbient);
  325.         }
  326.        
  327.         if (texture) {
  328.             glEnable(GL_TEXTURE_2D);
  329.             glBindTexture(GL_TEXTURE_2D, textureValue);
  330.             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  331.         }
  332.        
  333.         Draw_dimetrialProection();
  334.    
  335.         Draw_Tor(circleVelue,triangleValue, 0.2f , 0.1f);
  336.        
  337.         glDisable(GL_TEXTURE_2D);
  338.         glLoadIdentity();
  339.        
  340.         glfwPollEvents();
  341.         glfwSwapBuffers(window);
  342.     }
  343.    
  344.     glfwDestroyWindow(window);
  345.     glfwTerminate();
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement