Guest User

Untitled

a guest
Sep 13th, 2014
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <glm/glm.hpp>
  4.  
  5. #include <iostream>
  6. #include <string>
  7.  
  8. #include "util/shaderutil.hpp"
  9.  
  10. #define WIDTH   800
  11. #define HEIGHT  600
  12.  
  13. using namespace std;
  14. using namespace glm;
  15.  
  16. GLuint vao;
  17. GLuint vbo;
  18. GLuint shaderprogram;
  19.  
  20. static const GLfloat vertices[3 * 3] = {
  21.   0.0, 0.5, 0.0,
  22.   0.5,-0.5, 0.0,
  23.  -0.5,-0.5, 0.0
  24. };
  25.  
  26. bool found = false;
  27.  
  28. void check(int line) {
  29.   GLenum e = glGetError();
  30.   if (e != GL_NO_ERROR && !found) {
  31.     cerr << "ERROR FOUND @ LINE " << line << " WITH ERROR CODE " << e << endl;
  32.     found = true;
  33.   }
  34. }
  35.  
  36. void initialize() {
  37.   glGenVertexArrays(1, &vao);
  38.   glBindVertexArray(vao);
  39.  
  40.   glGenBuffers(1, &vbo);
  41.   glBindBuffer(GL_ARRAY_BUFFER, vbo);
  42.   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  43.    
  44.   string version = (char*) glGetString(GL_VERSION);
  45.   cout << "Using OpenGL Version: " << version << endl << endl;
  46.  
  47.   glClearColor(0.5, 0.7, 0.9, 1.0);
  48.  
  49.   string vShaderPath   = "shaders/shader.vert";
  50.   string fShaderPath   = "shaders/shader.frag";
  51.   shaderprogram        = ShaderUtil::createProgram(vShaderPath.c_str(), fShaderPath.c_str());
  52. }
  53.  
  54. void render() {
  55.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  56.  
  57.   glEnableVertexAttribArray(0);
  58.   glBindBuffer(GL_ARRAY_BUFFER, vbo);
  59.  
  60.   glUseProgram(shaderprogram);
  61.   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  62.   glDrawArrays(GL_TRIANGLES, 0, 3);
  63.  
  64.   glDisableVertexAttribArray(0);
  65. }
  66.  
  67. void clean() {
  68.   glDeleteProgram(shaderprogram);
  69. }
  70.  
  71.  
  72. int main(int argc, char** argv) {
  73.   if (!glfwInit()) {
  74.     cerr << "GLFW ERROR!" << endl;
  75.     return -1;
  76.   }
  77.  
  78.   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  79.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  80.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  81.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  82.  
  83.   GLFWwindow* win = glfwCreateWindow(WIDTH, HEIGHT, "Stuff happening...", NULL, NULL);
  84.   glfwMakeContextCurrent(win);
  85.   glewExperimental = GL_TRUE;
  86.  
  87.   if (glewInit() != GLEW_OK) {
  88.     cerr << "GLEW ERROR!" << endl;
  89.     return -1;
  90.   } else {
  91.     glGetError();
  92.     //GLEW BUG: SETTING THE ERRORFLAG TO INVALID_ENUM; THEREFORE RESET
  93.   }
  94.  
  95.   initialize();
  96.  
  97.   while (!glfwWindowShouldClose(win)) {
  98.     render();
  99.  
  100.     glfwPollEvents();
  101.     glfwSwapBuffers(win);
  102.   }
  103.  
  104.   clean();
  105.  
  106.   glfwDestroyWindow(win);
  107.   glfwTerminate();
  108.  
  109.   return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment