Skip to content

Commit

Permalink
Improved material system to enable colors for primitive meshes
Browse files Browse the repository at this point in the history
- fixed text rendering issue (disable depth testing)
- deleted extra 3d sandboxes. Now there are two: for primitives and models
  • Loading branch information
denyskryvytskyi committed Mar 19, 2024
1 parent bfa76ee commit 0d5b0a4
Show file tree
Hide file tree
Showing 72 changed files with 846 additions and 1,145 deletions.
3 changes: 2 additions & 1 deletion Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ set(ENGINE_HEADERS
"src/Renderer/Texture2D.h"
"src/Renderer/TextRenderer.h"
"src/Renderer/Material.h"
"src/Renderer/Light.h"
"src/Renderer/Mesh.h"
"src/Renderer/RenderTopology.h"
"src/Platform/Windows/WindowsWindow.h"
Expand All @@ -63,6 +62,7 @@ set(ENGINE_HEADERS
"src/Scene/Components/SceneComponents.h"
"src/Scene/Components/TransformComponent.h"
"src/Scene/Components/StaticMeshComponent.h"
"src/Scene/Components/LightComponent.h"
"src/Scene/Systems/Render2dSystem.h"
"src/Scene/Systems/BehaviorSystem.h"
"src/Scene/Systems/Physics2dSystem.h"
Expand Down Expand Up @@ -124,6 +124,7 @@ set(ENGINE_SOURCES
"src/Scene/Components/SceneComponents.cpp"
"src/Scene/Components/TransformComponent.cpp"
"src/Scene/Components/StaticMeshComponent.cpp"
"src/Scene/Components/LightComponent.cpp"
"src/Scene/Systems/Render2dSystem.cpp"
"src/Scene/Systems/BehaviorSystem.cpp"
"src/Scene/Systems/Physics2dSystem.cpp"
Expand Down
1 change: 0 additions & 1 deletion Engine/src/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Application::Application()
auto& scene = gSceneManager.GetScene();
m_orthoCameraEntity = scene.CreateEntity();

// Init renderers
gTextureManager.Init();
gMeshLibrary.Init();
elv::Renderer::Init();
Expand Down
4 changes: 1 addition & 3 deletions Engine/src/Elven.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@
#include "Renderer/Renderer2D.h"

#include "Renderer/Buffer.h"
#include "Renderer/Material.h"
#include "Renderer/Shader.h"
#include "Renderer/VertexArray.h"

#include "Renderer/Light.h"
#include "Renderer/Material.h"

#include "Resources/TextureManager.h"

/// Scene ////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion Engine/src/Platform/OpenGL/OpenGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void OpenGLShader::SetVector4f(const std::string& name, const lia::vec4& value)

void OpenGLShader::SetMatrix4(const std::string& name, const lia::mat4& matrix)
{
glUniformMatrix4fv(GetUniformLocation(name), 1, GL_TRUE, matrix.elementsPtr());
glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, matrix.elementsPtr());
}

int OpenGLShader::GetUniformLocation(const std::string& name)
Expand Down
40 changes: 0 additions & 40 deletions Engine/src/Renderer/Light.h

This file was deleted.

34 changes: 25 additions & 9 deletions Engine/src/Renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ constexpr const char* kDefaultTextureMapNames[Material::TextureSlot::Count] = {
"texture_transparency"
};

Material::Material()
{
// set default textures
m_textures[TextureSlot::Diffuse].name = "white";
m_textures[TextureSlot::Specular].name = "black";
m_textures[TextureSlot::Emission].name = "black";
m_textures[TextureSlot::Transparency].name = "white";

LoadTexture("", false, m_textures[TextureSlot::Diffuse]);
LoadTexture("", false, m_textures[TextureSlot::Specular]);
LoadTexture("", false, m_textures[TextureSlot::Emission]);
LoadTexture("", false, m_textures[TextureSlot::Transparency]);
}

void Material::SetTexture(const TextureSlot slot, const std::string& name, SharedPtr<Texture2D> texture)
{
auto& map = m_textures[slot];
Expand All @@ -34,6 +48,7 @@ void Material::SetTexture(const TextureSlot slot, const std::string& name)

auto& map = m_textures[slot];
map.name = name;
map.needReload = true;
}

void Material::SetTexture(const TextureSlot slot, const std::string& name, const std::string& dir, bool async)
Expand All @@ -46,15 +61,16 @@ void Material::SetTexture(const TextureSlot slot, const std::string& name, const
auto& map = m_textures[slot];
map.name = name;

LoadTexture(dir, async, map, slot);
LoadTexture(dir, async, map);
}

void Material::LoadTextures(const std::string& dir, const bool async)
{
for (int i = 0; i < TextureSlot::Count; ++i) {
auto& textureMap = m_textures[i];
if (!textureMap.texturePtr && !textureMap.name.empty()) {
LoadTexture(dir, async, textureMap, static_cast<TextureSlot>(i));
if (textureMap.needReload || (!textureMap.texturePtr && !textureMap.name.empty())) {
LoadTexture(dir, async, textureMap);
textureMap.needReload = false;
}
}
}
Expand All @@ -76,7 +92,7 @@ void Material::SetSpecularColor(const lia::vec3& color)

void Material::SetEmissionColor(const lia::vec3& color)
{
m_emissionColor = color;
m_emissiveColor = color;
}

void Material::SetShininess(const float shininess)
Expand All @@ -91,17 +107,17 @@ void Material::ApplyMaterial(const SharedPtr<Shader>& shader) const
if (textureMap.texturePtr) {
textureMap.texturePtr->BindToUnit(i);
shader->SetInteger(fmt::format("u_Material.{}", kDefaultTextureMapNames[i]), i);
// TODO: Need to implement white texture binding instead "enabled" flags after PBR implemenetation
shader->SetInteger(fmt::format("u_Material.{}_enabled", kDefaultTextureMapNames[i]), 1);
} else {
shader->SetInteger(fmt::format("u_Material.{}_enabled", kDefaultTextureMapNames[i]), 0);
}
}

shader->SetVector3f("u_Material.ambientColor", m_ambientColor);
shader->SetVector3f("u_Material.diffuseColor", m_diffuseColor);
shader->SetVector3f("u_Material.specularColor", m_specularColor);
shader->SetVector3f("u_Material.emissionColor", m_emissiveColor);
shader->SetFloat("u_Material.shininess", m_shininess);
}

void Material::LoadTexture(const std::string& dir, const bool async, Material::TextureMap& map, Material::TextureSlot slot)
void Material::LoadTexture(const std::string& dir, const bool async, Material::TextureMap& map)
{
auto readyTexturePtr = textures::Get(map.name);
if (readyTexturePtr) {
Expand Down
23 changes: 17 additions & 6 deletions Engine/src/Renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class Material {

private:
struct TextureMap {
bool needReload { false };
std::string name;
SharedPtr<Texture2D> texturePtr { nullptr };
};

public:
Material();

/**
* Set already loaded texture texture to the slot
*
Expand Down Expand Up @@ -62,20 +65,28 @@ class Material {
const lia::vec3& GetAmbientColor() const { return m_ambientColor; }
const lia::vec3& GetDiffuseColor() const { return m_diffuseColor; }
const lia::vec3& GetSpecularColor() const { return m_specularColor; }
const lia::vec3& GetEmissionColor() const { return m_emissionColor; }
const lia::vec3& GetEmissionColor() const { return m_emissiveColor; }
float GetShininess() const { return m_shininess; }

void ApplyMaterial(const SharedPtr<Shader>& shader) const;

private:
void LoadTexture(const std::string& dir, const bool async, TextureMap& map, TextureSlot slot);
void LoadTexture(const std::string& dir, const bool async, TextureMap& map);

private:
TextureMap m_textures[TextureSlot::Count];
lia::vec3 m_ambientColor;
lia::vec3 m_diffuseColor;
lia::vec3 m_specularColor;
lia::vec3 m_emissionColor;

// Ambient & diffuse color is enabled by default
lia::vec3 m_ambientColor { 1.0f };
lia::vec3 m_diffuseColor { 1.0f };

// Enabled by default but texture is disabled for properly calculating specular texture color.
// To enable: set white texture to the specular texture slot
lia::vec3 m_specularColor { 1.0f };

// Enabled by default but texture is disabled for properly calculating emission texture color.
// To enable: set white texture to the emission texture slot
lia::vec3 m_emissiveColor { 1.0f };

float m_shininess { 32.0f };
};
Expand Down
5 changes: 5 additions & 0 deletions Engine/src/Renderer/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ void Mesh::AddSubmesh(Mesh&& submesh)
m_submeshes.emplace_back(std::move(submesh));
}

void Mesh::AddMaterialTexture(const Material::TextureSlot slot, const std::string& name, const std::string& path, bool async)
{
m_material.SetTexture(slot, name, path, async);
}

void Mesh::LoadTextures(const std::string& dir, const bool async)
{
m_material.LoadTextures(dir, async);
Expand Down
1 change: 1 addition & 0 deletions Engine/src/Renderer/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Mesh {
void SetInfo(const std::vector<MeshVertex>& vertices, const std::vector<std::uint32_t>& indices, const std::vector<MeshTexture>& texturesInfo);
void Draw(const SharedPtr<elv::Shader>& shader) const;
void AddSubmesh(Mesh&& submesh);
void AddMaterialTexture(const Material::TextureSlot slot, const std::string& name, const std::string& path, bool async);

void LoadTextures(const std::string& dir, const bool async);

Expand Down
12 changes: 9 additions & 3 deletions Engine/src/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void Renderer::Shutdown()
void Renderer::BeginScene(Camera& camera)
{
m_sceneData->ViewProjectionMatrix = camera.GetViewProjectionMatrix();
RenderCommand::EnableDepthTesting(true); // may be turned off in text rendering
}

void Renderer::EndScene()
Expand All @@ -35,10 +36,15 @@ void Renderer::Submit(const SharedPtr<Shader>& shader, const SharedPtr<VertexArr

void Renderer::Submit(const SharedPtr<Shader>& shader, const SharedPtr<Mesh>& mesh, const lia::mat4& modelMatrix)
{
shader->SetMatrix4("u_ViewProjection", m_sceneData->ViewProjectionMatrix);
shader->SetMatrix4("u_Model", modelMatrix);
if (mesh) {

shader->SetMatrix4("u_ViewProjection", m_sceneData->ViewProjectionMatrix);
shader->SetMatrix4("u_Model", modelMatrix);

mesh->Draw(shader);
mesh->Draw(shader);
} else {
EL_CORE_ERROR("Failed to render mesh: is empty");
}
}

void Renderer::OnWindowResize(std::uint32_t width, std::uint32_t height)
Expand Down
2 changes: 2 additions & 0 deletions Engine/src/Renderer/TextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void TextRenderer::PreRender(const Camera& camera)

s_data.cameraBounds = GetScene().GetComponent<elv::CameraComponent>(Application::Get().GetOrthographicCameraEntity()).camera.GetOrthographicsBounds();
s_data.pixelToCamera = GetPixelToCameraVec(s_data.cameraBounds);

RenderCommand::EnableDepthTesting(false);
}

void TextRenderer::RenderText(std::string_view text, const std::string& fontName, const lia::vec2& pos, const lia::vec2& scale, lia::vec4 color)
Expand Down
1 change: 1 addition & 0 deletions Engine/src/Resources/ModelImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void ProcessMesh(aiMesh* mesh, const aiScene* scene, lia::mat4 worldMatrix, Load
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
LoadMaterialTextures(material, aiTextureType_DIFFUSE, Material::TextureSlot::Diffuse, info.textures);
LoadMaterialTextures(material, aiTextureType_SPECULAR, Material::TextureSlot::Specular, info.textures);
LoadMaterialTextures(material, aiTextureType_METALNESS, Material::TextureSlot::Specular, info.textures);
LoadMaterialTextures(material, aiTextureType_EMISSIVE, Material::TextureSlot::Emission, info.textures);
LoadMaterialTextures(material, aiTextureType_NORMALS, Material::TextureSlot::Normal, info.textures);
LoadMaterialTextures(material, aiTextureType_HEIGHT, Material::TextureSlot::Normal, info.textures);
Expand Down
4 changes: 4 additions & 0 deletions Engine/src/Resources/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ void TextureManager::Init()
SharedPtr<Texture2D> whiteTexture = Load("white", 1, 1, 3);
unsigned char whiteData[] = { 255, 255, 255 }; // RGB format
whiteTexture->SetData(&whiteData, false);

SharedPtr<Texture2D> blackTexture = Load("black", 1, 1, 3);
unsigned char blackData[] = { 0, 0, 0 }; // RGB format
blackTexture->SetData(&blackData, false);
}

void TextureManager::Update()
Expand Down
8 changes: 8 additions & 0 deletions Engine/src/Scene/Components/LightComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <json.hpp>

#include "LightComponent.h"

#include "ComponentSerializerHelper.h"

namespace elv {
} // namespace elv
36 changes: 36 additions & 0 deletions Engine/src/Scene/Components/LightComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <json_fwd.hpp>

namespace elv {
struct PointLightComponent {
lia::vec3 ambient;
lia::vec3 diffuse;
lia::vec3 specular;

// distance 50
float constant { 1.0f };
float linear { 0.09f };
float quadratic { 0.032f };
};

struct DirectionalLightComponent {
lia::vec3 ambient;
lia::vec3 diffuse;
lia::vec3 specular;
};

struct SpotLightComponent {
lia::vec3 ambient;
lia::vec3 diffuse;
lia::vec3 specular;

float cutOff { 12.5f };
float outerCutOff { 17.5f };
// distance 50
float constant { 1.0f };
float linear { 0.09f };
float quadratic { 0.032f };
};

} // namespace elv
10 changes: 8 additions & 2 deletions Engine/src/Scene/Components/StaticMeshComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ void StaticMeshComponent::LoadMesh(const RenderTopology topology)
}
}

Material& StaticMeshComponent::GetMaterial()
{
EL_ASSERT(m_meshPtr, "Failed to get material: mesh is empty");

return m_meshPtr->GetMaterial();
}

void StaticMeshComponent::AddMaterialTexture(const Material::TextureSlot slot, const std::string& name, const std::string& path, bool async)
{
auto& material = m_meshPtr->GetMaterial();
material.SetTexture(slot, name, path, async);
m_meshPtr->AddMaterialTexture(slot, name, path, async);
}
} // namespace elv
1 change: 1 addition & 0 deletions Engine/src/Scene/Components/StaticMeshComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class StaticMeshComponent {
}

const SharedPtr<Mesh>& GetMeshPtr() const { return m_meshPtr; }
Material& GetMaterial();

void AddMaterialTexture(const Material::TextureSlot slot, const std::string& name, const std::string& path, bool async = true);

Expand Down
4 changes: 4 additions & 0 deletions Engine/src/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Core/Profiler.h"

#include "Components/LightComponent.h"
#include "Components/SceneComponents.h"
#include "Components/StaticMeshComponent.h"
#include "Systems/BehaviorSystem.h"
Expand All @@ -27,6 +28,9 @@ void Scene::OnInit()
RegisterComponent<RectTransformComponent>();
RegisterComponent<AABBComponent>();
RegisterComponent<TagComponent>();
RegisterComponent<PointLightComponent>();
RegisterComponent<DirectionalLightComponent>();
RegisterComponent<SpotLightComponent>();
RegisterComponent<StaticMeshComponent>();
if (gEngineSettings.enableSceneGraph) {
RegisterComponent<SceneNodeComponent>();
Expand Down
Loading

0 comments on commit 0d5b0a4

Please sign in to comment.