Advertisement
AlejandroGY

main

May 27th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <GL/glew.h>
  5. #include <GLFW/glfw3.h>
  6. #include "shader_util.h"
  7.  
  8. void error_callback(GLint error, const GLchar* description);
  9.  
  10. GLuint createSqrt(GLuint* vbo, GLuint* ebo, GLfloat dx, GLfloat dy);
  11. void createFigure(GLuint figures[4], GLuint* vbo, GLuint* ebo, GLint x, GLint y, GLfloat d[8]);
  12. void change_tone(GLfloat color[4], GLint op);
  13.  
  14. int main( ) {
  15.     glfwSetErrorCallback(error_callback);
  16.     glfwInit( );
  17.  
  18.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  19.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  20.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  21.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  22.  
  23.     GLFWwindow* window = glfwCreateWindow(512, 512, "Tetris 1", nullptr, nullptr);
  24.     glfwMakeContextCurrent(window);
  25.  
  26.     glewExperimental = GL_TRUE;
  27.     GLenum status = glewInit( );
  28.  
  29.     if (status != GLEW_OK) {
  30.         std::cerr << "Error: " << glewGetErrorString(status) << std::endl;
  31.         return 1;
  32.     }
  33.  
  34.     GLuint shaderProgram = linkShaderProgram("vertex_shader.glsl", "fragment_shader.glsl");
  35.  
  36.     glViewport(0, 0, 512, 512);
  37.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  38.     GLfloat scale = 0.1;
  39.  
  40.     GLfloat I[8] = { 1.0f, 0.0f,    0.0f,  0.0f,   -1.0f,  0.0f,     2.0f,  0.0f };
  41.     GLfloat O[8] = { 0.0f, 0.0f,   -1.0f,  0.0f,   -1.0f, -1.0f,     0.0f, -1.0f };
  42.     GLfloat Z[8] = { 0.0f, 0.0f,    1.0f,  0.0f,    1.0f, -1.0f,     2.0f, -1.0f };
  43.     GLfloat T[8] = { 0.0f, 0.0f,    1.0f,  0.0f,    0.0f, -1.0f,    -1.0f,  0.0f };
  44.     GLfloat L[8] = { 0.0f, 0.0f,    0.0f, -1.0f,    0.0f, -2.0f,    -1.0f, -2.0f };
  45.  
  46.     GLuint vbo, ebo;
  47.     GLuint figures[5][4];
  48.     createFigure(figures[0], &vbo, &ebo, -5.0f,  4.0f, I);
  49.     createFigure(figures[1], &vbo, &ebo, -4.0f, -4.0f, O);
  50.     createFigure(figures[2], &vbo, &ebo,  0.0f,  0.0f, Z);
  51.     createFigure(figures[3], &vbo, &ebo,  5.0f,  4.0f, T);
  52.     createFigure(figures[4], &vbo, &ebo,  4.0f, -4.0f, L);
  53.  
  54.     for (; !glfwWindowShouldClose(window); glfwSwapBuffers(window)) {
  55.         glfwPollEvents( );
  56.         glClear(GL_COLOR_BUFFER_BIT);
  57.  
  58.         GLint location = glGetUniformLocation(shaderProgram, "scale");
  59.         glUniform1f(location, scale);
  60.         glUseProgram(shaderProgram);
  61.  
  62.         GLfloat color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
  63.         for (GLint i = 0; i < 5; ++i) {
  64.             color[i % 3] = (color[i % 3] >= 0.5 ? 0.0f : 1.0f);
  65.             for (GLint j = 0; j < 4; ++j) {
  66.                 change_tone(color, j % 2);
  67.                 GLint location_t = glGetUniformLocation(shaderProgram, "color");
  68.                 glUniform4f(location_t, color[0], color[1], color[2], color[3]);
  69.                 glBindVertexArray(figures[i][j]);
  70.                 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (GLvoid*)0);
  71.                 glBindVertexArray(0);
  72.             }
  73.         }
  74.     }
  75.  
  76.     glDeleteBuffers(1, &vbo);
  77.     glDeleteBuffers(1, &ebo);
  78.  
  79.     for (GLint i = 0; i < 5; ++i) {
  80.         glDeleteVertexArrays(4, figures[i]);
  81.     }
  82.  
  83.     glfwTerminate( );
  84. }
  85.  
  86. void error_callback(GLint error, const GLchar* description) {
  87.     std::cerr << "Error " << error << ": " << description << std::endl;
  88.     std::cin.get();
  89.     exit(1);
  90. }
  91.  
  92. GLuint createSqrt(GLuint* vbo, GLuint* ebo, GLfloat dx, GLfloat dy) {
  93.     GLfloat vertex[] = {
  94.         -0.5f, -0.5f,
  95.          0.5f, -0.5f,
  96.         -0.5f,  0.5f,
  97.          0.5f,  0.5f,
  98.     };
  99.  
  100.     for (GLint i = 0; i < 4; ++i) {
  101.         vertex[2 * i + 0] += dx;
  102.         vertex[2 * i + 1] += dy;
  103.     }
  104.  
  105.     GLuint elements[] = {
  106.         0, 1, 2,
  107.         1, 3, 2,
  108.     };
  109.  
  110.     GLuint vao;
  111.  
  112.     glGenVertexArrays(1, &vao);
  113.     glGenBuffers(1, vbo);
  114.     glGenBuffers(1, ebo);
  115.  
  116.     glBindVertexArray(vao);
  117.  
  118.     glBindBuffer(GL_ARRAY_BUFFER, *vbo);
  119.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
  120.  
  121.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *ebo);
  122.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
  123.  
  124.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
  125.     glEnableVertexAttribArray(0);
  126.  
  127.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  128.  
  129.     glBindVertexArray(0);
  130.     return vao;
  131. }
  132.  
  133. void createFigure(GLuint figures[4], GLuint* vbo, GLuint* ebo, GLint x, GLint y, GLfloat d[8]) {
  134.     for (GLint i = 0; i < 4; ++i) {
  135.         figures[i] = createSqrt(vbo, ebo, d[i * 2 + 0] + x, d[i * 2 + 1] + y);
  136.     }
  137. }
  138.  
  139. void change_tone(GLfloat color[4], GLint op) {
  140.     for (GLint i = 0; i < 3; ++i) {
  141.         color[i] = (color[i] >= 0.5f ? 1.0f - (op ? 0.0f : 0.2f) : 0.0f);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement