Advertisement
Guest User

glfw3_GL3.2_example

a guest
Jul 26th, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.74 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/matrix_transform.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9.  
  10. struct Vertex{
  11.     GLfloat x, y, z, nx, ny, nz, u0, v0;
  12.     Vertex() : x(0.0), y(0.0), z(0.0), nx(0.0), ny(0.0), nz(0.0), u0(0.0), v0(0.0){};
  13. };
  14.  
  15. struct Mesh{
  16.     Vertex *vertecies;
  17.     int num_verts;
  18.     int num_idx;
  19.     unsigned short* indicies;
  20.  
  21.     void setVertices(Vertex *verts, int num){
  22.         vertecies = verts; num_verts = num;
  23.     }
  24.     void setIndices(unsigned short* idx, int num){
  25.         indicies = idx; num_idx = num;
  26.     }
  27. };
  28.  
  29. GLuint _vertexBufferObjectID = 0, _indexBufferObjectID = 0
  30.     , _vertexArrayObjectID = 0;
  31. GLuint vertex_shader = 0, fragment_shader = 0, _programID = 0;
  32. glm::mat4 mvp_mat;
  33.  
  34. GLFWwindow*_mainWindow;
  35. int _mainWindowWidth;
  36. int _mainWindowHeight;
  37. const char *_openGLVersion;
  38.  
  39. Mesh *_mesh;
  40. int _vertexCount = 6;
  41.  
  42. void create_buffers();
  43. void setup_shaders();
  44.  
  45. static void error_callback(int error, const char* description)
  46. {
  47.     fputs(description, stderr);
  48. }
  49. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  50. {
  51.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  52.         glfwSetWindowShouldClose(window, GL_TRUE);
  53. }
  54. int main(void)
  55. {
  56.  
  57.  
  58.     int width = 600, height = 600;
  59.     char *title = "glfwtest";
  60.     GLFWmonitor *monitor = NULL;
  61.  
  62.  
  63.     glfwSetErrorCallback(error_callback);
  64.     if (!glfwInit())
  65.         exit(EXIT_FAILURE);
  66.  
  67.  
  68.     ////////////////////////////////////////////////////////////////
  69.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  70.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  71.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  72.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  73.     _mainWindow = glfwCreateWindow(width, height, title, monitor, NULL);
  74.     if (_mainWindow == NULL)
  75.     {
  76.         return false;
  77.     }
  78.     _mainWindowWidth = width;
  79.     _mainWindowHeight = height;
  80.     glfwSetKeyCallback(_mainWindow, key_callback);
  81.     glfwMakeContextCurrent(_mainWindow);
  82.  
  83.     glewExperimental = GL_TRUE;
  84.     glewInit();
  85.  
  86.     _openGLVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
  87.     //////////////////////////////////////////////////////////////////////
  88.     printf("version:%s", _openGLVersion);
  89.  
  90.  
  91.     if (!_mainWindow)
  92.     {
  93.         glfwTerminate();
  94.         exit(EXIT_FAILURE);
  95.     }
  96.     glfwMakeContextCurrent(_mainWindow);
  97.  
  98.     setup_shaders();
  99.     create_buffers();
  100.  
  101.     glClearColor(0.5, 0.5, 0.5, 0.0);
  102.     glDisable(GL_DEPTH_TEST);
  103.     //glDisable(GL_CULL_FACE);
  104.  
  105.     while (!glfwWindowShouldClose(_mainWindow))
  106.     {
  107.         float ratio;
  108.         int width, height;
  109.         glfwGetFramebufferSize(_mainWindow, &width, &height);
  110.         ratio = width / (float) height;
  111.         glViewport(0, 0, width, height);
  112.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  113.        
  114.         //Gets completely ignored under core profile
  115.         //glMatrixMode(GL_PROJECTION);
  116.         //glLoadIdentity();
  117.         //glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  118.         //glMatrixMode(GL_MODELVIEW);
  119.         //glLoadIdentity();
  120.         //glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
  121.         //glBegin(GL_TRIANGLES);
  122.         //glColor3f(1.f, 0.f, 0.f);
  123.         //glVertex3f(-0.6f, -0.4f, 0.f);
  124.         //glColor3f(0.f, 1.f, 0.f);
  125.         //glVertex3f(0.6f, -0.4f, 0.f);
  126.         //glColor3f(0.f, 0.f, 1.f);
  127.         //glVertex3f(0.f, 0.6f, 0.f);
  128.         //glEnd();
  129.  
  130.         float rotval = 0.0f;
  131.         //to test motion:
  132.         //rotval = (float) glfwGetTime() * 80.f;
  133.         glm::mat4 mvp_final = mvp_mat * glm::rotate(glm::mat4(1.0),rotval , glm::vec3(0.0, 0.0, 1.0));
  134.         glUseProgram(_programID);
  135.         GLuint mvp = glGetUniformLocation(_programID, "ModelViewProjection");
  136.         glUniformMatrix4fv(mvp, 1, GL_FALSE, glm::value_ptr(mvp_final));
  137.  
  138.         glBindVertexArray(_vertexArrayObjectID);
  139.  
  140.         // Draw
  141.         glDrawRangeElements(GL_TRIANGLES, 0, 4, _vertexCount, GL_UNSIGNED_SHORT, NULL);
  142.         glBindVertexArray(0);
  143.         glUseProgram(0);
  144.  
  145.  
  146.         glfwSwapBuffers(_mainWindow);
  147.         glfwPollEvents();
  148.     }
  149.     glfwDestroyWindow(_mainWindow);
  150.     glfwTerminate();
  151.     exit(EXIT_SUCCESS);
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158. void create_buffers(){
  159.     float width = 500.0, height = 500.0;
  160.  
  161.     _mesh = new Mesh();
  162.     Vertex* vertices = new Vertex[6];
  163.  
  164.     vertices[0].nz = 1.0f;
  165.     vertices[1].nz = 1.0f;
  166.     vertices[2].nz = 1.0f;
  167.     vertices[3].nz = 1.0f;
  168.     vertices[4].nz = 1.0f;
  169.     vertices[5].nz = 1.0f;
  170.  
  171.     vertices[1].y = height;
  172.     vertices[1].v0 = 1.0f;
  173.  
  174.     vertices[2].x = width;
  175.     vertices[2].y = height;
  176.     vertices[2].u0 = 1.0f;
  177.     vertices[2].v0 = 1.0f;
  178.  
  179.     vertices[4].x = width;
  180.     vertices[4].u0 = 1.0f;
  181.  
  182.     vertices[5].x = width;
  183.     vertices[5].y = height;
  184.     vertices[5].u0 = 1.0f;
  185.     vertices[5].v0 = 1.0f;
  186.  
  187.     _mesh->setVertices(vertices, 6);
  188.     vertices = NULL;
  189.  
  190.     unsigned short * indices = new unsigned short[6];
  191.  
  192.     indices[0] = 0;
  193.     indices[1] = 2;
  194.     indices[2] = 1;
  195.     indices[3] = 0;
  196.     indices[4] = 4;
  197.     indices[5] = 2;
  198.  
  199.     //
  200.     //indices[0] = 0;
  201.     //indices[1] = 2;
  202.     //indices[2] = 3;
  203.     //indices[3] = 0;
  204.     //indices[4] = 1;
  205.     //indices[5] = 3;
  206.  
  207.     _mesh->setIndices(indices, 6);
  208.     indices = NULL;
  209.  
  210.     //////////////////////////////////////////////////////////
  211.     Vertex *_vertices = _mesh->vertecies;
  212.     unsigned short*_indices = _mesh->indicies;
  213.     //////////////////////////////////////////////////////////
  214.     // Update VBO
  215.     if (_vertexBufferObjectID == 0)
  216.     {
  217.         glGenBuffers(1, &_vertexBufferObjectID);
  218.     }
  219.     glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID);
  220.  
  221.     float* data = new float[_vertexCount * sizeof(Vertex) / sizeof(float) ];
  222.     long int begin = 0;
  223.     for (int i = 0; i < _vertexCount; i++)
  224.     {
  225.         begin = i * 8;
  226.         data[begin] = _vertices[i].x;
  227.         data[begin + 1] = _vertices[i].y;
  228.         data[begin + 2] = _vertices[i].z;
  229.         data[begin + 3] = _vertices[i].nx;
  230.         data[begin + 4] = _vertices[i].ny;
  231.         data[begin + 5] = _vertices[i].nz;
  232.         data[begin + 6] = _vertices[i].u0;
  233.         data[begin + 7] = _vertices[i].v0;
  234.     }
  235.  
  236.     glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * _vertexCount, &data[0], GL_STATIC_DRAW);
  237.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  238.  
  239.     delete [] data;
  240.     data = NULL;
  241.  
  242.     // Update IBO
  243.     if (_indexBufferObjectID == 0)
  244.     {
  245.         glGenBuffers(1, &_indexBufferObjectID);
  246.     }
  247.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObjectID);
  248.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * _vertexCount, &_indices[0], GL_STATIC_DRAW);
  249.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  250.  
  251.     // Update VAO
  252.     if (_vertexArrayObjectID == 0)
  253.     {
  254.         glGenVertexArrays(1, &_vertexArrayObjectID);
  255.     }
  256.     glBindVertexArray(_vertexArrayObjectID);
  257.  
  258.     glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID);
  259.     // Vertices
  260.     glEnableVertexAttribArray(0);
  261.     glEnableVertexAttribArray(1);
  262.     glEnableVertexAttribArray(2);
  263.  
  264.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char*) NULL) + (0));
  265.     // Normals
  266.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char*) NULL) + (12));
  267.     // TexCoords
  268.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char*) NULL) + (24));
  269.  
  270.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObjectID);
  271.  
  272.     glBindVertexArray(0);
  273.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  274.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  275. }
  276.  
  277. void printMat(glm::mat4 &mat){
  278.     int i, j;
  279.     for (j = 0; j < 4; j++){
  280.         for (i = 0; i < 4; i++){
  281.             printf("%f ", mat[i][j]);
  282.         }
  283.         printf("\n");
  284.     }
  285. }
  286.  
  287. void setup_shaders(){
  288.  
  289.     //Make sure you use float values here
  290.     mvp_mat = glm::ortho(0.0f, (float) _mainWindowWidth, 0.0f, (float) _mainWindowHeight);
  291.     //printf("\n MatOrthoNorm:\n"); printMat(mvp_mat);
  292.  
  293.  
  294.     //mvp_mat = glm::perspective(60.0f, (float) _mainWindowWidth / (float) _mainWindowHeight, 0.1f, 100.0f);
  295.     //printf("\n MatPersp:\n"); printMat(mvp_mat);
  296.  
  297.     //optional for orthographic projection, doesn't do much
  298.     //mvp_mat = mvp_mat * glm::lookAt(glm::vec3(0,0,5), glm::vec3(0,0,0),glm::vec3(0,1,0));
  299.     //printf("\n MatLookAt:\n"); printMat(mvp_mat);
  300.  
  301.     //mvp_mat = glm::mat4(1.0);
  302.  
  303.  
  304.     const char *_vertexShaderSource =
  305.         "#version 150 core\n"
  306.         "in vec3 in_Position;\n"
  307.         "in vec3 in_Normal;\n"
  308.         "in vec2 in_TexCoord;\n"
  309.         "uniform mat4 ModelViewProjection;\n"
  310.         "void main()\n"
  311.         "{\n"
  312.         "   gl_Position = ModelViewProjection * vec4(in_Position, 1.0);\n"
  313.         "}\n";
  314.     const char *_fragmentShaderSource =
  315.         "#version 150 core\n"
  316.         "out vec4 FColor;"
  317.         "void main()\n"
  318.         "{\n"
  319.         "   FColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
  320.         "}\n";
  321.  
  322.     GLint result, loglen;
  323.  
  324.     vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  325.     fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  326.  
  327.     glShaderSource(vertex_shader, 1, &_vertexShaderSource, NULL);
  328.     glShaderSource(fragment_shader, 1, &_fragmentShaderSource, NULL);
  329.  
  330.     glCompileShader(vertex_shader);
  331.     glGetProgramiv(vertex_shader, GL_COMPILE_STATUS, &result);
  332.     glGetProgramiv(vertex_shader, GL_INFO_LOG_LENGTH, &loglen);
  333.  
  334.     if (result == GL_FALSE && loglen > 0)
  335.     {
  336.         char* log = new char[loglen + 1];
  337.  
  338.         glGetProgramInfoLog(vertex_shader, loglen + 1, 0, log);
  339.         //DANGER, passing pointer to content that is freed in the next lines
  340.         //_lastInfoLog = log;
  341.         printf("vertex_log: %s", log);
  342.         delete log;
  343.         log = NULL;
  344.     }
  345.  
  346.     glCompileShader(fragment_shader);
  347.     glGetProgramiv(fragment_shader, GL_COMPILE_STATUS, &result);
  348.     glGetProgramiv(fragment_shader, GL_INFO_LOG_LENGTH, &loglen);
  349.  
  350.     if (result == GL_FALSE && loglen > 0)
  351.     {
  352.         char* log = new char[loglen + 1];
  353.  
  354.         glGetProgramInfoLog(fragment_shader, loglen + 1, 0, log);
  355.         //_lastInfoLog = log; //DANGER
  356.         printf("fragment_log: %s", log);
  357.         delete log;
  358.         log = NULL;
  359.     }
  360.  
  361.     _programID = glCreateProgram();
  362.     glAttachShader(_programID, vertex_shader);
  363.     glAttachShader(_programID, fragment_shader);
  364.  
  365.     //might have to be put here, I think this needs to be set before linking
  366.     //otherwise it has no effect
  367.     //glBindAttribLocation(_programID, 0, "in_Position");
  368.     //glBindAttribLocation(_programID, 1, "in_Normal");
  369.     //glBindAttribLocation(_programID, 2, "in_TexCoord");
  370.     //glBindFragDataLocation(_programID, 0, "FColor");
  371.  
  372.  
  373.     glLinkProgram(_programID);
  374.     glGetProgramiv(_programID, GL_LINK_STATUS, &result);
  375.     glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &loglen);
  376.  
  377.     if (result == GL_FALSE && loglen > 0)
  378.     {
  379.         char* log = new char[loglen + 1];
  380.  
  381.         glGetProgramInfoLog(_programID, loglen + 1, 0, log);
  382.         //_lastInfoLog = log; //DANGER
  383.         printf("link_log: %s", log);
  384.         delete log;
  385.         log = NULL;
  386.     }
  387.  
  388.  
  389.  
  390.  
  391.  
  392.     if (result == GL_FALSE)
  393.     {
  394.         glDeleteProgram(_programID);
  395.         glDeleteShader(vertex_shader);
  396.         glDeleteShader(fragment_shader);
  397.         return;
  398.     }
  399.  
  400.     glUseProgram(_programID);
  401.     glBindAttribLocation(_programID, 0, "in_Position");
  402.     glBindAttribLocation(_programID, 1, "in_Normal");
  403.     glBindAttribLocation(_programID, 2, "in_TexCoord");
  404.     glBindFragDataLocation(_programID, 0, "FColor");
  405.     //if you want the last 4 calls to do anything at all you need to link again or do it before linking
  406.     //in this case it works out either way but just by luck
  407.     //glLinkProgram(_programID);
  408.     glUseProgram(0);
  409.  
  410.     glDeleteShader(vertex_shader);
  411.     glDeleteShader(fragment_shader);
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement