Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
47 changes: 29 additions & 18 deletions Assets/Shaders/phong.fs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#version 330 core
#define ICE_MAX_LIGHTS (256)
#define ICE_MAX_LIGHTS (16)

out vec4 frag_color;

struct Light {
vec3 position;
vec3 rotation;
vec3 color;
int type; //0 = Point, 1 = Directional, 2 = Spot
};

struct Material {
vec3 albedo;
vec3 specular;
vec3 ambient;
vec4 albedo;
vec4 specular;
vec4 ambient;
float alpha;
bool use_diffuse_map;
sampler2D diffuse_map;
Expand All @@ -36,33 +37,43 @@ in vec2 ftex_coords;

vec3 normal;

vec3 pointLight(Light light) {
vec3 rcolor = vec3(0.0);
vec4 computeLightEffect(Light light, vec3 light_direction) {
vec4 rcolor = vec4(0.0);

//diffuse
vec3 n = normalize(normal);
vec3 light_direction = normalize(light.position - fposition);
float diff = max(dot(n, light_direction), 0.0);
vec3 diffuse_color = material.albedo;
vec4 diffuse_color = material.albedo;
if(material.use_diffuse_map) {
diffuse_color *= texture(material.diffuse_map, ftex_coords).xyz;
diffuse_color *= texture(material.diffuse_map, ftex_coords);
}
rcolor += light.color * (diff * diffuse_color);
rcolor += vec4(light.color, 1.0) * (diff * diffuse_color);

if(diff > 0) {
//specular
vec3 view_direction = normalize(fview - fposition);
vec3 reflection_direction = reflect(-light_direction, n);
float spec = pow(max(dot(view_direction, reflection_direction), 0.0), material.alpha);
vec3 specular_color = material.specular;
vec4 specular_color = material.specular;
if(material.use_specular_map) {
specular_color *= texture(material.specular_map, ftex_coords).xyz;
specular_color *= texture(material.specular_map, ftex_coords);
}
rcolor += light.color * (spec * specular_color);
rcolor += vec4(light.color, 1.0) * (spec * specular_color);
}
return rcolor;
}

vec4 applyLight(Light light) {
if(light.type == 0) {
vec3 light_direction = normalize(light.position - fposition);
float distance = length(light.position - fposition);
return computeLightEffect(light, light_direction);
} else if(light.type == 1) {
return computeLightEffect(light, -normalize(light.rotation));
} else if(light.type == 2) {
//TODO: Spot lights
}
}

vec3 colorToNormal(vec3 color) {
return normalize(vec3(color.x * 2 - 1, color.y * 2 - 1, color.z * 2 - 1));
Expand All @@ -76,19 +87,19 @@ void main() {
normal = fnormal;
}
//ambient
vec3 color_accumulator = vec3(material.ambient*ambient_light);
vec4 color_accumulator = material.ambient * vec4(ambient_light, 1.0);
if(material.use_ambient_map) {
color_accumulator *= texture(material.ambient_map, ftex_coords).xyz;
color_accumulator *= texture(material.ambient_map, ftex_coords);
}
for(int i = 0; i < light_count; i++) {
color_accumulator += pointLight(lights[i]);
color_accumulator += applyLight(lights[i]);
}
if(light_count == 0) {
frag_color = vec4(material.albedo, 1.0);
frag_color = material.albedo;
if(material.use_diffuse_map) {
frag_color *= texture(material.diffuse_map, ftex_coords);
}
} else {
frag_color = vec4(color_accumulator, 1.0);
frag_color = color_accumulator;
}
}
4 changes: 1 addition & 3 deletions ICE/Assets/include/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ namespace ICE {

typedef unsigned long long AssetUID;

enum class AssetType { ETex2D, ETexCube, EShader, EMesh, EMaterial };
enum class AssetType { ETex2D, ETexCube, EShader, EMesh, EModel, EMaterial };

class Asset : public Resource {
public:
Asset() : Resource({}) {}
virtual AssetType getType() const = 0;
virtual std::string getTypeName() const = 0;
virtual void load() = 0;
virtual void unload() = 0;
};
} // namespace ICE
12 changes: 11 additions & 1 deletion ICE/Assets/include/AssetBank.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ class AssetBank {
return false;
}

bool addAssetWithSpecificUID(const AssetPath& name, const std::shared_ptr<Asset>& asset, AssetUID id) {
if (resources.find(id) == resources.end() && nameMapping.find(name) == nameMapping.end()) {
resources.try_emplace(id, AssetBankEntry{name, asset});
nameMapping.try_emplace(name, id);
nextUID = nextUID > id ? nextUID : id + 1;
return true;
}
return false;
}

bool renameAsset(const AssetPath& oldName, const AssetPath& newName) {
if (oldName.toString() == newName.toString()) {
return true;
Expand All @@ -108,7 +118,7 @@ class AssetBank {
if (nameMapping.find(name) != nameMapping.end()) {
AssetUID id = getUID(name);
nameMapping.erase(name);
resources[id].asset->unload();
//TODO: Check resources[id].asset->unload();
resources.erase(id);
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions ICE/Assets/include/AssetLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Asset.h"
#include "IAssetLoader.h"
#include "Model.h"

namespace ICE {
class AssetLoader {
Expand All @@ -32,9 +33,10 @@ class AssetLoader {
}

private:
std::unordered_map<std::type_index,
std::variant<std::shared_ptr<IAssetLoader<Mesh>>, std::shared_ptr<IAssetLoader<Material>>, std::shared_ptr<IAssetLoader<Shader>>,
std::shared_ptr<IAssetLoader<Texture2D>>, std::shared_ptr<IAssetLoader<TextureCube>>>>
std::unordered_map<
std::type_index,
std::variant<std::shared_ptr<IAssetLoader<Model>>, std::shared_ptr<IAssetLoader<Material>>, std::shared_ptr<IAssetLoader<Shader>>,
std::shared_ptr<IAssetLoader<Texture2D>>, std::shared_ptr<IAssetLoader<TextureCube>>>>
loaders;
};
} // namespace ICE
6 changes: 3 additions & 3 deletions ICE/Assets/src/AssetBank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "AssetBank.h"

#include <ICEException.h>
#include <MeshLoader.h>
#include <Model.h>

#include "MaterialLoader.h"
#include "MeshLoader.h"
#include "ModelLoader.h"
#include "ShaderLoader.h"
#include "TextureLoader.h"

Expand All @@ -17,7 +17,7 @@ namespace ICE {
AssetBank::AssetBank(const std::shared_ptr<GraphicsFactory> &factory) : graphics_factory(factory) {
loader.AddLoader<Texture2D>(std::make_shared<Texture2DLoader>(factory));
loader.AddLoader<TextureCube>(std::make_shared<TextureCubeLoader>(factory));
loader.AddLoader<Mesh>(std::make_shared<MeshLoader>(factory));
loader.AddLoader<Model>(std::make_shared<ModelLoader>(factory, *this));
loader.AddLoader<Shader>(std::make_shared<ShaderLoader>(factory));
loader.AddLoader<Material>(std::make_shared<MaterialLoader>());
}
Expand Down
78 changes: 39 additions & 39 deletions ICE/Assets/src/AssetPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@
// Created by Thomas Ibanez on 03.08.21.
//

#include <Texture.h>
#include <Mesh.h>
#include "AssetPath.h"

#include <Material.h>
#include <Model.h>
#include <Shader.h>
#include "AssetPath.h"
#include <Texture.h>

namespace ICE {

std::unordered_map<std::type_index, std::string> AssetPath::typenames = {
{typeid(Texture2D), "Textures"},
{typeid(TextureCube), "CubeMaps"},
{typeid(Mesh), "Meshes"},
{typeid(Material), "Materials"},
{typeid(Shader), "Shaders"}
};

AssetPath::AssetPath(std::string path) {
size_t last = 0;
for(size_t i = 0; i < path.length(); i++) {
if(path[i] == ASSET_PATH_SEPARATOR) {
this->path.push_back(path.substr(last, i-last));
last = i + 1;
}
std::unordered_map<std::type_index, std::string> AssetPath::typenames = {{typeid(Texture2D), "Textures"},
{typeid(TextureCube), "CubeMaps"},
{typeid(Model), "Models"},
{typeid(Material), "Materials"},
{typeid(Shader), "Shaders"}};

AssetPath::AssetPath(std::string path) {
size_t last = 0;
for (size_t i = 0; i < path.length(); i++) {
if (path[i] == ASSET_PATH_SEPARATOR) {
this->path.push_back(path.substr(last, i - last));
last = i + 1;
}
name = path.substr(last, path.length()-last);
}
name = path.substr(last, path.length() - last);
}

std::string AssetPath::toString() const {
return (prefix() + name);
}
std::string AssetPath::toString() const {
return (prefix() + name);
}

std::vector<std::string> AssetPath::getPath() const {
return path;
}
std::vector<std::string> AssetPath::getPath() const {
return path;
}

std::string AssetPath::getName() const {
return name;
}
std::string AssetPath::getName() const {
return name;
}

void AssetPath::setName(const std::string &name) {
AssetPath::name = name;
}
void AssetPath::setName(const std::string &name) {
AssetPath::name = name;
}

AssetPath::AssetPath(const AssetPath& cpy) : AssetPath(cpy.toString()) {}
AssetPath::AssetPath(const AssetPath &cpy) : AssetPath(cpy.toString()) {
}

std::string AssetPath::prefix() const {
auto str = std::string();
for(auto &p : path) {
str += p+ASSET_PATH_SEPARATOR;
}
return str;
std::string AssetPath::prefix() const {
auto str = std::string();
for (auto &p : path) {
str += p + ASSET_PATH_SEPARATOR;
}
}
return str;
}
} // namespace ICE
2 changes: 2 additions & 0 deletions ICE/Assets/test/AssetBankTest.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//
// Created by Thomas Ibanez on 24.02.21.
//
#define STB_IMAGE_IMPLEMENTATION
#include <gtest/gtest.h>

#include "AssetBank.h"
#include "NoneGraphicsFactory.h"

using namespace ICE;


TEST(AssetBankTest, AddedAssetsCanBeRetrieved) {
NoneGraphicsFactory g_fac;
AssetBank ab(std::make_shared<NoneGraphicsFactory>(g_fac));
Expand Down
4 changes: 2 additions & 2 deletions ICE/Assets/test/AssetPathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by Thomas Ibanez on 24.02.21.
//
#include <Material.h>
#include <Mesh.h>
#include <Model.h>
#include <Shader.h>
#include <Texture.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -32,7 +32,7 @@ TEST(AssetPathTest, setNameOk) {

TEST(AssetPathTest, TypePrefixOk) {
EXPECT_EQ(AssetPath::WithTypePrefix<Material>("a").toString(), "Materials/a");
EXPECT_EQ(AssetPath::WithTypePrefix<Mesh>("a").toString(), "Meshes/a");
EXPECT_EQ(AssetPath::WithTypePrefix<Model>("a").toString(), "Models/a");
EXPECT_EQ(AssetPath::WithTypePrefix<Shader>("a").toString(), "Shaders/a");
EXPECT_EQ(AssetPath::WithTypePrefix<Texture2D>("a").toString(), "Textures/a");
EXPECT_EQ(AssetPath::WithTypePrefix<TextureCube>("a").toString(), "CubeMaps/a");
Expand Down
2 changes: 2 additions & 0 deletions ICE/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ project(ICE)

message(STATUS "Building ${PROJECT_NAME}")

add_compile_definitions(NOMINMAX)

add_subdirectory(Assets)
add_subdirectory(Components)
add_subdirectory(Core)
Expand Down
5 changes: 2 additions & 3 deletions ICE/Components/include/RenderComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace ICE {
struct RenderComponent : public Component {
RenderComponent(AssetUID mesh_id, AssetUID material_id) : mesh(mesh_id), material(material_id) {}
AssetUID mesh;
AssetUID material;
RenderComponent(AssetUID model_id) : model(model_id) {}
AssetUID model;
};
} // namespace ICE

Expand Down
1 change: 1 addition & 0 deletions ICE/Core/src/ICEEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define STB_IMAGE_IMPLEMENTATION

#include "ICEEngine.h"

#include <EngineConfig.h>
Expand Down
3 changes: 2 additions & 1 deletion ICE/Graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ target_sources(${PROJECT_NAME} PRIVATE
src/ForwardRenderer.cpp
src/Material.cpp
src/Mesh.cpp
src/GeometryPass.cpp "include/RenderData.h")
src/Model.cpp
src/GeometryPass.cpp)

target_link_libraries(${PROJECT_NAME}
PUBLIC
Expand Down
1 change: 1 addition & 0 deletions ICE/Graphics/include/GraphicsFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class GraphicsFactory {
const std::string& fragmentFile) const = 0;

virtual std::shared_ptr<Texture2D> createTexture2D(const std::string& file) const = 0;
virtual std::shared_ptr<Texture2D> createTexture2D(const void* data, size_t w, size_t h, TextureFormat fmt) const = 0;

virtual std::shared_ptr<TextureCube> createTextureCube(const std::string& file) const = 0;
};
Expand Down
2 changes: 0 additions & 2 deletions ICE/Graphics/include/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class Material : public Asset {
//Asset interface
std::string getTypeName() const override;
AssetType getType() const override;
void load() override;
void unload() override;

private:
AssetUID m_shader = NO_ASSET_ID;
Expand Down
2 changes: 0 additions & 2 deletions ICE/Graphics/include/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class Mesh : public Asset {

std::string getTypeName() const override;
AssetType getType() const override;
void load() override;
void unload() override;

private:
std::vector<Eigen::Vector3f> vertices, normals;
Expand Down
Loading