Bubba-3D  0.9.0
Awesome game engine!
SkyBoxRenderer.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 "SkyBoxRenderer.h"
18 #include "ResourceManager.h"
19 #include "constants.h"
20 #include "Camera.h"
21 #include "CubeMapTexture.h"
22 #include "GameObject.h"
23 #include "Chunk.h"
24 
25 #define SKYBOX_SHADER_NAME "skybox_shader"
26 
27 SkyBoxRenderer::SkyBoxRenderer(Camera *camera, Mesh* skyMesh, GameObject* gameObject) : m_camera(camera), m_skyMesh(skyMesh), gameObject(gameObject) {
28 }
29 
30 bool SkyBoxRenderer::init(const std::string &posXFilename, const std::string &negXFilename,
31  const std::string &posYFilename, const std::string &negYFilename,
32  const std::string &posZFilename, const std::string &negZFilename)
33 {
34  m_pCubemap = new CubeMapTexture(posXFilename, negXFilename, posYFilename, negYFilename, posZFilename, negZFilename);
35  ResourceManager::loadShader("shaders/skybox.vert", "shaders/skybox.frag", SKYBOX_SHADER_NAME);
36  shaderProgram = ResourceManager::getShader(SKYBOX_SHADER_NAME);
37  shaderProgram->setUniformBufferObjectBinding(UNIFORM_BUFFER_OBJECT_MATRICES_NAME, UNIFORM_BUFFER_OBJECT_MATRICES_INDEX);
38 
39  return true;
40 }
41 
42 void SkyBoxRenderer::update(float dt) {
43  gameObject->move(chag::make_translation(chag::make_vector(0.0f, 2.0f, 0.0f)) * chag::make_translation(m_camera->getPosition()) *
44  chag::make_scale<chag::float4x4>(chag::make_vector(10000.0f, 10000.0f, 10000.0f)));
45 }
46 
47 void SkyBoxRenderer::render() {
48  shaderProgram->backupCurrentShaderProgram();
49  shaderProgram->use();
50 
51  GLint OldCullFaceMode;
52  glGetIntegerv(GL_CULL_FACE_MODE, &OldCullFaceMode);
53  GLint OldDepthFuncMode;
54  glGetIntegerv(GL_DEPTH_FUNC, &OldDepthFuncMode);
55 
56  glCullFace(GL_FRONT);
57  glDepthFunc(GL_LEQUAL);
58 
59  m_pCubemap->bind(GL_TEXTURE0);
60  shaderProgram->setUniform1i("cubeMapSampler", 0);
61 
62  chag::float4x4 modelMatrix = gameObject->getModelMatrix();
63  shaderProgram->setUniformMatrix4fv("modelMatrix", modelMatrix);
64 
65  for (size_t i = 0; i < m_skyMesh->getChunks()->size(); i++) {
66  renderChunk((*m_skyMesh->getChunks())[i]);
67  }
68 
69  glCullFace(OldCullFaceMode);
70  glDepthFunc(OldDepthFuncMode);
71  shaderProgram->restorePreviousShaderProgram();
72 }
73 
74 
75 void SkyBoxRenderer::renderChunk(Chunk& chunk) {
76  glBindVertexArray(chunk.m_vaob);
77  glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, chunk.m_ind_bo);
78 
79  glDrawElements(GL_TRIANGLES, chunk.m_indices.size(), GL_UNSIGNED_INT, 0);
80  CHECK_GL_ERROR();
81 }
82 
83 SkyBoxRenderer::~SkyBoxRenderer(){
84 
85 }
Definition: Mesh.h:45
Definition: Chunk.h:27
A class for containing all information about a object in the game world.
Definition: GameObject.h:67
Definition: Camera.h:26