Skip to content

Commit

Permalink
Fix editor previews use wrong/invalid materials
Browse files Browse the repository at this point in the history
Turns out it was a single line fix, but I did fix other issues with it,
including issue #153 fixed.
  • Loading branch information
enginmanap committed Dec 27, 2024
1 parent d1372f1 commit 3a64995
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 103 deletions.
1 change: 1 addition & 0 deletions src/API/Graphics/GraphicsProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ GraphicsProgram::~GraphicsProgram() {

//TODO remove with material editor
void GraphicsProgram::setSamplersAndUBOs() {
graphicsWrapper->attachMaterialUBO(getID());

//TODO these will be configurable with material editor
int diffuseMapAttachPoint = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/API/Graphics/GraphicsProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class GraphicsProgram {
return graphicsProgramAsset->getProgramName();
}

bool IsMaterialRequired() const {
bool isMaterialRequired() const {
return materialRequired;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,11 +1230,9 @@ void Editor::createObjectTreeRecursive(PhysicalRenderable *physicalRenderable, u
}
}

void Editor::renderSelectedObject(Model* model) {
void Editor::renderSelectedObject(Model* model) const {
backgroundRenderStage->activate(true);
std::vector<glm::uvec4> modelIndexes;
modelIndexes.push_back(glm::uvec4(model->getWorldObjectID(),0,0,0));
model->renderWithProgramInstanced(modelIndexes, *(graphicsProgram.get()), 0);
model->convertToRenderList(0, 0).render(world->graphicsWrapper, graphicsProgram);
world->renderPipeline->reActivateLastStage();
}

2 changes: 1 addition & 1 deletion src/Editor/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Editor {
ImGuiTreeNodeFlags nodeFlags, ImGuiTreeNodeFlags leafFlags,
std::vector<uint32_t> parentage);

void renderSelectedObject(Model* model);
void renderSelectedObject(Model* model) const;

void setTransformToModel(Model *model, const glm::vec3 &newObjectPosition);
};
Expand Down
28 changes: 6 additions & 22 deletions src/GameObjects/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Utils/HardCodedTags.h"
#include <random>
#include <Editor/Editor.h>
#include <Occlusion/RenderList.h>

#ifdef CEREAL_SUPPORT
#include <cereal/archives/binary.hpp>
Expand Down Expand Up @@ -191,37 +192,20 @@ void Model::renderWithProgram(std::shared_ptr<GraphicsProgram> program, uint32_t
} else {
program->setUniform("isAnimated", false);
}
if(program->IsMaterialRequired()) {
if(program->isMaterialRequired()) {
this->activateTexturesOnly((*iter)->material);
}
graphicsWrapper->render(program->getID(), (*iter)->mesh->getVao(), (*iter)->mesh->getEbo(), (*iter)->mesh->getTriangleCount()[lodLevel] * 3);
}
}

void Model::renderWithProgramInstanced(const std::vector<glm::uvec4> &modelIndices, GraphicsProgram &program, uint32_t lodLevel) {
RenderList Model::convertToRenderList(uint32_t lodLevel, float depth) const {
RenderList renderList;

for (auto iter = meshMetaData.begin(); iter != meshMetaData.end(); ++iter) {
if (animated) {
//set all of the bones to unitTransform for testing
program.setUniformArray("boneTransformArray[0]", boneTransforms);
program.setUniform("isAnimated", true);
} else {
program.setUniform("isAnimated", false);
}
if(program.IsMaterialRequired()) {
std::vector<glm::uvec4> modelIndicesWithMaterialIndex = modelIndices;
for (size_t i = 0; i < modelIndicesWithMaterialIndex.size(); ++i) {
modelIndicesWithMaterialIndex[i].y = (*iter)->material->getMaterialIndex();
}
graphicsWrapper->setModelIndexesUBO(modelIndicesWithMaterialIndex);
this->activateTexturesOnly((*iter)->material);
graphicsWrapper->renderInstanced(program.getID(), (*iter)->mesh->getVao(), (*iter)->mesh->getEbo(), (*iter)->mesh->getTriangleCount()[lodLevel] * 3, (*iter)->mesh->getOffsets()[lodLevel], modelIndices.size());

} else {
graphicsWrapper->setModelIndexesUBO(modelIndices);
graphicsWrapper->renderInstanced(program.getID(), (*iter)->mesh->getVao(), (*iter)->mesh->getEbo(), (*iter)->mesh->getTriangleCount()[lodLevel] * 3, (*iter)->mesh->getOffsets()[lodLevel], modelIndices.size());
}
renderList.addMeshMaterial((*iter)->material, (*iter)->mesh, this, lodLevel, depth);
}
return renderList;
}

bool Model::fillObjects(tinyxml2::XMLDocument &document, tinyxml2::XMLElement *objectsNode) const {
Expand Down
6 changes: 3 additions & 3 deletions src/GameObjects/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "Sound.h"

class ActorInterface;

class RenderList;
class Model : public PhysicalRenderable, public GameObject {
public:
struct MeshMeta {
Expand Down Expand Up @@ -114,7 +114,7 @@ class Model : public PhysicalRenderable, public GameObject {

void renderWithProgram(std::shared_ptr<GraphicsProgram> program, uint32_t lodLevel) override;


RenderList convertToRenderList(uint32_t lodLevel, float depth) const;
void renderWithProgramInstanced(const std::vector<glm::uvec4> & modelIndices, GraphicsProgram &program, uint32_t lodLevel);

bool isAnimated() const { return animated;}
Expand Down Expand Up @@ -191,7 +191,7 @@ class Model : public PhysicalRenderable, public GameObject {
return GameObject::MODEL;
}

std::vector<glm::mat4>* getBoneTransforms() {return &boneTransforms;}
const std::vector<glm::mat4>* getBoneTransforms() const {return &boneTransforms;}

std::string getName() const override {
return name + "_" + std::to_string(objectID);
Expand Down
52 changes: 51 additions & 1 deletion src/Occlusion/RenderList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "RenderList.h"

#include "../GameObjects/Model.h"
void RenderList::addMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, Model *model, uint32_t lod, float maxDepth) {
void RenderList::addMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, const Model *model, uint32_t lod, float maxDepth) {
auto materialIterator = getOrCreateMaterialEntry(material);
auto meshIterator = materialIterator->second.getOrCreateMeshEntry(meshAsset);
auto requestedObjectIterator = std::find_if(meshIterator->second.indices.begin(), meshIterator->second.indices.end(), [model](const glm::uvec4& entry) { return entry.x == model->getWorldObjectID(); });
Expand Down Expand Up @@ -58,6 +58,56 @@ void RenderList::removeModelFromAll(uint32_t modelId) {
}
}

void RenderList::render(GraphicsInterface *graphicsWrapper, const std::shared_ptr<GraphicsProgram> &renderProgram) const {
int diffuseMapAttachPoint = 1;
int ambientMapAttachPoint = 2;
int specularMapAttachPoint = 3;
int opacityMapAttachPoint = 4;
int normalMapAttachPoint = 5;

//now render all of the meshes
for (auto renderListIterator = this->getIterator(); !renderListIterator.isEnd(); ++renderListIterator) {
if (renderProgram->isMaterialRequired()) {
const auto& material = renderListIterator.getMaterial();
//activate textures
if(material->hasDiffuseMap()) {
graphicsWrapper->attachTexture(material->getDiffuseTexture()->getID(), diffuseMapAttachPoint);
}
if(material->hasAmbientMap()) {
graphicsWrapper->attachTexture(material->getAmbientTexture()->getID(), ambientMapAttachPoint);
}

if(material->hasSpecularMap()) {
graphicsWrapper->attachTexture(material->getSpecularTexture()->getID(), specularMapAttachPoint);
}

if(material->hasOpacityMap()) {
graphicsWrapper->attachTexture(material->getOpacityTexture()->getID(), opacityMapAttachPoint);
}

if(material->hasNormalMap()) {
graphicsWrapper->attachTexture(material->getNormalTexture()->getID(), normalMapAttachPoint);
}
}


if (renderListIterator.get().indices.empty()) {
std::cerr << "Empty meshInfo" << std::endl;
continue;
}
if (renderListIterator.get().isAnimated) {
//set all of the bones to unitTransform for testing
renderProgram->setUniformArray("boneTransformArray[0]", *renderListIterator.get().boneTransforms);
renderProgram->setUniform("isAnimated", true);
} else {
renderProgram->setUniform("isAnimated", false);
}

graphicsWrapper->setModelIndexesUBO(renderListIterator.get().indices);
graphicsWrapper->renderInstanced(renderProgram->getID(), renderListIterator.getMesh()->getVao(), renderListIterator.getMesh()->getEbo(), renderListIterator.getMesh()->getTriangleCount()[renderListIterator.get().lod] * 3, renderListIterator.getMesh()->getOffsets()[renderListIterator.get().lod], renderListIterator.get().indices.size());
}
}

void RenderList::cleanUpEmptyRenderLists() {
//When a model is no longer visible, it will be removed. That means it is possible some Per Mesh Render Informations have no indices. We should clean them up
auto materialIterator = perMaterialMeshMap.begin();
Expand Down
9 changes: 7 additions & 2 deletions src/Occlusion/RenderList.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <glm/glm.hpp>
#include <memory>

class GraphicsInterface;
class GraphicsProgram;
class MeshAsset;
class Material;
class Model;
Expand All @@ -21,7 +23,7 @@ struct PerMeshRenderInformation {
uint32_t lod = 0; //Max level of detail. Since we use instanced rendering, using single LOD for all meshes is faster than multiple draw calls (at least in my testing)
bool isAnimated = false;
float depth = 0; //max depth for this mesh set
std::vector<glm::mat4>* boneTransforms = nullptr;//known wrong, as it will only work for one model id. Placeholder until bone transform index lookup implementation
const std::vector<glm::mat4>* boneTransforms = nullptr;//known wrong, as it will only work for one model id. Placeholder until bone transform index lookup implementation
};

class RenderList {
Expand Down Expand Up @@ -150,7 +152,7 @@ class RenderList {
return it;
}
public:
void addMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, Model *model, uint32_t lod, float maxDepth);
void addMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, const Model *model, uint32_t lod, float maxDepth);
void removeMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, uint32_t modelId);
void removeModelFromAll(uint32_t modelId);
RenderListIterator getIterator() const {
Expand All @@ -163,6 +165,9 @@ class RenderList {
return RenderListIterator(*this);
}


void render(GraphicsInterface* graphicsWrapper, const std::shared_ptr<GraphicsProgram> &renderProgram) const;

void cleanUpEmptyRenderLists();

void clear() {
Expand Down
72 changes: 6 additions & 66 deletions src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@

SDL2MultiThreading::Condition VisibilityRequest::condition; //FIXME this variable doesn't looks like it belongs here

void World::setupRenderForPipeline() {
for (const auto& stageInfo:renderPipeline->getStages()) {
for(const auto& graphicsProgram:stageInfo.programs) {
graphicsWrapper->attachMaterialUBO(graphicsProgram->getID());
}
}
}
void World::setupRenderForPipeline() const {
}

World::World(const std::string &name, PlayerInfo startingPlayerType, InputHandler *inputHandler,
std::shared_ptr<AssetManager> assetManager, OptionsUtil::Options *options)
Expand Down Expand Up @@ -854,57 +849,6 @@ void World::renderDebug(const std::shared_ptr<GraphicsProgram>& renderProgram [[
debugDrawer->flushDraws();
}

void World::renderSingleRenderList(const std::shared_ptr<GraphicsProgram> &renderProgram, const RenderList& renderList) const {
int diffuseMapAttachPoint = 1;
int ambientMapAttachPoint = 2;
int specularMapAttachPoint = 3;
int opacityMapAttachPoint = 4;
int normalMapAttachPoint = 5;

//now render all of the meshes
for (auto renderListIterator = renderList.getIterator(); !renderListIterator.isEnd(); ++renderListIterator) {

const auto& material = renderListIterator.getMaterial();
{//activate textures
if(material->hasDiffuseMap()) {
graphicsWrapper->attachTexture(material->getDiffuseTexture()->getID(), diffuseMapAttachPoint);
}
if(material->hasAmbientMap()) {
graphicsWrapper->attachTexture(material->getAmbientTexture()->getID(), ambientMapAttachPoint);
}

if(material->hasSpecularMap()) {
graphicsWrapper->attachTexture(material->getSpecularTexture()->getID(), specularMapAttachPoint);
}

if(material->hasOpacityMap()) {
graphicsWrapper->attachTexture(material->getOpacityTexture()->getID(), opacityMapAttachPoint);
}

if(material->hasNormalMap()) {
graphicsWrapper->attachTexture(material->getNormalTexture()->getID(), normalMapAttachPoint);
}

}


if (renderListIterator.get().indices.empty()) {
std::cerr << "Empty meshInfo" << std::endl;
continue;
}
if (renderListIterator.get().isAnimated) {
//set all of the bones to unitTransform for testing
renderProgram->setUniformArray("boneTransformArray[0]", *renderListIterator.get().boneTransforms);
renderProgram->setUniform("isAnimated", true);
} else {
renderProgram->setUniform("isAnimated", false);
}

graphicsWrapper->setModelIndexesUBO(renderListIterator.get().indices);
graphicsWrapper->renderInstanced(renderProgram->getID(), renderListIterator.getMesh()->getVao(), renderListIterator.getMesh()->getEbo(), renderListIterator.getMesh()->getTriangleCount()[renderListIterator.get().lod] * 3, renderListIterator.getMesh()->getOffsets()[renderListIterator.get().lod], renderListIterator.get().indices.size());
}
}

void World::renderCameraByTag(const std::shared_ptr<GraphicsProgram> &renderProgram, const std::string &cameraName, const std::vector<HashUtil::HashedString> &tags) const {
uint64_t hashedCameraTag = HashUtil::hashString(cameraName);
tempRenderedObjectsSet.clear();
Expand All @@ -925,7 +869,7 @@ void World::renderCameraByTag(const std::shared_ptr<GraphicsProgram> &renderProg
continue;
}
const RenderList& renderList = renderListEntry.second;
renderSingleRenderList(renderProgram, renderList);
renderList.render(graphicsWrapper, renderProgram);
}
}
}
Expand Down Expand Up @@ -953,10 +897,8 @@ void World::renderPlayerAttachmentsRecursiveByTag(PhysicalRenderable *attachment
}
if(std::find(alreadyRenderedModelIds.begin(), alreadyRenderedModelIds.end(), attachmentObject->getWorldObjectID()) == alreadyRenderedModelIds.end() && attachmentObject->hasTag(renderTag)) {
alreadyRenderedModelIds.emplace_back(attachmentObject->getWorldObjectID());
std::vector<glm::uvec4> temp;
temp.push_back(glm::uvec4(attachmentObject->getWorldObjectID(), 0, 0, 0));
if(attachmentObject->getTypeID() == GameObject::MODEL) {
(static_cast<Model *>(attachment))->renderWithProgramInstanced(temp, *(renderProgram), 0);//it is guaranteed to be very close to the player.
static_cast<Model *>(attachment)->convertToRenderList(0,0.0f).render(graphicsWrapper, renderProgram);
}
}
for (const auto &child: children) {
Expand All @@ -981,7 +923,7 @@ void World::renderLight(unsigned int lightIndex, unsigned int renderLayer, const
continue;
}
const RenderList& renderList = iterator.second;
renderSingleRenderList(renderProgram, renderList);
renderList.render(graphicsWrapper, renderProgram);
}
}

Expand Down Expand Up @@ -1016,9 +958,7 @@ void World::ImGuiFrameSetup(std::shared_ptr<GraphicsProgram> graphicsProgram, co

playerPlaceHolder->getTransformation()->setTranslate(physicalPlayer->getPosition());
playerPlaceHolder->getTransformation()->setOrientation(physicalPlayer->getLookDirectionQuaternion());
std::vector<glm::uvec4> temp;
temp.push_back(glm::uvec4(playerPlaceHolder->getWorldObjectID(), 0, 0, 0));
playerPlaceHolder->renderWithProgramInstanced(temp, *(graphicsProgram.get()), 0);
playerPlaceHolder->convertToRenderList(0,0).render(graphicsWrapper, graphicsProgram);
}
editor->renderEditor();
}
Expand Down
4 changes: 1 addition & 3 deletions src/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class World {

void addLight(Light *light);

void setupRenderForPipeline();
void setupRenderForPipeline() const;

World(const std::string &name, PlayerInfo startingPlayerType, InputHandler *inputHandler,
std::shared_ptr<AssetManager> assetManager, OptionsUtil::Options *options);
Expand Down Expand Up @@ -415,8 +415,6 @@ class World {
void renderSky(const std::shared_ptr<GraphicsProgram>& renderProgram, const std::string &cameraName [[gnu::unused]], const std::vector<HashUtil::HashedString> &tags [[gnu::unused]]) const;
void renderDebug(const std::shared_ptr<GraphicsProgram>& renderProgram, const std::string &cameraName [[gnu::unused]], const std::vector<HashUtil::HashedString> &tags [[gnu::unused]]) const;

void renderSingleRenderList(const std::shared_ptr<GraphicsProgram> &renderProgram, const RenderList& renderList) const;

void renderPlayerAttachmentsRecursiveByTag(PhysicalRenderable *attachment, uint64_t renderTag, const std::shared_ptr<GraphicsProgram> &renderProgram,
std::vector<uint32_t> &alreadyRenderedModelIds) const;

Expand Down

0 comments on commit 3a64995

Please sign in to comment.