Advertisement
Margoshinka

3

Feb 26th, 2023
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <chrono>
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/matrix_transform.hpp>
  7. #include <glm/gtx/transform.hpp>
  8. #include <glm/vec3.hpp> // vec3
  9. #include <glm/vec4.hpp> // vec4
  10. #include <glm/mat4x4.hpp>
  11.  
  12. #pragma comment(lib, "glfw3.lib")
  13. #pragma comment(lib, "glew32.lib")
  14. #pragma comment(lib, "opengl32.lib")
  15.  
  16. using namespace std;
  17. using namespace glm;
  18. mat4 ModelObj = mat4(1.0f);
  19.  
  20. //const int Tess_N = 128;
  21.  
  22.  
  23. const GLint n_size = 10;
  24. //Точка отсчета
  25. GLfloat x0 = -(GLfloat)n_size / 2.0; //x
  26. GLfloat z0 = x0;//z
  27. //Шаг
  28. GLfloat dx = 0.1; //по x
  29. GLfloat dz = dx; //по z
  30. //Количество ячеек
  31. GLint Tess_N = n_size / dx;
  32.  
  33. chrono::time_point<chrono::system_clock> g_callTime;
  34. GLFWwindow* g_window;
  35. GLint g_uMVP;
  36. GLint g_uMV;
  37. GLuint g_shaderProgram;
  38.  
  39. class Model
  40. {
  41. public:
  42.     GLuint vbo;
  43.     GLuint ibo;
  44.     GLuint vao;
  45.     GLsizei indexCount;
  46. };
  47.  
  48. class MyMatrix {
  49.     float m_mat[16];
  50. public:
  51.     static MyMatrix translation(float x, float y, float z) { //матрица переноса
  52.         MyMatrix mat;
  53.         mat.m_mat[0] = 1.0f; mat.m_mat[1] = 0.0f; mat.m_mat[2] = 0.0f; mat.m_mat[3] = 0.0f;
  54.         mat.m_mat[4] = 0.0f; mat.m_mat[5] = 1.0f; mat.m_mat[6] = 0.0f; mat.m_mat[7] = 0.0f;
  55.         mat.m_mat[8] = 0.0f; mat.m_mat[9] = 0.0f; mat.m_mat[10] = 1.0f; mat.m_mat[11] = 0.0f;
  56.         mat.m_mat[12] = x; mat.m_mat[13] = y; mat.m_mat[14] = z; mat.m_mat[15] = 1.0f;
  57.         return mat;
  58.     }
  59.     static MyMatrix xRotation(float a) {
  60.         float cosA = cos(a);
  61.         float sinA = sin(a);
  62.         MyMatrix mat;
  63.         mat.m_mat[0] = 1.0f; mat.m_mat[1] = 0.0f; mat.m_mat[2] = 0.0f; mat.m_mat[3] = 0.0f;
  64.         mat.m_mat[4] = 0.0f; mat.m_mat[5] = cosA; mat.m_mat[6] = sinA; mat.m_mat[7] = 0.0f;
  65.         mat.m_mat[8] = 0.0f; mat.m_mat[9] = -sinA; mat.m_mat[10] = cosA; mat.m_mat[11] = 0.0f;
  66.         mat.m_mat[12] = 0.0f; mat.m_mat[13] = 0.0f; mat.m_mat[14] = 0.0f; mat.m_mat[15] = 1.0f;
  67.         return mat;
  68.     }
  69.     MyMatrix operator * (const MyMatrix& m) {
  70.         MyMatrix mat;
  71.         const float* a = m_mat;
  72.         const float* b = m.m_mat;
  73.         float* c = mat.m_mat;
  74.         for (int i = 0; i < 4; ++i) {
  75.             for (int j = 0; j < 4; ++j) {
  76.                 *c = a[0] * b[j] + a[4] * b[j + 4] + a[8] * b[j + 8] + a[12] * b[j + 12];
  77.                 ++c;
  78.             }
  79.             a += 4;
  80.         }
  81.         return mat;
  82.     }
  83.     static MyMatrix scale(float x, float y, float z) {
  84.         MyMatrix mat;
  85.         mat.m_mat[0] = x;
  86.         mat.m_mat[1] = 0.0f;
  87.         mat.m_mat[2] = 0.0f;
  88.         mat.m_mat[3] = 0.0f;
  89.         mat.m_mat[4] = 0.0f;
  90.         mat.m_mat[5] = y;
  91.         mat.m_mat[6] = 0.0f;
  92.         mat.m_mat[7] = 0.0f;
  93.         mat.m_mat[8] = 0.0f;
  94.         mat.m_mat[9] = 0.0f;
  95.         mat.m_mat[10] = z;
  96.         mat.m_mat[11] = 0.0f;
  97.         mat.m_mat[12] = 0.0f;
  98.         mat.m_mat[13] = 0.0f;
  99.         mat.m_mat[14] = 0.0f;
  100.         mat.m_mat[15] = 1.0f;
  101.         return mat;
  102.     }
  103.     static MyMatrix zRotation(float a)
  104.     {
  105.         MyMatrix m;
  106.         float c = cos(a);
  107.         float s = sin(a);
  108.         m.m_mat[0] = c;
  109.         m.m_mat[1] = s;
  110.         m.m_mat[2] = 0.0f;
  111.         m.m_mat[3] = 0.0f;
  112.         m.m_mat[4] = -s;
  113.         m.m_mat[5] = c;
  114.         m.m_mat[6] = 0.0f;
  115.         m.m_mat[7] = 0.0f;
  116.         m.m_mat[8] = 0.0f;
  117.         m.m_mat[9] = 0.0f;
  118.         m.m_mat[10] = 1.0f;
  119.         m.m_mat[11] = 0.0f;
  120.         m.m_mat[12] = 0.0f;
  121.         m.m_mat[13] = 0.0f;
  122.         m.m_mat[14] = 0.0f;
  123.         m.m_mat[15] = 1.0f;
  124.         return m;
  125.     }
  126.     static MyMatrix yRotation(float a) {
  127.         MyMatrix m;
  128.         float c = cos(a);
  129.         float s = sin(a);
  130.  
  131.         m.m_mat[0] = c;
  132.         m.m_mat[1] = 0.0f;
  133.         m.m_mat[2] = -s;
  134.         m.m_mat[3] = 0.0f;
  135.         m.m_mat[4] = 0.0f;
  136.         m.m_mat[5] = 1.0f;
  137.         m.m_mat[6] = 0.0f;
  138.         m.m_mat[7] = 0.0f;
  139.         m.m_mat[8] = s;
  140.         m.m_mat[9] = 0.0f;
  141.         m.m_mat[10] = c;
  142.         m.m_mat[11] = 0.0f;
  143.         m.m_mat[12] = 0.0f;
  144.         m.m_mat[13] = 0.0f;
  145.         m.m_mat[14] = 0.0f;
  146.         m.m_mat[15] = 1.0f;
  147.  
  148.         return m;
  149.     }
  150.     static MyMatrix Projection(float n, float r, float t, float l, float b, float f) {
  151.         MyMatrix m;
  152.         m.m_mat[0] = 2 * n / (r - l);
  153.         m.m_mat[1] = 0.0f;
  154.         m.m_mat[2] = (r + l) / (r - l);
  155.         m.m_mat[3] = 0.0f;
  156.         m.m_mat[4] = 0.0f;
  157.         m.m_mat[5] = 2 * n / (t - b);
  158.         m.m_mat[6] = (t + b) / (t - b);
  159.         m.m_mat[7] = 0.0f;
  160.         m.m_mat[8] = 0.0f;
  161.         m.m_mat[9] = 0.0f;
  162.         m.m_mat[10] = -(f + n) / (f - n);
  163.         m.m_mat[11] = -2 * f * n / (f - n);
  164.         m.m_mat[12] = 0.0f;
  165.         m.m_mat[13] = 0.0f;
  166.         m.m_mat[14] = -1;
  167.         m.m_mat[15] = 0.0f;
  168.         return m;
  169.     }
  170.    
  171.  
  172.     const float* get() const { return m_mat; };
  173. };
  174. Model g_model;
  175.  
  176.  
  177. float func(float x, float z) {
  178.     return 0.25 * (1 - x * z) * sinf(1 - x * z);
  179. }
  180.  
  181. float d_f_x(float x, float z) {
  182.     return  -0.25 * z * sinf(1 - x * z) - 0.25 * z * (1 - x * z) * cosf(-1 + x * z);
  183. }
  184.  
  185. float d_f_z(float x, float z) {
  186.     return -0.25 * x * sinf(1 - x * z) - 0.25 * x * (1 - x * z) * cosf(-1 + x * z);
  187. }
  188.  
  189. GLuint createShader(const GLchar* code, GLenum type)
  190. {
  191.     GLuint result = glCreateShader(type);
  192.  
  193.     glShaderSource(result, 1, &code, NULL);
  194.     glCompileShader(result);
  195.  
  196.     GLint compiled;
  197.     glGetShaderiv(result, GL_COMPILE_STATUS, &compiled);
  198.  
  199.     if (!compiled)
  200.     {
  201.         GLint infoLen = 0;
  202.         glGetShaderiv(result, GL_INFO_LOG_LENGTH, &infoLen);
  203.         if (infoLen > 0)
  204.         {
  205.             char* infoLog = (char*)alloca(infoLen);
  206.             glGetShaderInfoLog(result, infoLen, NULL, infoLog);
  207.             cout << "Shader compilation error" << endl << infoLog << endl;
  208.         }
  209.         glDeleteShader(result);
  210.         return 0;
  211.     }
  212.  
  213.     return result;
  214. }
  215.  
  216. GLuint createProgram(GLuint vsh, GLuint fsh)
  217. {
  218.     GLuint result = glCreateProgram();
  219.  
  220.     glAttachShader(result, vsh);
  221.     glAttachShader(result, fsh);
  222.  
  223.     glLinkProgram(result);
  224.  
  225.     GLint linked;
  226.     glGetProgramiv(result, GL_LINK_STATUS, &linked);
  227.  
  228.     if (!linked)
  229.     {
  230.         GLint infoLen = 0;
  231.         glGetProgramiv(result, GL_INFO_LOG_LENGTH, &infoLen);
  232.         if (infoLen > 0)
  233.         {
  234.             char* infoLog = (char*)alloca(infoLen);
  235.             glGetProgramInfoLog(result, infoLen, NULL, infoLog);
  236.             cout << "Shader program linking error" << endl << infoLog << endl;
  237.         }
  238.         glDeleteProgram(result);
  239.         return 0;
  240.     }
  241.  
  242.     return result;
  243. }
  244.  
  245. bool createShaderProgram()
  246. {
  247.     g_shaderProgram = 0;
  248.  
  249.  
  250.     const GLchar vsh[] =
  251.         "#version 330\n"
  252.         ""
  253.         "layout(location = 0) in vec2 a_position;"
  254.         ""
  255.         "uniform mat4 u_mv;"
  256.         "uniform mat4 u_mvp;"
  257.         ""
  258.         "out vec3 v_p;"
  259.         "out vec3 v_normal;"
  260.         ""
  261.         "void main()"
  262.         "{"
  263.         "    float x = a_position[0];"
  264.         "    float z = a_position[1];"
  265.         "    float func =0.25 *(1-x* z)* sin(1-x *z); "
  266.         "    vec4 pos = vec4(a_position[0], func, a_position[1], 1.0);"
  267.         "    gl_Position = u_mvp*pos;"
  268.         "    v_p = (u_mv*pos).xyz;"
  269.         "    float g_x = -0.25*z*sin(1 - x*z) - 0.25*z*(1 - x*z)*cos(-1 + x*z);"
  270.         "    float g_z = -0.25*x*sin(1 - x*z) - 0.25*x*(1 - x*z)*cos(-1 + x*z);"
  271.        
  272.         "    v_normal = normalize(transpose(inverse(mat3(u_mv)))*vec3(g_x, 1.0, g_z));"
  273.         "}"
  274.         ;
  275.  
  276.  
  277.     const GLchar fsh[] =
  278.         "#version 330\n"
  279.         ""
  280.         "in vec3 v_p;"
  281.         "in vec3 v_normal;"
  282.         ""
  283.         "layout(location = 0) out vec4 o_color;"
  284.         ""
  285.         "void main()"
  286.         "{"
  287.         "   vec3 u_l = vec3(5.0, 10.0, 0.0);" //Положение источника света
  288.         "   vec3 n = normalize(v_normal);" //нормировка нормали
  289.         "   vec3 l = normalize(v_p-u_l);"
  290.         "   float a = dot(-l, n);"
  291.         "   float d = max(a, 0.1);" //диффузное освещение
  292.         "   vec3 r = reflect(l, n);" //вектор отражения
  293.         "   vec3 e = normalize(-v_p);"
  294.         "   float s = pow(max(dot(r, e), 0.0), 5.0)*int(a >= 0);" //компонент зеркального блика
  295.         "   o_color = vec4(d*vec3(1.0, 0.0, 0.0)+s*vec3(1.0), 1.0);" //цвет поверхности
  296.         "}"
  297.         ;
  298.  
  299.  
  300.     GLuint vertexShader, fragmentShader;
  301.  
  302.     vertexShader = createShader(vsh, GL_VERTEX_SHADER);
  303.     fragmentShader = createShader(fsh, GL_FRAGMENT_SHADER);
  304.  
  305.     g_shaderProgram = createProgram(vertexShader, fragmentShader);
  306.     g_uMV = glGetUniformLocation(g_shaderProgram, "u_mv");
  307.     g_uMVP = glGetUniformLocation(g_shaderProgram, "u_mvp");
  308.  
  309.     glDeleteShader(vertexShader);
  310.     glDeleteShader(fragmentShader);
  311.  
  312.     return g_shaderProgram != 0;
  313. }
  314. GLint getNumVertex(int j, int i, int n) {
  315.     return (GLint)(i + j * (n + 1));
  316. }
  317. void generateMesh(GLfloat* vertices, GLint* indices, int Tess_N) {
  318.     int index = 0;
  319.     float sum_x = 0.0f;
  320.     float sum_y = 0.0f;
  321.     float sum_z = 0.0f;
  322.     GLfloat* arr_x = new GLfloat[Tess_N + 1];//координаты x
  323.     GLfloat* arr_z = new GLfloat[Tess_N + 1];//координаты z
  324.     //Заполнение массива с координатами x
  325.     for (int i = 0; i <= Tess_N; i++) {
  326.         arr_x[i] = x0 + i * dx;
  327.     }
  328.  
  329.     //Заполнение массива с координатами z
  330.     for (int i = 0; i <= Tess_N; i++) {
  331.         arr_z[i] = z0 + i * dz;
  332.     }
  333.  
  334.     //Заполнение итогового массива с вершинами
  335.     int k = 0;
  336.     for (int j = 0; j <= Tess_N; j++) {
  337.         for (int i = 0; i <= Tess_N; i++) {
  338.             //Координаты
  339.             vertices[k] = arr_x[i];
  340.             k++;
  341.             vertices[k] = arr_z[j];
  342.             k++;
  343.         }
  344.     }
  345.  
  346.     // Заполняем массив индексов
  347.     k = 0;
  348.     int j = 0;
  349.     while (j < Tess_N) {
  350.         for (int i = 0; i <= Tess_N; i++) {
  351.             indices[k] = getNumVertex(j, i, Tess_N);
  352.             k++;
  353.             indices[k] = getNumVertex(j + 1, i, Tess_N);
  354.             k++;
  355.         }
  356.         if (j < Tess_N - 1) {
  357.             indices[k] = getNumVertex(j + 1, Tess_N, Tess_N);
  358.             k++;
  359.         }
  360.         j++;
  361.         if (j < Tess_N) {
  362.             for (int i = Tess_N; i >= 0; i--) {
  363.                 indices[k] = getNumVertex(j, i, Tess_N);
  364.                 k++;
  365.                 indices[k] = getNumVertex(j + 1, i, Tess_N);
  366.                 k++;
  367.             }
  368.             if (j < Tess_N - 1) {
  369.                 indices[k] = getNumVertex(j + 1, 0, Tess_N);
  370.                 k++;
  371.             }
  372.             j++;
  373.         }
  374.     }
  375.  
  376. }
  377. bool createModel()
  378. {
  379.     /*GLfloat* vertices = (GLfloat*)malloc(Tess_N * Tess_N * 2 * sizeof(GLfloat));
  380.  
  381.    
  382.  
  383.     GLuint* indices = (GLuint*)malloc(
  384.         (Tess_N - 1) * (Tess_N - 1) * 2 * 3 * sizeof(GLuint)
  385.     );*/
  386.     GLint arr_vertex_size = (Tess_N + 1) * (Tess_N + 1) * 2; //Размерность одномерного массива с вершинами
  387.     GLint arr_index_size = 2 * (Tess_N + 1) * Tess_N + (Tess_N - 1); //Размерность одномерного массива с индексами
  388.  
  389.    
  390.     GLfloat* vertices = new GLfloat[arr_vertex_size]; //Создали массив с координатами вершин x, y, z
  391.     GLint* indices = new GLint[arr_index_size]; //Создали массив с индексами обхода
  392.     generateMesh(vertices, indices, Tess_N);
  393.  
  394.     glGenVertexArrays(1, &g_model.vao);
  395.     glBindVertexArray(g_model.vao);
  396.  
  397.     glGenBuffers(1, &g_model.vbo);
  398.     glBindBuffer(GL_ARRAY_BUFFER, g_model.vbo);
  399.     glBufferData(GL_ARRAY_BUFFER, arr_vertex_size * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
  400.  
  401.     glGenBuffers(1, &g_model.ibo);
  402.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_model.ibo);
  403.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, arr_index_size * sizeof(GLuint), indices, GL_STATIC_DRAW);
  404.     g_model.indexCount = arr_index_size;
  405.  
  406.     glEnableVertexAttribArray(0);
  407.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (const GLvoid*)0);
  408.     /* glEnableVertexAttribArray(1);
  409.      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (const GLvoid*)(2 * sizeof(GLfloat)));*/
  410.  
  411.     return g_model.vbo != 0 && g_model.ibo != 0 && g_model.vao != 0;
  412. }
  413.  
  414. bool init()
  415. {
  416.     // Set initial color of color buffer to white.
  417.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  418.  
  419.     return createShaderProgram() && createModel();
  420. }
  421.  
  422. void reshape(GLFWwindow* window, int width, int height)
  423. {
  424.     glViewport(0, 0, width, height);
  425.     //P=...
  426. }
  427. int max(int a, int b) {
  428.     return a > b ? a : b;
  429. }
  430.  
  431. void draw(/*GLfloat delta_draw*/)
  432. {
  433.     // Clear color buffer.
  434.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  435.  
  436.     glUseProgram(g_shaderProgram);
  437.     glBindVertexArray(g_model.vao);
  438.  
  439.     float n = 0.01;
  440.     float f = 1000;
  441.     float r = n * tanf(45 / 2);
  442.     float l = -r;
  443.     float t = r;
  444.     float b = -t;
  445.     MyMatrix Projection;
  446.     Projection.Projection(n, r, t, l, b, f);
  447.  
  448.     ////Проекционная матрица: 45 градусов, соотношение 4:3
  449.     //mat4 Projection = perspective(radians(45.0f), 4.0f / 3.0f, x0, abs(x0));
  450.  
  451.     MyMatrix T;
  452.     T.translation(x0, 0, z0);
  453.     MyMatrix S;
  454.     int x = x0;
  455.     int z = z0;
  456.    
  457.     S.scale(1 / max(x, z), 0, 1 / max(x, z));
  458.     MyMatrix R;
  459.     R.xRotation(10);
  460.     MyMatrix MV;
  461.  
  462.     MV = T * S * R;
  463.  
  464.  
  465.    
  466.     //mat4 View = lookAt(
  467.     //    vec3(5, 3, 3) * abs(x0), //Положение камеры
  468.     //    vec3(0, 0, 0), //Направление камеры в точку
  469.     //    vec3(0, 1, 0)  //Наблюдатель
  470.     //);
  471.  
  472.     /*GLfloat a = delta_draw;*/
  473.  
  474.     //if (glfwGetKey(g_window, GLFW_KEY_LEFT) || glfwGetKey(g_window, GLFW_KEY_RIGHT)) {
  475.     //    /*ModelObj = rotate(ModelObj, delta_draw, vec3(0.0, 1.0, 0.0));*/
  476.     //    R.yRotation(delta_draw);
  477.     //    MV = MV * R;
  478.  
  479.     //}
  480.     //else if (glfwGetKey(g_window, GLFW_KEY_UP) || glfwGetKey(g_window, GLFW_KEY_DOWN))
  481.     //{
  482.     //   /* ModelObj = rotate(ModelObj, delta_draw, vec3(0.0, 0.0, 1.0));*/
  483.     //    R.zRotation(delta_draw);
  484.     //    MV = MV * R;
  485.     //}
  486.  
  487.     ////Матрица MV
  488.     //mat4 MV = View * ModelObj;
  489.  
  490.     //Матрица MVP
  491.     MyMatrix MVP = Projection * MV;
  492.  
  493.     //Передача данных в шейдер
  494.     glUniformMatrix4fv(g_uMV, 1, GL_FALSE, MV.get());
  495.     glUniformMatrix4fv(g_uMVP, 1, GL_FALSE, MVP.get());
  496.     glDrawElements(GL_TRIANGLE_STRIP, g_model.indexCount, GL_UNSIGNED_INT, NULL);
  497. }
  498.  
  499. void cleanup()
  500. {
  501.     if (g_shaderProgram != 0)
  502.         glDeleteProgram(g_shaderProgram);
  503.     if (g_model.vbo != 0)
  504.         glDeleteBuffers(1, &g_model.vbo);
  505.     if (g_model.ibo != 0)
  506.         glDeleteBuffers(1, &g_model.ibo);
  507.     if (g_model.vao != 0)
  508.         glDeleteVertexArrays(1, &g_model.vao);
  509. }
  510.  
  511. bool initOpenGL()
  512. {
  513.     // Initialize GLFW functions.
  514.     if (!glfwInit())
  515.     {
  516.         cout << "Failed to initialize GLFW" << endl;
  517.         return false;
  518.     }
  519.  
  520.     // Request OpenGL 3.3 without obsoleted functions.
  521.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  522.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  523.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  524.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  525.  
  526.     // Create window.
  527.     g_window = glfwCreateWindow(800, 600, "OpenGL Test", NULL, NULL);
  528.     if (g_window == NULL)
  529.     {
  530.         cout << "Failed to open GLFW window" << endl;
  531.         glfwTerminate();
  532.         return false;
  533.     }
  534.  
  535.     // Initialize OpenGL context with.
  536.     glfwMakeContextCurrent(g_window);
  537.  
  538.     // Set internal GLEW variable to activate OpenGL core profile.
  539.     glewExperimental = true;
  540.  
  541.     // Initialize GLEW functions.
  542.     if (glewInit() != GLEW_OK)
  543.     {
  544.         cout << "Failed to initialize GLEW" << endl;
  545.         return false;
  546.     }
  547.  
  548.     // Ensure we can capture the escape key being pressed.
  549.     glfwSetInputMode(g_window, GLFW_STICKY_KEYS, GL_TRUE);
  550.  
  551.     // Set callback for framebuffer resizing event.
  552.     glfwSetFramebufferSizeCallback(g_window, reshape);
  553.  
  554.     return true;
  555. }
  556.  
  557. void tearDownOpenGL()
  558. {
  559.     // Terminate GLFW.
  560.     glfwTerminate();
  561. }
  562.  
  563.  
  564. int main()
  565. {
  566.     // Initialize OpenGL
  567.     //cout << n << ", " << x0 << ", " << dx;
  568.     // Initialize OpenGL
  569.     if (!initOpenGL())
  570.         return -1;
  571.  
  572.     // Initialize graphical resources.
  573.     bool isOk = init();
  574.  
  575.     if (isOk)
  576.     {
  577.        /* GLfloat lastTime = glfwGetTime();*/
  578.  
  579.         // Main loop until window closed or escape pressed.
  580.         while (glfwGetKey(g_window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(g_window) == 0)
  581.         {
  582.            /* GLfloat currentTime = glfwGetTime();
  583.             GLfloat deltaTime = GLfloat(currentTime - lastTime);
  584.             lastTime = currentTime;
  585.  
  586.             GLfloat delta = 0;
  587.             GLfloat angle = 200.0;
  588.             if (glfwGetKey(g_window, GLFW_KEY_LEFT) || glfwGetKey(g_window, GLFW_KEY_UP)) {
  589.                 delta = radians(angle) * deltaTime;
  590.             }
  591.             else if (glfwGetKey(g_window, GLFW_KEY_RIGHT) || glfwGetKey(g_window, GLFW_KEY_DOWN)) {
  592.                 delta = -radians(angle) * deltaTime;
  593.             }*/
  594.             draw();
  595.             // Swap buffers.
  596.             glfwSwapBuffers(g_window);
  597.             // Poll window events.
  598.             glfwPollEvents();
  599.         }
  600.     }
  601.  
  602.     // Cleanup graphical resources.
  603.     cleanup();
  604.  
  605.     // Tear down OpenGL.
  606.     tearDownOpenGL();
  607.  
  608.     return isOk ? 0 : -1;
  609. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement