Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 68.41 KB | None | 0 0
  1. #ifndef MAIN_GAME
  2. #define MAIN_GAME
  3.  
  4. #include "IOmanager.h"
  5.  
  6. #include "model.h"
  7.  
  8.  
  9. class MainGame
  10. {
  11. public:
  12.     static void DisplayWindow(void);
  13. private:
  14.     static Shader shadeProgram;
  15.  
  16. };
  17. #endif
  18.  
  19.  
  20.  
  21. #include "MainGame.h"
  22.  
  23. Shader MainGame::shadeProgram;
  24.  
  25. void MainGame::DisplayWindow(void)
  26. {
  27.     IOmanager::initWindow();
  28.  
  29.     shadeProgram.compileShaders("Shaders/vertShader.txt", "Shaders/fragShader.txt");
  30.     shadeProgram.linkShaders();
  31.  
  32.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  33.  
  34.     std::cout << glGetString(GL_VERSION);
  35.  
  36.     Batch::addModel(SMALL_CUBE,"C:/Users/Alan/Desktop/MSVS/graphics/Textures/RTS_Crate.png",glm::vec3(25.0f, 15.0f, 0.0f),0.0f,glm::vec3(1.0f, 0.0f, 0.0f));
  37.  
  38.     Batches::set_all_vao_state();
  39.  
  40.     while (GameEnums::currGS == GameEnums::PLAY){
  41.         IOmanager::pollEvent();
  42.  
  43.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  44.         glUseProgram(shadeProgram.programID);
  45.  
  46.         GLint textureLocation = glGetUniformLocation(Shader::programID, "myTexture");
  47.         glUniform1i(textureLocation, 0);
  48.  
  49.         Batches::render_all();
  50.  
  51.    
  52.         glUseProgram(0);
  53.         SDL_GL_SwapWindow(IOmanager::_window);
  54.     }
  55. }
  56.  
  57.  
  58. #ifndef IO_MANAGER
  59. #define IO_MANAGER
  60.  
  61. #include <Windows.h>
  62. #include <SDL/SDL.h>
  63. #include <GL/glew.h>
  64. #include "Camera.h"
  65. #include "GameEnums.h"
  66. #include <iostream>
  67. #include <string>
  68.  
  69.  
  70. class IOmanager
  71. {
  72. public:
  73.     static void initWindow(void);
  74.     static void pollEvent(void);
  75.     static SDL_Window* _window;
  76.     static bool getleftMouseisNotReleasedFlag(void){return leftMouseisNotReleasedFlag;}
  77. private:
  78.     static int scrW, scrH;
  79.     static SDL_Event evt;
  80.     static bool keyWisNotReleasedFlag;
  81.     static bool keySisNotReleasedFlag;
  82.     static bool keyAisNotReleasedFlag;
  83.     static bool keyDisNotReleasedFlag;
  84.     static bool leftMouseisNotReleasedFlag;
  85.    
  86. };
  87.  
  88. #endif
  89.  
  90.  
  91.  
  92. #include "IOmanager.h"
  93.  
  94. SDL_Window* IOmanager::_window;
  95. int IOmanager::scrW;
  96. int IOmanager::scrH;
  97. SDL_Event IOmanager::evt;
  98.  
  99. bool IOmanager::keyWisNotReleasedFlag;
  100. bool IOmanager::keySisNotReleasedFlag;
  101. bool IOmanager::keyAisNotReleasedFlag;
  102. bool IOmanager::keyDisNotReleasedFlag;
  103. bool IOmanager::leftMouseisNotReleasedFlag;
  104.  
  105. void IOmanager::initWindow(void){
  106.     SDL_Init(SDL_INIT_EVERYTHING);
  107.     scrW = 1180;
  108.     scrH = 924;
  109.     _window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,scrW,scrH, SDL_WINDOW_OPENGL);
  110.     SDL_GLContext glContext = SDL_GL_CreateContext(_window);
  111.     glewInit();
  112.  
  113.     glClearDepth(1.0);
  114.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  115.    
  116.     glEnable(GL_DEPTH_TEST);
  117.     glEnable(GL_CULL_FACE);
  118. }
  119. void IOmanager::pollEvent(void){
  120.     GameEnums::currGS = GameEnums::PLAY;
  121.     while (SDL_PollEvent(&evt)){
  122.         switch (evt.type){
  123.             case SDL_QUIT:
  124.                 GameEnums::currGS = GameEnums::EXIT;
  125.                 break;
  126.             case SDL_MOUSEMOTION:
  127.                 Camera::mouseUpdate((glm::vec2(evt.motion.x, evt.motion.y)));
  128.                 break;
  129.             case SDL_KEYDOWN:
  130.                 switch(evt.key.keysym.sym){
  131.                 case SDLK_w:
  132.                     keyWisNotReleasedFlag = true;
  133.                     break;
  134.                 case SDLK_s:
  135.                     keySisNotReleasedFlag = true;
  136.                     break;
  137.                 case SDLK_a:
  138.                     keyAisNotReleasedFlag = true;
  139.                     break;
  140.                 case SDLK_d:
  141.                     keyDisNotReleasedFlag = true;
  142.                     break;
  143.             }
  144.             break;
  145.             case SDL_KEYUP:
  146.                 switch(evt.key.keysym.sym){
  147.                 case SDLK_w:
  148.                     keyWisNotReleasedFlag = false;
  149.                     break;
  150.                 case SDLK_s:
  151.                     keySisNotReleasedFlag = false;
  152.                     break;
  153.                 case SDLK_a:
  154.                     keyAisNotReleasedFlag = false;
  155.                     break;
  156.                 case SDLK_d:
  157.                     keyDisNotReleasedFlag = false;
  158.                     break;
  159.             }
  160.             break;
  161.             case SDL_MOUSEBUTTONDOWN:
  162.                 switch(evt.button.button){
  163.                 case SDL_BUTTON_LEFT:
  164.                     leftMouseisNotReleasedFlag = true;
  165.                     break;
  166.             }
  167.             break;
  168.             case SDL_MOUSEBUTTONUP:
  169.                 switch(evt.button.button){
  170.                 case SDL_BUTTON_LEFT:
  171.                     leftMouseisNotReleasedFlag = false;
  172.                     break;
  173.             }
  174.             break;
  175.         }
  176.     }
  177.     if(keyWisNotReleasedFlag == true){
  178.         Camera::moveForward();
  179.     }
  180.     if(keySisNotReleasedFlag == true){
  181.         Camera::moveBackward();
  182.     }
  183.     if(keyAisNotReleasedFlag == true){
  184.         Camera::moveLeft();
  185.     }
  186.     if(keyDisNotReleasedFlag == true){
  187.         Camera::moveRight();
  188.     }
  189.     if(leftMouseisNotReleasedFlag == true){
  190.         //std::cout << std::endl << "is NOT released" << std::endl;
  191.     }
  192.     if(leftMouseisNotReleasedFlag == false){
  193.         //std::cout << std::endl << "is released" << std::endl;
  194.     }
  195. }
  196.  
  197.  
  198. #ifndef MODEL_CLASS
  199. #define MODEL_CLASS
  200.  
  201. #include "shapeData.h"
  202. #include "resourceManager.h"
  203. #include "Camera.h"
  204. #include "Shader.h"
  205. #include <algorithm>
  206.  
  207.  
  208. class Batch{
  209. public:
  210.     Batch(SHAPE);
  211.     static void addModel(SHAPE shape, std::string tex_path_key, glm::vec3 world_pos, GLfloat rot, glm::vec3 ax_of_rot);
  212.     std::vector <glm::mat4> & get_instances_vec(void){return instances;}
  213.  
  214.     GLuint& get_vao(void){return vao;}
  215.    
  216.     Vertex* get_vert_Ptr(){return vert_Ptr;}
  217.     GLushort get_vertsByteSize(){return vertsByteSize;}
  218.     GLushort* get_ind_Ptr(){return ind_Ptr;}
  219.     GLushort get_indsByteSize(){return indsByteSize;}
  220.     Texture& get_texture(){return texture;}
  221.  
  222.     GLuint get_instances_byte_size(void);
  223. private:
  224.     Vertex* vert_Ptr;
  225.     GLushort vertsByteSize;
  226.     GLushort* ind_Ptr;
  227.     GLushort indsByteSize;
  228.     Texture texture;
  229.  
  230.     GLuint vao;
  231.  
  232.     std::vector <glm::mat4> instances;
  233. };
  234.  
  235.  
  236. class Batches{
  237. public:
  238.     static void render_all(void);
  239.     Batches();//does nothing
  240.     Batches(Texture texture, glm::vec3, GLfloat, glm::vec3);
  241.     Batches(SHAPE shape, glm::vec3, GLfloat, glm::vec3);
  242.     static GLuint& get_global_vbo(void){return global_vbo;}
  243.     static GLuint& get_global_instance_vbo(void){return global_instance_vbo;}
  244.    
  245.     static std::vector <Batch*> get_batches_vec(void){return batches;}
  246.     static GLuint get_batches_vec_vert_byte_size();
  247.     static GLuint get_batches_vec_ind_byte_size();
  248.     static GLuint get_total_num_instances_byte_size(void);
  249.  
  250.  
  251.     static void set_all_vao_state(void);
  252.    
  253.  
  254. private:
  255.     static GLuint global_vbo;
  256.     static GLuint global_instance_vbo;
  257.     static std::vector <Batch*> batches;
  258. };
  259.  
  260.  
  261. #endif
  262.  
  263.  
  264.  
  265. #include "model.h"
  266.  
  267. std::vector <Batch*> Batches::batches;
  268.  
  269. GLuint Batches::global_vbo;
  270. GLuint Batches::global_instance_vbo;
  271.  
  272.  
  273. Batch::Batch(SHAPE shape){
  274.     vert_Ptr = shapeData::getVerts(shape);
  275.     vertsByteSize = shapeData::get_vertsByteSize();
  276.     ind_Ptr = shapeData::getInds(shape);
  277.     indsByteSize = shapeData::get_indsByteSize();
  278.  
  279.     texture = loadTexture::genTexture();//generates a texture consisting of a unique texture ID, with width and height of 0
  280. }
  281.  
  282.  
  283. void Batch::addModel(SHAPE shape, std::string tex_path_key, glm::vec3 world_pos, GLfloat rot, glm::vec3 ax_of_rot){
  284. auto map_itr = resourceManager::getTexture_cache().find(tex_path_key);
  285.     if(map_itr == resourceManager::getTexture_cache().end()){
  286.         Batches(shape, world_pos, rot, ax_of_rot);  //adds another batch to the batches vector
  287.         loadTexture::loadBufferToOpengl(tex_path_key, Batches::get_batches_vec().back()->get_texture());
  288.     }
  289.     else{
  290.         Texture texture = map_itr->second;
  291.         Batches(texture, world_pos, rot, ax_of_rot);    //add an instance of a model to an existing batch
  292.     }
  293. }
  294.  
  295. GLuint Batch::get_instances_byte_size(void){
  296.     GLuint instances_byte_size = (instances.size() * sizeof(glm::mat4));
  297.     return instances_byte_size;
  298. }
  299.  
  300. Batches::Batches(Texture texture, glm::vec3 world_pos, GLfloat rot, glm::vec3 ax_of_rot){
  301.     batches[texture.textureID - 1]->get_instances_vec().push_back(glm::rotate(glm::translate((Camera::get_projectMat()) * (Camera::getWtoVmat()), world_pos),rot,ax_of_rot));//fulltranformation
  302.     batches[texture.textureID - 1]->get_instances_vec().push_back(glm::rotate(glm::translate(glm::mat4(), world_pos), rot,ax_of_rot));//modeltoworld only
  303. }
  304.  
  305. Batches::Batches(SHAPE shape, glm::vec3 world_pos, GLfloat rot, glm::vec3 ax_of_rot){
  306.     batches.emplace_back(new Batch(shape));
  307.     batches[(batches.back()->get_texture().textureID) - 1]->get_instances_vec().push_back(glm::rotate(glm::translate((Camera::get_projectMat()) * (Camera::getWtoVmat()), world_pos),rot,ax_of_rot));//fulltranformation
  308.     batches[(batches.back()->get_texture().textureID) - 1]->get_instances_vec().push_back(glm::rotate(glm::translate(glm::mat4(), world_pos), rot,ax_of_rot));//modeltoworld only
  309. }
  310.  
  311. GLuint Batches::get_batches_vec_vert_byte_size(){
  312.     GLuint total_batches_vec_vert_byte_size = 0;
  313.     for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  314.         total_batches_vec_vert_byte_size += Batches::get_batches_vec()[i]->get_vertsByteSize();
  315.     }
  316.     return total_batches_vec_vert_byte_size;
  317.     Batches::get_batches_vec();
  318. }
  319.  
  320. GLuint Batches::get_batches_vec_ind_byte_size(){
  321.     GLuint total_batches_vec_ind_byte_size = 0;
  322.         for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  323.             total_batches_vec_ind_byte_size += Batches::get_batches_vec()[i]->get_indsByteSize();
  324.         }
  325.     return total_batches_vec_ind_byte_size;
  326. }
  327.  
  328. GLuint Batches::get_total_num_instances_byte_size(void){
  329.     GLuint total_num_instances = 0;
  330.     for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  331.         total_num_instances += (Batches::get_batches_vec()[i]->get_instances_vec()).size();
  332.     }
  333.     GLuint total_num_instances_byte_size = (total_num_instances * sizeof(glm::mat4));
  334.     return total_num_instances_byte_size;
  335. }
  336.  
  337.  
  338. void Batches::set_all_vao_state(void){
  339.  
  340. glGenBuffers(1, &Batches::get_global_vbo());
  341. GLuint vert_byte_offset = 0;
  342. GLuint elem_byte_offset = 0;
  343.  
  344.     for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  345.         glGenVertexArrays(1, &Batches::get_batches_vec()[i]->get_vao());
  346.         glBindVertexArray(Batches::get_batches_vec()[i]->get_vao());
  347.         glBindBuffer(GL_ARRAY_BUFFER, (Batches::get_global_vbo()));
  348.         if (i == 0){
  349.             glBufferData(GL_ARRAY_BUFFER, Batches::get_batches_vec_vert_byte_size(), nullptr, GL_DYNAMIC_DRAW);
  350.         }
  351.         glBufferSubData(GL_ARRAY_BUFFER, vert_byte_offset, Batches::get_batches_vec()[i]->get_vertsByteSize(), Batches::get_batches_vec()[i]->get_vert_Ptr());
  352.         glEnableVertexAttribArray(0);
  353.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 11, (char*)((sizeof(GLfloat) * 0) + vert_byte_offset));
  354.         glEnableVertexAttribArray(1);
  355.         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 11, (char*)((sizeof(GLfloat) * 3) + vert_byte_offset));
  356.         glEnableVertexAttribArray(2);
  357.         glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 11, (char*)((sizeof(GLfloat) * 6) + vert_byte_offset));
  358.         glEnableVertexAttribArray(3);
  359.         glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 11, (char*)((sizeof(GLfloat) * 9) + vert_byte_offset));
  360.         vert_byte_offset += Batches::get_batches_vec()[i]->get_vertsByteSize();
  361.         glBindVertexArray(0);
  362.     }
  363.     for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  364.         glBindVertexArray(Batches::get_batches_vec()[i]->get_vao());
  365.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,Batches::get_global_vbo());
  366.         if (i == 0){
  367.             glBufferData(GL_ELEMENT_ARRAY_BUFFER, Batches::get_batches_vec_ind_byte_size(), nullptr, GL_DYNAMIC_DRAW);
  368.         }
  369.         glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, elem_byte_offset, Batches::get_batches_vec()[i]->get_indsByteSize(), Batches::get_batches_vec()[i]->get_ind_Ptr());
  370.         elem_byte_offset += Batches::get_batches_vec()[i]->get_indsByteSize();
  371.         glBindVertexArray(0);
  372.     }
  373.     glGenBuffers(1, &Batches::get_global_instance_vbo());
  374.     vert_byte_offset = 0;//we now bind gl_array_buffer to a different buffer object, so set vert_byte_offset to 0
  375.     for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  376.         glBindVertexArray(Batches::get_batches_vec()[i]->get_vao());
  377.         glBindBuffer(GL_ARRAY_BUFFER, Batches::get_global_instance_vbo());
  378.         if (i == 0){
  379.             glBufferData(GL_ARRAY_BUFFER, (Batches::get_total_num_instances_byte_size()),
  380.                             nullptr, GL_DYNAMIC_DRAW);
  381.         }
  382.        
  383.         glBufferSubData(GL_ARRAY_BUFFER, vert_byte_offset, Batches::get_batches_vec()[i]->get_instances_byte_size(),
  384.         &(Batches::get_batches_vec()[i]->get_instances_vec()[0]));
  385.         std::cout << std::endl;
  386.        
  387.         glEnableVertexAttribArray(4);
  388.         glEnableVertexAttribArray(5);
  389.         glEnableVertexAttribArray(6);
  390.         glEnableVertexAttribArray(7);
  391.         glEnableVertexAttribArray(8);
  392.         glEnableVertexAttribArray(9);
  393.         glEnableVertexAttribArray(10);
  394.         glEnableVertexAttribArray(11);
  395.  
  396.         glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 0));
  397.         glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 4));
  398.         glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 8));
  399.         glVertexAttribPointer(7, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 12));
  400.         glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 16));
  401.         glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 20));
  402.         glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 24));
  403.         glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::mat4) * 2), (char*)(sizeof(GLfloat) * 28));
  404.  
  405.         glVertexAttribDivisor(4,1);
  406.         glVertexAttribDivisor(5,1);
  407.         glVertexAttribDivisor(6,1);
  408.         glVertexAttribDivisor(7,1);
  409.         glVertexAttribDivisor(8,1);
  410.         glVertexAttribDivisor(9,1);
  411.         glVertexAttribDivisor(10,1);
  412.         glVertexAttribDivisor(11,1);
  413.     }
  414. }
  415.  
  416. void Batches::render_all(void){
  417.     GLint getLocforLightPos = glGetUniformLocation(Shader::programID, "lightPos");
  418.     glm::vec3 lightPos(35.0f,14.5f,10.0f);
  419.     glUniform3fv(getLocforLightPos, 1, &lightPos[0]);
  420.  
  421.     GLint getLocforAmbientLight = glGetUniformLocation(Shader::programID, "ambientLight");
  422.     glm::vec4 ambientLight(0.2f,0.01f,0.03f, 1.0f);
  423.     glUniform4fv(getLocforAmbientLight, 1, &ambientLight[0]);
  424.  
  425.     GLint getLocforEyePosition = glGetUniformLocation(Shader::programID, "eyePosition");
  426.     glm::vec3 eyePosition = Camera::getEyePosition();
  427.     glUniform3fv(getLocforEyePosition, 1, &eyePosition[0]);
  428.  
  429.     GLuint indices_byte_offset = 0;
  430.    
  431.     for (int i = 0; i < Batches::get_batches_vec().size(); i++){
  432.         glBindVertexArray(Batches::get_batches_vec()[i]->get_vao());
  433.         glActiveTexture(GL_TEXTURE0);
  434.         glBindTexture(GL_TEXTURE_2D, Batches::get_batches_vec()[i]->get_texture().textureID);
  435.  
  436.         glDrawElementsInstanced(GL_TRIANGLES, (Batches::get_batches_vec()[i]->get_indsByteSize() / sizeof(GLushort)),
  437.                                 GL_UNSIGNED_SHORT, (void*)indices_byte_offset, (Batches::get_batches_vec()[i]->get_instances_vec().size())/2);
  438.         indices_byte_offset += Batches::get_batches_vec()[i]->get_indsByteSize();
  439.     }
  440. }
  441.  
  442.  
  443. #ifndef CAMERA
  444. #define CAMERA
  445. #include <glm/glm.hpp>
  446. #include <glm/gtc/matrix_transform.hpp>
  447.  
  448. class Camera
  449. {
  450. public:
  451.     static void mouseUpdate(glm::vec2 &newScreenPos);
  452.     static void moveForward();
  453.     static void moveBackward();
  454.     static void moveLeft();
  455.     static void moveRight();
  456.     static const glm::mat4 getWtoVmat() {return glm::lookAt(position, position + viewDir, UP);}
  457.     static const glm::mat4 get_projectMat() {return (glm::perspective(95.0f, 1.28f, 0.1f, 90.0f));}
  458.     static glm::vec3 getEyePosition() {return position;}
  459.     static glm::vec3 getViewDir() {return viewDir;}
  460. private:
  461.     static glm::vec3 position;
  462.     static glm::vec3 viewDir;
  463.     static glm::vec3 UP;
  464.     static glm::vec3 rotationAroundX;
  465.     static glm::vec2 oldScreenPos;
  466. };
  467.  
  468. #endif
  469.  
  470.  
  471. #include "Camera.h"
  472.  
  473. glm::vec3 Camera::position = glm::vec3(12.0f, 15.0f, 36.0f);
  474. glm::vec3 Camera::viewDir = glm::vec3(0.0f, -0.5f, -1.0f);
  475. glm::vec3 Camera::UP = glm::vec3(0.0f, 1.0f, 0.0f);
  476. glm::vec3 Camera::rotationAroundX = glm::vec3(1.0f, 0.0f, 0.0f);
  477. glm::vec2 Camera::oldScreenPos = glm::vec2(0.0f, 0.0f);
  478.  
  479. void Camera::mouseUpdate(glm::vec2 &newScreenPos){
  480.     glm::vec2 mouseDelta = newScreenPos - oldScreenPos;
  481.     if (abs (mouseDelta.x) > 120.0f)
  482.         mouseDelta.x = 0.0f;
  483.     if (abs (mouseDelta.y) > 120.0f)
  484.         mouseDelta.y = 0.0f;
  485.     viewDir = glm::mat3 (glm::rotate(glm::mat4(),-mouseDelta.x * 0.20f, UP)) * viewDir; // sets x motion
  486.     viewDir = glm::mat3 (glm::rotate(glm::mat4(),-mouseDelta.y * 0.22f, rotationAroundX)) * viewDir; //sets y motion
  487.     rotationAroundX = glm::mat3 (glm::rotate(glm::mat4(),-mouseDelta.x * 0.20f, UP)) * rotationAroundX;// updates rotationAroundX vector
  488.     // to be perp to UP and viewDir
  489.     oldScreenPos = newScreenPos;
  490. }
  491.  
  492. void Camera::moveForward(){
  493.         position += (0.2f * viewDir);
  494. }
  495. void Camera::moveBackward(){
  496.         position += (-0.2f * viewDir);
  497. }
  498. void Camera::moveLeft(){
  499.         position += (-0.2f * rotationAroundX);
  500. }
  501. void Camera::moveRight(){
  502.         position += (0.2f * rotationAroundX);
  503. }
  504.  
  505. //shapeData.h
  506. #ifndef SHAPE_DATA
  507. #define SHAPE_DATA
  508.  
  509. #define GLEW_STATIC
  510. #include <GL/glew.h>
  511. #include "Vertex.h"
  512.  
  513.  
  514. enum SHAPE {CUBE, SMALL_CUBE, FLOOR_TILE, TINY_CUBE};
  515. struct shapeData{
  516.     static Vertex* Verts;
  517.     static GLushort* Indices;
  518.     static Vertex*  getVerts(SHAPE);
  519.     static GLushort* getInds(SHAPE);
  520.     static GLushort VertsByteSize;
  521.     static GLushort IndicesByteSize;
  522.     static GLushort get_vertsByteSize(void);
  523.     static GLushort get_indsByteSize(void);
  524. };
  525.  
  526. #endif
  527.  
  528.  
  529. #include "shapeData.h"
  530.  
  531. Vertex* shapeData::Verts;
  532. GLushort* shapeData::Indices;
  533. GLushort shapeData::VertsByteSize;
  534. GLushort shapeData::IndicesByteSize;
  535.  
  536. Vertex* shapeData::getVerts(SHAPE shape) {
  537.     switch(shape){
  538.     case CUBE:
  539.         {
  540.             Vertex cubeDat[] = {                       
  541.                         glm::vec3(-5.0f, -5.0f, -5.0f),
  542.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  543.                         glm::vec3(0.0f, -1.0f, 0.0f),
  544.                         glm::vec2(0.0, 0.0),
  545.  
  546.                         glm::vec3(-5.0f, -5.0f, 5.0f),
  547.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  548.                         glm::vec3(0.0f, -1.0f, 0.0f),
  549.                         glm::vec2(0.0, 1.0),
  550.                        
  551.                         glm::vec3(+5.0f, -5.0f, 5.0f),
  552.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  553.                         glm::vec3(0.0f, -1.0f, 0.0f),
  554.                         glm::vec2(1.0, 1.0),
  555.  
  556.                         glm::vec3(+5.0f, -5.0f, -5.0f),
  557.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  558.                         glm::vec3(0.0f, -1.0f, 0.0f),
  559.                         glm::vec2(1.0, 0.0),//good great 6 lower
  560.  
  561.                         glm::vec3(-5.0f, 5.0f, -5.0f),
  562.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  563.                         glm::vec3(0.0f, 1.0f, 0.0f),
  564.                         glm::vec2(0.0, 1.0),
  565.  
  566.                         glm::vec3(-5.0f, 5.0f, 5.0f),
  567.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  568.                         glm::vec3(0.0f, 1.0f, 0.0f),
  569.                         glm::vec2(0.0, 0.0),
  570.                        
  571.                         glm::vec3(+5.0f, 5.0f, 5.0f),
  572.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  573.                         glm::vec3(0.0f, 1.0f, 0.0f),
  574.                         glm::vec2(1.0, 0.0),
  575.  
  576.                         glm::vec3(+5.0f, 5.0f, -5.0f),
  577.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  578.                         glm::vec3(0.0f, 1.0f, 0.0f),
  579.                         glm::vec2(1.0, 1.0),////////good great 1 upper
  580.                        
  581.                         glm::vec3(-5.0f, -5.0f, -5.0f),
  582.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  583.                         glm::vec3(-1.0f, 0.0f, 0.0f),
  584.                         glm::vec2(0.0, 0.0),
  585.  
  586.                         glm::vec3(-5.0f, -5.0f, 5.0f),
  587.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  588.                         glm::vec3(-1.0f, 0.0f, 0.0f),
  589.                         glm::vec2(1.0, 0.0),
  590.  
  591.                         glm::vec3(-5.0f, +5.0f, 5.0f),
  592.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  593.                         glm::vec3(-1.0f, 0.0f, 0.0f),
  594.                         glm::vec2(1.0, 1.0),
  595.  
  596.                         glm::vec3(-5.0f, +5.0f, -5.0f),
  597.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  598.                         glm::vec3(-1.0f, 0.0f, 0.0f),//good great 3 left
  599.                         glm::vec2(0.0, 1.0),
  600.  
  601.                         glm::vec3(5.0f, -5.0f, -5.0f), ////
  602.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  603.                         glm::vec3(1.0f, 0.0f, 0.0f),
  604.                         glm::vec2(1.0, 0.0),
  605.  
  606.                         glm::vec3(5.0f, -5.0f, 5.0f),
  607.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  608.                         glm::vec3(1.0f, 0.0f, 0.0f),
  609.                         glm::vec2(0.0, 0.0),
  610.  
  611.                         glm::vec3(+5.0f, +5.0f, 5.0f),
  612.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  613.                         glm::vec3(1.0f, 0.0f, 0.0f),
  614.                         glm::vec2(0.0, 1.0),
  615.  
  616.                         glm::vec3(+5.0f, +5.0f, -5.0f),
  617.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  618.                         glm::vec3(1.0f, 0.0f, 0.0f),//good great 5 right
  619.                         glm::vec2(1.0, 1.0),
  620.                         /////
  621.                         glm::vec3(-5.0f, -5.0f, -5.0f),
  622.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  623.                         glm::vec3(0.0f, 0.0f, -1.0f),
  624.                         glm::vec2(1.0, 0.0),
  625.  
  626.                         glm::vec3(-5.0f, +5.0f, -5.0f),
  627.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  628.                         glm::vec3(0.0f, 0.0f, -1.0f),  
  629.                         glm::vec2(1.0, 1.0),
  630.  
  631.                         glm::vec3(+5.0f, +5.0f, -5.0f),
  632.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  633.                         glm::vec3(0.0f, 0.0f, -1.0f),
  634.                         glm::vec2(0.0, 1.0),
  635.  
  636.                         glm::vec3(+5.0f, -5.0f, -5.0f),
  637.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  638.                         glm::vec3(0.0f, 0.0f, -1.0f),//good great 4 back
  639.                         glm::vec2(0.0, 0.0),
  640.  
  641.                         glm::vec3(-5.0f, -5.0f, 5.0f),
  642.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  643.                         glm::vec3(0.0f, 0.0f, 1.0f),
  644.                         glm::vec2(0.0, 0.0),
  645.  
  646.                         glm::vec3(-5.0f, +5.0f, 5.0f),
  647.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  648.                         glm::vec3(0.0f, 0.0f, 1.0f),
  649.                         glm::vec2(0.0, 1.0),
  650.  
  651.                         glm::vec3(+5.0f, +5.0f, 5.0f),
  652.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  653.                         glm::vec3(0.0f, 0.0f, 1.0f),
  654.                         glm::vec2(1.0, 1.0),
  655.  
  656.                         glm::vec3(+5.0f, -5.0f, 5.0f),
  657.                         glm::vec3(+0.5f, +0.5f, +0.5f),
  658.                         glm::vec3(0.0f, 0.0f, 1.0f),//good great 2 front
  659.                         glm::vec2(1.0, 0.0)
  660.                        
  661.                     };
  662.             VertsByteSize = sizeof(cubeDat);
  663.             Verts = new Vertex[sizeof(cubeDat)/sizeof(*cubeDat)];
  664.             memcpy(Verts, cubeDat, (sizeof(cubeDat)/sizeof(*cubeDat)) * sizeof(Vertex));
  665.         }
  666.         break;
  667.     case SMALL_CUBE:
  668.         {
  669.             Vertex cubeDat[] = {                       
  670.                     glm::vec3(-2.5f, -2.5f, -2.5f),
  671.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  672.                     glm::vec3(0.0f, -1.0f, 0.0f),
  673.                     glm::vec2(0.0, 0.0),
  674.  
  675.                     glm::vec3(-2.5f, -2.5f, 2.5f),
  676.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  677.                     glm::vec3(0.0f, -1.0f, 0.0f),
  678.                     glm::vec2(0.0, 1.0),
  679.                        
  680.                     glm::vec3(+2.5f, -2.5f, 2.5f),
  681.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  682.                     glm::vec3(0.0f, -1.0f, 0.0f),
  683.                     glm::vec2(1.0, 1.0),
  684.  
  685.                     glm::vec3(+2.5f, -2.5f, -2.5f),
  686.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  687.                     glm::vec3(0.0f, -1.0f, 0.0f),
  688.                     glm::vec2(1.0, 0.0),//good great 6 lower
  689.  
  690.                     glm::vec3(-2.5f, 2.5f, -2.5f),
  691.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  692.                     glm::vec3(0.0f, 1.0f, 0.0f),
  693.                     glm::vec2(0.0, 1.0),
  694.  
  695.                     glm::vec3(-2.5f, 2.5f, 2.5f),
  696.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  697.                     glm::vec3(0.0f, 1.0f, 0.0f),
  698.                     glm::vec2(0.0, 0.0),
  699.                        
  700.                     glm::vec3(+2.5f, 2.5f, 2.5f),
  701.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  702.                     glm::vec3(0.0f, 1.0f, 0.0f),
  703.                     glm::vec2(1.0, 0.0),
  704.  
  705.                     glm::vec3(+2.5f, 2.5f, -2.5f),
  706.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  707.                     glm::vec3(0.0f, 1.0f, 0.0f),
  708.                     glm::vec2(1.0, 1.0),////////good great 1 upper
  709.                        
  710.                     glm::vec3(-2.5f, -2.5f, -2.5f),
  711.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  712.                     glm::vec3(-1.0f, 0.0f, 0.0f),
  713.                     glm::vec2(0.0, 0.0),
  714.  
  715.                     glm::vec3(-2.5f, -2.5f, 2.5f),
  716.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  717.                     glm::vec3(-1.0f, 0.0f, 0.0f),
  718.                     glm::vec2(1.0, 0.0),
  719.  
  720.                     glm::vec3(-2.5f, +2.5f, 2.5f),
  721.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  722.                     glm::vec3(-1.0f, 0.0f, 0.0f),
  723.                     glm::vec2(1.0, 1.0),
  724.  
  725.                     glm::vec3(-2.5f, +2.5f, -2.5f),
  726.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  727.                     glm::vec3(-1.0f, 0.0f, 0.0f),//good great 3 left
  728.                     glm::vec2(0.0, 1.0),
  729.  
  730.                     glm::vec3(2.5f, -2.5f, -2.5f),
  731.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  732.                     glm::vec3(1.0f, 0.0f, 0.0f),
  733.                     glm::vec2(1.0, 0.0),
  734.  
  735.                     glm::vec3(2.5f, -2.5f, 2.5f),
  736.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  737.                     glm::vec3(1.0f, 0.0f, 0.0f),
  738.                     glm::vec2(0.0, 0.0),
  739.  
  740.                     glm::vec3(+2.5f, +2.5f, 2.5f),
  741.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  742.                     glm::vec3(1.0f, 0.0f, 0.0f),
  743.                     glm::vec2(0.0, 1.0),
  744.  
  745.                     glm::vec3(+2.5f, +2.5f, -2.5f),
  746.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  747.                     glm::vec3(1.0f, 0.0f, 0.0f),//good great 5 right
  748.                     glm::vec2(1.0, 1.0),
  749.                     /////
  750.                     glm::vec3(-2.5f, -2.5f, -2.5f),
  751.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  752.                     glm::vec3(0.0f, 0.0f, -1.0f),
  753.                     glm::vec2(1.0, 0.0),
  754.  
  755.                     glm::vec3(-2.5f, +2.5f, -2.5f),
  756.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  757.                     glm::vec3(0.0f, 0.0f, -1.0f),  
  758.                     glm::vec2(1.0, 1.0),
  759.  
  760.                     glm::vec3(+2.5f, +2.5f, -2.5f),
  761.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  762.                     glm::vec3(0.0f, 0.0f, -1.0f),
  763.                     glm::vec2(0.0, 1.0),
  764.  
  765.                     glm::vec3(+2.5f, -2.5f, -2.5f),
  766.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  767.                     glm::vec3(0.0f, 0.0f, -1.0f),//good great 4 back
  768.                     glm::vec2(0.0, 0.0),
  769.  
  770.                     glm::vec3(-2.5f, -2.5f, 2.5f),
  771.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  772.                     glm::vec3(0.0f, 0.0f, 1.0f),
  773.                     glm::vec2(0.0, 0.0),
  774.  
  775.                     glm::vec3(-2.5f, +2.5f, 2.5f),
  776.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  777.                     glm::vec3(0.0f, 0.0f, 1.0f),
  778.                     glm::vec2(0.0, 1.0),
  779.  
  780.                     glm::vec3(+2.5f, +2.5f, 2.5f),
  781.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  782.                     glm::vec3(0.0f, 0.0f, 1.0f),
  783.                     glm::vec2(1.0, 1.0),
  784.  
  785.                     glm::vec3(+2.5f, -2.5f, 2.5f),
  786.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  787.                     glm::vec3(0.0f, 0.0f, 1.0f),//good great 2 front
  788.                     glm::vec2(1.0, 0.0)    
  789.                 };
  790.             VertsByteSize = sizeof(cubeDat);
  791.             Verts = new Vertex[sizeof(cubeDat)/sizeof(*cubeDat)];
  792.             memcpy(Verts, cubeDat, (sizeof(cubeDat)/sizeof(*cubeDat)) * sizeof(Vertex));
  793.     }
  794.     break;
  795.     case FLOOR_TILE:
  796.         {
  797.             Vertex cubeDat[] = {                       
  798.                     glm::vec3(-5.0f, 5.0f, -5.0f),
  799.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  800.                     glm::vec3(0.0f, 1.0f, 0.0f),
  801.                     glm::vec2(0.0, 1.0),
  802.  
  803.                     glm::vec3(-5.0f, 5.0f, 5.0f),
  804.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  805.                     glm::vec3(0.0f, 1.0f, 0.0f),
  806.                     glm::vec2(0.0, 0.0),
  807.                        
  808.                     glm::vec3(+5.0f, 5.0f, 5.0f),
  809.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  810.                     glm::vec3(0.0f, 1.0f, 0.0f),
  811.                     glm::vec2(1.0, 0.0),
  812.  
  813.                     glm::vec3(+5.0f, 5.0f, -5.0f),
  814.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  815.                     glm::vec3(0.0f, 1.0f, 0.0f),
  816.                     glm::vec2(1.0, 1.0),////////good great 1 upper
  817.                 };
  818.             VertsByteSize = sizeof(cubeDat);
  819.             Verts = new Vertex[sizeof(cubeDat)/sizeof(*cubeDat)];
  820.             memcpy(Verts, cubeDat, (sizeof(cubeDat)/sizeof(*cubeDat)) * sizeof(Vertex));
  821.     }
  822.     break;
  823.     case TINY_CUBE:
  824.         {
  825.             Vertex cubeDat[] = {                       
  826.                     glm::vec3(-0.5f, -0.5f, -0.5f),
  827.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  828.                     glm::vec3(0.0f, -1.0f, 0.0f),
  829.                     glm::vec2(0.0, 0.0),
  830.  
  831.                     glm::vec3(-0.5f, -0.5f, 0.5f),
  832.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  833.                     glm::vec3(0.0f, -1.0f, 0.0f),
  834.                     glm::vec2(0.0, 1.0),
  835.                        
  836.                     glm::vec3(+0.5f, -0.5f, 0.5f),
  837.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  838.                     glm::vec3(0.0f, -1.0f, 0.0f),
  839.                     glm::vec2(1.0, 1.0),
  840.  
  841.                     glm::vec3(+0.5f, -0.5f, -0.5f),
  842.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  843.                     glm::vec3(0.0f, -1.0f, 0.0f),
  844.                     glm::vec2(1.0, 0.0),//good great 6 lower
  845.  
  846.                     glm::vec3(-0.5f, 0.5f, -0.5f),
  847.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  848.                     glm::vec3(0.0f, 1.0f, 0.0f),
  849.                     glm::vec2(0.0, 1.0),
  850.  
  851.                     glm::vec3(-0.5f, 0.5f, 0.5f),
  852.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  853.                     glm::vec3(0.0f, 1.0f, 0.0f),
  854.                     glm::vec2(0.0, 0.0),
  855.                        
  856.                     glm::vec3(+0.5f, 0.5f, 0.5f),
  857.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  858.                     glm::vec3(0.0f, 1.0f, 0.0f),
  859.                     glm::vec2(1.0, 0.0),
  860.  
  861.                     glm::vec3(+0.5f, 0.5f, -0.5f),
  862.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  863.                     glm::vec3(0.0f, 1.0f, 0.0f),
  864.                     glm::vec2(1.0, 1.0),////////good great 1 upper
  865.                        
  866.                     glm::vec3(-0.5f, -0.5f, -0.5f),
  867.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  868.                     glm::vec3(-1.0f, 0.0f, 0.0f),
  869.                     glm::vec2(0.0, 0.0),
  870.  
  871.                     glm::vec3(-0.5f, -0.5f, 0.5f),
  872.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  873.                     glm::vec3(-1.0f, 0.0f, 0.0f),
  874.                     glm::vec2(1.0, 0.0),
  875.  
  876.                     glm::vec3(-0.5f, +0.5f, 0.5f),
  877.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  878.                     glm::vec3(-1.0f, 0.0f, 0.0f),
  879.                     glm::vec2(1.0, 1.0),
  880.  
  881.                     glm::vec3(-0.5f, +0.5f, -0.5f),
  882.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  883.                     glm::vec3(-1.0f, 0.0f, 0.0f),//good great 3 left
  884.                     glm::vec2(0.0, 1.0),
  885.  
  886.                     glm::vec3(0.5f, -0.5f, -0.5f),
  887.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  888.                     glm::vec3(1.0f, 0.0f, 0.0f),
  889.                     glm::vec2(1.0, 0.0),
  890.  
  891.                     glm::vec3(0.5f, -0.5f, 0.5f),
  892.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  893.                     glm::vec3(1.0f, 0.0f, 0.0f),
  894.                     glm::vec2(0.0, 0.0),
  895.  
  896.                     glm::vec3(+0.5f, +0.5f, 0.5f),
  897.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  898.                     glm::vec3(1.0f, 0.0f, 0.0f),
  899.                     glm::vec2(0.0, 1.0),
  900.  
  901.                     glm::vec3(+0.5f, +0.5f, -0.5f),
  902.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  903.                     glm::vec3(1.0f, 0.0f, 0.0f),//good great 5 right
  904.                     glm::vec2(1.0, 1.0),
  905.                     /////
  906.                     glm::vec3(-0.5f, -0.5f, -0.5f),
  907.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  908.                     glm::vec3(0.0f, 0.0f, -1.0f),
  909.                     glm::vec2(1.0, 0.0),
  910.  
  911.                     glm::vec3(-0.5f, +0.5f, -0.5f),
  912.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  913.                     glm::vec3(0.0f, 0.0f, -1.0f),  
  914.                     glm::vec2(1.0, 1.0),
  915.  
  916.                     glm::vec3(+0.5f, +0.5f, -0.5f),
  917.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  918.                     glm::vec3(0.0f, 0.0f, -1.0f),
  919.                     glm::vec2(0.0, 1.0),
  920.  
  921.                     glm::vec3(+0.5f, -0.5f, -0.5f),
  922.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  923.                     glm::vec3(0.0f, 0.0f, -1.0f),//good great 4 back
  924.                     glm::vec2(0.0, 0.0),
  925.  
  926.                     glm::vec3(-0.5f, -0.5f, 0.5f),
  927.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  928.                     glm::vec3(0.0f, 0.0f, 1.0f),
  929.                     glm::vec2(0.0, 0.0),
  930.  
  931.                     glm::vec3(-0.5f, +0.5f, 0.5f),
  932.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  933.                     glm::vec3(0.0f, 0.0f, 1.0f),
  934.                     glm::vec2(0.0, 1.0),
  935.  
  936.                     glm::vec3(+0.5f, +0.5f, 0.5f),
  937.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  938.                     glm::vec3(0.0f, 0.0f, 1.0f),
  939.                     glm::vec2(1.0, 1.0),
  940.  
  941.                     glm::vec3(+0.5f, -0.5f, 0.5f),
  942.                     glm::vec3(+0.5f, +0.5f, +0.5f),
  943.                     glm::vec3(0.0f, 0.0f, 1.0f),//good great 2 front
  944.                     glm::vec2(1.0, 0.0)    
  945.                 };
  946.             VertsByteSize = sizeof(cubeDat);
  947.             Verts = new Vertex[sizeof(cubeDat)/sizeof(*cubeDat)];
  948.             memcpy(Verts, cubeDat, (sizeof(cubeDat)/sizeof(*cubeDat)) * sizeof(Vertex));
  949.     }
  950.     break;
  951.     }
  952.     return Verts;
  953. }
  954.  
  955. GLushort* shapeData:: getInds(SHAPE shape) {
  956.     switch(shape){
  957.     case CUBE:
  958.         {
  959.             GLushort indices[] = {2,1,0, 0,3,2, 4,5,6, 6,7,4, 8,9,10, 10,11,8, 13,12,15, 15,14,13, 19,16,17, 17,18,19, 20,23,22, 22,21,20};
  960.            
  961.             IndicesByteSize = sizeof(indices);
  962.  
  963.             Indices = new GLushort [sizeof(indices)/sizeof(*indices)];
  964.             memcpy(Indices, indices, (sizeof(indices)/sizeof(*indices)) * sizeof(GLushort));
  965.         }
  966.     break;
  967.     case SMALL_CUBE: //I know I dont actually need this shape since I already have CUBE, just added it for variety
  968.         {
  969.             GLushort indices[] = {2,1,0, 0,3,2, 4,5,6, 6,7,4, 8,9,10, 10,11,8, 13,12,15, 15,14,13, 19,16,17, 17,18,19, 20,23,22, 22,21,20};
  970.            
  971.             IndicesByteSize = sizeof(indices);
  972.  
  973.             Indices = new GLushort [sizeof(indices)/sizeof(*indices)];
  974.             memcpy(Indices, indices, (sizeof(indices)/sizeof(*indices)) * sizeof(GLushort));
  975.         }
  976.     break;
  977.     case FLOOR_TILE:
  978.         {
  979.             GLushort indices[] = {0,1,2, 2,3,0};
  980.             IndicesByteSize = sizeof(indices);
  981.  
  982.             Indices = new GLushort [sizeof(indices)/sizeof(*indices)];
  983.             memcpy(Indices, indices, (sizeof(indices)/sizeof(*indices)) * sizeof(GLushort));
  984.         }
  985.     break;
  986.     }
  987.  
  988.     return Indices;
  989. }
  990.                    
  991.                    
  992. GLushort shapeData::get_vertsByteSize(void){
  993.     return VertsByteSize;
  994. }
  995. GLushort shapeData::get_indsByteSize(void){
  996.     return IndicesByteSize;
  997. }
  998.  
  999.  
  1000. #ifndef TEXTURE
  1001. #define TEXTURE
  1002.  
  1003. #include "Texture.h"
  1004. #include <string>
  1005. #include <fstream>
  1006. #include <vector>
  1007. #include <iostream>
  1008.  
  1009. class loadTexture
  1010. {
  1011. public:
  1012.     static void loadBufferToOpengl(std::string filePath, Texture &);
  1013.     static Texture genTexture(void);
  1014. private:
  1015.     static void fileToBuffer(std::string,std::vector <unsigned char>&);
  1016.     static int decodePNGbuffer(std::vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32 = true);
  1017. };
  1018.  
  1019. #endif
  1020.  
  1021.  
  1022. #include "loadTexture.h"
  1023.  
  1024.  
  1025. void loadTexture::fileToBuffer(std::string path, std::vector <unsigned char> &buffer)
  1026. {
  1027.    
  1028.     std::ifstream textureFile(path, std::ios::binary);
  1029.     if(textureFile.fail())
  1030.         perror(path.c_str());
  1031.  
  1032.     textureFile.seekg(0, std::ios::end);
  1033.     int fileSize = textureFile.tellg();
  1034.     textureFile.seekg(0, std::ios::beg);
  1035.  
  1036.     fileSize -= textureFile.tellg();
  1037.  
  1038.     buffer.resize(fileSize);
  1039.     textureFile.read((char *)&(buffer[0]),fileSize);
  1040. }
  1041.  
  1042. /*
  1043. decodePNG: The picoPNG function, decodes a PNG file buffer in memory, into a raw pixel buffer.
  1044. out_image: output parameter, this will contain the raw pixels after decoding.
  1045.   By default the output is 32-bit RGBA color.
  1046.   The std::vector is automatically resized to the correct size.
  1047. image_width: output_parameter, this will contain the width of the image in pixels.
  1048. image_height: output_parameter, this will contain the height of the image in pixels.
  1049. in_png: pointer to the buffer of the PNG file in memory. To get it from a file on
  1050.   disk, load it and store it in a memory buffer yourself first.
  1051. in_size: size of the input PNG file in bytes.
  1052. convert_to_rgba32: optional parameter, true by default.
  1053.   Set to true to get the output in RGBA 32-bit (8 bit per channel) color format
  1054.   no matter what color type the original PNG image had. This gives predictable,
  1055.   useable data from any random input PNG.
  1056.   Set to false to do no color conversion at all. The result then has the same data
  1057.   type as the PNG image, which can range from 1 bit to 64 bits per pixel.
  1058.   Information about the color type or palette colors are not provided. You need
  1059.   to know this information yourself to be able to use the data so this only
  1060.   works for trusted PNG files. Use LodePNG instead of picoPNG if you need this information.
  1061. return: 0 if success, not 0 if some error occured.
  1062. */
  1063. int loadTexture::decodePNGbuffer(std::vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32)
  1064. {
  1065.   // picoPNG version 20101224
  1066.   // Copyright (c) 2005-2010 Lode Vandevenne
  1067.   //
  1068.   // This software is provided 'as-is', without any express or implied
  1069.   // warranty. In no event will the authors be held liable for any damages
  1070.   // arising from the use of this software.
  1071.   //
  1072.   // Permission is granted to anyone to use this software for any purpose,
  1073.   // including commercial applications, and to alter it and redistribute it
  1074.   // freely, subject to the following restrictions:
  1075.   //
  1076.   //     1. The origin of this software must not be misrepresented; you must not
  1077.   //     claim that you wrote the original software. If you use this software
  1078.   //     in a product, an acknowledgment in the product documentation would be
  1079.   //     appreciated but is not required.
  1080.   //     2. Altered source versions must be plainly marked as such, and must not be
  1081.   //     misrepresented as being the original software.
  1082.   //     3. This notice may not be removed or altered from any source distribution.
  1083.  
  1084.   // picoPNG is a PNG decoder in one C++ function of around 500 lines. Use picoPNG for
  1085.   // programs that need only 1 .cpp file. Since it's a single function, it's very limited,
  1086.   // it can convert a PNG to raw pixel data either converted to 32-bit RGBA color or
  1087.   // with no color conversion at all. For anything more complex, another tiny library
  1088.   // is available: LodePNG (lodepng.c(pp)), which is a single source and header file.
  1089.   // Apologies for the compact code style, it's to make this tiny.
  1090.  
  1091.   static const unsigned long LENBASE[29] =  {3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258};
  1092.   static const unsigned long LENEXTRA[29] = {0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,  4,  5,  5,  5,  5,  0};
  1093.   static const unsigned long DISTBASE[30] =  {1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577};
  1094.   static const unsigned long DISTEXTRA[30] = {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5,  6,  6,  7,  7,  8,  8,   9,   9,  10,  10,  11,  11,  12,   12,   13,   13};
  1095.   static const unsigned long CLCL[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; //code length code lengths
  1096.   struct Zlib //nested functions for zlib decompression
  1097.   {
  1098.     static unsigned long readBitFromStream(size_t& bitp, const unsigned char* bits) { unsigned long result = (bits[bitp >> 3] >> (bitp & 0x7)) & 1; bitp++; return result;}
  1099.     static unsigned long readBitsFromStream(size_t& bitp, const unsigned char* bits, size_t nbits)
  1100.     {
  1101.       unsigned long result = 0;
  1102.       for(size_t i = 0; i < nbits; i++) result += (readBitFromStream(bitp, bits)) << i;
  1103.       return result;
  1104.     }
  1105.     struct HuffmanTree
  1106.     {
  1107.       int makeFromLengths(const std::vector<unsigned long>& bitlen, unsigned long maxbitlen)
  1108.       { //make tree given the lengths
  1109.         unsigned long numcodes = (unsigned long)(bitlen.size()), treepos = 0, nodefilled = 0;
  1110.         std::vector<unsigned long> tree1d(numcodes), blcount(maxbitlen + 1, 0), nextcode(maxbitlen + 1, 0);
  1111.         for(unsigned long bits = 0; bits < numcodes; bits++) blcount[bitlen[bits]]++; //count number of instances of each code length
  1112.         for(unsigned long bits = 1; bits <= maxbitlen; bits++) nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1;
  1113.         for(unsigned long n = 0; n < numcodes; n++) if(bitlen[n] != 0) tree1d[n] = nextcode[bitlen[n]]++; //generate all the codes
  1114.         tree2d.clear(); tree2d.resize(numcodes * 2, 32767); //32767 here means the tree2d isn't filled there yet
  1115.         for(unsigned long n = 0; n < numcodes; n++) //the codes
  1116.         for(unsigned long i = 0; i < bitlen[n]; i++) //the bits for this code
  1117.         {
  1118.           unsigned long bit = (tree1d[n] >> (bitlen[n] - i - 1)) & 1;
  1119.           if(treepos > numcodes - 2) return 55;
  1120.           if(tree2d[2 * treepos + bit] == 32767) //not yet filled in
  1121.           {
  1122.             if(i + 1 == bitlen[n]) { tree2d[2 * treepos + bit] = n; treepos = 0; } //last bit
  1123.             else { tree2d[2 * treepos + bit] = ++nodefilled + numcodes; treepos = nodefilled; } //addresses are encoded as values > numcodes
  1124.           }
  1125.           else treepos = tree2d[2 * treepos + bit] - numcodes; //subtract numcodes from address to get address value
  1126.         }
  1127.         return 0;
  1128.       }
  1129.       int decode(bool& decoded, unsigned long& result, size_t& treepos, unsigned long bit) const
  1130.       { //Decodes a symbol from the tree
  1131.         unsigned long numcodes = (unsigned long)tree2d.size() / 2;
  1132.         if(treepos >= numcodes) return 11; //error: you appeared outside the codetree
  1133.         result = tree2d[2 * treepos + bit];
  1134.         decoded = (result < numcodes);
  1135.         treepos = decoded ? 0 : result - numcodes;
  1136.         return 0;
  1137.       }
  1138.       std::vector<unsigned long> tree2d; //2D representation of a huffman tree: The one dimension is "0" or "1", the other contains all nodes and leaves of the tree.
  1139.     };
  1140.     struct Inflator
  1141.     {
  1142.       int error;
  1143.       void inflate(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, size_t inpos = 0)
  1144.       {
  1145.         size_t bp = 0, pos = 0; //bit pointer and byte pointer
  1146.         error = 0;
  1147.         unsigned long BFINAL = 0;
  1148.         while(!BFINAL && !error)
  1149.         {
  1150.           if(bp >> 3 >= in.size()) { error = 52; return; } //error, bit pointer will jump past memory
  1151.           BFINAL = readBitFromStream(bp, &in[inpos]);
  1152.           unsigned long BTYPE = readBitFromStream(bp, &in[inpos]); BTYPE += 2 * readBitFromStream(bp, &in[inpos]);
  1153.           if(BTYPE == 3) { error = 20; return; } //error: invalid BTYPE
  1154.           else if(BTYPE == 0) inflateNoCompression(out, &in[inpos], bp, pos, in.size());
  1155.           else inflateHuffmanBlock(out, &in[inpos], bp, pos, in.size(), BTYPE);
  1156.         }
  1157.         if(!error) out.resize(pos); //Only now we know the true size of out, resize it to that
  1158.       }
  1159.       void generateFixedTrees(HuffmanTree& tree, HuffmanTree& treeD) //get the tree of a deflated block with fixed tree
  1160.       {
  1161.         std::vector<unsigned long> bitlen(288, 8), bitlenD(32, 5);;
  1162.         for(size_t i = 144; i <= 255; i++) bitlen[i] = 9;
  1163.         for(size_t i = 256; i <= 279; i++) bitlen[i] = 7;
  1164.         tree.makeFromLengths(bitlen, 15);
  1165.         treeD.makeFromLengths(bitlenD, 15);
  1166.       }
  1167.       HuffmanTree codetree, codetreeD, codelengthcodetree; //the code tree for Huffman codes, dist codes, and code length codes
  1168.       unsigned long huffmanDecodeSymbol(const unsigned char* in, size_t& bp, const HuffmanTree& codetree, size_t inlength)
  1169.       { //decode a single symbol from given list of bits with given code tree. return value is the symbol
  1170.         bool decoded; unsigned long ct;
  1171.         for(size_t treepos = 0;;)
  1172.         {
  1173.           if((bp & 0x07) == 0 && (bp >> 3) > inlength) { error = 10; return 0; } //error: end reached without endcode
  1174.           error = codetree.decode(decoded, ct, treepos, readBitFromStream(bp, in)); if(error) return 0; //stop, an error happened
  1175.           if(decoded) return ct;
  1176.         }
  1177.       }
  1178.       void getTreeInflateDynamic(HuffmanTree& tree, HuffmanTree& treeD, const unsigned char* in, size_t& bp, size_t inlength)
  1179.       { //get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree
  1180.         std::vector<unsigned long> bitlen(288, 0), bitlenD(32, 0);
  1181.         if(bp >> 3 >= inlength - 2) { error = 49; return; } //the bit pointer is or will go past the memory
  1182.         size_t HLIT =  readBitsFromStream(bp, in, 5) + 257; //number of literal/length codes + 257
  1183.         size_t HDIST = readBitsFromStream(bp, in, 5) + 1; //number of dist codes + 1
  1184.         size_t HCLEN = readBitsFromStream(bp, in, 4) + 4; //number of code length codes + 4
  1185.         std::vector<unsigned long> codelengthcode(19); //lengths of tree to decode the lengths of the dynamic tree
  1186.         for(size_t i = 0; i < 19; i++) codelengthcode[CLCL[i]] = (i < HCLEN) ? readBitsFromStream(bp, in, 3) : 0;
  1187.         error = codelengthcodetree.makeFromLengths(codelengthcode, 7); if(error) return;
  1188.         size_t i = 0, replength;
  1189.         while(i < HLIT + HDIST)
  1190.         {
  1191.           unsigned long code = huffmanDecodeSymbol(in, bp, codelengthcodetree, inlength); if(error) return;
  1192.           if(code <= 15)  { if(i < HLIT) bitlen[i++] = code; else bitlenD[i++ - HLIT] = code; } //a length code
  1193.           else if(code == 16) //repeat previous
  1194.           {
  1195.             if(bp >> 3 >= inlength) { error = 50; return; } //error, bit pointer jumps past memory
  1196.             replength = 3 + readBitsFromStream(bp, in, 2);
  1197.             unsigned long value; //set value to the previous code
  1198.             if((i - 1) < HLIT) value = bitlen[i - 1];
  1199.             else value = bitlenD[i - HLIT - 1];
  1200.             for(size_t n = 0; n < replength; n++) //repeat this value in the next lengths
  1201.             {
  1202.               if(i >= HLIT + HDIST) { error = 13; return; } //error: i is larger than the amount of codes
  1203.               if(i < HLIT) bitlen[i++] = value; else bitlenD[i++ - HLIT] = value;
  1204.             }
  1205.           }
  1206.           else if(code == 17) //repeat "0" 3-10 times
  1207.           {
  1208.             if(bp >> 3 >= inlength) { error = 50; return; } //error, bit pointer jumps past memory
  1209.             replength = 3 + readBitsFromStream(bp, in, 3);
  1210.             for(size_t n = 0; n < replength; n++) //repeat this value in the next lengths
  1211.             {
  1212.               if(i >= HLIT + HDIST) { error = 14; return; } //error: i is larger than the amount of codes
  1213.               if(i < HLIT) bitlen[i++] = 0; else bitlenD[i++ - HLIT] = 0;
  1214.             }
  1215.           }
  1216.           else if(code == 18) //repeat "0" 11-138 times
  1217.           {
  1218.             if(bp >> 3 >= inlength) { error = 50; return; } //error, bit pointer jumps past memory
  1219.             replength = 11 + readBitsFromStream(bp, in, 7);
  1220.             for(size_t n = 0; n < replength; n++) //repeat this value in the next lengths
  1221.             {
  1222.               if(i >= HLIT + HDIST) { error = 15; return; } //error: i is larger than the amount of codes
  1223.               if(i < HLIT) bitlen[i++] = 0; else bitlenD[i++ - HLIT] = 0;
  1224.             }
  1225.           }
  1226.           else { error = 16; return; } //error: somehow an unexisting code appeared. This can never happen.
  1227.         }
  1228.         if(bitlen[256] == 0) { error = 64; return; } //the length of the end code 256 must be larger than 0
  1229.         error = tree.makeFromLengths(bitlen, 15); if(error) return; //now we've finally got HLIT and HDIST, so generate the code trees, and the function is done
  1230.         error = treeD.makeFromLengths(bitlenD, 15); if(error) return;
  1231.       }
  1232.       void inflateHuffmanBlock(std::vector<unsigned char>& out, const unsigned char* in, size_t& bp, size_t& pos, size_t inlength, unsigned long btype)
  1233.       {
  1234.         if(btype == 1) { generateFixedTrees(codetree, codetreeD); }
  1235.         else if(btype == 2) { getTreeInflateDynamic(codetree, codetreeD, in, bp, inlength); if(error) return; }
  1236.         for(;;)
  1237.         {
  1238.           unsigned long code = huffmanDecodeSymbol(in, bp, codetree, inlength); if(error) return;
  1239.           if(code == 256) return; //end code
  1240.           else if(code <= 255) //literal symbol
  1241.           {
  1242.             if(pos >= out.size()) out.resize((pos + 1) * 2); //reserve more room
  1243.             out[pos++] = (unsigned char)(code);
  1244.           }
  1245.           else if(code >= 257 && code <= 285) //length code
  1246.           {
  1247.             size_t length = LENBASE[code - 257], numextrabits = LENEXTRA[code - 257];
  1248.             if((bp >> 3) >= inlength) { error = 51; return; } //error, bit pointer will jump past memory
  1249.             length += readBitsFromStream(bp, in, numextrabits);
  1250.             unsigned long codeD = huffmanDecodeSymbol(in, bp, codetreeD, inlength); if(error) return;
  1251.             if(codeD > 29) { error = 18; return; } //error: invalid dist code (30-31 are never used)
  1252.             unsigned long dist = DISTBASE[codeD], numextrabitsD = DISTEXTRA[codeD];
  1253.             if((bp >> 3) >= inlength) { error = 51; return; } //error, bit pointer will jump past memory
  1254.             dist += readBitsFromStream(bp, in, numextrabitsD);
  1255.             size_t start = pos, back = start - dist; //backwards
  1256.             if(pos + length >= out.size()) out.resize((pos + length) * 2); //reserve more room
  1257.             for(size_t i = 0; i < length; i++) { out[pos++] = out[back++]; if(back >= start) back = start - dist; }
  1258.           }
  1259.         }
  1260.       }
  1261.       void inflateNoCompression(std::vector<unsigned char>& out, const unsigned char* in, size_t& bp, size_t& pos, size_t inlength)
  1262.       {
  1263.         while((bp & 0x7) != 0) bp++; //go to first boundary of byte
  1264.         size_t p = bp / 8;
  1265.         if(p >= inlength - 4) { error = 52; return; } //error, bit pointer will jump past memory
  1266.         unsigned long LEN = in[p] + 256 * in[p + 1], NLEN = in[p + 2] + 256 * in[p + 3]; p += 4;
  1267.         if(LEN + NLEN != 65535) { error = 21; return; } //error: NLEN is not one's complement of LEN
  1268.         if(pos + LEN >= out.size()) out.resize(pos + LEN);
  1269.         if(p + LEN > inlength) { error = 23; return; } //error: reading outside of in buffer
  1270.         for(unsigned long n = 0; n < LEN; n++) out[pos++] = in[p++]; //read LEN bytes of literal data
  1271.         bp = p * 8;
  1272.       }
  1273.     };
  1274.     int decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in) //returns error value
  1275.     {
  1276.       Inflator inflator;
  1277.       if(in.size() < 2) { return 53; } //error, size of zlib data too small
  1278.       if((in[0] * 256 + in[1]) % 31 != 0) { return 24; } //error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way
  1279.       unsigned long CM = in[0] & 15, CINFO = (in[0] >> 4) & 15, FDICT = (in[1] >> 5) & 1;
  1280.       if(CM != 8 || CINFO > 7) { return 25; } //error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec
  1281.       if(FDICT != 0) { return 26; } //error: the specification of PNG says about the zlib stream: "The additional flags shall not specify a preset dictionary."
  1282.       inflator.inflate(out, in, 2);
  1283.       return inflator.error; //note: adler32 checksum was skipped and ignored
  1284.     }
  1285.   };
  1286.   struct PNG //nested functions for PNG decoding
  1287.   {
  1288.     struct Info
  1289.     {
  1290.       unsigned long width, height, colorType, bitDepth, compressionMethod, filterMethod, interlaceMethod, key_r, key_g, key_b;
  1291.       bool key_defined; //is a transparent color key given?
  1292.       std::vector<unsigned char> palette;
  1293.     } info;
  1294.     int error;
  1295.     void decode(std::vector<unsigned char>& out, const unsigned char* in, size_t size, bool convert_to_rgba32)
  1296.     {
  1297.       error = 0;
  1298.       if(size == 0 || in == 0) { error = 48; return; } //the given data is empty
  1299.       readPngHeader(&in[0], size); if(error) return;
  1300.       size_t pos = 33; //first byte of the first chunk after the header
  1301.       std::vector<unsigned char> idat; //the data from idat chunks
  1302.       bool IEND = false, known_type = true;
  1303.       info.key_defined = false;
  1304.       while(!IEND) //loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is put at the start of the in buffer
  1305.       {
  1306.         if(pos + 8 >= size) { error = 30; return; } //error: size of the in buffer too small to contain next chunk
  1307.         size_t chunkLength = read32bitInt(&in[pos]); pos += 4;
  1308.         if(chunkLength > 2147483647) { error = 63; return; }
  1309.         if(pos + chunkLength >= size) { error = 35; return; } //error: size of the in buffer too small to contain next chunk
  1310.         if(in[pos + 0] == 'I' && in[pos + 1] == 'D' && in[pos + 2] == 'A' && in[pos + 3] == 'T') //IDAT chunk, containing compressed image data
  1311.         {
  1312.           idat.insert(idat.end(), &in[pos + 4], &in[pos + 4 + chunkLength]);
  1313.           pos += (4 + chunkLength);
  1314.         }
  1315.         else if(in[pos + 0] == 'I' && in[pos + 1] == 'E' && in[pos + 2] == 'N' && in[pos + 3] == 'D')  { pos += 4; IEND = true; }
  1316.         else if(in[pos + 0] == 'P' && in[pos + 1] == 'L' && in[pos + 2] == 'T' && in[pos + 3] == 'E') //palette chunk (PLTE)
  1317.         {
  1318.           pos += 4; //go after the 4 letters
  1319.           info.palette.resize(4 * (chunkLength / 3));
  1320.           if(info.palette.size() > (4 * 256)) { error = 38; return; } //error: palette too big
  1321.           for(size_t i = 0; i < info.palette.size(); i += 4)
  1322.           {
  1323.             for(size_t j = 0; j < 3; j++) info.palette[i + j] = in[pos++]; //RGB
  1324.             info.palette[i + 3] = 255; //alpha
  1325.           }
  1326.         }
  1327.         else if(in[pos + 0] == 't' && in[pos + 1] == 'R' && in[pos + 2] == 'N' && in[pos + 3] == 'S') //palette transparency chunk (tRNS)
  1328.         {
  1329.           pos += 4; //go after the 4 letters
  1330.           if(info.colorType == 3)
  1331.           {
  1332.             if(4 * chunkLength > info.palette.size()) { error = 39; return; } //error: more alpha values given than there are palette entries
  1333.             for(size_t i = 0; i < chunkLength; i++) info.palette[4 * i + 3] = in[pos++];
  1334.           }
  1335.           else if(info.colorType == 0)
  1336.           {
  1337.             if(chunkLength != 2) { error = 40; return; } //error: this chunk must be 2 bytes for greyscale image
  1338.             info.key_defined = 1; info.key_r = info.key_g = info.key_b = 256 * in[pos] + in[pos + 1]; pos += 2;
  1339.           }
  1340.           else if(info.colorType == 2)
  1341.           {
  1342.             if(chunkLength != 6) { error = 41; return; } //error: this chunk must be 6 bytes for RGB image
  1343.             info.key_defined = 1;
  1344.             info.key_r = 256 * in[pos] + in[pos + 1]; pos += 2;
  1345.             info.key_g = 256 * in[pos] + in[pos + 1]; pos += 2;
  1346.             info.key_b = 256 * in[pos] + in[pos + 1]; pos += 2;
  1347.           }
  1348.           else { error = 42; return; } //error: tRNS chunk not allowed for other color models
  1349.         }
  1350.         else //it's not an implemented chunk type, so ignore it: skip over the data
  1351.         {
  1352.           if(!(in[pos + 0] & 32)) { error = 69; return; } //error: unknown critical chunk (5th bit of first byte of chunk type is 0)
  1353.           pos += (chunkLength + 4); //skip 4 letters and uninterpreted data of unimplemented chunk
  1354.           known_type = false;
  1355.         }
  1356.         pos += 4; //step over CRC (which is ignored)
  1357.       }
  1358.       unsigned long bpp = getBpp(info);
  1359.       std::vector<unsigned char> scanlines(((info.width * (info.height * bpp + 7)) / 8) + info.height); //now the out buffer will be filled
  1360.       Zlib zlib; //decompress with the Zlib decompressor
  1361.       error = zlib.decompress(scanlines, idat); if(error) return; //stop if the zlib decompressor returned an error
  1362.       size_t bytewidth = (bpp + 7) / 8, outlength = (info.height * info.width * bpp + 7) / 8;
  1363.       out.resize(outlength); //time to fill the out buffer
  1364.       unsigned char* out_ = outlength ? &out[0] : 0; //use a regular pointer to the std::vector for faster code if compiled without optimization
  1365.       if(info.interlaceMethod == 0) //no interlace, just filter
  1366.       {
  1367.         size_t linestart = 0, linelength = (info.width * bpp + 7) / 8; //length in bytes of a scanline, excluding the filtertype byte
  1368.         if(bpp >= 8) //byte per byte
  1369.         for(unsigned long y = 0; y < info.height; y++)
  1370.         {
  1371.           unsigned long filterType = scanlines[linestart];
  1372.           const unsigned char* prevline = (y == 0) ? 0 : &out_[(y - 1) * info.width * bytewidth];
  1373.           unFilterScanline(&out_[linestart - y], &scanlines[linestart + 1], prevline, bytewidth, filterType,  linelength); if(error) return;
  1374.           linestart += (1 + linelength); //go to start of next scanline
  1375.         }
  1376.         else //less than 8 bits per pixel, so fill it up bit per bit
  1377.         {
  1378.           std::vector<unsigned char> templine((info.width * bpp + 7) >> 3); //only used if bpp < 8
  1379.           for(size_t y = 0, obp = 0; y < info.height; y++)
  1380.           {
  1381.             unsigned long filterType = scanlines[linestart];
  1382.             const unsigned char* prevline = (y == 0) ? 0 : &out_[(y - 1) * info.width * bytewidth];
  1383.             unFilterScanline(&templine[0], &scanlines[linestart + 1], prevline, bytewidth, filterType, linelength); if(error) return;
  1384.             for(size_t bp = 0; bp < info.width * bpp;) setBitOfReversedStream(obp, out_, readBitFromReversedStream(bp, &templine[0]));
  1385.             linestart += (1 + linelength); //go to start of next scanline
  1386.           }
  1387.         }
  1388.       }
  1389.       else //interlaceMethod is 1 (Adam7)
  1390.       {
  1391.         size_t passw[7] = { (info.width + 7) / 8, (info.width + 3) / 8, (info.width + 3) / 4, (info.width + 1) / 4, (info.width + 1) / 2, (info.width + 0) / 2, (info.width + 0) / 1 };
  1392.         size_t passh[7] = { (info.height + 7) / 8, (info.height + 7) / 8, (info.height + 3) / 8, (info.height + 3) / 4, (info.height + 1) / 4, (info.height + 1) / 2, (info.height + 0) / 2 };
  1393.         size_t passstart[7] = {0};
  1394.         size_t pattern[28] = {0,4,0,2,0,1,0,0,0,4,0,2,0,1,8,8,4,4,2,2,1,8,8,8,4,4,2,2}; //values for the adam7 passes
  1395.         for(int i = 0; i < 6; i++) passstart[i + 1] = passstart[i] + passh[i] * ((passw[i] ? 1 : 0) + (passw[i] * bpp + 7) / 8);
  1396.         std::vector<unsigned char> scanlineo((info.width * bpp + 7) / 8), scanlinen((info.width * bpp + 7) / 8); //"old" and "new" scanline
  1397.         for(int i = 0; i < 7; i++)
  1398.           adam7Pass(&out_[0], &scanlinen[0], &scanlineo[0], &scanlines[passstart[i]], info.width, pattern[i], pattern[i + 7], pattern[i + 14], pattern[i + 21], passw[i], passh[i], bpp);
  1399.       }
  1400.       if(convert_to_rgba32 && (info.colorType != 6 || info.bitDepth != 8)) //conversion needed
  1401.       {
  1402.         std::vector<unsigned char> data = out;
  1403.         error = convert(out, &data[0], info, info.width, info.height);
  1404.       }
  1405.     }
  1406.     void readPngHeader(const unsigned char* in, size_t inlength) //read the information from the header and store it in the Info
  1407.     {
  1408.       if(inlength < 29) { error = 27; return; } //error: the data length is smaller than the length of the header
  1409.       if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) { error = 28; return; } //no PNG signature
  1410.       if(in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') { error = 29; return; } //error: it doesn't start with a IHDR chunk!
  1411.       info.width = read32bitInt(&in[16]); info.height = read32bitInt(&in[20]);
  1412.       info.bitDepth = in[24]; info.colorType = in[25];
  1413.       info.compressionMethod = in[26]; if(in[26] != 0) { error = 32; return; } //error: only compression method 0 is allowed in the specification
  1414.       info.filterMethod = in[27]; if(in[27] != 0) { error = 33; return; } //error: only filter method 0 is allowed in the specification
  1415.       info.interlaceMethod = in[28]; if(in[28] > 1) { error = 34; return; } //error: only interlace methods 0 and 1 exist in the specification
  1416.       error = checkColorValidity(info.colorType, info.bitDepth);
  1417.     }
  1418.     void unFilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, size_t bytewidth, unsigned long filterType, size_t length)
  1419.     {
  1420.       switch(filterType)
  1421.       {
  1422.         case 0: for(size_t i = 0; i < length; i++) recon[i] = scanline[i]; break;
  1423.         case 1:
  1424.           for(size_t i =         0; i < bytewidth; i++) recon[i] = scanline[i];
  1425.           for(size_t i = bytewidth; i <    length; i++) recon[i] = scanline[i] + recon[i - bytewidth];
  1426.           break;
  1427.         case 2:
  1428.           if(precon) for(size_t i = 0; i < length; i++) recon[i] = scanline[i] + precon[i];
  1429.           else       for(size_t i = 0; i < length; i++) recon[i] = scanline[i];
  1430.           break;
  1431.         case 3:
  1432.           if(precon)
  1433.           {
  1434.             for(size_t i =         0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2;
  1435.             for(size_t i = bytewidth; i <    length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
  1436.           }
  1437.           else
  1438.           {
  1439.             for(size_t i =         0; i < bytewidth; i++) recon[i] = scanline[i];
  1440.             for(size_t i = bytewidth; i <    length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2;
  1441.           }
  1442.           break;
  1443.         case 4:
  1444.           if(precon)
  1445.           {
  1446.             for(size_t i =         0; i < bytewidth; i++) recon[i] = scanline[i] + paethPredictor(0, precon[i], 0);
  1447.             for(size_t i = bytewidth; i <    length; i++) recon[i] = scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]);
  1448.           }
  1449.           else
  1450.           {
  1451.             for(size_t i =         0; i < bytewidth; i++) recon[i] = scanline[i];
  1452.             for(size_t i = bytewidth; i <    length; i++) recon[i] = scanline[i] + paethPredictor(recon[i - bytewidth], 0, 0);
  1453.           }
  1454.           break;
  1455.         default: error = 36; return; //error: unexisting filter type given
  1456.       }
  1457.     }
  1458.     void adam7Pass(unsigned char* out, unsigned char* linen, unsigned char* lineo, const unsigned char* in, unsigned long w, size_t passleft, size_t passtop, size_t spacex, size_t spacey, size_t passw, size_t passh, unsigned long bpp)
  1459.     { //filter and reposition the pixels into the output when the image is Adam7 interlaced. This function can only do it after the full image is already decoded. The out buffer must have the correct allocated memory size already.
  1460.       if(passw == 0) return;
  1461.       size_t bytewidth = (bpp + 7) / 8, linelength = 1 + ((bpp * passw + 7) / 8);
  1462.       for(unsigned long y = 0; y < passh; y++)
  1463.       {
  1464.         unsigned char filterType = in[y * linelength], *prevline = (y == 0) ? 0 : lineo;
  1465.         unFilterScanline(linen, &in[y * linelength + 1], prevline, bytewidth, filterType, (w * bpp + 7) / 8); if(error) return;
  1466.         if(bpp >= 8) for(size_t i = 0; i < passw; i++) for(size_t b = 0; b < bytewidth; b++) //b = current byte of this pixel
  1467.           out[bytewidth * w * (passtop + spacey * y) + bytewidth * (passleft + spacex * i) + b] = linen[bytewidth * i + b];
  1468.         else for(size_t i = 0; i < passw; i++)
  1469.         {
  1470.           size_t obp = bpp * w * (passtop + spacey * y) + bpp * (passleft + spacex * i), bp = i * bpp;
  1471.           for(size_t b = 0; b < bpp; b++) setBitOfReversedStream(obp, out, readBitFromReversedStream(bp, &linen[0]));
  1472.         }
  1473.         unsigned char* temp = linen; linen = lineo; lineo = temp; //swap the two buffer pointers "line old" and "line new"
  1474.       }
  1475.     }
  1476.     static unsigned long readBitFromReversedStream(size_t& bitp, const unsigned char* bits) { unsigned long result = (bits[bitp >> 3] >> (7 - (bitp & 0x7))) & 1; bitp++; return result;}
  1477.     static unsigned long readBitsFromReversedStream(size_t& bitp, const unsigned char* bits, unsigned long nbits)
  1478.     {
  1479.       unsigned long result = 0;
  1480.       for(size_t i = nbits - 1; i < nbits; i--) result += ((readBitFromReversedStream(bitp, bits)) << i);
  1481.       return result;
  1482.     }
  1483.     void setBitOfReversedStream(size_t& bitp, unsigned char* bits, unsigned long bit) { bits[bitp >> 3] |=  (bit << (7 - (bitp & 0x7))); bitp++; }
  1484.     unsigned long read32bitInt(const unsigned char* buffer) { return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; }
  1485.     int checkColorValidity(unsigned long colorType, unsigned long bd) //return type is a LodePNG error code
  1486.     {
  1487.       if((colorType == 2 || colorType == 4 || colorType == 6)) { if(!(bd == 8 || bd == 16)) return 37; else return 0; }
  1488.       else if(colorType == 0) { if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; else return 0; }
  1489.       else if(colorType == 3) { if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8            )) return 37; else return 0; }
  1490.       else return 31; //unexisting color type
  1491.     }
  1492.     unsigned long getBpp(const Info& info)
  1493.     {
  1494.       if(info.colorType == 2) return (3 * info.bitDepth);
  1495.       else if(info.colorType >= 4) return (info.colorType - 2) * info.bitDepth;
  1496.       else return info.bitDepth;
  1497.     }
  1498.     int convert(std::vector<unsigned char>& out, const unsigned char* in, Info& infoIn, unsigned long w, unsigned long h)
  1499.     { //converts from any color type to 32-bit. return value = LodePNG error code
  1500.       size_t numpixels = w * h, bp = 0;
  1501.       out.resize(numpixels * 4);
  1502.       unsigned char* out_ = out.empty() ? 0 : &out[0]; //faster if compiled without optimization
  1503.       if(infoIn.bitDepth == 8 && infoIn.colorType == 0) //greyscale
  1504.       for(size_t i = 0; i < numpixels; i++)
  1505.       {
  1506.         out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[i];
  1507.         out_[4 * i + 3] = (infoIn.key_defined && in[i] == infoIn.key_r) ? 0 : 255;
  1508.       }
  1509.       else if(infoIn.bitDepth == 8 && infoIn.colorType == 2) //RGB color
  1510.       for(size_t i = 0; i < numpixels; i++)
  1511.       {
  1512.         for(size_t c = 0; c < 3; c++) out_[4 * i + c] = in[3 * i + c];
  1513.         out_[4 * i + 3] = (infoIn.key_defined == 1 && in[3 * i + 0] == infoIn.key_r && in[3 * i + 1] == infoIn.key_g && in[3 * i + 2] == infoIn.key_b) ? 0 : 255;
  1514.       }
  1515.       else if(infoIn.bitDepth == 8 && infoIn.colorType == 3) //indexed color (palette)
  1516.       for(size_t i = 0; i < numpixels; i++)
  1517.       {
  1518.         if(4U * in[i] >= infoIn.palette.size()) return 46;
  1519.         for(size_t c = 0; c < 4; c++) out_[4 * i + c] = infoIn.palette[4 * in[i] + c]; //get rgb colors from the palette
  1520.       }
  1521.       else if(infoIn.bitDepth == 8 && infoIn.colorType == 4) //greyscale with alpha
  1522.       for(size_t i = 0; i < numpixels; i++)
  1523.       {
  1524.         out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[2 * i + 0];
  1525.         out_[4 * i + 3] = in[2 * i + 1];
  1526.       }
  1527.       else if(infoIn.bitDepth == 8 && infoIn.colorType == 6) for(size_t i = 0; i < numpixels; i++) for(size_t c = 0; c < 4; c++) out_[4 * i + c] = in[4 * i + c]; //RGB with alpha
  1528.       else if(infoIn.bitDepth == 16 && infoIn.colorType == 0) //greyscale
  1529.       for(size_t i = 0; i < numpixels; i++)
  1530.       {
  1531.         out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[2 * i];
  1532.         out_[4 * i + 3] = (infoIn.key_defined && 256U * in[i] + in[i + 1] == infoIn.key_r) ? 0 : 255;
  1533.       }
  1534.       else if(infoIn.bitDepth == 16 && infoIn.colorType == 2) //RGB color
  1535.       for(size_t i = 0; i < numpixels; i++)
  1536.       {
  1537.         for(size_t c = 0; c < 3; c++) out_[4 * i + c] = in[6 * i + 2 * c];
  1538.         out_[4 * i + 3] = (infoIn.key_defined && 256U*in[6*i+0]+in[6*i+1] == infoIn.key_r && 256U*in[6*i+2]+in[6*i+3] == infoIn.key_g && 256U*in[6*i+4]+in[6*i+5] == infoIn.key_b) ? 0 : 255;
  1539.       }
  1540.       else if(infoIn.bitDepth == 16 && infoIn.colorType == 4) //greyscale with alpha
  1541.       for(size_t i = 0; i < numpixels; i++)
  1542.       {
  1543.         out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[4 * i]; //most significant byte
  1544.         out_[4 * i + 3] = in[4 * i + 2];
  1545.       }
  1546.       else if(infoIn.bitDepth == 16 && infoIn.colorType == 6) for(size_t i = 0; i < numpixels; i++) for(size_t c = 0; c < 4; c++) out_[4 * i + c] = in[8 * i + 2 * c]; //RGB with alpha
  1547.       else if(infoIn.bitDepth < 8 && infoIn.colorType == 0) //greyscale
  1548.       for(size_t i = 0; i < numpixels; i++)
  1549.       {
  1550.         unsigned long value = (readBitsFromReversedStream(bp, in, infoIn.bitDepth) * 255) / ((1 << infoIn.bitDepth) - 1); //scale value from 0 to 255
  1551.         out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = (unsigned char)(value);
  1552.         out_[4 * i + 3] = (infoIn.key_defined && value && ((1U << infoIn.bitDepth) - 1U) == infoIn.key_r && ((1U << infoIn.bitDepth) - 1U)) ? 0 : 255;
  1553.       }
  1554.       else if(infoIn.bitDepth < 8 && infoIn.colorType == 3) //palette
  1555.       for(size_t i = 0; i < numpixels; i++)
  1556.       {
  1557.         unsigned long value = readBitsFromReversedStream(bp, in, infoIn.bitDepth);
  1558.         if(4 * value >= infoIn.palette.size()) return 47;
  1559.         for(size_t c = 0; c < 4; c++) out_[4 * i + c] = infoIn.palette[4 * value + c]; //get rgb colors from the palette
  1560.       }
  1561.       return 0;
  1562.     }
  1563.     unsigned char paethPredictor(short a, short b, short c) //Paeth predicter, used by PNG filter type 4
  1564.     {
  1565.       short p = a + b - c, pa = p > a ? (p - a) : (a - p), pb = p > b ? (p - b) : (b - p), pc = p > c ? (p - c) : (c - p);
  1566.       return (unsigned char)((pa <= pb && pa <= pc) ? a : pb <= pc ? b : c);
  1567.     }
  1568.   };
  1569.   PNG decoder; decoder.decode(out_image, in_png, in_size, convert_to_rgba32);
  1570.   image_width = decoder.info.width; image_height = decoder.info.height;
  1571.   return decoder.error;
  1572. }
  1573.  
  1574.  
  1575.  
  1576.  
  1577. /*
  1578. //an example using the PNG loading function:
  1579.  
  1580. #include <iostream>
  1581. #include <fstream>
  1582.  
  1583. void loadFile(std::vector<unsigned char>& buffer, const std::string& filename) //designed for loading files from hard disk in an std::vector
  1584. {
  1585.   std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
  1586.  
  1587.   //get filesize
  1588.   std::streamsize size = 0;
  1589.   if(file.seekg(0, std::ios::end).good()) size = file.tellg();
  1590.   if(file.seekg(0, std::ios::beg).good()) size -= file.tellg();
  1591.  
  1592.   //read contents of the file into the vector
  1593.   if(size > 0)
  1594.   {
  1595.     buffer.resize((size_t)size);
  1596.     file.read((char*)(&buffer[0]), size);
  1597.   }
  1598.   else buffer.clear();
  1599. }
  1600.  
  1601. int main(int argc, char *argv[])
  1602. {
  1603.   const char* filename = argc > 1 ? argv[1] : "test.png";
  1604.  
  1605.   //load and decode
  1606.   std::vector<unsigned char> buffer, image;
  1607.   loadFile(buffer, filename);
  1608.   unsigned long w, h;
  1609.   int error = decodePNG(image, w, h, buffer.empty() ? 0 : &buffer[0], (unsigned long)buffer.size());
  1610.  
  1611.   //if there's an error, display it
  1612.   if(error != 0) std::cout << "error: " << error << std::endl;
  1613.  
  1614.   //the pixels are now in the vector "image", use it as texture, draw it, ...
  1615.  
  1616.   if(image.size() > 4) std::cout << "width: " << w << " height: " << h << " first pixel: " << std::hex << int(image[0]) << int(image[1]) << int(image[2]) << int(image[3]) << std::endl;
  1617. }
  1618. */
  1619. /*
  1620.   //this is test code, it displays the pixels of a 1 bit PNG. To use it, set the flag convert_to_rgba32 to false and load a 1-bit PNG image with a small size (so that its ASCII representation can fit in a console window)
  1621.   for(int y = 0; y < h; y++)
  1622.   {
  1623.     for(int x = 0; x < w; x++)
  1624.     {
  1625.       int i = y * h + x;
  1626.       std::cout << (((image[i/8] >> (7-i%8)) & 1) ? '.' : '#');
  1627.     }
  1628.     std::cout << std::endl;
  1629.   }
  1630. */
  1631.  
  1632. void loadTexture::loadBufferToOpengl(std::string filePath, Texture &texture){
  1633.     std::vector<unsigned char> in_buffer; //undecoded PNG file data
  1634.     std::vector<unsigned char> out_buffer; //raw pixel data
  1635.    
  1636.     fileToBuffer(filePath,in_buffer);
  1637.  
  1638.     unsigned long width, height;
  1639.     int checkError;
  1640.     checkError = decodePNGbuffer(out_buffer, width, height, &in_buffer[0],in_buffer.size());
  1641.  
  1642.     glActiveTexture(GL_TEXTURE0);
  1643.     glBindTexture(GL_TEXTURE_2D, texture.textureID);
  1644.  
  1645.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &out_buffer[0]);
  1646.  
  1647.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  1648.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  1649.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1650.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  1651.     glGenerateMipmap(GL_TEXTURE_2D);
  1652.     texture.width = width;
  1653.     texture.height = height;
  1654. }
  1655.  
  1656. Texture loadTexture::genTexture(void){//texture ID is needed, width and height can be 0 for now
  1657.     Texture texture;
  1658.     glGenTextures(1, &texture.textureID);
  1659.     texture.width = 0;
  1660.     texture.height = 0;
  1661.     return texture;
  1662. }
  1663.  
  1664.  
  1665. #ifndef TEXTURE_STRUCT
  1666. #define TEXTURE_STRUCT
  1667.  
  1668. #include <GL/glew.h>
  1669. struct Texture{
  1670.     GLuint textureID;
  1671.     int width;
  1672.     int height;
  1673. };
  1674. #endif
  1675.  
  1676. #ifndef VERTEX_STRUCT
  1677. #define VERTEX_STRUCT
  1678.  
  1679. #include <glm/glm.hpp>
  1680.  
  1681. struct Vertex {
  1682.     glm::vec3 position;
  1683.     glm::vec3 color;
  1684.     glm::vec3 normal;
  1685.     glm::vec2 UV;
  1686. };
  1687.  
  1688.  
  1689. #endif
  1690.  
  1691.  
  1692. #ifndef TEXTURE_CACHE
  1693. #define TEXTURE_CACHE
  1694. #include <string>
  1695. #include <map>
  1696. #include "loadTexture.h"
  1697.  
  1698.  
  1699.  
  1700. class resourceManager
  1701. {
  1702. public:
  1703.     static std::map <std::string, Texture>& getTexture_cache(void){return texture_cache;}
  1704. private:
  1705.     static std::map <std::string, Texture> texture_cache;
  1706.    
  1707. };
  1708.  
  1709. #endif
  1710.  
  1711. #include "resourceManager.h"
  1712.  
  1713.  
  1714. std::map <std::string, Texture> resourceManager::texture_cache;
  1715.  
  1716.  
  1717. #ifndef GAME_ENUMS
  1718. #define GAME_ENUMS
  1719. class GameEnums
  1720. {
  1721. public:
  1722.     static enum GameState {PLAY, EXIT} currGS;
  1723. };
  1724.  
  1725. #endif
  1726.  
  1727. #include "GameEnums.h"
  1728.  
  1729. GameEnums::GameState GameEnums::currGS = PLAY;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement