Skip to content

Commit

Permalink
Diffuse Shading works (#67)
Browse files Browse the repository at this point in the history
* Merged mesh factory branch to main

* Working Vertex buffer class that replaces vk::VulkanModel to contains the vertices of the mesh

* Working index buffers and added mesh class that contains both the vertex and index buffers

* Working mesh class can also now load 3D models and work with new vertex and index buffers as well

* Working diffuse shading and fake point lighting working, with some modifications to the renderer instead of taking a vector, it takes in an unordered_map for ease of use when rendering

---------

Co-authored-by: ZacharyH777 <[email protected]>
  • Loading branch information
SpinnerX and ZacharyH777 authored Nov 23, 2024
1 parent d8499cf commit c0e5b03
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 248 deletions.
3 changes: 2 additions & 1 deletion Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ namespace engine3d{
//! @note Handle Events.
// m_EditorScene->OnUpdate();
m_EditorScene->OnMoveCamUpdate();
Renderer::RecordSceneGameObjects(m_EditorScene->GetSceneObjects(), m_EditorScene->GetCameraObject());
// Renderer::RecordSceneGameObjects(m_EditorScene->GetSceneObjects(), m_EditorScene->GetCameraObject());
Renderer::RecordSceneGameObjects(m_EditorScene->GetAllSceneObjects());
}

ApplicationInstance* InitializeApplication(){
Expand Down
162 changes: 24 additions & 138 deletions Editor/Editor/EditorScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,6 @@
#include <Core/Event/InputPoll.hpp>

namespace engine3d{
static Mesh CreateCubeMesh(glm::vec3 offset){
std::vector<Vertex> vertices = { // left face (white)
{{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}},
{{-.5f, .5f, .5f}, {.9f, .9f, .9f}},
{{-.5f, -.5f, .5f}, {.9f, .9f, .9f}},
{{-.5f, .5f, -.5f}, {.9f, .9f, .9f}},

// right face (yellow)
{{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
{{.5f, .5f, .5f}, {.8f, .8f, .1f}},
{{.5f, -.5f, .5f}, {.8f, .8f, .1f}},
{{.5f, .5f, -.5f}, {.8f, .8f, .1f}},

// top face (orange, remember y axis points down)
{{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, .5f}, {.9f, .6f, .1f}},
{{-.5f, -.5f, .5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, -.5f}, {.9f, .6f, .1f}},

// bottom face (red)
{{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
{{.5f, .5f, .5f}, {.8f, .1f, .1f}},
{{-.5f, .5f, .5f}, {.8f, .1f, .1f}},
{{.5f, .5f, -.5f}, {.8f, .1f, .1f}},

// nose face (blue)
{{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
{{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},

// tail face (green)
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
{{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
};
for (auto& v : vertices) {
v.Position += offset;
}

std::vector<uint32_t> indices = {0, 1, 2, 0, 3, 1, 4, 5, 6, 4, 7, 5, 8, 9, 10, 8, 11, 9,
12, 13, 14, 12, 15, 13, 16, 17, 18, 16, 19, 17, 20, 21, 22, 20, 23, 21};

Ref<VertexBuffer> vb = VertexBuffer::Create(vertices);
Ref<IndexBuffer> ib = IndexBuffer::Create(indices);
// return VertexBuffer::Create(vertices);
return Mesh(vb, ib);
}

EditorScene::EditorScene(){
m_Scene = new Scene();

Expand All @@ -67,28 +17,29 @@ namespace engine3d{
// auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f});

//! @note Instead of loading in primitive cubes by hand, we load in .obj's instead.
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/smooth_vase.obj");
auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/smooth_vase.obj");
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/colored_cube.obj");
auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");

// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");
//! @note Make this scene object as part of our current scene.

// -----------------------------
// Camera Scene Object Creation
// -----------------------------
m_CameraObject = new SceneObject(m_Scene);
SceneObject* m_CameraObject = new SceneObject(m_Scene);
m_CameraObject->AddComponent<EditorCamera>();
auto& camera_transform = m_CameraObject->SceneGetComponent<Transform>();
camera_transform.m_Position = {-1.f, -2.f, -20.f};
auto camera = m_CameraObject->SceneGetComponent<EditorCamera>();
m_CameraObjects.push_back(m_CameraObject);


// -----------------------------
// Cube 1 Scene object Creation
// -----------------------------
SceneObject* cube1 = new SceneObject(m_Scene);
auto& cube1_transform = cube1->SceneGetComponent<Transform>();
cube1_transform.m_Position = {.0f, .0f, 2.5};
cube1_transform.m_Scale = {.5f, .5f, 0.5};
cube1_transform.m_Scale = {10.5f, 10.5f, 10.5};
cube1->SetMesh(cube_mesh);

// -----------------------------
Expand All @@ -98,100 +49,41 @@ namespace engine3d{
auto& cube2_transform = cube2->SceneGetComponent<Transform>();
// auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio();
cube2_transform.m_Position = {5.f, .0f, -7.f};
cube2_transform.m_Scale = {.5f, .5f, 0.5};
cube2_transform.m_Scale = {5.5f.f, 5.5f, 5.5};

cube2->SetMesh(cube_mesh);

SceneObject* sphere_point_light = new SceneObject(m_Scene);
Mesh mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");
auto& sphere_transform = sphere_point_light->SceneGetComponent<Transform>();
sphere_transform.m_Position = {-10.0, 3.0, -1.0};
sphere_transform.m_Scale = {1.f, 1.f, 1.f};
sphere_point_light->SetMesh(mesh);


//! @note Then we add them to our vector.
m_SceneObjects.push_back(cube1);
m_SceneObjects.push_back(cube2);
m_PointLightObjects.push_back(sphere_point_light);

}
m_AllSceneObjecs.insert({"SceneObjects", m_SceneObjects});
m_AllSceneObjecs.insert({"PointLights", m_PointLightObjects});
m_AllSceneObjecs.insert({"PointRadioLights", m_PointRadioLights});
m_AllSceneObjecs.insert({"Cameras", m_CameraObjects});

void EditorScene::OnCreate(){
}

void EditorScene::OnUpdate(){
// glm::vec3 m_MoveDirection{0.f};
// glm::vec3 m_Rotation{0};

// for(const auto& obj : m_SceneObjects){
// // auto& transform_compoent = obj->SceneGetComponent<Transform>();
// auto& camera_component = obj->SceneGetComponent<EditorCamera>();
// camera_component.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 50.f);
// }
}

void EditorScene::OnMoveCamUpdate(){
auto& transform = m_CameraObject->SceneGetComponent<Transform>();
auto& camera = m_CameraObject->SceneGetComponent<EditorCamera>();

auto& cameraObject = m_AllSceneObjecs["Cameras"].at(0);
auto& transform = cameraObject->SceneGetComponent<Transform>();
auto& camera = cameraObject->SceneGetComponent<EditorCamera>();
auto cube_transform = m_SceneObjects[0]->SceneGetComponent<Transform>();
// float tempDt_Y;
glm::vec2 temp_position = {0.f, 0.f};
constexpr float sensitivity = 2.0f;
constexpr float pos_sensitivity = 2.f;
constexpr glm::vec2 invert_pos = {1, -1};
// ConsoleLogInfo("x = {}, y = {}, z = {}", transform.m_Position.x, transform.m_Position.y, transform.m_Position.z);
// ConsoleLogInfo("x = {}, y = {}, z = {}\n", cube_transform.m_Position.x, cube_transform.m_Position.y, cube_transform.m_Position.z);

/*
move-right = D
move-left = A
move-forward = W
move-backward = S
move-up = E
move-down = Q
look-left = LEFT
look-right = RIGHT
look-up = UP
look-down = DOWN
*/

/*
for(const auto& obj : m_SceneObjects){
glm::vec3 rotate{0};
if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT)) rotate.y += 1.f;
if(InputPoll::IsKeyPressed(ENGINE_KEY_RIGHT)) rotate.y -= 1.f;
if(InputPoll::IsKeyPressed(ENGINE_KEY_UP)) rotate.x += 1.f;
if(InputPoll::IsKeyPressed(ENGINE_KEY_DOWN)) rotate.x -= 1.f;
auto& transform = obj->SceneGetComponent<Transform>();
auto& camera = obj->SceneGetComponent<EditorCamera>();
if(glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()){
transform.m_AxisRotation += m_LookSpeed * SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * glm::normalize(rotate);
}
transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f);
transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi<float>());
float yaw = transform.m_AxisRotation.y;
const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)};
const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y};
const glm::vec3 up_dir{0.f, -1.f, 0.f};
glm::vec3 move_dir{0.f};
if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD
if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD
if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT
if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT
if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP
if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN
if(glm::dot(move_dir, move_dir) > std::numeric_limits<float>::epsilon()){
transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir);
}
// camera.SetViewTarget({-1.f, -2.f, -20.f}, transform.m_Position);
// camera.SetViewTarget({0.f, 0.f, 0.f}, transform.m_Position);
camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation);
}
*/

glm::vec3 rotate{0};

//! @note Make sure that our mouse controls how camera rotates.
Expand All @@ -215,7 +107,7 @@ namespace engine3d{

float yaw = transform.m_AxisRotation.y;
const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)};
const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y};
const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.x};
const glm::vec3 up_dir{0.f, -1.f, 0.f};

glm::vec3 move_dir{0.f};
Expand All @@ -237,10 +129,4 @@ namespace engine3d{
camera.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 100.f);

}

// void EditorScene::OnCameraUpdate(){
// for(const auto& obj : m_SceneObjects){
// obj->SceneGetComponent<EditorCamera>().OnUpdate();
// }
// }
};
16 changes: 13 additions & 3 deletions Editor/Editor/EditorScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// #include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Core/ApplicationManager/Scene.hpp>
#include <unordered_map>

namespace engine3d{
class EditorScene{
Expand All @@ -15,14 +16,23 @@ namespace engine3d{

void OnMoveCamUpdate();

std::vector<SceneObject*>& GetSceneObjects() { return m_SceneObjects; }
// std::vector<SceneObject*>& GetSceneObjects() { return m_SceneObjects; }

SceneObject* GetCameraObject() const { return m_CameraObject; }
std::unordered_map<std::string, std::vector<SceneObject*>>& GetAllSceneObjects() { return m_AllSceneObjecs; }

// SceneObject* GetCameraObject() const { return m_CameraObject; }

private:
std::vector<SceneObject*> m_SceneObjects;
std::vector<SceneObject*> m_PointLightObjects;
std::vector<SceneObject*> m_CameraObjects;
std::vector<SceneObject*> m_PointRadioLights;


//! @note All scene objects within the scene.
std::unordered_map<std::string, std::vector<SceneObject *>> m_AllSceneObjecs;
Scene* m_Scene;
SceneObject* m_CameraObject;
// SceneObject* m_CameraObject;
glm::vec2 m_MousePosition;
float m_MoveSpeed = {3.f};
float m_LookSpeed = {1.5f};
Expand Down
1 change: 1 addition & 0 deletions engine3d/Core/Renderer/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace engine3d{
//! @note Currently the record command buffers just does what "simple_render_system.hpp/.cpp" does already
//! @note In the future I'll add API's for submitting draw calls.
static void RecordSceneGameObjects(std::vector<SceneObject*>& p_SceneObjects, SceneObject* p_CameraObject);
static void RecordSceneGameObjects(std::unordered_map<std::string, std::vector<SceneObject*>>& p_AllSceneObjects);
static void EndFrame();

template<typename UFunction>
Expand Down
8 changes: 4 additions & 4 deletions sim_shader_transforms/simple_shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@


layout(location = 0) in vec3 fragColor;

layout(location = 0) out vec4 outColor;


layout(push_constant) uniform Push {
mat4 transform;
vec2 iResolution;
vec3 color;
mat4 Transform; // proj * view * model
mat4 ModelMatrix;
vec3 LightTransform;
} push;

void main(){
Expand Down
Binary file modified sim_shader_transforms/simple_shader.frag.spv
Binary file not shown.
58 changes: 23 additions & 35 deletions sim_shader_transforms/simple_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,33 @@
//! @note Position and Color used in the Vertex class.
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Color;
layout(location = 2) in vec3 Normals;
layout(location = 3) in vec2 Uv;


layout(location = 0) out vec3 fragColor;

layout(push_constant) uniform Push {
mat4 transform;
mat4 Projection;
vec2 iResolution;
vec3 color;
mat4 Transform; // proj * view * model
mat4 ModelMatrix;
vec3 LightTransform;
} push;

// const vec3 dir_to_light = normalize(vec3(1.0, -3.0, -1.0));
// vec3 light_transform = vec3(1.0, -3.0, -1.0);
// const vec3 dir_to_light = normalize(vec3(1.0, -3.0, -1.0));
// vec3 dir_to_light = normalize(push.LightTransform);
// const vec3 dir_to_light = normalize();
vec3 dir_to_light = normalize(push.LightTransform);

void main(){
// gl_Position = vec4(Position, 0.0, 1.0);
// mat2 p = (push.transform * 1.f);
// p[0] *= (push.iResolution.y / push.iResolution.x);
// p[1] *= (push.iResolution.y / push.iResolution.x);
// gl_Position = vec4(p * Position + push.offset, 0.0, 1.0);
// mat4 p = push.transform;
// p[0] /= (push.iResolution.x / push.iResolution.y);
// p[1] /= (push.iResolution.x / push.iResolution.y);
// p[2] /= (push.iResolution.x / push.iResolution.y);
// gl_Position = p * vec4(Position, 1.0);
gl_Position = push.transform * vec4(Position, 1.0);
fragColor = Color;
}

// layout(location = 0) in vec2 Position;
// layout(location = 1) in vec3 Color;

// // layout(location = 0) out vec3 fragColor;

// layout(push_constant) uniform Push {
// mat2 transform;
// vec2 offset;
// vec2 iResolution;
// vec3 color;
// } push;

// void main(){
// // gl_Position = vec4(Position, 0.0, 1.0);
// gl_Position = vec4(push.transform * Position + push.offset, 0.0, 1.0);
// // fragColor = Color;
// }
// vec4 worldPositionSpace = push.ModelMatrix * vec3(Position, 1.0);
gl_Position = push.Transform * vec4(Position, 1.0);

// mat3 normal_mat = transpose(inverse(mat3(push.ModelMatrix)));
vec3 normalize_world_space = normalize(mat3(push.ModelMatrix) * Normals);
float LightIntensity = max(dot(normalize_world_space, dir_to_light), 0) / 1.5f;

// fragColor = Color;
fragColor = LightIntensity * Color;
}
Binary file modified sim_shader_transforms/simple_shader.vert.spv
Binary file not shown.
17 changes: 5 additions & 12 deletions src/engine3d/Core/GraphicDrivers/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,11 @@ namespace engine3d{
attrib.vertices[3 * index.vertex_index + 2]
};

//! @note Setting the colors
auto color_idx = 3 * index.vertex_index + 2;
if(color_idx < attrib.colors.size()){
vertex.Color = {
attrib.colors[color_idx - 2],
attrib.colors[color_idx - 1],
attrib.colors[color_idx - 0]
};
}
else{
vertex.Color = {0.f, 0.f, 0.f};
}
vertex.Color = {
attrib.colors[3 * index.vertex_index + 0],
attrib.colors[3 * index.vertex_index + 1],
attrib.colors[3 * index.vertex_index + 2]
};
}

if(index.normal_index >= 0){
Expand Down
Loading

0 comments on commit c0e5b03

Please sign in to comment.