Bubba-3D  0.9.0
Awesome game engine!
StandardRenderer.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include <GameObject.h>
18 #include <ShaderProgram.h>
19 #include <SFML/System/Clock.hpp>
20 #include "StandardRenderer.h"
21 #include "Mesh.h"
22 #include "ShaderProgram.h"
23 #include "objects/Chunk.h"
24 
25 #define NORMAL_TEXTURE_LOCATION 3
26 #define DIFFUSE_TEXTURE_LOCATION 0
27 
28 StandardRenderer::StandardRenderer(){
29 
30 }
31 
32 StandardRenderer::StandardRenderer(Mesh* mesh, GameObject* gameObject, ShaderProgram* shaderProgram):
33  mesh(mesh), gameObject(gameObject)
34 {
35  this->shaderProgram = shaderProgram;
36 }
37 
38 
39 void StandardRenderer::update(float dt){
40  ;
41 }
42 
43 void StandardRenderer::render() {
44  shaderProgram->use();
45  CHECK_GL_ERROR();
46 
47  chag::float4x4 modelMatrix = gameObject->getModelMatrix();
48 
49  chag::float4x4 normalMatrix = chag::inverse(chag::transpose(modelMatrix));
50  shaderProgram->setUniformMatrix4fv("modelMatrix", modelMatrix);
51  shaderProgram->setUniformMatrix4fv("normalMatrix", normalMatrix);
52 
53  for (size_t i = 0; i < mesh->getChunks()->size(); i++) {
54  CHECK_GL_ERROR();
55 
56  Chunk &chunk = (*mesh->getChunks())[i];
57  Material &material = (*mesh->getMaterials())[chunk.materialIndex];
58 
59  if (material.diffuseTexture != NULL) {
60  shaderProgram->setUniform1i("diffuse_texture", DIFFUSE_TEXTURE_LOCATION);
61  material.diffuseTexture->bind(GL_TEXTURE0);
62  }
63  if (material.bumpMapTexture != NULL) {
64  shaderProgram->setUniform1i("normal_texture", NORMAL_TEXTURE_LOCATION);
65  material.bumpMapTexture->bind(GL_TEXTURE3);
66  }
67 
68  if(mesh->hasAnimations()) {
69  float currentTimeInSeconds = clock.getElapsedTime().asSeconds();
70  std::vector<chag::float4x4> boneTransforms = mesh->getBoneTransforms(currentTimeInSeconds);
71  shaderProgram->setUniform1i("has_animations", 1);
72 
73  for (unsigned int j = 0; j < boneTransforms.size(); j++) {
74  shaderProgram->setUniformMatrix4fv("bones[" + std::to_string(j) + "]", boneTransforms[j]);
75  }
76  } else {
77  shaderProgram->setUniform1i("has_animations", 0);
78  }
79 
80  shaderProgram->setUniform1i("has_diffuse_texture", material.diffuseTexture != NULL);
81  shaderProgram->setUniform3f("material_diffuse_color", material.diffuseColor);
82  shaderProgram->setUniform3f("material_diffuse_color", chag::make_vector(0.5f, 0.5f, 0.5f));
83  shaderProgram->setUniform3f("material_specular_color", material.specularColor);
84  shaderProgram->setUniform3f("material_ambient_color", material.ambientColor);
85  shaderProgram->setUniform3f("material_emissive_color", material.emissiveColor);
86  shaderProgram->setUniform1i("has_normal_texture", material.bumpMapTexture != NULL);
87  shaderProgram->setUniform1f("material_shininess", material.specularExponent);
88  CHECK_GL_ERROR();
89 
90  glBindVertexArray(chunk.m_vaob);
91  glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, chunk.m_ind_bo);
92 
93  glDrawElements(GL_TRIANGLES, chunk.m_indices.size(), GL_UNSIGNED_INT, 0);
94  CHECK_GL_ERROR();
95  }
96  CHECK_GL_ERROR();
97 }
98 
99 void StandardRenderer::renderShadow(ShaderProgram *shaderProgram) {
100 
101  chag::float4x4 modelMatrix = gameObject->getModelMatrix();
102  shaderProgram->setUniformMatrix4fv("modelMatrix", modelMatrix);
103 
104  for (size_t i = 0; i < mesh->getChunks()->size(); i++) {
105  CHECK_GL_ERROR();
106 
107  Chunk &chunk = (*mesh->getChunks())[i];
108 
109  glBindVertexArray(chunk.m_vaob);
110  glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, chunk.m_ind_bo);
111 
112  glDrawElements(GL_TRIANGLES, chunk.m_indices.size(), GL_UNSIGNED_INT, 0);
113  CHECK_GL_ERROR();
114  }
115 
116  CHECK_GL_ERROR();
117 }
Definition: Mesh.h:45
Definition: Chunk.h:27
Class for maintaining OpenGL shader programs.
Definition: ShaderProgram.h:39
A class for containing all information about a object in the game world.
Definition: GameObject.h:67