Compare commits
5 Commits
4e86d92987
...
f56e524d05
Author | SHA1 | Date | |
---|---|---|---|
f56e524d05 | |||
b989d74fca | |||
42d5def07e | |||
fefe273fce | |||
99f5cd3715 |
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -58,6 +58,9 @@
|
|||||||
"typeinfo": "cpp",
|
"typeinfo": "cpp",
|
||||||
"variant": "cpp",
|
"variant": "cpp",
|
||||||
"codecvt": "cpp",
|
"codecvt": "cpp",
|
||||||
"typeindex": "cpp"
|
"typeindex": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"unordered_set": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,6 +16,13 @@ if (UNIX)
|
|||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(glm)
|
FetchContent_MakeAvailable(glm)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
EnTT
|
||||||
|
GIT_REPOSITORY https://github.com/skypjack/entt.git
|
||||||
|
GIT_TAG d4014c74dc3793aba95ae354d6e23a026c2796db
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(EnTT)
|
||||||
|
|
||||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(GLEW REQUIRED)
|
find_package(GLEW REQUIRED)
|
||||||
@ -58,6 +65,7 @@ target_link_libraries(CodingGame PRIVATE
|
|||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
GLEW::GLEW
|
GLEW::GLEW
|
||||||
glm::glm
|
glm::glm
|
||||||
|
EnTT::EnTT
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------- Visibility (helps optimizer & smaller binaries on Release) ----------
|
# ---------- Visibility (helps optimizer & smaller binaries on Release) ----------
|
||||||
|
12
assets/sphere.mtl
Normal file
12
assets/sphere.mtl
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# Blender 4.3.2 MTL File: 'None'
|
||||||
|
# www.blender.org
|
||||||
|
|
||||||
|
newmtl Material.001
|
||||||
|
Ns 250.000000
|
||||||
|
Ka 1.000000 1.000000 1.000000
|
||||||
|
Kd 0.799999 0.715049 0.364110
|
||||||
|
Ks 0.500000 0.500000 0.500000
|
||||||
|
Ke 0.000000 0.000000 0.000000
|
||||||
|
Ni 1.500000
|
||||||
|
d 1.000000
|
||||||
|
illum 2
|
2537
assets/sphere.obj
Normal file
2537
assets/sphere.obj
Normal file
File diff suppressed because it is too large
Load Diff
6
include/components/camera.h
Normal file
6
include/components/camera.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef COMPONENTS_PLAYER_H_
|
||||||
|
#define COMPONENTS_PLAYER_H_
|
||||||
|
|
||||||
|
struct camera {};
|
||||||
|
|
||||||
|
#endif // COMPONENTS_PLAYER_H_
|
6
include/components/light.h
Normal file
6
include/components/light.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef COMPONENTS_LIGHT_H_
|
||||||
|
#define COMPONENTS_LIGHT_H_
|
||||||
|
|
||||||
|
struct light {};
|
||||||
|
|
||||||
|
#endif // COMPONENTS_LIGHT_H_
|
11
include/components/mesh.h
Normal file
11
include/components/mesh.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef COMPONENTS_MESH_H_
|
||||||
|
#define COMPONENTS_MESH_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include "renderer/wavefront.h"
|
||||||
|
|
||||||
|
struct mesh {
|
||||||
|
std::unique_ptr<Object> object;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMPONENTS_MESH_H_
|
12
include/components/transform.h
Normal file
12
include/components/transform.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COMPONENTS_TRANSFORM_H_
|
||||||
|
#define COMPONENTS_TRANSFORM_H_
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
struct transform {
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::vec3 rotation;
|
||||||
|
glm::vec3 scale;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMPONENTS_TRANSFORM_H_
|
@ -1,14 +0,0 @@
|
|||||||
#ifndef ECS_ENTITY_CAMERA_H_
|
|
||||||
#define ECS_ENTITY_CAMERA_H
|
|
||||||
|
|
||||||
#include "ecs/entity.h"
|
|
||||||
|
|
||||||
namespace ecs {
|
|
||||||
class Camera : Entity {
|
|
||||||
public:
|
|
||||||
Camera() = default;
|
|
||||||
~Camera() = default;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ECS_ENTITY_CAMERA_H_
|
|
@ -1,9 +0,0 @@
|
|||||||
#ifndef ECS_COMPONENT_H_
|
|
||||||
#define ECS_COMPONENT_H_
|
|
||||||
|
|
||||||
namespace ecs {
|
|
||||||
class Component {
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ECS_COMPONENT_H_
|
|
@ -1,16 +0,0 @@
|
|||||||
#ifndef ECS_COMP_MESH_H_
|
|
||||||
#define ECS_COMP_MESH_H_
|
|
||||||
|
|
||||||
#include "renderer/wavefront.h"
|
|
||||||
#include "ecs/component.h"
|
|
||||||
|
|
||||||
namespace ecs {
|
|
||||||
class Mesh : Component {
|
|
||||||
public:
|
|
||||||
Object* object;
|
|
||||||
|
|
||||||
Mesh(Object* model) : object(model) {}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ECS_COMP_MESH_H_
|
|
@ -1,18 +0,0 @@
|
|||||||
#ifndef ECS_COMP_TRANSFORM_H_
|
|
||||||
#define ECS_COMP_TRANSFORM_H_
|
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include "ecs/component.h"
|
|
||||||
|
|
||||||
namespace ecs {
|
|
||||||
class Transform : Component {
|
|
||||||
public:
|
|
||||||
Transform() = default;
|
|
||||||
public:
|
|
||||||
glm::vec3 pos;
|
|
||||||
glm::vec3 rot;
|
|
||||||
glm::vec3 scale;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ECS_COMP_TRANSFORM_H_
|
|
@ -1,18 +0,0 @@
|
|||||||
#ifndef ECS_ENTITY_H_
|
|
||||||
#define ECS_ENTITY_H_
|
|
||||||
|
|
||||||
#include "ecs/components/transform.h"
|
|
||||||
#include "ecs/components/mesh.h"
|
|
||||||
|
|
||||||
namespace ecs {
|
|
||||||
class Entity {
|
|
||||||
public:
|
|
||||||
Transform transform;
|
|
||||||
Mesh mesh;
|
|
||||||
|
|
||||||
Entity(Mesh m) : mesh(m) {}
|
|
||||||
~Entity() = default;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ECS_ENTITY_H_
|
|
@ -2,16 +2,16 @@
|
|||||||
#define RENDERER_H_
|
#define RENDERER_H_
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include <entt/entity/registry.hpp>
|
||||||
|
|
||||||
#include "renderer/shader.h"
|
#include "renderer/shader.h"
|
||||||
#include "ecs/entity.h"
|
|
||||||
|
|
||||||
// TODO: make static or singleton
|
// TODO: make static or singleton
|
||||||
class Renderer {
|
class Renderer {
|
||||||
public:
|
public:
|
||||||
Renderer(ecs::Entity& light, ecs::Entity& camera);
|
Renderer();
|
||||||
|
|
||||||
void RenderLight();
|
void Render(entt::registry& registry);
|
||||||
void RenderEntity(const ecs::Entity& entity);
|
|
||||||
|
|
||||||
void OnWindowResized(int w, int h);
|
void OnWindowResized(int w, int h);
|
||||||
private:
|
private:
|
||||||
@ -20,9 +20,6 @@ private:
|
|||||||
glm::mat4 m_model;
|
glm::mat4 m_model;
|
||||||
glm::mat4 m_proj;
|
glm::mat4 m_proj;
|
||||||
glm::mat4 m_view;
|
glm::mat4 m_view;
|
||||||
|
|
||||||
ecs::Entity& m_light;
|
|
||||||
ecs::Entity& m_camera;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // RENDERER_H_
|
#endif // RENDERER_H_
|
@ -35,6 +35,7 @@ private:
|
|||||||
void CreateNewMesh(const std::string& materialName);
|
void CreateNewMesh(const std::string& materialName);
|
||||||
public:
|
public:
|
||||||
void Render(Shader& shader);
|
void Render(Shader& shader);
|
||||||
|
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::vector<glm::vec3> m_vertices;
|
std::vector<glm::vec3> m_vertices;
|
||||||
|
55
src/main.cpp
55
src/main.cpp
@ -16,26 +16,31 @@
|
|||||||
#include "renderer/wavefront.h"
|
#include "renderer/wavefront.h"
|
||||||
#include "renderer/engine.h"
|
#include "renderer/engine.h"
|
||||||
#include "renderer/renderer.h"
|
#include "renderer/renderer.h"
|
||||||
#include "ecs/entity.h"
|
|
||||||
|
|
||||||
#include "IO/file_manager.h"
|
#include "IO/file_manager.h"
|
||||||
|
|
||||||
|
#include "components/transform.h"
|
||||||
|
#include "components/light.h"
|
||||||
|
#include "components/camera.h"
|
||||||
|
#include "components/mesh.h"
|
||||||
|
|
||||||
class Game : public IApplication {
|
class Game : public IApplication {
|
||||||
public:
|
public:
|
||||||
Game()
|
Game() {
|
||||||
: m_light(ecs::Entity(ecs::Mesh(Object::LoadFile("./assets/cube.obj")))),
|
Object* lightObj = Object::LoadFile("./assets/sphere.obj");
|
||||||
m_camera(ecs::Entity(ecs::Mesh(nullptr))),
|
const auto lightEntity = m_registry.create();
|
||||||
m_target(ecs::Entity(ecs::Mesh(Object::LoadFile("./assets/monkey.obj")))),
|
m_registry.emplace<transform>(lightEntity, glm::vec3(-5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||||
m_renderer(m_light, m_camera) {
|
m_registry.emplace<light>(lightEntity);
|
||||||
// Object* lightObj = Object::LoadFile("./assets/cube.obj");
|
m_registry.emplace<mesh>(lightEntity, std::unique_ptr<Object>(lightObj));
|
||||||
// ecs::Mesh lightMesh = ecs::Mesh(lightObj);
|
|
||||||
// m_light = ecs::Entity(lightMesh);
|
|
||||||
m_light.transform.pos = glm::vec3(-5.f, 5.f, 5.f);
|
|
||||||
|
|
||||||
// Object* cameraObj = nullptr;
|
const auto cameraEntity = m_registry.create();
|
||||||
// ecs::Mesh cameraMesh = ecs::Mesh(cameraObj);
|
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 0.f, 2.f));
|
||||||
// m_camera = ecs::Entity(cameraMesh);
|
m_registry.emplace<camera>(cameraEntity, glm::vec3(0.f, 0.f, 2.f));
|
||||||
m_camera.transform.pos = glm::vec3(0.f, 0.f, 2.f);
|
|
||||||
|
Object* targetObj = Object::LoadFile("./assets/monkey.obj");
|
||||||
|
const auto targetEntity = m_registry.create();
|
||||||
|
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f));
|
||||||
|
m_registry.emplace<mesh>(targetEntity, std::unique_ptr<Object>(targetObj));
|
||||||
}
|
}
|
||||||
~Game() override {}
|
~Game() override {}
|
||||||
|
|
||||||
@ -100,8 +105,11 @@ public:
|
|||||||
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
||||||
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
||||||
|
|
||||||
m_camera.transform.pos += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
auto view = m_registry.view<camera, transform>();
|
||||||
m_camera.transform.rot = cameraViewDirection;
|
for (auto [cam, camTransform] : view.each()) {
|
||||||
|
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||||
|
camTransform.rotation = cameraViewDirection;
|
||||||
|
}
|
||||||
|
|
||||||
// update rotation
|
// update rotation
|
||||||
if (!m_paused) {
|
if (!m_paused) {
|
||||||
@ -111,12 +119,17 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_target.transform.rot.y = m_angle;
|
auto rotateEntts = m_registry.view<transform, const mesh>();
|
||||||
|
for (auto [entity, transform, mesh] : rotateEntts.each()) {
|
||||||
|
// auto targetTransform = rotateEntts.get<transform>(entity);
|
||||||
|
if (!m_registry.all_of<light>(entity)) {
|
||||||
|
transform.rotation.y = m_angle;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnRender() override {
|
void OnRender() override {
|
||||||
m_renderer.RenderLight();
|
m_renderer.Render(m_registry);
|
||||||
m_renderer.RenderEntity(m_target);
|
|
||||||
|
|
||||||
m_frameCount++;
|
m_frameCount++;
|
||||||
m_currentTicks = SDL_GetTicks();
|
m_currentTicks = SDL_GetTicks();
|
||||||
@ -131,9 +144,7 @@ public:
|
|||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
Renderer m_renderer;
|
Renderer m_renderer;
|
||||||
ecs::Entity m_target;
|
entt::registry m_registry;
|
||||||
ecs::Entity m_light;
|
|
||||||
ecs::Entity m_camera;
|
|
||||||
|
|
||||||
float m_angle;
|
float m_angle;
|
||||||
Uint64 m_lastTicks;
|
Uint64 m_lastTicks;
|
||||||
|
@ -7,8 +7,12 @@
|
|||||||
#include "window/window.h"
|
#include "window/window.h"
|
||||||
#include "IO/file_manager.h"
|
#include "IO/file_manager.h"
|
||||||
|
|
||||||
Renderer::Renderer(ecs::Entity& light, ecs::Entity& camera)
|
#include "components/transform.h"
|
||||||
: m_light(light), m_camera(camera)
|
#include "components/camera.h"
|
||||||
|
#include "components/light.h"
|
||||||
|
#include "components/mesh.h"
|
||||||
|
|
||||||
|
Renderer::Renderer()
|
||||||
{
|
{
|
||||||
m_proj = glm::perspective(
|
m_proj = glm::perspective(
|
||||||
static_cast<float>(M_PI_2),
|
static_cast<float>(M_PI_2),
|
||||||
@ -19,12 +23,14 @@ Renderer::Renderer(ecs::Entity& light, ecs::Entity& camera)
|
|||||||
|
|
||||||
m_shader.init(
|
m_shader.init(
|
||||||
FileManager::read("./src/shaders/simple.vs"),
|
FileManager::read("./src/shaders/simple.vs"),
|
||||||
FileManager::read("./src/shaders/simple.fs")
|
FileManager::read("./src/shaders/pbr.fs")
|
||||||
);
|
);
|
||||||
|
|
||||||
m_model = glm::mat4(1.f);
|
m_model = glm::mat4(1.f);
|
||||||
|
|
||||||
m_shader.use();
|
m_shader.use();
|
||||||
|
|
||||||
|
m_shader.setMat4("u_projection", m_proj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::OnWindowResized(int w, int h) {
|
void Renderer::OnWindowResized(int w, int h) {
|
||||||
@ -34,53 +40,55 @@ void Renderer::OnWindowResized(int w, int h) {
|
|||||||
0.01f,
|
0.01f,
|
||||||
100.0f
|
100.0f
|
||||||
);
|
);
|
||||||
|
m_shader.setMat4("u_projection", m_proj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::RenderLight() {
|
void Renderer::Render(entt::registry& registry) {
|
||||||
|
auto view = registry.view<transform, mesh>();
|
||||||
|
|
||||||
|
auto cam = registry.view<transform, camera>().back();
|
||||||
|
auto camTransform = registry.get<transform>(cam);
|
||||||
|
|
||||||
|
auto lightEntt = registry.view<transform, light>().back();
|
||||||
|
auto lightTransform = registry.get<transform>(lightEntt);
|
||||||
|
|
||||||
m_view = glm::lookAt(
|
m_view = glm::lookAt(
|
||||||
m_camera.transform.pos,
|
camTransform.position,
|
||||||
m_camera.transform.pos + m_camera.transform.rot,
|
camTransform.position + camTransform.rotation,
|
||||||
glm::vec3(0.f, 1.f, 0.f)
|
glm::vec3(0.f, 1.f, 0.f)
|
||||||
);
|
);
|
||||||
|
|
||||||
m_shader.setMat4("u_view", m_view);
|
m_shader.setMat4("u_view", m_view);
|
||||||
m_shader.setMat4("u_projection", m_proj);
|
|
||||||
|
|
||||||
m_shader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
m_shader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||||
m_shader.setVec3("lightPos", m_light.transform.pos);
|
m_shader.setVec3("lightPos", lightTransform.position);
|
||||||
m_shader.setVec3("viewPos", m_camera.transform.pos);
|
m_shader.setVec3("viewPos", camTransform.position);
|
||||||
|
|
||||||
m_model = glm::mat4(1.f);
|
// std::cout << "cam pos: " << "vec(" << camTransform.position.x << ", " << camTransform.position.y << ", " << camTransform.position.z << ")" << std::endl;
|
||||||
m_model = glm::translate(m_model, m_light.transform.pos);
|
// std::cout << "cam rot: " << "vec(" << camTransform.rotation.x << ", " << camTransform.rotation.y << ", " << camTransform.rotation.z << ")" << std::endl;
|
||||||
|
|
||||||
m_shader.setMat4("u_model", m_model);
|
// std::cout << "light pos: " << "vec(" << lightTransform.position.x << ", " << lightTransform.position.y << ", " << lightTransform.position.z << ")" << std::endl;
|
||||||
|
// std::cout << "light rot: " << "vec(" << lightTransform.rotation.x << ", " << lightTransform.rotation.y << ", " << lightTransform.rotation.z << ")" << std::endl;
|
||||||
|
|
||||||
m_light.mesh.object->Render(m_shader);
|
for (auto [entity, transf, mesh] : view.each()) {
|
||||||
}
|
if (mesh.object == nullptr) {
|
||||||
|
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
void Renderer::RenderEntity(const ecs::Entity& entity) {
|
m_shader.setBool("isLight", registry.all_of<light>(entity));
|
||||||
if (entity.mesh.object == nullptr) {
|
|
||||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
m_model = glm::mat4(1.0f);
|
||||||
return;
|
|
||||||
|
// Apply translation
|
||||||
|
m_model = glm::translate(m_model, transf.position);
|
||||||
|
|
||||||
|
// Apply rotations (order matters!)
|
||||||
|
m_model = glm::rotate(m_model, transf.rotation.x, glm::vec3(1, 0, 0)); // pitch
|
||||||
|
m_model = glm::rotate(m_model, transf.rotation.y, glm::vec3(0, 1, 0)); // yaw
|
||||||
|
m_model = glm::rotate(m_model, transf.rotation.z, glm::vec3(0, 0, 1)); // roll
|
||||||
|
|
||||||
|
m_shader.setMat4("u_model", m_model);
|
||||||
|
|
||||||
|
mesh.object->Render(m_shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_view = glm::lookAt(
|
|
||||||
m_camera.transform.pos,
|
|
||||||
m_camera.transform.pos + m_camera.transform.rot,
|
|
||||||
glm::vec3(0.f, 1.f, 0.f)
|
|
||||||
);
|
|
||||||
|
|
||||||
m_model = glm::mat4(1.0f);
|
|
||||||
|
|
||||||
// Apply translation
|
|
||||||
m_model = glm::translate(m_model, entity.transform.pos);
|
|
||||||
|
|
||||||
// Apply rotations (order matters!)
|
|
||||||
m_model = glm::rotate(m_model, entity.transform.rot.x, glm::vec3(1, 0, 0)); // pitch
|
|
||||||
m_model = glm::rotate(m_model, entity.transform.rot.y, glm::vec3(0, 1, 0)); // yaw
|
|
||||||
m_model = glm::rotate(m_model, entity.transform.rot.z, glm::vec3(0, 0, 1)); // roll
|
|
||||||
|
|
||||||
m_shader.setMat4("u_model", m_model);
|
|
||||||
|
|
||||||
entity.mesh.object->Render(m_shader);
|
|
||||||
}
|
}
|
@ -393,29 +393,97 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// void Object::Render(Shader& shader)
|
||||||
|
// {
|
||||||
|
// for (auto &mesh : m_meshes) {
|
||||||
|
// auto material = GetMaterial(mesh.materialName);
|
||||||
|
|
||||||
|
// shader.setFloat("ambientStrength", 0.2f);
|
||||||
|
// shader.setFloat("shininess", material->GetSpecularWeight());
|
||||||
|
// shader.setFloat("opacity", material->GetOpacity());
|
||||||
|
// shader.setBool("useSpecular", material->GetIllumination() >= 2);
|
||||||
|
// shader.setFloat("specularStrength", 1.0f);
|
||||||
|
// shader.setVec3("ambientColor", material->GetAmbientColor());
|
||||||
|
// shader.setVec3("diffuseColor", material->GetDiffuseColor());
|
||||||
|
// shader.setVec3("specularColor", material->GetSpecularColor());
|
||||||
|
|
||||||
|
// if (material->HasDiffuseTexture()) {
|
||||||
|
// shader.setBool("useTexture", true);
|
||||||
|
// glActiveTexture(GL_TEXTURE0);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||||
|
// shader.setInt("diffuseTex", 0);
|
||||||
|
// } else {
|
||||||
|
// shader.setBool("useTexture", false);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// mesh.Render();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
void Object::Render(Shader& shader)
|
void Object::Render(Shader& shader)
|
||||||
{
|
{
|
||||||
for (auto &mesh : m_meshes) {
|
for (auto &mesh : m_meshes)
|
||||||
|
{
|
||||||
auto material = GetMaterial(mesh.materialName);
|
auto material = GetMaterial(mesh.materialName);
|
||||||
|
|
||||||
shader.setFloat("ambientStrength", 0.2f);
|
// --- Basic material properties ---
|
||||||
shader.setFloat("shininess", material->GetSpecularWeight());
|
|
||||||
shader.setFloat("opacity", material->GetOpacity());
|
shader.setFloat("opacity", material->GetOpacity());
|
||||||
shader.setBool("useSpecular", material->GetIllumination() >= 2);
|
|
||||||
shader.setFloat("specularStrength", 1.0f);
|
|
||||||
shader.setVec3("ambientColor", material->GetAmbientColor());
|
|
||||||
shader.setVec3("diffuseColor", material->GetDiffuseColor());
|
|
||||||
shader.setVec3("specularColor", material->GetSpecularColor());
|
|
||||||
|
|
||||||
|
// Albedo (base color)
|
||||||
|
shader.setVec3("albedo", material->GetDiffuseColor());
|
||||||
|
|
||||||
|
// Metallic and roughness (defaults)
|
||||||
|
shader.setFloat("metallic", 0.8f);
|
||||||
|
shader.setFloat("roughness", 0.5f);
|
||||||
|
shader.setFloat("ao", 1.0f); // default ambient occlusion if none
|
||||||
|
|
||||||
|
// --- Optional textures ---
|
||||||
|
int texUnit = 0;
|
||||||
|
|
||||||
|
// Albedo texture
|
||||||
if (material->HasDiffuseTexture()) {
|
if (material->HasDiffuseTexture()) {
|
||||||
shader.setBool("useTexture", true);
|
shader.setBool("useAlbedoMap", true);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||||
shader.setInt("diffuseTex", 0);
|
shader.setInt("albedoTex", texUnit++);
|
||||||
} else {
|
} else {
|
||||||
shader.setBool("useTexture", false);
|
shader.setBool("useAlbedoMap", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Metallic texture
|
||||||
|
// if (material->HasMetallicTexture()) {
|
||||||
|
if (false) {
|
||||||
|
shader.setBool("useMetallicMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetMetallicTexture()->GetID());
|
||||||
|
shader.setInt("metallicTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
shader.setBool("useMetallicMap", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Roughness texture
|
||||||
|
// if (material->HasRoughnessTexture()) {
|
||||||
|
if (false) {
|
||||||
|
shader.setBool("useRoughnessMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetRoughnessTexture()->GetID());
|
||||||
|
shader.setInt("roughnessTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
shader.setBool("useRoughnessMap", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// AO texture
|
||||||
|
// if (material->HasAoTexture()) {
|
||||||
|
if (false) {
|
||||||
|
shader.setBool("useAoMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetAoTexture()->GetID());
|
||||||
|
shader.setInt("aoTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
shader.setBool("useAoMap", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Render mesh ---
|
||||||
mesh.Render();
|
mesh.Render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,42 @@
|
|||||||
#version 410 core
|
#version 410 core
|
||||||
|
|
||||||
// Output color
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec3 vertexPos;
|
in vec3 vertexPos;
|
||||||
in vec3 vertexNormal;
|
in vec3 vertexNormal;
|
||||||
in vec2 TexCoords;
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
// Lighting inputs
|
||||||
uniform vec3 lightPos;
|
uniform vec3 lightPos;
|
||||||
uniform vec3 viewPos;
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
// From Object Renderer
|
// Material parameters
|
||||||
uniform vec3 ambientColor;
|
uniform vec3 albedo; // Base color (replaces diffuseColor)
|
||||||
uniform vec3 diffuseColor;
|
uniform float metallic; // 0 = dielectric, 1 = metal
|
||||||
uniform vec3 specularColor; // used as F0 (base reflectance)
|
uniform float roughness; // 0 = smooth mirror, 1 = rough
|
||||||
|
uniform float ao; // Ambient occlusion
|
||||||
|
|
||||||
uniform float ambientStrength;
|
// Textures
|
||||||
uniform float specularStrength;
|
uniform sampler2D albedoTex;
|
||||||
uniform float shininess; // mapped to roughness
|
uniform sampler2D metallicTex;
|
||||||
uniform bool useSpecular;
|
uniform sampler2D roughnessTex;
|
||||||
|
uniform sampler2D aoTex;
|
||||||
|
uniform bool useAlbedoMap;
|
||||||
|
uniform bool useMetallicMap;
|
||||||
|
uniform bool useRoughnessMap;
|
||||||
|
uniform bool useAoMap;
|
||||||
|
|
||||||
uniform float opacity;
|
uniform float opacity;
|
||||||
|
|
||||||
uniform sampler2D diffuseTex;
|
// Used for emissive light sources
|
||||||
uniform bool useTexture;
|
uniform bool isLight;
|
||||||
|
|
||||||
|
#define PI 3.14159265359
|
||||||
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Helper functions for Cook-Torrance BRDF
|
// Helper functions
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// Normal Distribution Function (GGX/Trowbridge-Reitz)
|
|
||||||
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
||||||
{
|
{
|
||||||
float a = roughness * roughness;
|
float a = roughness * roughness;
|
||||||
@ -41,16 +46,15 @@ float DistributionGGX(vec3 N, vec3 H, float roughness)
|
|||||||
|
|
||||||
float num = a2;
|
float num = a2;
|
||||||
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||||
denom = 3.14159265 * denom * denom;
|
denom = PI * denom * denom;
|
||||||
|
|
||||||
return num / denom;
|
return num / denom;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Geometry function (Schlick-GGX)
|
|
||||||
float GeometrySchlickGGX(float NdotV, float roughness)
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
||||||
{
|
{
|
||||||
float r = (roughness + 1.0);
|
float r = (roughness + 1.0);
|
||||||
float k = (r * r) / 8.0; // remapped for direct lighting
|
float k = (r * r) / 8.0;
|
||||||
|
|
||||||
float num = NdotV;
|
float num = NdotV;
|
||||||
float denom = NdotV * (1.0 - k) + k;
|
float denom = NdotV * (1.0 - k) + k;
|
||||||
@ -58,69 +62,77 @@ float GeometrySchlickGGX(float NdotV, float roughness)
|
|||||||
return num / denom;
|
return num / denom;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Smith's geometry function
|
|
||||||
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
||||||
{
|
{
|
||||||
float NdotV = max(dot(N, V), 0.0);
|
float NdotV = max(dot(N, V), 0.0);
|
||||||
float NdotL = max(dot(N, L), 0.0);
|
float NdotL = max(dot(N, L), 0.0);
|
||||||
float ggx1 = GeometrySchlickGGX(NdotV, roughness);
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||||
float ggx2 = GeometrySchlickGGX(NdotL, roughness);
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||||
|
|
||||||
return ggx1 * ggx2;
|
return ggx1 * ggx2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fresnel term (Schlick's approximation)
|
vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
||||||
vec3 FresnelSchlick(float cosTheta, vec3 F0)
|
|
||||||
{
|
{
|
||||||
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Main
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
if (isLight) {
|
||||||
|
vec3 emissive = LIGHT_COLOR * 10.0; // bright light source
|
||||||
|
FragColor = vec4(emissive, 1.0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inputs
|
||||||
vec3 N = normalize(vertexNormal);
|
vec3 N = normalize(vertexNormal);
|
||||||
vec3 V = normalize(viewPos - vertexPos);
|
vec3 V = normalize(viewPos - vertexPos);
|
||||||
vec3 L = normalize(lightPos - vertexPos);
|
vec3 L = normalize(lightPos - vertexPos);
|
||||||
vec3 H = normalize(V + L);
|
vec3 H = normalize(V + L);
|
||||||
|
|
||||||
// Texture or uniform color
|
// Base color (albedo)
|
||||||
vec3 albedo = (useTexture)
|
vec3 baseColor = useAlbedoMap ? texture(albedoTex, TexCoords).rgb : albedo;
|
||||||
? texture(diffuseTex, TexCoords).rgb
|
|
||||||
: diffuseColor;
|
|
||||||
|
|
||||||
// Map shininess to roughness (inverse relationship)
|
float metal = useMetallicMap ? texture(metallicTex, TexCoords).r : metallic;
|
||||||
float roughness = clamp(1.0 - (shininess / 256.0), 0.05, 1.0);
|
float rough = useRoughnessMap ? texture(roughnessTex, TexCoords).r : roughness;
|
||||||
|
float aoValue = useAoMap ? texture(aoTex, TexCoords).r : ao;
|
||||||
|
|
||||||
// Base reflectivity (F0)
|
// Reflectance at normal incidence (F0)
|
||||||
vec3 F0 = mix(vec3(0.04), specularColor, specularStrength);
|
vec3 F0 = vec3(0.04); // typical dielectric reflectance
|
||||||
|
F0 = mix(F0, baseColor, metal); // metals use albedo as F0
|
||||||
|
|
||||||
// Cook-Torrance BRDF
|
// Cook-Torrance BRDF
|
||||||
float NDF = DistributionGGX(N, H, roughness);
|
float NDF = DistributionGGX(N, H, rough);
|
||||||
float G = GeometrySmith(N, V, L, roughness);
|
float G = GeometrySmith(N, V, L, rough);
|
||||||
vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
|
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
||||||
|
|
||||||
vec3 numerator = NDF * G * F;
|
vec3 numerator = NDF * G * F;
|
||||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
|
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
|
||||||
vec3 specular = numerator / denominator;
|
vec3 specular = numerator / denominator;
|
||||||
|
|
||||||
// Energy conservation
|
// kS is specular reflection, kD is diffuse reflection (energy conservation)
|
||||||
vec3 kS = F;
|
vec3 kS = F;
|
||||||
vec3 kD = vec3(1.0) - kS;
|
vec3 kD = vec3(1.0) - kS;
|
||||||
kD *= (useSpecular ? 1.0 : 1.0); // keep diffuse always unless specular is off
|
kD *= 1.0 - metal;
|
||||||
|
|
||||||
float NdotL = max(dot(N, L), 0.0);
|
float NdotL = max(dot(N, L), 0.0);
|
||||||
|
|
||||||
vec3 diffuse = kD * albedo / 3.14159265;
|
vec3 radiance = LIGHT_COLOR; // single light source color/intensity
|
||||||
vec3 radiance = LIGHT_COLOR;
|
|
||||||
|
|
||||||
vec3 Lo = (diffuse + specular) * radiance * NdotL;
|
vec3 Lo = (kD * baseColor / PI + specular) * radiance * NdotL;
|
||||||
|
|
||||||
// Ambient (simple, not IBL)
|
// Ambient (IBL approximation using ao)
|
||||||
vec3 ambient = ambientStrength * ambientColor * albedo;
|
vec3 ambient = vec3(0.03) * baseColor * aoValue;
|
||||||
|
|
||||||
vec3 result = ambient + Lo;
|
vec3 color = ambient + Lo;
|
||||||
|
|
||||||
// Gamma correction
|
// HDR tonemapping and gamma correction
|
||||||
result = pow(result, vec3(1.0/2.2));
|
color = color / (color + vec3(1.0));
|
||||||
|
color = pow(color, vec3(1.0 / 2.2));
|
||||||
|
|
||||||
FragColor = vec4(result, opacity);
|
FragColor = vec4(color, opacity);
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,8 @@ uniform float opacity;
|
|||||||
uniform sampler2D diffuseTex;
|
uniform sampler2D diffuseTex;
|
||||||
uniform bool useTexture;
|
uniform bool useTexture;
|
||||||
|
|
||||||
|
uniform bool isLight;
|
||||||
|
|
||||||
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
@ -57,5 +59,11 @@ void main()
|
|||||||
|
|
||||||
vec3 result = (ambient + diffuse + specular) * texColor;
|
vec3 result = (ambient + diffuse + specular) * texColor;
|
||||||
|
|
||||||
|
if (isLight) {
|
||||||
|
vec3 emissive = LIGHT_COLOR * 10.0; // big intensity
|
||||||
|
FragColor = vec4(emissive, 1.0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
FragColor = vec4(result, opacity);
|
FragColor = vec4(result, opacity);
|
||||||
}
|
}
|
Reference in New Issue
Block a user