Implemented a renderQueue which is sorted by materials. Not feasible. Closes #12.

This commit is contained in:
Faerbit 2015-03-20 23:45:28 +01:00
parent 795801faa6
commit 2a0cb3ca1d
8 changed files with 76 additions and 8 deletions

View File

@ -16,6 +16,12 @@ void Chunk::render(SharedShaderProgram shader, bool lightingPass, bool texturePa
}
}
void Chunk::enqueueObjects(std::vector<std::vector<Object*>>* renderQueue) {
for(unsigned int i = 0; i<objects.size(); i++) {
renderQueue->at(objects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(objects.at(i));
}
}
void Chunk::addObject(Object* object) {
objects.push_back(object);
}

View File

@ -1,7 +1,7 @@
#pragma once
#include "object.hh"
#include <ACGL/OpenGL/Objects.hh>
#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<glm::mat4>* additionalMatrices=0);
void addObject(Object* object);
void enqueueObjects(std::vector<std::vector<Object*>>* renderQueue);
private:
std::vector<Object*> objects;
};

View File

@ -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<std::vector<Object*>>(Material::getAllTextures()->size());
for (unsigned int i = 0; i<renderQueue.size(); i++) {
renderQueue.at(i) = std::vector<Object*>();
}
}
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; i<renderQueue.size(); i++) {
if (renderQueue.at(i).size() != 0) {
Material* material = renderQueue.at(i).at(0)->getMaterial();
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; j<renderQueue.at(i).size(); j++) {
renderQueue.at(i).at(j)->render(lightingShader, true, false, &lightingViewProjectionMatrix, &depthBiasVPs);
}
}
}
}
if (renderDebug) {

View File

@ -88,7 +88,7 @@ class Graphics {
SharedArrayBuffer debug_ab;
SharedVertexArrayObject debug_vao;
SharedShaderProgram debugShader;
std::vector<std::vector<Object*>> renderQueue;
};
#endif

View File

@ -101,6 +101,42 @@ void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
}
}
void Level::enqueueObjects(std::vector<std::vector<Object*>>* 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; i<crossChunkObjects.size(); i++) {
renderQueue->at(crossChunkObjects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(crossChunkObjects.at(i));
}
}
void Level::update(float runTimeSinceLastUpdate, float runTime, glm::vec2 mouseDelta,
KeyboardState* keyboardState) {

View File

@ -77,6 +77,7 @@ class Level {
void generateChunks(int chunkSize);
std::vector<std::vector<Chunk>>* getChunks();
void addToSpecificChunk(Object* object, int xPosition, int zPosition);
void enqueueObjects(std::vector<std::vector<Object*>>* renderQueue);
private:
lua_State* luaState=nullptr;
std::vector<Object*> crossChunkObjects;

View File

@ -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;
}

View File

@ -18,6 +18,7 @@ class Object : public Entity {
void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
bool texturePass, glm::mat4* viewProjcetionMatrix,
std::vector<glm::mat4>* additionalMatrices=0);
Material* getMaterial();
private:
Model model;
Material material;