Guest User

My Main CPP File

a guest
Dec 25th, 2015
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <streambuf>
  6. #include <string>
  7.  
  8. #include "GL/glew.h"
  9. #include "GLFW/glfw3.h"
  10. #include "glm/glm.hpp"
  11. #include "glm/gtc/matrix_transform.hpp"
  12. #include "fileloader.hpp"
  13. #include "meshrenderer.hpp"
  14. #include "fbo.hpp"
  15. #include "display.hpp"
  16. #include "shader.hpp"
  17.  
  18. Display* display;
  19. MeshRenderer* mesh;
  20. Shader* stdShader;
  21.  
  22. glm::mat4 projectionMatrix(1.0);
  23. glm::mat4 viewMatrix(1.0);
  24. glm::mat4 modelMatrix(1.0);
  25.  
  26. void initMesh()
  27. {
  28.     GLfloat data[] =
  29.     {
  30.         -200.0f, -200.0f, 0.0f,
  31.         1.0f, 0.0f, 0.0f, 1.0f,
  32.         0.0f, 0.0f,
  33.  
  34.         200.0f, -200.0f, 0.0f,
  35.         0.0f, 1.0f, 0.0f, 1.0f,
  36.         0.0f, 0.0f,
  37.  
  38.         0.0f, 200.0f, 0.0f,
  39.         0.0f, 0.0f, 1.0f, 1.0f,
  40.         0.0f, 0.0f
  41.     };
  42.  
  43.     mesh = new MeshRenderer();
  44.     mesh->createInterleaved(data, 3);
  45. }
  46.  
  47. void init()
  48. {
  49.     glEnable(GL_TEXTURE_2D);
  50.     glEnable(GL_DEPTH_TEST);
  51.  
  52.     stdShader = new Shader("standard");
  53.     initMesh();
  54. }
  55.  
  56. void setUniforms()
  57. {
  58.     glUniformMatrix4fv(stdShader->getUniform("projection_matrix"), 1, GL_FALSE, &projectionMatrix[0][0]);
  59.     glUniformMatrix4fv(stdShader->getUniform("view_matrix"), 1, GL_FALSE, &viewMatrix[0][0]);
  60.     glUniformMatrix4fv(stdShader->getUniform("model_matrix"), 1, GL_FALSE, &modelMatrix[0][0]);
  61.  
  62.     glUniform1i(stdShader->getUniform("textured"), 0);
  63.     glUniform1i(stdShader->getUniform("sampler"), 0);
  64. }
  65.  
  66. void render()
  67. {
  68.     stdShader->bind();
  69.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70.     glViewport(0, 0, display->getWidth(), display->getHeight());
  71.     float hw = display->getWidth() / 2.0f;
  72.     float hh = display->getHeight() / 2.0f;
  73.     projectionMatrix = glm::ortho(-hw, hw, -hh, hh, -10.0f, 10.0f);
  74.     setUniforms();
  75.     mesh->renderInterleaved();
  76.     stdShader->release();
  77. }
  78.  
  79. void destroy()
  80. {
  81.     stdShader->destroy();
  82.     mesh->destroy();
  83. }
  84.  
  85. void initDisplay()
  86. {
  87.     display = new Display();
  88.     display->setTitle("OpenGL Test");
  89.     display->setDimensions(800, 450);
  90.     display->setVSync(true);
  91.     display->create();
  92. }
  93.  
  94. int main(int argCount, char **args)
  95. {
  96.     initDisplay();
  97.     init();
  98.  
  99.     do
  100.     {
  101.         render();
  102.         display->update();
  103.     } while (!display->isCloseRequested());
  104.  
  105.     destroy();
  106.     display->destroy();
  107.     exit(EXIT_SUCCESS);
  108. }
RAW Paste Data