Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 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. const unsigned int WIN_WIDTH = 1024;
  10. const unsigned int WIN_HEIGHT = 768;
  11. const char TITLE[] = "Tutorial 02";
  12.  
  13. GLFWwindow *init() {
  14.     if (!glfwInit()) {
  15.         fprintf(stderr, "Failed to iniatialize GLFW\n");
  16.         return NULL;
  17.     }
  18.  
  19.     glfwWindowHint(GLFW_SAMPLES, 4);               // 4x antialiasing
  20.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL version 3.3
  21.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  22.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  23.     // dla kompatybilności z MacOS. Normalnie niepotrzebne
  24.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  25.     // nie chcemy starego openGL.
  26.  
  27.     GLFWwindow *window;
  28.     window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, TITLE, NULL, NULL);
  29.  
  30.     if (window == NULL) {
  31.         fprintf(stderr, "Failed to open GLFW window\n");
  32.         glfwTerminate();
  33.         return NULL;
  34.     }
  35.  
  36.     // upewniamy się , czy klawisz "escape" jest łapany odpowiednio
  37.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  38.  
  39.     glfwMakeContextCurrent(window);
  40.     glewExperimental = GL_TRUE;
  41.     GLenum err = glewInit();
  42.     if (GLEW_OK != err) {
  43.         fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  44.         glfwTerminate();
  45.         exit(1);
  46.     }
  47.  
  48.     return window;
  49. }
  50.  
  51. GLuint initVAO() {
  52.     glewExperimental = GL_TRUE;
  53.     GLuint VertexArrayID;
  54.     glGenVertexArrays(1, &VertexArrayID);
  55.     glBindVertexArray(VertexArrayID);
  56.     return VertexArrayID;
  57. }
  58.  
  59. static const GLfloat g_vertex_buffer_data[] = {
  60.     -1.0f, -1.0f, 0.0f,
  61.     1.0f, -1.0f, 0.0f,
  62.     0.f, 1.0f, 0.0f,
  63. };
  64.  
  65. int main() {
  66.     GLFWwindow *window = init();
  67.     if (!window) {
  68.         return -1;
  69.     }
  70.  
  71.     initVAO();
  72.  
  73.     GLuint vertex_buffer;
  74.     glGenBuffers(1, &vertex_buffer);
  75.     glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  76.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data),
  77.             g_vertex_buffer_data, GL_STATIC_DRAW);
  78.  
  79.     do {
  80.         initVAO();
  81.         glEnableVertexAttribArray(0);
  82.  
  83.         glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  84.         glVertexAttribPointer(
  85.                 0, // attribute 0. Brak uzasadnienia
  86.                 3, // rozmiar
  87.                 GL_FLOAT, // type
  88.                 GL_FALSE, // znormalizowane
  89.                 0,        // stride
  90.                 (void*)0  // array buffer offset
  91.         );
  92.  
  93.         glDrawArrays(GL_TRIANGLES, 0, 3);
  94.         glDisableVertexAttribArray(0);
  95.  
  96.         glfwSwapBuffers(window);
  97.         glfwPollEvents();
  98.  
  99.     } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  100.             glfwWindowShouldClose(window) == 0);
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement