Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

StructureRenderer.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "graphics/StructureRenderer.h"
  19. #include "graphics/MeshRenderData.h"
  20. #include "graphics/Renderer.h"
  21. #include "graphics/util.h"
  22. #include "Camera.h"
  23. #include "Core.h"
  24. #include "Landcell.h"
  25. #include "LandcellManager.h"
  26. #include "Structure.h"
  27. #include <glm/gtc/matrix_transform.hpp>
  28. // We're sharing shaders with the model renderer for now
  29. #include "graphics/shaders/ModelVertexShader.h"
  30. #include "graphics/shaders/ModelFragmentShader.h"
  31. StructureRenderer::StructureRenderer()
  32. {
  33. program_.create();
  34. program_.attach(GL_VERTEX_SHADER, ModelVertexShader);
  35. program_.attach(GL_FRAGMENT_SHADER, ModelFragmentShader);
  36. program_.link();
  37. program_.use();
  38. GLuint texLocation = program_.getUniform("tex");
  39. glUniform1i(texLocation, 0);
  40. }
  41. StructureRenderer::~StructureRenderer()
  42. {
  43. program_.destroy();
  44. }
  45. void StructureRenderer::render(const glm::mat4& projectionMat, const glm::mat4& viewMat)
  46. {
  47. program_.use();
  48. LandcellManager& landcellManager = Core::get().landcellManager();
  49. glm::vec3 cameraPosition = Core::get().camera().position();
  50. glUniform4f(program_.getUniform("cameraPosition"), GLfloat(cameraPosition.x), GLfloat(cameraPosition.y), GLfloat(cameraPosition.z), 1.0f);
  51. for(auto& pair : landcellManager)
  52. {
  53. if(!pair.first.isStructure())
  54. {
  55. continue;
  56. }
  57. int dx = pair.first.x() - landcellManager.center().x();
  58. int dy = pair.first.y() - landcellManager.center().y();
  59. glm::vec3 blockPosition{dx * 192.0, dy * 192.0, 0.0};
  60. const Structure& structure = static_cast<const Structure&>(*pair.second);
  61. renderStructure(structure, projectionMat, viewMat, blockPosition + structure.location().position, structure.location().rotation);
  62. }
  63. }
  64. void StructureRenderer::renderStructure(
  65. const Structure& structure,
  66. const glm::mat4& projectionMat,
  67. const glm::mat4& viewMat,
  68. const glm::vec3& position,
  69. const glm::quat& rotation)
  70. {
  71. glm::mat4 worldMat = glm::translate(glm::mat4{}, position) * glm::mat4_cast(rotation);
  72. loadMat4ToUniform(worldMat, program_.getUniform("worldMatrix"));
  73. loadMat4ToUniform(projectionMat * viewMat, program_.getUniform("projectionViewMatrix"));
  74. if(!structure.renderData())
  75. {
  76. structure.renderData().reset(new MeshRenderData(structure));
  77. }
  78. MeshRenderData& renderData = static_cast<MeshRenderData&>(*structure.renderData());
  79. renderData.render();
  80. }