Skip to content

Commit dc998a4

Browse files
committed
Allow to load and reload models and textures in App flow
1 parent b4250ac commit dc998a4

12 files changed

+118
-16
lines changed

src/Application.cpp

+28-14
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ void Application::Start()
5959
{
6060
CheckInitialization();
6161

62-
SetupModel();
62+
SetupShader();
6363
SetupCamera();
6464
SetupLight();
6565

66+
SetupModel(m_assetsManager->GetAssetPath({"models", "backpack", "Survival_BackPack_2.fbx"}));
67+
LoadTexture(m_assetsManager->GetAssetPath({"models", "backpack", "1001_albedo.jpg"}), TextureType::Diffuse);
68+
LoadTexture(m_assetsManager->GetAssetPath({"models", "backpack", "1001_metallic.jpg"}), TextureType::Specular);
69+
6670
while (!glfwWindowShouldClose(m_window))
6771
{
6872
glfwPollEvents();
@@ -102,24 +106,34 @@ void Application::CheckInitialization()
102106
}
103107
}
104108

105-
void Application::SetupModel()
109+
void Application::SetupShader()
106110
{
107-
Shader* shader = new Shader(m_assetsManager->GetAssetPath({"shaders", "basic_vertex.vert"}),
111+
m_shader = new Shader(m_assetsManager->GetAssetPath({"shaders", "basic_vertex.vert"}),
108112
m_assetsManager->GetAssetPath({"shaders", "basic_fragment.frag"}));
109-
shader->LoadAndCompile();
113+
m_shader->LoadAndCompile();
114+
}
110115

111-
// Material* material = new Material();
112-
// material->diffuse = m_textureLoader
113-
// ->LoadTexture(g_config.modelFolder + g_config.textures.diffuseName, TextureType::Diffuse);
114-
// material->specular = m_textureLoader
115-
// ->LoadTexture(g_config.modelFolder + g_config.textures.specularName, TextureType::Specular);
116+
void Application::SetupModel(std::string path)
117+
{
118+
if(m_model)
119+
m_model->Unload();
116120

117-
// Model* model = m_loader->LoadModel(g_config.GetModelPath());
118-
// model->SetShader(shader);
119-
// model->SetScale(m_startModelScale);
120-
// model->SetMaterial(material);
121+
m_model = m_loader->LoadModel(path);
122+
m_model->SetShader(m_shader);
123+
m_model->SetScale(m_startModelScale);
124+
m_model->SetMaterial(new Material());
125+
}
121126

122-
// m_model = model;
127+
void Application::LoadTexture(std::string path, TextureType type)
128+
{
129+
if(!m_model)
130+
throw CannotSetTexture();
131+
132+
Texture texture = m_textureLoader
133+
->LoadTexture(path, type);
134+
135+
Material* material = m_model->GetMaterial();
136+
material->ReplaceTexture(texture, type);
123137
}
124138

125139
void Application::SetupCamera()

src/Application.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ class Application
3737
TextureLoader* m_textureLoader = nullptr;
3838
Renderer* m_renderer = nullptr;
3939
AssetsManager* m_assetsManager = nullptr;
40+
ImGuiIO* m_imguiIo;
4041

4142
Camera* m_camera = nullptr;
4243
Model* m_model = nullptr;
4344
DirLight* m_light = nullptr;
44-
ImGuiIO* m_imguiIo;
45+
Shader* m_shader = nullptr;
4546

4647
glm::vec3 m_startCameraPosition = glm::vec3(30.f, 0.f, 0.f);
4748
glm::vec4 m_startLightColor = glm::vec4(1.f, 1.f, 1.f, 1.f);
@@ -64,10 +65,14 @@ class Application
6465

6566
void CheckInitialization();
6667

67-
void SetupModel();
68+
void SetupShader();
69+
void SetupModel(std::string path);
70+
void LoadTexture(std::string path, TextureType type);
6871
void SetupCamera();
6972
void SetupLight();
7073

74+
void UnloadCurrentModel();
75+
7176
void InitImgui();
7277
void ShutdownImgui();
7378
void StartImguiFrame();

src/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ target_sources(ModelViewer PRIVATE
1212
Mesh.cpp
1313
Model.h
1414
Model.cpp
15+
Material.h
16+
Material.cpp
17+
Texture.h
18+
Texture.cpp
1519
Camera.h
1620
Camera.cpp
1721
Object.h

src/Exceptions.h

+8
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,12 @@ struct CannotConvertException : public CustomException
137137
{
138138
return "Cannot convert exception";
139139
}
140+
};
141+
142+
struct CannotSetTexture : public CustomException
143+
{
144+
const std::string GetMessage() const noexcept override
145+
{
146+
return "Cannot set texture exception";
147+
}
140148
};

src/Material.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "Material.h"
2+
3+
void Material::UnloadMaterial()
4+
{
5+
specular.Unload();
6+
diffuse.Unload();
7+
}
8+
9+
void Material::ReplaceTexture(Texture newTexture, TextureType type)
10+
{
11+
Texture* texture = nullptr;
12+
switch (type)
13+
{
14+
case TextureType::Diffuse:
15+
texture = &diffuse;
16+
break;
17+
case TextureType::Specular:
18+
texture = &specular;
19+
break;
20+
default:
21+
throw NotImplementedException();
22+
}
23+
24+
texture->Unload();
25+
*texture = newTexture;
26+
}

src/Material.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#pragma once
22

33
#include "Texture.h"
4+
#include "glad/glad.h"
5+
#include "Exceptions.h"
46

57
struct Material
68
{
79
Texture diffuse;
810
Texture specular;
11+
12+
void UnloadMaterial();
13+
void ReplaceTexture(Texture newTexture, TextureType type);
914
};

src/Mesh.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@ MeshData::MeshData(GLuint vao, GLuint vbo, GLuint ebo, unsigned int dataLength)
88
Mesh::Mesh(MeshData meshData)
99
: m_meshData(meshData)
1010
{
11+
}
12+
13+
void Mesh::Unload()
14+
{
15+
if(m_meshData.ebo != 0)
16+
glDeleteBuffers(1, &m_meshData.ebo);
17+
18+
if(m_meshData.vao != 0)
19+
glDeleteBuffers(1, &m_meshData.vao);
20+
21+
if(m_meshData.vbo != 0)
22+
glDeleteBuffers(1, &m_meshData.vbo);
1123
}

src/Mesh.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ class Mesh
3333
public:
3434

3535
Mesh(MeshData meshData);
36+
void Unload();
3637

3738
};

src/Model.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Model::~Model()
1010
delete m_meshes;
1111
}
1212

13+
Material* Model::GetMaterial()
14+
{
15+
return m_material;
16+
}
17+
1318
void Model::SetMaterial(Material* material)
1419
{
1520
m_material = material;
@@ -19,3 +24,13 @@ void Model::SetShader(Shader* shader)
1924
{
2025
m_shader = shader;
2126
}
27+
28+
void Model::Unload()
29+
{
30+
m_material->UnloadMaterial();
31+
32+
for(auto mesh : m_meshes[0])
33+
{
34+
mesh.Unload();
35+
}
36+
}

src/Model.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class Model : public Object
2323
Model(std::vector<Mesh>* meshesm);
2424
~Model();
2525

26+
Material* GetMaterial();
2627
void SetMaterial(Material* material);
2728
void SetShader(Shader* shader);
2829

30+
void Unload();
31+
2932
};

src/Texture.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Texture.h"
2+
3+
void Texture::Unload()
4+
{
5+
if(id != 0)
6+
glDeleteTextures(1, &id);
7+
}

src/Texture.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ struct Texture
1212
{
1313
TextureType type;
1414
GLuint id;
15+
16+
void Unload();
1517
};

0 commit comments

Comments
 (0)