diff --git a/demos/combustion_demo/src/combustion_demo.cpp b/demos/combustion_demo/src/combustion_demo.cpp index ab7bedcef95122ba366b7797bb0f60e947c649c9..1703a2e915ec08804cb3b5925d32dc6d0890735c 100644 --- a/demos/combustion_demo/src/combustion_demo.cpp +++ b/demos/combustion_demo/src/combustion_demo.cpp @@ -103,11 +103,12 @@ int main(int, char**) { "models/combustion/data_box.stl", scene.get()); auto boundingbox_transform = model_boundingbox->GetFirstComponent<phx::Transform>(); - auto boundingbox_mesh_handle = boundingbox_transform->GetChild(0) - ->GetEntity() - ->GetFirstComponent<phx::MeshHandle>(); + auto boundigbox_mesh_entity = + boundingbox_transform->GetChild(0)->GetEntity(); + auto boundingbox_mesh_handle = + boundigbox_mesh_entity->GetFirstComponent<phx::MeshHandle>(); auto render_settings = - model_boundingbox->AddComponent<phx::MeshRenderSettings>(); + boundigbox_mesh_entity->AddComponent<phx::MeshRenderSettings>(); render_settings->SetWireframeMode(true); std::array<glm::vec3, 2> bbox = diff --git a/library/phx/rendering/render_passes/geometry_pass.cpp b/library/phx/rendering/render_passes/geometry_pass.cpp index 434b0b0a6e91d04957279a1f8b31e21a1ee2bfdb..aeaf4a167e430320f97bdc6b11101f152b261998 100644 --- a/library/phx/rendering/render_passes/geometry_pass.cpp +++ b/library/phx/rendering/render_passes/geometry_pass.cpp @@ -67,6 +67,10 @@ void GeometryPass::UploadMeshData( mesh_cache_.clear(); RenderOffset offset{0, 0}; for (const auto& instance : rendering_instances) { + if (instance.material != nullptr) { + instance.material->UploadTextures(); + } + Mesh* mesh = instance.mesh; if (mesh_cache_.find(mesh) != mesh_cache_.end()) { continue; diff --git a/library/phx/resources/types/material.cpp b/library/phx/resources/types/material.cpp index cf0b925a2b12089a464e9910b295ad9bf89fcf69..49bd551988f68eedfbee0d585a378d54e50c785a 100644 --- a/library/phx/resources/types/material.cpp +++ b/library/phx/resources/types/material.cpp @@ -42,7 +42,7 @@ ResourcePointer<Image> Material::GetDiffuseImage() const { } void Material::SetDiffuseImage(ResourcePointer<Image> image) { diffuse_image_ = image; - SetTexture(diffuse_image_, &diffuse_texture_); + ResetTexture(&diffuse_texture_); } gl::texture_2d* Material::GetDiffuseTexture() const { @@ -57,7 +57,7 @@ ResourcePointer<Image> Material::GetAmbientImage() const { } void Material::SetAmbientImage(ResourcePointer<Image> image) { ambient_image_ = image; - SetTexture(ambient_image_, &ambient_texture_); + ResetTexture(&ambient_texture_); } gl::texture_2d* Material::GetAmbientTexture() const { @@ -72,7 +72,7 @@ ResourcePointer<Image> Material::GetSpecularImage() const { } void Material::SetSpecularImage(ResourcePointer<Image> image) { specular_image_ = image; - SetTexture(specular_image_, &specular_texture_); + ResetTexture(&specular_texture_); } gl::texture_2d* Material::GetSpecularTexture() const { @@ -93,12 +93,23 @@ void Material::SetShininess(float shininess) { const Material* Material::GetDefault() { return &default_material_; } +void Material::UploadTextures() { + if (!diffuse_texture_) { + SetTexture(diffuse_image_, &diffuse_texture_); + } + if (!specular_texture_) { + SetTexture(specular_image_, &specular_texture_); + } + if (!ambient_texture_) { + SetTexture(ambient_image_, &ambient_texture_); + } +} + void Material::SetTexture(ResourcePointer<Image> image, std::shared_ptr<gl::texture_2d>* texture) { if (image == nullptr) return; - if (*texture && (*texture)->is_valid()) { - gl::texture_handle handle(*texture->get()); - handle.set_resident(false); + if (*texture) { + ResetTexture(texture); } auto dimensions = image->GetDimensions(); @@ -123,4 +134,12 @@ void Material::SetTexture(ResourcePointer<Image> image, gl::print_error("[Material::SetTexture] OpenGl Error code"); } +void Material::ResetTexture(std::shared_ptr<gl::texture_2d>* texture) { + if (*texture && (*texture)->is_valid()) { + gl::texture_handle handle(*texture->get()); + handle.set_resident(false); + } + texture->reset(); +} + } // namespace phx diff --git a/library/phx/resources/types/material.hpp b/library/phx/resources/types/material.hpp index ae74ee21dba19043c2478ae9a31cc2237e009b3b..fc839341bbf95af5e3ec719aeaaef0caf15a56b6 100644 --- a/library/phx/resources/types/material.hpp +++ b/library/phx/resources/types/material.hpp @@ -83,9 +83,13 @@ class PHOENIX_EXPORT Material : public Resource, public Nameable { static const Material* GetDefault(); + // unless this was called Get..Texture return nullptr + void UploadTextures(); + private: void SetTexture(ResourcePointer<Image> image, std::shared_ptr<gl::texture_2d>* texture); + void ResetTexture(std::shared_ptr<gl::texture_2d>* texture); ResourcePointer<Image> ambient_image_{nullptr}; ResourcePointer<Image> diffuse_image_{nullptr};