Advertisement
hnOsmium0001

uniform parameters

Oct 14th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5. #include <glm/glm.hpp>
  6.  
  7. using namespace glm;
  8.  
  9. #include "../common/shader.hpp"
  10.  
  11. GLFWwindow* window;
  12.  
  13. int main(void) {
  14.   // Initialise GLFW
  15.   if (!glfwInit()) {
  16.     fprintf(stderr, "Failed to initialize GLFW\n");
  17.     getchar();
  18.     return -1;
  19.   }
  20.  
  21.   glfwWindowHint(GLFW_SAMPLES, 4);
  22.   glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  23.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  24.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  25.   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
  26.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  27.  
  28.   // Open a window and create its OpenGL context
  29.   window = glfwCreateWindow(1024, 768, "Playground", nullptr, nullptr);
  30.   if (window == nullptr) {
  31.     fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
  32.     getchar();
  33.     glfwTerminate();
  34.     return -1;
  35.   }
  36.   glfwMakeContextCurrent(window);
  37.  
  38.   // Initialize GLEW
  39.   // IMPORTANT: if this is not enable this might create SegmentationFault
  40.   glewExperimental = true; // For core profile
  41.   if (glewInit() != GLEW_OK) {
  42.     fprintf(stderr, "Failed to initialize GLEW\n");
  43.     getchar();
  44.     glfwTerminate();
  45.     return -1;
  46.   }
  47.  
  48.   // Ensure we can capture the escape key being pressed below
  49.   glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  50.  
  51.   // GL code starts
  52.  
  53.   // Dark blue background
  54.   glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  55.  
  56.   GLuint vao;
  57.   glGenVertexArrays(1, &vao);
  58.   glBindVertexArray(vao);
  59.  
  60.   GLuint programID = LoadShaders("shader.vert", "shader.frag");
  61.  
  62.   static const GLfloat vertices[] = {
  63.       -0.5f, -0.5f, 0.0f,
  64.       0.5f, -0.5f, 0.0f,
  65.       0.0f, 0.5f, 0.0f,
  66.   };
  67.  
  68.   GLuint vbo;
  69.   glGenBuffers(1, &vbo);
  70.   glBindBuffer(GL_ARRAY_BUFFER, vbo);
  71.   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  72.  
  73.   int colorLoc = glGetUniformLocation(programID, "color");
  74.  
  75.   do {
  76.     glClear(GL_COLOR_BUFFER_BIT);
  77.  
  78.     glUseProgram(programID);
  79.     float time = glfwGetTime();
  80.     float color = sin(time) * 0.2f;
  81.     glUniform4f(colorLoc, color, color + 0.5f, color, 1.0f);
  82.  
  83.     glEnableVertexAttribArray(0);
  84.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  85.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  86.     glDrawArrays(GL_TRIANGLES, 0, 3);
  87.     glDisableVertexAttribArray(0);
  88.  
  89.     // Swap buffers
  90.     glfwSwapBuffers(window);
  91.     glfwPollEvents();
  92.  
  93.   } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  94.     glfwWindowShouldClose(window) == 0);
  95.  
  96.   glDeleteBuffers(1, & vbo);
  97.   glDeleteVertexArrays(1, & vao);
  98.   glDeleteProgram(programID);
  99.  
  100.   // GL code ends
  101.  
  102.   // Close OpenGL window and terminate GLFW
  103.   glfwTerminate();
  104.  
  105.   return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement