Guest User

glvertexattribtest.cpp

a guest
Aug 19th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4.  
  5. #define GLEW_STATIC
  6. #include <GL/glew.h>
  7. #include <GLFW/glfw3.h>
  8. #include <glm/glm.hpp>
  9. #include <glm/gtx/transform.hpp>
  10.  
  11. #include "../common/shader.h"
  12.  
  13.  
  14. const GLfloat reticle_vertices[] =
  15. {
  16.     -1.0f, -1.0f, 0.0f,
  17.     1.0f, -1.0f, 0.0f,
  18.     0.0f, 1.0f, 0.0f
  19. };
  20.  
  21.  
  22. glm::mat4 MVP;
  23. glm::mat4 vp;
  24.  
  25.  
  26. const char *vertexSource =
  27.     "#version 150\n"
  28.     "layout(location = 0) in vec3 position;"
  29.     "layout(location = 1) in vec3 color;"
  30.     "out vec3 fragColor;"
  31.     "uniform mat4 MVP;"
  32.     "void main() {"
  33.     "gl_Position = MVP * vec4(position, 1.0);"
  34.     "fragColor = color;"
  35.     "}";
  36.  
  37. const char *fragmentSource =
  38.     "#version 150\n"
  39.     "in vec3 fragColor;"
  40.     "out vec4 outColor;"
  41.     "void main() {"
  42.     "outColor = vec4(fragColor, 1.0);"
  43.     "}";
  44.  
  45.  
  46. // Key callback: exit on ESCAPE
  47. static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
  48. {
  49.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  50.         glfwSetWindowShouldClose(window, GL_TRUE);
  51. }
  52.  
  53.  
  54. // Initialize and return a window with GLFW
  55. GLFWwindow *initGLWindow(int width, int height, int samples, const char *title, GLFWkeyfun keycb)
  56. {
  57.     if (!glfwInit())
  58.     {
  59.         fprintf(stderr, "Failed to initialize GLFW\n");
  60.         return NULL;
  61.     }
  62.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  63.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  64.     glfwWindowHint(GLFW_SAMPLES, samples);
  65.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  66.  
  67.     GLFWwindow *w = glfwCreateWindow(width, height, title, NULL, NULL);
  68.     if (!w)
  69.     {
  70.         glfwTerminate();
  71.         return NULL;
  72.     }
  73.  
  74.     glfwMakeContextCurrent(w);
  75.     glfwSetKeyCallback(w, keycb);
  76.  
  77.     glewExperimental = true;
  78.     if (glewInit() != GLEW_OK)
  79.     {
  80.         fprintf(stderr, "Failed to initialize GLEW\n");
  81.         return NULL;
  82.     }
  83.    
  84.     return w;
  85. }
  86.  
  87.  
  88. int main()
  89. {
  90.     // Initialize the OpenGL window
  91.     GLFWwindow *window = initGLWindow(1024, 768, 2, "Schnuff", key_callback);
  92.  
  93.     // Load and use the shaders
  94.     GLuint programID = LoadShaders(vertexSource, fragmentSource);
  95.     glUseProgram(programID);
  96.  
  97.     // Get the attribute locations
  98.     GLuint posAttrib = glGetAttribLocation(programID, "position");
  99.     printf("posAttrib = %d\n", posAttrib);
  100.     GLuint colAttrib = glGetAttribLocation(programID, "color");
  101.     printf("colAttrib = %d\n", colAttrib);
  102.     //GLint normAttrib = glGetAttribLocation(programID, "normal");
  103.     //printf("normAttrib = %d\n", normAttrib);
  104.     GLuint mvpID = glGetUniformLocation(programID, "MVP");
  105.  
  106.     // Vertex array object
  107.     GLuint vao;
  108.     glGenVertexArrays(1, &vao);
  109.     glBindVertexArray(vao);
  110.  
  111.     // Vertex buffer object
  112.     GLuint vbo;
  113.     glGenBuffers(1, &vbo);
  114.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  115.     glBufferData(GL_ARRAY_BUFFER, sizeof(reticle_vertices), reticle_vertices, GL_STATIC_DRAW);
  116.  
  117.     glEnableVertexAttribArray(posAttrib);
  118.     glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  119.  
  120.     // Hide the cursor
  121.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  122.  
  123.     glEnable(GL_DEPTH_TEST);
  124.     glDepthFunc(GL_LESS);
  125.     glEnable(GL_CULL_FACE);
  126.  
  127.     glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
  128.     glm::mat4 m = glm::mat4(1.0f);
  129.  
  130.     // Main loop
  131.     do
  132.     {
  133.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  134.  
  135.         glUniformMatrix4fv(mvpID, 1, GL_FALSE, &m[0][0]);
  136.         glVertexAttrib3f(colAttrib, 1.0f, 0.0f, 0.0f);
  137.         glDrawArrays(GL_TRIANGLES, 0, 3);
  138.  
  139.         glfwSwapBuffers(window);
  140.         glfwPollEvents();
  141.     }
  142.     while (!glfwWindowShouldClose(window));
  143.  
  144.     glDeleteProgram(programID);
  145.     glfwDestroyWindow(window);
  146.     glfwTerminate();
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment