Skip to content

Commit

Permalink
Connect mesh selection and material list
Browse files Browse the repository at this point in the history
Also fix changing materials break joining
  • Loading branch information
enginmanap committed Dec 1, 2024
1 parent 88512f2 commit 46c7e7c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/Assets/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ AssetManager::getAvailableAssetsTreeFilteredRecursive(const AvailableAssetsNode
* @return material that should be used.
*/
std::shared_ptr<Material> AssetManager::registerMaterial(std::shared_ptr<Material> material) {
auto foundIt = materials.find(material->getHash());
const auto foundIt = materials.find(material->getOriginalHash());
if(foundIt != materials.end()) {
//std::cerr << "Material found, return "<< material->getName() << std::endl;

foundIt->second.second++;
return foundIt->second.first;
}
Expand All @@ -208,7 +207,7 @@ std::shared_ptr<Material> AssetManager::registerMaterial(std::shared_ptr<Materia

}
void AssetManager::unregisterMaterial(std::shared_ptr<Material> material) {
auto materialIt = materials.find(material->getHash());
auto materialIt = materials.find(material->getOriginalHash());
if(materialIt == materials.end()) {
std::cerr << "Unregister for non existent material found!" << std::endl;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Assets/ModelAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ std::shared_ptr<Material> ModelAsset::loadMaterials(const aiScene *scene, unsign
}

newMaterial->setMaps(maps);
std::string requestedName = newMaterial->getName();

newMaterial->calculateOriginalHash();
newMaterial = assetManager->registerMaterial(newMaterial);
std::string requestedName = newMaterial->getName();
materialMap[requestedName] = newMaterial;
} else {
newMaterial = materialMap[property.C_Str()];
Expand Down
9 changes: 7 additions & 2 deletions src/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "AI/AIMovementGrid.h"


std::shared_ptr<const Material> EditorNS::selectedMaterial = nullptr;

Editor::Editor(World *world) : world(world){
backgroundRenderStage = std::make_unique<GraphicsPipelineStage>(world->graphicsWrapper, 640,480,"","",true,true,true,false,false);
colorTexture = std::make_shared<Texture>(world->graphicsWrapper, GraphicsInterface::TextureTypes::T2D, GraphicsInterface::InternalFormatTypes::RGBA, GraphicsInterface::FormatTypes::RGBA, GraphicsInterface::DataTypes::UNSIGNED_BYTE, 640, 480);
Expand Down Expand Up @@ -671,11 +673,15 @@ void Editor::renderEditor() {
if(ImGui::CollapsingHeader("List materials")) {
//listing
static size_t selectedHash = 0;
if (EditorNS::selectedMaterial != nullptr) {
selectedHash = EditorNS::selectedMaterial->getHash();
EditorNS::selectedMaterial = nullptr;
}
bool isSelected = false;
auto allMaterials = world->assetManager->getMaterials();
ImGui::Text("Total material count is %llu", allMaterials.size());
ImGui::BeginListBox("Materials");
for (auto it = allMaterials.begin(); it != allMaterials.end(); it++) {
for (auto it = allMaterials.begin(); it != allMaterials.end(); ++it) {
isSelected = selectedHash == it->first;
if (ImGui::Selectable((it->second.first->getName() + " -> " + std::to_string(it->first)).c_str(), isSelected)) {
selectedHash = it->first;
Expand All @@ -684,7 +690,6 @@ void Editor::renderEditor() {
ImGui::EndListBox();
auto selectedMaterialIt = allMaterials.find(selectedHash);
if(selectedMaterialIt != allMaterials.end()) {

selectedMaterialIt->second.first->addImGuiEditorElements(*world->request);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Editor/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class GraphicsPipelineStage;
class Model;
class GraphicsProgram;
class ImGuiImageWrapper;
class Material;

namespace EditorNS {
//This is used as a global variable store. For multiple windows, ImGui doesn't provide anything else
extern std::shared_ptr<const Material> selectedMaterial;
}
class Editor {
World* world;
std::shared_ptr<Texture> colorTexture;
Expand Down
15 changes: 12 additions & 3 deletions src/GameObjects/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "GamePlay/APISerializer.h"
#include "Utils/HardCodedTags.h"
#include <random>
#include <Editor/Editor.h>

#ifdef CEREAL_SUPPORT
#include <cereal/archives/binary.hpp>
Expand Down Expand Up @@ -410,13 +411,21 @@ ImGuiResult Model::addImGuiEditorElements(const ImGuiRequest &request) {
}
}
//add material listing
static uint32_t selectedIndex = 0;
static int32_t selectedIndex = -1;
static uint32_t selectedModel = 0;
if (this->getWorldObjectID() != selectedModel) {
selectedIndex = -1;
selectedModel = this->getWorldObjectID();
}
bool isSelected = false;
if(ImGui::BeginListBox("Meshes##GODWHY")) {
for (size_t i = 0; i < meshMetaData.size(); ++i) {
isSelected = selectedIndex == i;
isSelected = selectedIndex == static_cast<int32_t>(i);
if (ImGui::Selectable((meshMetaData[i]->mesh->getName() + " -> " + meshMetaData[i]->material->getName()).c_str(), isSelected)) {
selectedIndex = i;
if (selectedIndex != static_cast<int32_t>(i)) { //means selection changed, trigger material change on main window
EditorNS::selectedMaterial = meshMetaData[i]->mesh->getMaterial();
}
selectedIndex = static_cast<int32_t>(i);
}
}
ImGui::EndListBox();
Expand Down
4 changes: 4 additions & 0 deletions src/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ size_t Material::getHash() const {
std::hash<Material> hashGenerator;
return hashGenerator(*this);
}

size_t Material::getOriginalHash() const {
return originalHash;
}
35 changes: 21 additions & 14 deletions src/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@

class Material {
private:
AssetManager *assetManager;
std::string name;
float specularExponent = 0;
uint32_t materialIndex;
uint32_t maps = 0;
bool deserialized = false;

glm::vec3 ambientColor;
glm::vec3 diffuseColor;
glm::vec3 specularColor;

uint32_t materialIndex;
uint32_t maps = 0;
AssetManager *assetManager;
size_t originalHash = 0;//With this, we can still match assets loaded after the material is changed
float specularExponent = 0;
float refractionIndex = 0;
bool deserialized = false;
bool isAmbientMap = false;
bool isDiffuseMap = false;
bool isSpecularMap = false;
bool isNormalMap = false;
bool isOpacityMap = false;
float refractionIndex = 0;

/**
* This is a list of texture names.
Expand Down Expand Up @@ -62,23 +64,23 @@ class Material {
public:
Material(AssetManager *assetManager, const std::string &name, uint32_t materialIndex, float specularExponent, const glm::vec3 &ambientColor,
const glm::vec3 &diffuseColor, const glm::vec3 &specularColor, float refractionIndex)
: assetManager(assetManager),
name(name),
specularExponent(specularExponent),
materialIndex(materialIndex),
: name(name),
ambientColor(ambientColor),
diffuseColor(diffuseColor),
specularColor(specularColor),
materialIndex(materialIndex),
assetManager(assetManager),
specularExponent(specularExponent),
refractionIndex(refractionIndex) { }


Material(AssetManager *assetManager, const std::string &name, uint32_t materialIndex)
: assetManager(assetManager),
name(name),
materialIndex(materialIndex),
: name(name),
ambientColor(glm::vec3(0,0,0)),
diffuseColor(glm::vec3(0,0,0)),
specularColor(glm::vec3(0,0,0)) { }
specularColor(glm::vec3(0,0,0)),
materialIndex(materialIndex),
assetManager(assetManager) { }

void loadGPUSide(AssetManager *assetManager);

Expand Down Expand Up @@ -260,6 +262,11 @@ class Material {
}

size_t getHash() const;
size_t getOriginalHash() const;
void calculateOriginalHash() {
this->originalHash = getHash();
}


ImGuiResult addImGuiEditorElements(const ImGuiRequest &request);

Expand Down

0 comments on commit 46c7e7c

Please sign in to comment.