Advertisement
Guest User

Untitled

a guest
Mar 12th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.63 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "IndirectRend.hpp"
  3. #include "MtlImporter.hpp"
  4. #include "InputManager.hpp"
  5. #include "Cubemap.hpp"
  6. #include "Shader.hpp"
  7. namespace game_core {
  8.  
  9.     void GenerateTexArray(GLuint &tex) {
  10.         glActiveTexture(GL_TEXTURE7);
  11.         glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
  12.         glTexStorage3D(GL_TEXTURE_2D_ARRAY,
  13.             1,                    //No mipmaps as textures are 1x1
  14.             GL_RGB8,              //Internal format
  15.             1, 1,                 //width,height
  16.             NUM_DEBUG_COLORS      //Number of layers
  17.         );
  18.  
  19.         for (unsigned int i(0); i < NUM_DEBUG_COLORS; i++)
  20.         {
  21.             //Choose a random color for the i-essim image
  22.             GLubyte color[3] = { GLubyte(rand() % 255),GLubyte(rand() % 255),GLubyte(rand() % 255) };
  23.  
  24.             //Specify i-essim image
  25.             glTexSubImage3D(GL_TEXTURE_2D_ARRAY,
  26.                 0,                     //Mipmap number
  27.                 0, 0, i,               //xoffset, yoffset, zoffset
  28.                 1, 1, 1,               //width, height, depth
  29.                 GL_RGB,                //format
  30.                 GL_UNSIGNED_BYTE,      //type
  31.                 color);                //pointer to data
  32.         }
  33.  
  34.         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  35.         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  36.         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  37.         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  38.  
  39.     }
  40.  
  41.  
  42.     std::unique_ptr<IndirectRend> IndirectRend::_instance(nullptr);
  43.  
  44.     void IndirectRend::Init() {
  45.         _instance.reset(new IndirectRend());
  46.         glGenVertexArrays(1, &_instance->BindInfoComplete.VAO);
  47.         glGenBuffers(1, &_instance->BindInfoComplete.EBO);
  48.         glGenBuffers(1, &_instance->_indirectBuffer);
  49.  
  50.         glGenTextures(1, &_instance->_colorArrayTex);
  51.         GenerateTexArray(_instance->_colorArrayTex);
  52.     }
  53.  
  54.     IndirectRend::IndirectRend() : _indirectBuffer(0), _vertexOffset(0) { }
  55.  
  56.     IndirectRend::~IndirectRend() = default;
  57.  
  58.     idxVal IndirectRend::GenerateDrawCommands(const Mesh &mesh)
  59.     {
  60.         int baseVertex = 0;
  61.         for (const auto &m : mesh.subMeshes) {
  62.             DrawElementsCommand command{};
  63.             command.vertexCount = m.indices.size();             //Number of indices for this submesh
  64.             command.instanceCount = 1;                          //One instance
  65.             command.firstIndex = _instance->_commandHeadIndex;  //The highest index consumed by the draw command
  66.             command.baseVertex = _instance->_vertexOffset;      //The first index of the parent mesh
  67.             command.baseInstance = _instance->_cmdIndex;        //Instance ID
  68.  
  69.             _instance->_drawCommands.push_back(command);
  70.  
  71.             _instance->_cmdIndex++;
  72.             _instance->_commandHeadIndex += m.indices.size();
  73.         }
  74.  
  75.         //For some reason, Intel hardware seriously does not like this particular buffer being re-filled without being re created first.
  76.         //Failure to do so results in the draw command chucking invalid ops
  77.         glDeleteBuffers(1, &_instance->_indirectBuffer);
  78.         glCreateBuffers(1, &_instance->_indirectBuffer);
  79.         glBindBuffer(GL_DRAW_INDIRECT_BUFFER, _instance->_indirectBuffer);
  80.         glBufferData(GL_DRAW_INDIRECT_BUFFER, _instance->_drawCommands.size() * sizeof(_drawCommands[0]), &_instance->_drawCommands[0], GL_DYNAMIC_DRAW);
  81.         glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
  82.         return _instance->_drawCommands.size();
  83.     }
  84.  
  85.     void IndirectRend::AddMesh(std::shared_ptr<Mesh> manager) {
  86.         for (auto mesh : manager->subMeshes)
  87.             _instance->_indices.insert(_instance->_indices.end(), mesh.indices.begin(), mesh.indices.end());
  88.        
  89.            
  90.  
  91.         _instance->_verts.insert(_instance->_verts.end(), manager->vertices.begin(), manager->vertices.end());
  92.         _instance->_uvs.insert(_instance->_uvs.end(), manager->uvs.begin(), manager->uvs.end());
  93.         _instance->_normals.insert(_instance->_normals.end(), manager->normals.begin(), manager->normals.end());
  94.         _instance->_tangents.insert(_instance->_tangents.end(), manager->tangents.begin(), manager->tangents.end());
  95.         _instance->_binormals.insert(_instance->_binormals.end(), manager->binormals.begin(), manager->binormals.end());
  96.  
  97.         BindVertexInfo(_instance->BindInfoComplete, _instance->_verts,
  98.             _instance->_uvs, _instance->_normals, _instance->_tangents, _instance->_binormals, "Indirect Renderer");
  99.  
  100.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _instance->BindInfoComplete.EBO);
  101.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, _instance->_indices.size() * sizeof(idxVal), &_instance->_indices[0], GL_STATIC_DRAW);
  102.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  103.  
  104.         GenerateDrawCommands(*manager);
  105.  
  106.         _instance->_vertexOffset += manager->vertices.size();
  107.     }
  108.  
  109.     void IndirectRend::Render() {
  110.         //Content aquisition is happening at the head of this method solely for testing purposes, as is the unnecessary rebinding
  111.         const auto shader = Shader::get_resource("sparky");
  112.         glUseProgram(shader->shaderProgram);
  113.  
  114.         GLuint skybox = Cubemap::get_resource("Yokohama_HighRes", false)->texture;
  115.  
  116.  
  117.         glBindVertexArray(_instance->BindInfoComplete.VAO);
  118.  
  119.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _instance->BindInfoComplete.EBO);
  120.  
  121.         glBindBuffer(GL_DRAW_INDIRECT_BUFFER, _instance->_indirectBuffer);
  122.         glBindBuffer(GL_ARRAY_BUFFER, _instance->_indirectBuffer);
  123.         glEnableVertexAttribArray(5);
  124.         glVertexAttribIPointer(5, 1, GL_UNSIGNED_INT, sizeof(DrawElementsCommand), reinterpret_cast<void*>(offsetof(DrawElementsCommand, baseInstance)));
  125.         glVertexAttribDivisor(5, 1);
  126.  
  127.         glm::mat4 proj = InputManager::projMatrix();
  128.         glm::mat4 view = InputManager::viewMatrix;
  129.  
  130.         glm::mat4 model = glm::mat4(1); //Model matrix is going to be un a uniform buffer later
  131.         glm::mat4 MV = view * model;
  132.         glm::mat4 PV = proj * view;
  133.         glm::mat4 MVP = proj * view * model;
  134.  
  135.         glUniformMatrix4fv(shader->GetUniform("M"), 1, GL_FALSE, &model[0][0]);
  136.         glUniformMatrix4fv(shader->GetUniform("V"), 1, GL_FALSE, &view[0][0]);
  137.         glUniformMatrix4fv(shader->GetUniform("P"), 1, GL_FALSE, &proj[0][0]);
  138.         glUniformMatrix4fv(shader->GetUniform("PV"), 1, GL_FALSE, &PV[0][0]);
  139.         glUniformMatrix4fv(shader->GetUniform("MV"), 1, GL_FALSE, &MV[0][0]);
  140.         glUniformMatrix4fv(shader->GetUniform("MVP"), 1, GL_FALSE, &MVP[0][0]);
  141.  
  142.         glShadeModel(GL_FLAT);
  143.         glEnable(GL_CULL_FACE);
  144.         glEnable(GL_DEPTH_TEST);
  145.         glDepthFunc(GL_LEQUAL);
  146.  
  147.         glUniform1f(shader->GetUniform("u_UsingAlbedoMap"), 1);
  148.         glUniform1i(shader->GetUniform("u_AlbedoMap"), 1);
  149.         glUniform1i(shader->GetUniform("u_EnvironmentMap"), 4);
  150.  
  151.         glActiveTexture(GL_TEXTURE1);
  152.         glBindTexture(GL_TEXTURE_2D_ARRAY, _instance->_colorArrayTex);
  153.  
  154.         glActiveTexture(GL_TEXTURE4);
  155.         glBindTexture(GL_TEXTURE_CUBE_MAP, skybox);
  156.  
  157.         glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, _instance->_drawCommands.size(), 0);//Here be dragons
  158.  
  159.         glBindVertexArray(0);
  160.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  161.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement