From 2a0cb3ca1dd575c41e8270e93a47bb315e175952 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 20 Mar 2015 23:45:28 +0100 Subject: [PATCH] Implemented a renderQueue which is sorted by materials. Not feasible. Closes #12. --- game/chunk.cc | 6 ++++++ game/chunk.hh | 3 ++- game/graphics.cc | 26 +++++++++++++++++++++++++- game/graphics.hh | 2 +- game/level.cc | 36 ++++++++++++++++++++++++++++++++++++ game/level.hh | 1 + game/object.cc | 9 ++++----- game/object.hh | 1 + 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/game/chunk.cc b/game/chunk.cc index d7d381d..7d20fb4 100644 --- a/game/chunk.cc +++ b/game/chunk.cc @@ -16,6 +16,12 @@ void Chunk::render(SharedShaderProgram shader, bool lightingPass, bool texturePa } } +void Chunk::enqueueObjects(std::vector>* renderQueue) { + for(unsigned int i = 0; iat(objects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(objects.at(i)); + } +} + void Chunk::addObject(Object* object) { objects.push_back(object); } diff --git a/game/chunk.hh b/game/chunk.hh index bc9d603..f6d0510 100644 --- a/game/chunk.hh +++ b/game/chunk.hh @@ -1,7 +1,7 @@ #pragma once -#include "object.hh" #include +#include "object.hh" using namespace ACGL::OpenGL; @@ -12,6 +12,7 @@ class Chunk { void render(SharedShaderProgram shader, bool lightingPass, bool texturePass, glm::mat4* viewProjcetionMatrix, std::vector* additionalMatrices=0); void addObject(Object* object); + void enqueueObjects(std::vector>* renderQueue); private: std::vector objects; }; diff --git a/game/graphics.cc b/game/graphics.cc index 5c177c7..2897768 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -197,6 +197,11 @@ void Graphics::init(Level* level) { lightingShader->setUniform("fogColorRise", level->getFogColourRise()); lightingShader->setUniform("fogColorNight", level->getFogColourNight()); lightingShader->setUniform("ambientColor", level->getAmbientLight()); + + renderQueue = std::vector>(Material::getAllTextures()->size()); + for (unsigned int i = 0; i(); + } } void Graphics::bindTextureUnits(){ @@ -486,7 +491,26 @@ void Graphics::render(double time) if (renderWorld) { // render the level - level->render(lightingShader, true, &lightingViewProjectionMatrix, &depthBiasVPs); + level->enqueueObjects(&renderQueue); + for (unsigned int i = 0; igetMaterial(); + if (material->isMoving()) { + lightingShader->setUniform("movingTexture", true); + } + else { + lightingShader->setUniform("movingTexture", false); + } + lightingShader->setUniform("uTexture", material->getTextureUnit()); + lightingShader->setUniform("ambientFactor", material->getAmbientFactor()); + lightingShader->setUniform("diffuseFactor", material->getDiffuseFactor()); + lightingShader->setUniform("specularFactor", material->getSpecularFactor()); + lightingShader->setUniform("shininess", material->getShininess()); + for(unsigned int j = 0; jrender(lightingShader, true, false, &lightingViewProjectionMatrix, &depthBiasVPs); + } + } + } } if (renderDebug) { diff --git a/game/graphics.hh b/game/graphics.hh index 503fbdb..ea5ad44 100644 --- a/game/graphics.hh +++ b/game/graphics.hh @@ -88,7 +88,7 @@ class Graphics { SharedArrayBuffer debug_ab; SharedVertexArrayObject debug_vao; SharedShaderProgram debugShader; - + std::vector> renderQueue; }; #endif diff --git a/game/level.cc b/game/level.cc index 1040f3a..83e1158 100644 --- a/game/level.cc +++ b/game/level.cc @@ -101,6 +101,42 @@ void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, } } +void Level::enqueueObjects(std::vector>* renderQueue) { + int renderDistance = 0; + if ((int)farPlane % chunkSize == 0) { + renderDistance = (((int)skydomeSize)+chunkSize/2)/chunkSize; + } + else { + renderDistance = ((((int)skydomeSize)+chunkSize/2)/chunkSize) + 1; + } + int xPosition = ((int)cameraCenter->getPosition().x + (terrain.getHeightmapWidth()/2))/chunkSize; + int zPosition = ((int)cameraCenter->getPosition().z + (terrain.getHeightmapHeight()/2))/chunkSize; + int xStart = xPosition - renderDistance; + unsigned int xEnd = xPosition + renderDistance; + int zStart = zPosition - renderDistance; + unsigned int zEnd = zPosition + renderDistance; + if (xStart < 0) { + xStart = 0; + } + if (zStart < 0) { + zStart = 0; + } + if (xEnd >= chunks.size()) { + xEnd = chunks.size()-1; + } + if (zEnd >= chunks.at(0).size()) { + zEnd = chunks.at(0).size()-1; + } + for(unsigned int i = xStart; i<=xEnd; i++) { + for(unsigned int j = zStart; j<=zEnd; j++) { + chunks.at(i).at(j).enqueueObjects(renderQueue); + } + } + for(unsigned int i = 0; iat(crossChunkObjects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(crossChunkObjects.at(i)); + } +} + void Level::update(float runTimeSinceLastUpdate, float runTime, glm::vec2 mouseDelta, KeyboardState* keyboardState) { diff --git a/game/level.hh b/game/level.hh index f5f61c3..bc3980a 100644 --- a/game/level.hh +++ b/game/level.hh @@ -77,6 +77,7 @@ class Level { void generateChunks(int chunkSize); std::vector>* getChunks(); void addToSpecificChunk(Object* object, int xPosition, int zPosition); + void enqueueObjects(std::vector>* renderQueue); private: lua_State* luaState=nullptr; std::vector crossChunkObjects; diff --git a/game/object.cc b/game/object.cc index 2d32020..52fa5f3 100644 --- a/game/object.cc +++ b/game/object.cc @@ -33,11 +33,6 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, shader->setUniform("modelMatrix", modelMatrix); } if (lightingPass) { - // set lightning parameters for this object - shader->setUniform("ambientFactor", material.getAmbientFactor()); - shader->setUniform("diffuseFactor", material.getDiffuseFactor()); - shader->setUniform("specularFactor", material.getSpecularFactor()); - shader->setUniform("shininess", material.getShininess()); // set model matrix shader->setUniform("modelMatrix", modelMatrix); // set shadowMVPs @@ -58,3 +53,7 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, // draw model.getReference()->render(); } + +Material* Object::getMaterial() { + return &material; +} diff --git a/game/object.hh b/game/object.hh index f3d9c28..1b9ff59 100644 --- a/game/object.hh +++ b/game/object.hh @@ -18,6 +18,7 @@ class Object : public Entity { void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, bool texturePass, glm::mat4* viewProjcetionMatrix, std::vector* additionalMatrices=0); + Material* getMaterial(); private: Model model; Material material;