Compare commits
34 Commits
4e86d92987
...
165073c36d
Author | SHA1 | Date | |
---|---|---|---|
165073c36d | |||
87168d42c3 | |||
4deb22f37d | |||
ec92a3310e | |||
b991a85b6b | |||
282f8e8cb2 | |||
4cc6f0cb26 | |||
a7a4840dd4 | |||
2144b8a03a | |||
8563b424e9 | |||
4326ecd23f | |||
a68b4a85f0 | |||
19988d9c1d | |||
fde96d1419 | |||
6972ca3cb5 | |||
4757ba8e58 | |||
5fa9a04cb2 | |||
e38bb50245 | |||
75e1748302 | |||
0c589e4d27 | |||
0165afab95 | |||
3fce829eca | |||
0cf21248f6 | |||
94afd17d65 | |||
fdbf1296de | |||
8cb94e8c95 | |||
884696feaa | |||
ff9e23255c | |||
bedd6c3ca0 | |||
f56e524d05 | |||
b989d74fca | |||
42d5def07e | |||
fefe273fce | |||
99f5cd3715 |
13
.vscode/settings.json
vendored
13
.vscode/settings.json
vendored
@ -58,6 +58,17 @@
|
|||||||
"typeinfo": "cpp",
|
"typeinfo": "cpp",
|
||||||
"variant": "cpp",
|
"variant": "cpp",
|
||||||
"codecvt": "cpp",
|
"codecvt": "cpp",
|
||||||
"typeindex": "cpp"
|
"typeindex": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"unordered_set": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"regex": "cpp",
|
||||||
|
"semaphore": "cpp",
|
||||||
|
"shared_mutex": "cpp",
|
||||||
|
"stop_token": "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)
|
||||||
@ -24,6 +31,7 @@ elseif (MSVC) # vcpkg
|
|||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(GLEW CONFIG REQUIRED)
|
find_package(GLEW CONFIG REQUIRED)
|
||||||
find_package(glm CONFIG REQUIRED)
|
find_package(glm CONFIG REQUIRED)
|
||||||
|
find_package(EnTT CONFIG REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(CodingGame
|
add_executable(CodingGame
|
||||||
@ -32,8 +40,9 @@ add_executable(CodingGame
|
|||||||
|
|
||||||
src/window/window.cpp
|
src/window/window.cpp
|
||||||
|
|
||||||
|
src/components/batch.cpp
|
||||||
|
|
||||||
src/renderer/debug.cpp
|
src/renderer/debug.cpp
|
||||||
src/renderer/basics.cpp
|
|
||||||
src/renderer/mesh.cpp
|
src/renderer/mesh.cpp
|
||||||
src/renderer/shader.cpp
|
src/renderer/shader.cpp
|
||||||
src/renderer/texture.cpp
|
src/renderer/texture.cpp
|
||||||
@ -58,6 +67,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) ----------
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Blender 4.3.2 MTL File: 'untitled.blend'
|
# Blender 4.3.2 MTL File: 'None'
|
||||||
# www.blender.org
|
# www.blender.org
|
||||||
|
|
||||||
newmtl Grass_Bottom
|
newmtl Grass_Bottom
|
||||||
|
@ -2,25 +2,25 @@
|
|||||||
# www.blender.org
|
# www.blender.org
|
||||||
mtllib grass_block.mtl
|
mtllib grass_block.mtl
|
||||||
o Cube
|
o Cube
|
||||||
v -1.000000 -1.000000 1.000000
|
v -0.389000 -0.389000 0.389000
|
||||||
v -1.000000 1.000000 1.000000
|
v -0.389000 0.389000 0.389000
|
||||||
v -1.000000 -1.000000 -1.000000
|
v -0.389000 -0.389000 -0.389000
|
||||||
v -1.000000 1.000000 -1.000000
|
v -0.389000 0.389000 -0.389000
|
||||||
v 1.000000 -1.000000 1.000000
|
v 0.389000 -0.389000 0.389000
|
||||||
v 1.000000 1.000000 1.000000
|
v 0.389000 0.389000 0.389000
|
||||||
v 1.000000 -1.000000 -1.000000
|
v 0.389000 -0.389000 -0.389000
|
||||||
v 1.000000 1.000000 -1.000000
|
v 0.389000 0.389000 -0.389000
|
||||||
vn -1.0000 -0.0000 -0.0000
|
vn -1.0000 -0.0000 -0.0000
|
||||||
vn -0.0000 -0.0000 -1.0000
|
vn -0.0000 -0.0000 -1.0000
|
||||||
vn 1.0000 -0.0000 -0.0000
|
vn 1.0000 -0.0000 -0.0000
|
||||||
vn -0.0000 -0.0000 1.0000
|
vn -0.0000 -0.0000 1.0000
|
||||||
vn -0.0000 -1.0000 -0.0000
|
|
||||||
vn -0.0000 1.0000 -0.0000
|
vn -0.0000 1.0000 -0.0000
|
||||||
|
vn -0.0000 -1.0000 -0.0000
|
||||||
vt 0.000000 1.000000
|
vt 0.000000 1.000000
|
||||||
vt 1.000000 0.000000
|
vt 1.000000 0.000000
|
||||||
vt 0.000000 0.000000
|
vt 0.000000 0.000000
|
||||||
vt 1.000000 1.000000
|
vt 1.000000 1.000000
|
||||||
s 0
|
s 1
|
||||||
usemtl Grass_Side
|
usemtl Grass_Side
|
||||||
f 2/1/1 3/2/1 1/3/1
|
f 2/1/1 3/2/1 1/3/1
|
||||||
f 4/1/2 7/2/2 3/3/2
|
f 4/1/2 7/2/2 3/3/2
|
||||||
@ -31,8 +31,8 @@ f 4/1/2 8/4/2 7/2/2
|
|||||||
f 8/4/3 6/1/3 5/3/3
|
f 8/4/3 6/1/3 5/3/3
|
||||||
f 6/4/4 2/1/4 1/3/4
|
f 6/4/4 2/1/4 1/3/4
|
||||||
usemtl Grass_Top
|
usemtl Grass_Top
|
||||||
f 4/1/6 6/2/6 8/4/6
|
f 4/1/5 6/2/5 8/4/5
|
||||||
f 4/1/6 2/3/6 6/2/6
|
f 4/1/5 2/3/5 6/2/5
|
||||||
usemtl Grass_Bottom
|
usemtl Grass_Bottom
|
||||||
f 7/4/5 1/3/5 3/1/5
|
f 7/4/6 1/3/6 3/1/6
|
||||||
f 7/4/5 5/2/5 1/3/5
|
f 7/4/6 5/2/6 1/3/6
|
||||||
|
2
assets/plane.mtl
Normal file
2
assets/plane.mtl
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# Blender 4.3.2 MTL File: 'None'
|
||||||
|
# www.blender.org
|
16
assets/plane.obj
Normal file
16
assets/plane.obj
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Blender 4.3.2
|
||||||
|
# www.blender.org
|
||||||
|
mtllib plane.mtl
|
||||||
|
o Plane
|
||||||
|
v -5.000000 0.000000 5.000000
|
||||||
|
v 5.000000 0.000000 5.000000
|
||||||
|
v -5.000000 0.000000 -5.000000
|
||||||
|
v 5.000000 0.000000 -5.000000
|
||||||
|
vn -0.0000 1.0000 -0.0000
|
||||||
|
vt 1.000000 0.000000
|
||||||
|
vt 0.000000 1.000000
|
||||||
|
vt 0.000000 0.000000
|
||||||
|
vt 1.000000 1.000000
|
||||||
|
s 0
|
||||||
|
f 2/1/1 3/2/1 1/3/1
|
||||||
|
f 2/1/1 4/4/1 3/2/1
|
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
27
include/components/batch.h
Normal file
27
include/components/batch.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef COMPONENT_BATCH_H_
|
||||||
|
#define COMPONENT_BATCH_H_
|
||||||
|
|
||||||
|
#include "renderer/renderer.h"
|
||||||
|
|
||||||
|
// requires mesh component
|
||||||
|
struct batch {
|
||||||
|
friend class Renderer;
|
||||||
|
public:
|
||||||
|
// requires transform component
|
||||||
|
struct item {
|
||||||
|
unsigned int batchId;
|
||||||
|
};
|
||||||
|
|
||||||
|
batch();
|
||||||
|
|
||||||
|
inline const unsigned int id() const { return m_id; }
|
||||||
|
protected:
|
||||||
|
static unsigned int LastID;
|
||||||
|
private:
|
||||||
|
unsigned int m_id;
|
||||||
|
unsigned int m_instance_vbo { 0 };
|
||||||
|
private:
|
||||||
|
void prepare(glm::mat4 *instances, unsigned int count);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMPONENT_BATCH_H_
|
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_
|
26
include/components/light.h
Normal file
26
include/components/light.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef COMPONENTS_LIGHT_H_
|
||||||
|
#define COMPONENTS_LIGHT_H_
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include "renderer/renderer.h"
|
||||||
|
|
||||||
|
struct light {
|
||||||
|
friend class Renderer;
|
||||||
|
public:
|
||||||
|
enum LightType {
|
||||||
|
DIRECTIONAL = 0,
|
||||||
|
};
|
||||||
|
LightType type;
|
||||||
|
glm::vec3 color;
|
||||||
|
float intensity;
|
||||||
|
|
||||||
|
light(LightType t, const glm::vec3& c, float i)
|
||||||
|
: type(t), color(c), intensity(i),
|
||||||
|
shadowMap(0), fbo(0), lightSpace(1.0f) {}
|
||||||
|
private:
|
||||||
|
unsigned int shadowMap;
|
||||||
|
unsigned int fbo;
|
||||||
|
glm::mat4 lightSpace;
|
||||||
|
};
|
||||||
|
|
||||||
|
#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::shared_ptr<Object> object;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMPONENTS_MESH_H_
|
6
include/components/rotate.h
Normal file
6
include/components/rotate.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef COMPONENT_ROTATE_H_
|
||||||
|
#define COMPONENT_ROTATE_H_
|
||||||
|
|
||||||
|
struct rotate {};
|
||||||
|
|
||||||
|
#endif // COMPONENT_ROTATE_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_
|
|
@ -3,7 +3,10 @@
|
|||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "renderer/mesh.h"
|
||||||
|
|
||||||
class Vertex {
|
class Vertex {
|
||||||
|
friend class Mesh;
|
||||||
private:
|
private:
|
||||||
glm::vec3 m_position;
|
glm::vec3 m_position;
|
||||||
glm::vec3 m_normal;
|
glm::vec3 m_normal;
|
||||||
|
@ -21,7 +21,7 @@ public:
|
|||||||
public:
|
public:
|
||||||
Mesh();
|
Mesh();
|
||||||
public:
|
public:
|
||||||
void Render();
|
void Render(unsigned int count);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MESH_H_
|
#endif // MESH_H_
|
@ -2,27 +2,36 @@
|
|||||||
#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(entt::registry& registry);
|
||||||
|
|
||||||
void RenderLight();
|
void Render();
|
||||||
void RenderEntity(const ecs::Entity& entity);
|
void Init();
|
||||||
|
void GenerateShadowMaps();
|
||||||
|
|
||||||
void OnWindowResized(int w, int h);
|
void OnWindowResized(int w, int h);
|
||||||
|
private:
|
||||||
|
void ApplyLights(Shader &shader);
|
||||||
|
void UpdateView();
|
||||||
|
void RenderScene(Shader &shader);
|
||||||
private:
|
private:
|
||||||
Shader m_shader;
|
Shader m_shader;
|
||||||
|
Shader m_depthShader;
|
||||||
|
|
||||||
|
entt::registry& m_registry;
|
||||||
|
|
||||||
|
// unsigned int m_depth_fbo;
|
||||||
|
// unsigned int m_depthMap;
|
||||||
|
|
||||||
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_
|
@ -8,6 +8,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
#include "renderer/renderer.h"
|
||||||
#include "renderer/material.h"
|
#include "renderer/material.h"
|
||||||
#include "renderer/mesh.h"
|
#include "renderer/mesh.h"
|
||||||
|
|
||||||
@ -15,6 +16,7 @@ enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
|||||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||||
|
|
||||||
class Object {
|
class Object {
|
||||||
|
friend class Renderer;
|
||||||
private:
|
private:
|
||||||
static inline int NormalizeIndex(int idx, int baseCount);
|
static inline int NormalizeIndex(int idx, int baseCount);
|
||||||
|
|
||||||
@ -34,7 +36,10 @@ private:
|
|||||||
Mesh& GetLastMesh();
|
Mesh& GetLastMesh();
|
||||||
void CreateNewMesh(const std::string& materialName);
|
void CreateNewMesh(const std::string& materialName);
|
||||||
public:
|
public:
|
||||||
void Render(Shader& shader);
|
void Render(Shader& shader, unsigned int count);
|
||||||
|
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||||
|
protected:
|
||||||
|
void EnableBatch(unsigned int instanceVBO);
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::vector<glm::vec3> m_vertices;
|
std::vector<glm::vec3> m_vertices;
|
||||||
|
19
src/components/batch.cpp
Normal file
19
src/components/batch.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "components/batch.h"
|
||||||
|
|
||||||
|
unsigned int batch::LastID = 0;
|
||||||
|
|
||||||
|
batch::batch() {
|
||||||
|
m_id = ++LastID;
|
||||||
|
}
|
||||||
|
|
||||||
|
void batch::prepare(glm::mat4 *instances, unsigned int count) {
|
||||||
|
if (m_instance_vbo == 0) {
|
||||||
|
glGenBuffers(1, &m_instance_vbo);
|
||||||
|
}
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * count, reinterpret_cast<void*>(instances), GL_DYNAMIC_DRAW);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
}
|
126
src/main.cpp
126
src/main.cpp
@ -16,26 +16,62 @@
|
|||||||
#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"
|
||||||
|
#include "components/rotate.h"
|
||||||
|
#include "components/batch.h"
|
||||||
|
|
||||||
class Game : public IApplication {
|
class Game : public IApplication {
|
||||||
public:
|
public:
|
||||||
Game()
|
Game() : m_renderer(m_registry) {
|
||||||
: 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 lght = m_registry.create();
|
||||||
m_target(ecs::Entity(ecs::Mesh(Object::LoadFile("./assets/monkey.obj")))),
|
m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||||
m_renderer(m_light, m_camera) {
|
m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||||
// Object* lightObj = Object::LoadFile("./assets/cube.obj");
|
m_registry.emplace<mesh>(lght, std::shared_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, 2.f, 2.f));
|
||||||
// m_camera = ecs::Entity(cameraMesh);
|
m_registry.emplace<camera>(cameraEntity);
|
||||||
m_camera.transform.pos = glm::vec3(0.f, 0.f, 2.f);
|
|
||||||
|
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
|
||||||
|
const auto targetEntity = m_registry.create();
|
||||||
|
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||||
|
m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
||||||
|
|
||||||
|
Object* grass = Object::LoadFile("./assets/grass_block/grass_block.obj");
|
||||||
|
const auto cubeEntity = m_registry.create();
|
||||||
|
m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
||||||
|
m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||||
|
|
||||||
|
// Cube template (use shared object to avoid reloading 1000 times)
|
||||||
|
std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
|
||||||
|
const auto batchEntt = m_registry.create();
|
||||||
|
m_registry.emplace<batch>(batchEntt);
|
||||||
|
m_registry.emplace<mesh>(batchEntt, cubeObj);
|
||||||
|
auto cubeBatch = m_registry.get<batch>(batchEntt);
|
||||||
|
// Generate 1000 random cubes
|
||||||
|
for (int i = 0; i < 1000; ++i) {
|
||||||
|
const auto cubeEntity = m_registry.create();
|
||||||
|
|
||||||
|
float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
||||||
|
float y = static_cast<float>(rand()) / RAND_MAX * 10.f; // range [0, 10]
|
||||||
|
float z = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
||||||
|
|
||||||
|
m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z));
|
||||||
|
m_registry.emplace<rotate>(cubeEntity);
|
||||||
|
m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
Object* floorObj = Object::LoadFile("./assets/plane.obj");
|
||||||
|
const auto floorEntt = m_registry.create();
|
||||||
|
m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||||
|
m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
||||||
}
|
}
|
||||||
~Game() override {}
|
~Game() override {}
|
||||||
|
|
||||||
@ -53,6 +89,9 @@ public:
|
|||||||
// FPS tracking
|
// FPS tracking
|
||||||
m_startTicks = SDL_GetTicks();
|
m_startTicks = SDL_GetTicks();
|
||||||
m_frameCount = 0;
|
m_frameCount = 0;
|
||||||
|
|
||||||
|
m_renderer.Init();
|
||||||
|
m_renderer.GenerateShadowMaps();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnWindowResized(const WindowResized& event) override {
|
void OnWindowResized(const WindowResized& event) override {
|
||||||
@ -100,8 +139,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 +153,51 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_target.transform.rot.y = m_angle;
|
// ---- Day-night simulation ----
|
||||||
|
m_dayTime += deltaTime;
|
||||||
|
if (m_dayTime > m_dayLength)
|
||||||
|
m_dayTime -= m_dayLength; // loop every "day"
|
||||||
|
|
||||||
|
float dayProgress = m_dayTime / m_dayLength; // 0.0 -> 1.0
|
||||||
|
float sunAngle = dayProgress * glm::two_pi<float>(); // radians through the sky
|
||||||
|
|
||||||
|
// Compute sun direction (rotating around X axis)
|
||||||
|
// At t=0.0 sun at east horizon, at π/2 overhead, at π west horizon
|
||||||
|
glm::vec3 sunDir = glm::normalize(glm::vec3(0.0f, sin(sunAngle), cos(sunAngle)));
|
||||||
|
|
||||||
|
// Compute intensity: bright at noon, dim at dusk/dawn, dark at night
|
||||||
|
float intensity = glm::max(sin(sunAngle), (double)0.0f); // 0 at night, 1 at noon
|
||||||
|
intensity = glm::mix(0.05f, 1.5f, intensity); // keep some ambient even at night
|
||||||
|
|
||||||
|
// Optional: tint color (warm at sunrise/sunset)
|
||||||
|
glm::vec3 dayColor = glm::vec3(1.0f, 0.95f, 0.9f);
|
||||||
|
glm::vec3 sunsetColor= glm::vec3(1.0f, 0.6f, 0.3f);
|
||||||
|
float sunsetFactor = glm::clamp(1.0f - abs(sin(sunAngle)) * 2.0f, 0.0f, 1.0f);
|
||||||
|
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
|
||||||
|
|
||||||
|
// Update the directional light in the registry
|
||||||
|
auto lightsView = m_registry.view<light, transform>();
|
||||||
|
for (auto [entity, l, t] : lightsView.each()) {
|
||||||
|
if (l.type == light::LightType::DIRECTIONAL) {
|
||||||
|
// "position" for directional light often stores direction vector
|
||||||
|
// If your system instead uses transform.rotation, adjust accordingly
|
||||||
|
t.position = sunDir * 15.f; // use this as light direction
|
||||||
|
l.color = sunColor;
|
||||||
|
l.intensity = intensity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto rotateEntts = m_registry.view<transform, rotate>();
|
||||||
|
for (auto [entity, t] : rotateEntts.each()) {
|
||||||
|
// auto targetTransform = rotateEntts.get<transform>(entity);
|
||||||
|
if (!m_registry.all_of<light>(entity)) {
|
||||||
|
t.rotation.y = m_angle;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnRender() override {
|
void OnRender() override {
|
||||||
m_renderer.RenderLight();
|
m_renderer.Render();
|
||||||
m_renderer.RenderEntity(m_target);
|
|
||||||
|
|
||||||
m_frameCount++;
|
m_frameCount++;
|
||||||
m_currentTicks = SDL_GetTicks();
|
m_currentTicks = SDL_GetTicks();
|
||||||
@ -131,13 +212,14 @@ 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;
|
||||||
|
|
||||||
|
float m_dayTime = 0.0f; // accumulates time for day-night cycle
|
||||||
|
float m_dayLength = 60.0f; // seconds per full day cycle
|
||||||
|
|
||||||
bool m_paused = false;
|
bool m_paused = false;
|
||||||
|
|
||||||
float m_yaw = -90.0f; // looking along -Z initially
|
float m_yaw = -90.0f; // looking along -Z initially
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
#include "renderer/basics.h"
|
|
||||||
|
|
||||||
void Vertex::DefineAttrib()
|
|
||||||
{
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_position)));
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_normal)));
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_texCoord)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
}
|
|
@ -10,6 +10,43 @@ void MessageCallback(GLenum source,
|
|||||||
const GLchar* message,
|
const GLchar* message,
|
||||||
const void* userParam)
|
const void* userParam)
|
||||||
{
|
{
|
||||||
|
if(id == 131169 || id == 131185 || id == 131218 || id == 131204) return;
|
||||||
|
|
||||||
|
std::cout << "---------------" << std::endl;
|
||||||
|
std::cout << "Debug message (" << id << "): " << message << std::endl;
|
||||||
|
|
||||||
|
switch (source)
|
||||||
|
{
|
||||||
|
case GL_DEBUG_SOURCE_API: std::cout << "Source: API"; break;
|
||||||
|
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: std::cout << "Source: Window System"; break;
|
||||||
|
case GL_DEBUG_SOURCE_SHADER_COMPILER: std::cout << "Source: Shader Compiler"; break;
|
||||||
|
case GL_DEBUG_SOURCE_THIRD_PARTY: std::cout << "Source: Third Party"; break;
|
||||||
|
case GL_DEBUG_SOURCE_APPLICATION: std::cout << "Source: Application"; break;
|
||||||
|
case GL_DEBUG_SOURCE_OTHER: std::cout << "Source: Other"; break;
|
||||||
|
} std::cout << std::endl;
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case GL_DEBUG_TYPE_ERROR: std::cout << "Type: Error"; break;
|
||||||
|
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: std::cout << "Type: Deprecated Behaviour"; break;
|
||||||
|
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: std::cout << "Type: Undefined Behaviour"; break;
|
||||||
|
case GL_DEBUG_TYPE_PORTABILITY: std::cout << "Type: Portability"; break;
|
||||||
|
case GL_DEBUG_TYPE_PERFORMANCE: std::cout << "Type: Performance"; break;
|
||||||
|
case GL_DEBUG_TYPE_MARKER: std::cout << "Type: Marker"; break;
|
||||||
|
case GL_DEBUG_TYPE_PUSH_GROUP: std::cout << "Type: Push Group"; break;
|
||||||
|
case GL_DEBUG_TYPE_POP_GROUP: std::cout << "Type: Pop Group"; break;
|
||||||
|
case GL_DEBUG_TYPE_OTHER: std::cout << "Type: Other"; break;
|
||||||
|
} std::cout << std::endl;
|
||||||
|
|
||||||
|
switch (severity)
|
||||||
|
{
|
||||||
|
case GL_DEBUG_SEVERITY_HIGH: std::cout << "Severity: high"; break;
|
||||||
|
case GL_DEBUG_SEVERITY_MEDIUM: std::cout << "Severity: medium"; break;
|
||||||
|
case GL_DEBUG_SEVERITY_LOW: std::cout << "Severity: low"; break;
|
||||||
|
case GL_DEBUG_SEVERITY_NOTIFICATION: std::cout << "Severity: notification"; break;
|
||||||
|
} std::cout << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
return;
|
||||||
(void) source;
|
(void) source;
|
||||||
(void) id;
|
(void) id;
|
||||||
(void) length;
|
(void) length;
|
||||||
|
@ -28,9 +28,6 @@ void Engine::Run(std::unique_ptr<IApplication> app) {
|
|||||||
s_window->ProcessEvents();
|
s_window->ProcessEvents();
|
||||||
|
|
||||||
s_app->OnUpdate();
|
s_app->OnUpdate();
|
||||||
|
|
||||||
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
s_app->OnRender();
|
s_app->OnRender();
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#include <cstddef>
|
||||||
|
|
||||||
#include "renderer/mesh.h"
|
#include "renderer/mesh.h"
|
||||||
|
|
||||||
Mesh::Mesh() {
|
Mesh::Mesh() {
|
||||||
@ -13,13 +15,21 @@ Mesh::Mesh() {
|
|||||||
|
|
||||||
// VBO (vertex buffer)
|
// VBO (vertex buffer)
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
// EBO (index buffer)
|
// EBO (index buffer)
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
Vertex::DefineAttrib();
|
// attributes
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_position)));
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_normal)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_texCoord)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
@ -29,18 +39,22 @@ void Mesh::Upload() const {
|
|||||||
glBindVertexArray(m_vao);
|
glBindVertexArray(m_vao);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
// Upload indices
|
// Upload indices
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::Render()
|
void Mesh::Render(unsigned int count)
|
||||||
{
|
{
|
||||||
Bind();
|
Bind();
|
||||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0);
|
if (count > 1) {
|
||||||
|
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0, count);
|
||||||
|
} else {
|
||||||
|
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0);
|
||||||
|
}
|
||||||
Unbind();
|
Unbind();
|
||||||
}
|
}
|
@ -1,14 +1,25 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/ext/matrix_clip_space.hpp>
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <corecrt_math_defines.h>
|
||||||
|
#endif
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
|
#include <glm/gtx/euler_angles.hpp>
|
||||||
|
|
||||||
#include "renderer/renderer.h"
|
#include "renderer/renderer.h"
|
||||||
#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"
|
||||||
|
#include "components/batch.h"
|
||||||
|
|
||||||
|
Renderer::Renderer(entt::registry& registry) : m_registry(registry)
|
||||||
{
|
{
|
||||||
m_proj = glm::perspective(
|
m_proj = glm::perspective(
|
||||||
static_cast<float>(M_PI_2),
|
static_cast<float>(M_PI_2),
|
||||||
@ -18,13 +29,31 @@ Renderer::Renderer(ecs::Entity& light, ecs::Entity& camera)
|
|||||||
);
|
);
|
||||||
|
|
||||||
m_shader.init(
|
m_shader.init(
|
||||||
FileManager::read("./src/shaders/simple.vs"),
|
FileManager::read("./src/shaders/main.vs"),
|
||||||
FileManager::read("./src/shaders/simple.fs")
|
FileManager::read("./src/shaders/pbr.fs")
|
||||||
|
);
|
||||||
|
|
||||||
|
m_depthShader.init(
|
||||||
|
FileManager::read("./src/shaders/depth.vs"),
|
||||||
|
FileManager::read("./src/shaders/depth.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::Init() {
|
||||||
|
// auto view = m_registry.view<batch, mesh>();
|
||||||
|
// for (auto [_, b, m] : m_registry.view<batch, mesh>().each()) {
|
||||||
|
// unsigned int items = 0;
|
||||||
|
// for (auto [entt, item] : m_registry.view<batch::item>().each()) {
|
||||||
|
// if (item.batchId == b.id()) ++items;
|
||||||
|
// }
|
||||||
|
// b.prepare()
|
||||||
|
// m.object->EnableBatch(b.m_instance_vbo);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::OnWindowResized(int w, int h) {
|
void Renderer::OnWindowResized(int w, int h) {
|
||||||
@ -36,51 +65,182 @@ void Renderer::OnWindowResized(int w, int h) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::RenderLight() {
|
void Renderer::ApplyLights(Shader &shader) {
|
||||||
|
auto lights = m_registry.view<light>();
|
||||||
|
// TODO: Pass Lights Data to depth shader as well
|
||||||
|
shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
||||||
|
size_t lightIndex = 0;
|
||||||
|
for (auto entity : lights) {
|
||||||
|
auto &l = m_registry.get<light>(entity);
|
||||||
|
auto &transf = m_registry.get<transform>(entity);
|
||||||
|
|
||||||
|
shader.setInt("lights[" + std::to_string(lightIndex) + "].type", static_cast<int>(l.type));
|
||||||
|
shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
||||||
|
shader.setVec3("lights[" + std::to_string(lightIndex) + "].color", l.color);
|
||||||
|
shader.setFloat("lights[" + std::to_string(lightIndex) + "].intensity", l.intensity);
|
||||||
|
shader.setMat4("lights[" + std::to_string(lightIndex) + "].lightSpace", l.lightSpace);
|
||||||
|
shader.setInt("lights[" + std::to_string(lightIndex) + "].shadowMap", 10 + lightIndex);
|
||||||
|
glActiveTexture(GL_TEXTURE10 + lightIndex);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, l.shadowMap);
|
||||||
|
|
||||||
|
++lightIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::UpdateView() {
|
||||||
|
auto cam = m_registry.view<transform, camera>().back();
|
||||||
|
auto camTransform = m_registry.get<transform>(cam);
|
||||||
|
|
||||||
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.setMat4("u_projection", m_proj);
|
||||||
|
|
||||||
m_shader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
m_shader.setVec3("viewPos", camTransform.position);
|
||||||
m_shader.setVec3("lightPos", m_light.transform.pos);
|
|
||||||
m_shader.setVec3("viewPos", m_camera.transform.pos);
|
|
||||||
|
|
||||||
m_model = glm::mat4(1.f);
|
|
||||||
m_model = glm::translate(m_model, m_light.transform.pos);
|
|
||||||
|
|
||||||
m_shader.setMat4("u_model", m_model);
|
|
||||||
|
|
||||||
m_light.mesh.object->Render(m_shader);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::RenderEntity(const ecs::Entity& entity) {
|
void Renderer::RenderScene(Shader &shader) {
|
||||||
if (entity.mesh.object == nullptr) {
|
std::unordered_map<unsigned int, std::vector<entt::entity>> batches;
|
||||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
|
||||||
return;
|
for (auto [entt, item] : m_registry.view<batch::item>().each()) {
|
||||||
|
if (batches.find(item.batchId) == batches.end())
|
||||||
|
batches.insert(std::make_pair(item.batchId, std::vector<entt::entity>()));
|
||||||
|
|
||||||
|
batches[item.batchId].push_back(entt);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_view = glm::lookAt(
|
shader.setBool("u_isInstanced", true);
|
||||||
m_camera.transform.pos,
|
shader.setBool("isLight", false);
|
||||||
m_camera.transform.pos + m_camera.transform.rot,
|
shader.setVec3("currentLightColor", glm::vec3(0.f));
|
||||||
glm::vec3(0.f, 1.f, 0.f)
|
for (auto [entt, b, m] : m_registry.view<batch, mesh>().each()) {
|
||||||
);
|
// check if have items for batch render
|
||||||
|
if (batches.find(b.id()) == batches.end()) continue;
|
||||||
|
|
||||||
m_model = glm::mat4(1.0f);
|
auto &batchItems = batches[b.id()];
|
||||||
|
|
||||||
// Apply translation
|
std::vector<glm::mat4> models;
|
||||||
m_model = glm::translate(m_model, entity.transform.pos);
|
models.reserve(batchItems.size());
|
||||||
|
|
||||||
// Apply rotations (order matters!)
|
for (auto item : batchItems) {
|
||||||
m_model = glm::rotate(m_model, entity.transform.rot.x, glm::vec3(1, 0, 0)); // pitch
|
auto &t = m_registry.get<transform>(item);
|
||||||
m_model = glm::rotate(m_model, entity.transform.rot.y, glm::vec3(0, 1, 0)); // yaw
|
glm::mat4 rotation = glm::yawPitchRoll(t.rotation.y, t.rotation.x, t.rotation.z);
|
||||||
m_model = glm::rotate(m_model, entity.transform.rot.z, glm::vec3(0, 0, 1)); // roll
|
auto itemModel = glm::translate(glm::mat4(1.f), t.position) * rotation;
|
||||||
|
models.push_back(itemModel);
|
||||||
|
}
|
||||||
|
|
||||||
m_shader.setMat4("u_model", m_model);
|
auto prevInstanceVBO = b.m_instance_vbo;
|
||||||
|
b.prepare(models.data(), models.size());
|
||||||
|
if (prevInstanceVBO <= 0) {
|
||||||
|
std::cout << "[DEBUG] enabling batch"<<std::endl;
|
||||||
|
m.object->EnableBatch(b.m_instance_vbo);
|
||||||
|
}
|
||||||
|
m.object->Render(shader, batchItems.size());
|
||||||
|
}
|
||||||
|
shader.setBool("u_isInstanced", false);
|
||||||
|
|
||||||
entity.mesh.object->Render(m_shader);
|
for (auto [entity, transf, mesh] : m_registry.view<transform, mesh>(entt::exclude<batch, batch::item>).each()) {
|
||||||
|
if (mesh.object == nullptr) {
|
||||||
|
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_registry.all_of<light>(entity)) {
|
||||||
|
auto &l = m_registry.get<light>(entity);
|
||||||
|
shader.setBool("isLight", true);
|
||||||
|
shader.setVec3("currentLightColor", l.color);
|
||||||
|
} else {
|
||||||
|
shader.setBool("isLight", false);
|
||||||
|
shader.setVec3("currentLightColor", glm::vec3(0.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 rotation = glm::yawPitchRoll(transf.rotation.y, transf.rotation.x, transf.rotation.z);
|
||||||
|
m_model = glm::translate(glm::mat4(1.f), transf.position) * rotation;
|
||||||
|
|
||||||
|
shader.setMat4("u_model", m_model);
|
||||||
|
|
||||||
|
mesh.object->Render(shader, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::GenerateShadowMaps() {
|
||||||
|
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||||
|
|
||||||
|
m_depthShader.use();
|
||||||
|
|
||||||
|
auto lights = m_registry.view<light>();
|
||||||
|
|
||||||
|
for (auto [lEntt, l] : lights.each()) {
|
||||||
|
// TODO: support other light types when ready
|
||||||
|
if (l.type != light::LightType::DIRECTIONAL) return;
|
||||||
|
|
||||||
|
glGenFramebuffers(1, &l.fbo);
|
||||||
|
glGenTextures(1, &l.shadowMap);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, l.shadowMap);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,
|
||||||
|
SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
||||||
|
|
||||||
|
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||||
|
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, l.fbo);
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, l.shadowMap, 0);
|
||||||
|
glDrawBuffer(GL_NONE);
|
||||||
|
glReadBuffer(GL_NONE);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::Render() {
|
||||||
|
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||||
|
|
||||||
|
m_depthShader.use();
|
||||||
|
|
||||||
|
auto lights = m_registry.view<light, transform>();
|
||||||
|
|
||||||
|
for (auto [lEntt, l, t] : lights.each()) {
|
||||||
|
// TODO: support other light types when ready
|
||||||
|
if (l.type != light::LightType::DIRECTIONAL) return;
|
||||||
|
|
||||||
|
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
float near_plane = 0.1f, far_plane = 50.0f;
|
||||||
|
glm::vec3 target = glm::vec3(0.0f, 0.5f, 0.0f);
|
||||||
|
glm::mat4 lightView = glm::lookAt(t.position, target, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
glm::mat4 lightProjection = glm::ortho(-6.0f, 6.0f, -6.0f, 6.0f, 1.0f, 20.0f);
|
||||||
|
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
|
||||||
|
|
||||||
|
m_depthShader.setMat4("u_lightSpace", lightSpaceMatrix);
|
||||||
|
l.lightSpace = lightSpaceMatrix;
|
||||||
|
|
||||||
|
glCullFace(GL_FRONT);
|
||||||
|
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, l.fbo);
|
||||||
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
|
RenderScene(m_depthShader);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
glCullFace(GL_BACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
// actual rendering
|
||||||
|
|
||||||
|
glViewport(0, 0, Window::GetWidth(), Window::GetHeight());
|
||||||
|
|
||||||
|
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
m_shader.use();
|
||||||
|
|
||||||
|
ApplyLights(m_shader);
|
||||||
|
UpdateView();
|
||||||
|
|
||||||
|
RenderScene(m_shader);
|
||||||
}
|
}
|
@ -392,31 +392,117 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Object::EnableBatch(unsigned int instanceVBO) {
|
||||||
void Object::Render(Shader& shader)
|
|
||||||
{
|
|
||||||
for (auto &mesh : m_meshes) {
|
for (auto &mesh : m_meshes) {
|
||||||
auto material = GetMaterial(mesh.materialName);
|
mesh.Bind();
|
||||||
|
|
||||||
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()) {
|
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
|
||||||
shader.setBool("useTexture", true);
|
std::size_t vec4Size = sizeof(glm::vec4);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
for (int i = 0; i < 4; ++i) {
|
||||||
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
glEnableVertexAttribArray(3 + i); // use locations 3,4,5,6 for instance matrix
|
||||||
shader.setInt("diffuseTex", 0);
|
glVertexAttribPointer(3 + i, 4, GL_FLOAT, GL_FALSE,
|
||||||
} else {
|
sizeof(glm::mat4), (void*)(i * vec4Size));
|
||||||
shader.setBool("useTexture", false);
|
glVertexAttribDivisor(3 + i, 1); // IMPORTANT: one per instance, not per vertex
|
||||||
}
|
}
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
mesh.Render();
|
|
||||||
|
mesh.Unbind();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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, unsigned int count)
|
||||||
|
{
|
||||||
|
for (auto &mesh : m_meshes)
|
||||||
|
{
|
||||||
|
auto material = GetMaterial(mesh.materialName);
|
||||||
|
|
||||||
|
// --- Basic material properties ---
|
||||||
|
shader.setFloat("opacity", material->GetOpacity());
|
||||||
|
|
||||||
|
// 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()) {
|
||||||
|
shader.setBool("useAlbedoMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||||
|
shader.setInt("albedoTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
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(count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
src/shaders/depth.fs
Normal file
6
src/shaders/depth.fs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#version 410 core
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
// gl_FragDepth = gl_FragCoord.z;
|
||||||
|
}
|
13
src/shaders/depth.vs
Normal file
13
src/shaders/depth.vs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#version 410 core
|
||||||
|
|
||||||
|
// Input vertex attributes
|
||||||
|
layout (location = 0) in vec3 position; // Vertex position in local space (model space)
|
||||||
|
|
||||||
|
// Uniforms for transformation matrices
|
||||||
|
uniform mat4 u_model; // Model matrix: transforms from local space to world space
|
||||||
|
uniform mat4 u_lightSpace;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = u_lightSpace * u_model * vec4(position, 1.0);
|
||||||
|
}
|
@ -4,26 +4,33 @@
|
|||||||
layout (location = 0) in vec3 position; // Vertex position in local space (model space)
|
layout (location = 0) in vec3 position; // Vertex position in local space (model space)
|
||||||
layout (location = 1) in vec3 normal; // vertex normal
|
layout (location = 1) in vec3 normal; // vertex normal
|
||||||
layout (location = 2) in vec2 texCoord; // Vertex texture uv
|
layout (location = 2) in vec2 texCoord; // Vertex texture uv
|
||||||
|
layout (location = 3) in mat4 instanceModel; // Vertex texture uv
|
||||||
|
|
||||||
// Output to fragment shader
|
// Output to fragment shader
|
||||||
out vec3 vertexPos;
|
out vec3 vertexPos;
|
||||||
out vec3 vertexNormal;
|
out vec3 vertexNormal;
|
||||||
out vec2 TexCoords;
|
out vec2 TexCoords;
|
||||||
|
out vec4 fragPosLightSpace;
|
||||||
|
|
||||||
// Uniforms for transformation matrices
|
// Uniforms for transformation matrices
|
||||||
uniform mat4 u_model; // Model matrix: transforms from local space to world space
|
uniform mat4 u_model; // Model matrix: transforms from local space to world space
|
||||||
uniform mat4 u_view; // View matrix: transforms from world space to camera space (view space)
|
uniform mat4 u_view; // View matrix: transforms from world space to camera space (view space)
|
||||||
uniform mat4 u_projection; // Projection matrix: transforms from camera space to clip space
|
uniform mat4 u_projection; // Projection matrix: transforms from camera space to clip space
|
||||||
|
uniform bool u_isInstanced;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vertexPos = vec3(u_model * vec4(position, 1.0));
|
mat4 model = u_isInstanced ? instanceModel : u_model;
|
||||||
|
|
||||||
mat3 normalMatrix = mat3(transpose(inverse(u_model)));
|
vertexPos = vec3(model * vec4(position, 1.0));
|
||||||
|
|
||||||
|
mat3 normalMatrix = mat3(transpose(inverse(model)));
|
||||||
vertexNormal = normalMatrix * normal;
|
vertexNormal = normalMatrix * normal;
|
||||||
// vertexNormal = normal;
|
// vertexNormal = normal;
|
||||||
|
|
||||||
TexCoords = texCoord;
|
TexCoords = texCoord;
|
||||||
|
|
||||||
|
// fragPosLightSpace = u_lightSpace * vec4(vertexPos, 1.0);
|
||||||
|
|
||||||
gl_Position = u_projection * u_view * vec4(vertexPos, 1.0);
|
gl_Position = u_projection * u_view * vec4(vertexPos, 1.0);
|
||||||
}
|
}
|
@ -1,37 +1,90 @@
|
|||||||
#version 410 core
|
#version 410 core
|
||||||
|
|
||||||
// Output color
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec3 vertexPos;
|
in vec3 vertexPos; // must be world-space position
|
||||||
in vec3 vertexNormal;
|
in vec3 vertexNormal;
|
||||||
in vec2 TexCoords;
|
in vec2 TexCoords;
|
||||||
|
|
||||||
uniform vec3 lightPos;
|
|
||||||
uniform vec3 viewPos;
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
// From Object Renderer
|
// Lights
|
||||||
uniform vec3 ambientColor;
|
struct Light {
|
||||||
uniform vec3 diffuseColor;
|
int type; // 0 = directional (shadowed), other = non-directional (no shadow)
|
||||||
uniform vec3 specularColor; // used as F0 (base reflectance)
|
vec3 position; // for directional: encode light direction (see note)
|
||||||
|
vec3 color;
|
||||||
|
float intensity;
|
||||||
|
mat4 lightSpace;
|
||||||
|
sampler2D shadowMap;
|
||||||
|
};
|
||||||
|
#define MAX_LIGHTS 10
|
||||||
|
uniform int lightsCount;
|
||||||
|
uniform Light lights[MAX_LIGHTS];
|
||||||
|
|
||||||
uniform float ambientStrength;
|
uniform bool isLight;
|
||||||
uniform float specularStrength;
|
uniform vec3 currentLightColor;
|
||||||
uniform float shininess; // mapped to roughness
|
|
||||||
uniform bool useSpecular;
|
// Material parameters
|
||||||
|
uniform vec3 albedo;
|
||||||
|
uniform float metallic;
|
||||||
|
uniform float roughness;
|
||||||
|
uniform float ao;
|
||||||
|
|
||||||
|
uniform sampler2D albedoTex;
|
||||||
|
uniform sampler2D metallicTex;
|
||||||
|
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;
|
#define PI 3.14159265359
|
||||||
uniform bool useTexture;
|
|
||||||
|
|
||||||
#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
|
// Improved ShadowCalculation: returns [0,1] shadow factor (1 = fully in shadow)
|
||||||
// ----------------------------------------------------------------------------
|
float ShadowCalculation(sampler2D shadowMap, mat4 lightSpace, vec3 N, vec3 L)
|
||||||
|
{
|
||||||
|
// Transform fragment position to light's clip / NDC space
|
||||||
|
vec4 fragPosLightSpace = lightSpace * vec4(vertexPos, 1.0);
|
||||||
|
|
||||||
// Normal Distribution Function (GGX/Trowbridge-Reitz)
|
// perspective divide
|
||||||
|
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
|
||||||
|
// to [0,1]
|
||||||
|
projCoords = projCoords * 0.5 + 0.5;
|
||||||
|
|
||||||
|
// if outside the light's orthographic frustum (xy outside or z > 1), consider unshadowed
|
||||||
|
if (projCoords.z > 1.0) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
if (projCoords.x < 0.0 || projCoords.x > 1.0 || projCoords.y < 0.0 || projCoords.y > 1.0) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
float currentDepth = projCoords.z;
|
||||||
|
// basic bias (slope-dependent)
|
||||||
|
float bias = max(0.001 * (1.0 - dot(N, L)), 0.0005);
|
||||||
|
|
||||||
|
// PCF (3x3)
|
||||||
|
float shadow = 0.0;
|
||||||
|
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
|
||||||
|
for (int x = -1; x <= 1; ++x) {
|
||||||
|
for (int y = -1; y <= 1; ++y) {
|
||||||
|
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
|
||||||
|
shadow += (currentDepth - bias) > pcfDepth ? 1.0 : 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shadow /= 9.0;
|
||||||
|
|
||||||
|
// clamp to [0,1]
|
||||||
|
shadow = clamp(shadow, 0.0, 1.0);
|
||||||
|
return shadow;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// PBR helpers (unchanged)
|
||||||
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,86 +94,110 @@ 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;
|
||||||
|
|
||||||
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 ggx1 = GeometrySchlickGGX(max(dot(N,L),0.0), roughness);
|
||||||
float NdotL = max(dot(N, L), 0.0);
|
float ggx2 = GeometrySchlickGGX(max(dot(N,V),0.0), roughness);
|
||||||
float ggx1 = GeometrySchlickGGX(NdotV, roughness);
|
|
||||||
float ggx2 = 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 = currentLightColor * 10.0;
|
||||||
|
FragColor = vec4(emissive, 1.0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
vec3 N = normalize(vertexNormal);
|
vec3 N = normalize(vertexNormal);
|
||||||
vec3 V = normalize(viewPos - vertexPos);
|
vec3 V = normalize(viewPos - vertexPos);
|
||||||
vec3 L = normalize(lightPos - vertexPos);
|
|
||||||
vec3 H = normalize(V + L);
|
|
||||||
|
|
||||||
// Texture or uniform color
|
vec3 baseColor = useAlbedoMap ? texture(albedoTex, TexCoords).rgb : albedo;
|
||||||
vec3 albedo = (useTexture)
|
float metal = useMetallicMap ? texture(metallicTex, TexCoords).r : metallic;
|
||||||
? texture(diffuseTex, TexCoords).rgb
|
float rough = useRoughnessMap ? texture(roughnessTex, TexCoords).r : roughness;
|
||||||
: diffuseColor;
|
float aoValue = useAoMap ? texture(aoTex, TexCoords).r : ao;
|
||||||
|
|
||||||
// Map shininess to roughness (inverse relationship)
|
vec3 F0 = mix(vec3(0.04), baseColor, metal);
|
||||||
float roughness = clamp(1.0 - (shininess / 256.0), 0.05, 1.0);
|
|
||||||
|
|
||||||
// Base reflectivity (F0)
|
vec3 Lo = vec3(0.0);
|
||||||
vec3 F0 = mix(vec3(0.04), specularColor, specularStrength);
|
|
||||||
|
|
||||||
// Cook-Torrance BRDF
|
// Loop over all lights
|
||||||
float NDF = DistributionGGX(N, H, roughness);
|
for (int i = 0; i < lightsCount; i++)
|
||||||
float G = GeometrySmith(N, V, L, roughness);
|
{
|
||||||
vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
|
// compute light vector L depending on type
|
||||||
|
vec3 L = vec3(0);
|
||||||
|
if (lights[i].type == 0) {
|
||||||
|
// directional light: convention here is that lights[i].position stores the direction
|
||||||
|
// *towards* the light (for example, for sun direction you may upload -sunDir).
|
||||||
|
// Adjust according to your CPU-side convention.
|
||||||
|
L = normalize(lights[i].position); // expect this to be a direction
|
||||||
|
} else {
|
||||||
|
// point / spot style light: position is world-space point
|
||||||
|
L = normalize(lights[i].position - vertexPos);
|
||||||
|
}
|
||||||
|
|
||||||
vec3 numerator = NDF * G * F;
|
vec3 H = normalize(V + L);
|
||||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
|
|
||||||
vec3 specular = numerator / denominator;
|
|
||||||
|
|
||||||
// Energy conservation
|
float NDF = DistributionGGX(N, H, rough);
|
||||||
vec3 kS = F;
|
float G = GeometrySmith(N, V, L, rough);
|
||||||
vec3 kD = vec3(1.0) - kS;
|
vec3 F = fresnelSchlick(max(dot(H,V),0.0), F0);
|
||||||
kD *= (useSpecular ? 1.0 : 1.0); // keep diffuse always unless specular is off
|
|
||||||
|
|
||||||
float NdotL = max(dot(N, L), 0.0);
|
// compute shadow only for directional (type==0) lights that have shadow maps
|
||||||
|
float shadow_i = 0.0;
|
||||||
|
if (lights[i].type == 0) {
|
||||||
|
shadow_i = ShadowCalculation(lights[i].shadowMap, lights[i].lightSpace, N, L);
|
||||||
|
}
|
||||||
|
|
||||||
vec3 diffuse = kD * albedo / 3.14159265;
|
vec3 numerator = NDF * G * F;
|
||||||
vec3 radiance = LIGHT_COLOR;
|
float denominator = 4.0 * max(dot(N,V),0.0) * max(dot(N,L),0.0) + 0.001;
|
||||||
|
vec3 specular = numerator / denominator;
|
||||||
|
|
||||||
vec3 Lo = (diffuse + specular) * radiance * NdotL;
|
vec3 kS = F;
|
||||||
|
vec3 kD = vec3(1.0) - kS;
|
||||||
|
kD *= 1.0 - metal;
|
||||||
|
|
||||||
// Ambient (simple, not IBL)
|
float NdotL = max(dot(N,L), 0.0);
|
||||||
vec3 ambient = ambientStrength * ambientColor * albedo;
|
|
||||||
|
|
||||||
vec3 result = ambient + Lo;
|
vec3 radiance = lights[i].color * lights[i].intensity;
|
||||||
|
|
||||||
// Gamma correction
|
// Apply shadow_i only to this light's contribution:
|
||||||
result = pow(result, vec3(1.0/2.2));
|
// when shadow_i == 1.0 -> this light contributes 0
|
||||||
|
// when shadow_i == 0.0 -> full contribution
|
||||||
|
vec3 contrib = (kD * baseColor / PI + specular) * radiance * NdotL * (1.0 - shadow_i);
|
||||||
|
|
||||||
FragColor = vec4(result, opacity);
|
Lo += contrib;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ambient (unshadowed by design)
|
||||||
|
vec3 ambient = vec3(0.03) * baseColor * aoValue;
|
||||||
|
|
||||||
|
vec3 color = ambient + Lo;
|
||||||
|
|
||||||
|
// HDR tonemapping + gamma
|
||||||
|
color = color / (color + vec3(1.0));
|
||||||
|
color = pow(color, vec3(1.0/2.2));
|
||||||
|
|
||||||
|
FragColor = vec4(color, opacity);
|
||||||
}
|
}
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
#version 410 core
|
|
||||||
|
|
||||||
// Output color of the fragment (pixel)
|
|
||||||
out vec4 FragColor; // RGBA color for the fragment, where A is the alpha (opacity)
|
|
||||||
|
|
||||||
in vec3 vertexPos;
|
|
||||||
in vec3 vertexNormal;
|
|
||||||
in vec2 TexCoords;
|
|
||||||
|
|
||||||
uniform vec3 lightPos;
|
|
||||||
uniform vec3 viewPos;
|
|
||||||
|
|
||||||
// From Object Renderer
|
|
||||||
|
|
||||||
uniform vec3 ambientColor;
|
|
||||||
uniform vec3 diffuseColor;
|
|
||||||
uniform vec3 specularColor;
|
|
||||||
|
|
||||||
uniform float ambientStrength;
|
|
||||||
|
|
||||||
uniform float specularStrength;
|
|
||||||
uniform float shininess;
|
|
||||||
uniform bool useSpecular;
|
|
||||||
|
|
||||||
uniform float opacity;
|
|
||||||
|
|
||||||
uniform sampler2D diffuseTex;
|
|
||||||
uniform bool useTexture;
|
|
||||||
|
|
||||||
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
// Lighting vectors
|
|
||||||
vec3 lightDir = normalize(lightPos - vertexPos);
|
|
||||||
vec3 norm = normalize(vertexNormal);
|
|
||||||
vec3 viewDir = normalize(viewPos - vertexPos);
|
|
||||||
vec3 reflectDir = reflect(-lightDir, norm);
|
|
||||||
|
|
||||||
// Phong components
|
|
||||||
// float spec = pow(max(dot(viewDir, reflectDir), 0.0), clamp(shininess, 2, 256));
|
|
||||||
// vec3 specular = (useSpecular) ? specularStrength * spec * specularColor : vec3(0.0);
|
|
||||||
|
|
||||||
// Blinn Phong
|
|
||||||
vec3 halfDir = normalize(lightDir + viewDir);
|
|
||||||
float spec = pow(max(dot(norm, halfDir), 0.0), clamp(shininess, 2.0, 256.0));
|
|
||||||
vec3 specular = (useSpecular) ? specularStrength * spec * specularColor : vec3(0.0);
|
|
||||||
|
|
||||||
float diff = max(dot(norm, lightDir), 0.0);
|
|
||||||
vec3 diffuse = diff * diffuseColor;
|
|
||||||
|
|
||||||
vec3 ambient = ambientStrength * ambientColor;
|
|
||||||
|
|
||||||
vec3 texColor = (useTexture)
|
|
||||||
? texture(diffuseTex, TexCoords).rgb
|
|
||||||
: diffuseColor;
|
|
||||||
|
|
||||||
vec3 result = (ambient + diffuse + specular) * texColor;
|
|
||||||
|
|
||||||
FragColor = vec4(result, opacity);
|
|
||||||
}
|
|
@ -54,6 +54,7 @@ Window::Window(const char* title, int width, int height) {
|
|||||||
|
|
||||||
glEnable(GL_DEBUG_OUTPUT);
|
glEnable(GL_DEBUG_OUTPUT);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
glDebugMessageCallback(MessageCallback, nullptr);
|
glDebugMessageCallback(MessageCallback, nullptr);
|
||||||
|
|
||||||
glViewport(0, 0, m_width, m_height);
|
glViewport(0, 0, m_width, m_height);
|
||||||
@ -103,8 +104,13 @@ void Window::ProcessEvents() {
|
|||||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||||
Dispatch(WindowCloseRequested());
|
Dispatch(WindowCloseRequested());
|
||||||
}
|
}
|
||||||
|
if (event.key.scancode == SDL_SCANCODE_F11) {
|
||||||
|
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
||||||
|
SDL_SetWindowFullscreen(m_handle, !isFullscreen);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_WINDOW_RESIZED:
|
case SDL_EVENT_WINDOW_RESIZED:
|
||||||
|
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
|
||||||
int width, height;
|
int width, height;
|
||||||
if (SDL_GetWindowSizeInPixels(m_handle, &width, &height)) {
|
if (SDL_GetWindowSizeInPixels(m_handle, &width, &height)) {
|
||||||
m_width = width;
|
m_width = width;
|
||||||
|
Reference in New Issue
Block a user