From c0e5b0349c1fe80d75fa001822afd50be4c98395 Mon Sep 17 00:00:00 2001 From: Aaron <56617292+SpinnerX@users.noreply.github.com> Date: Sat, 23 Nov 2024 15:28:52 -0800 Subject: [PATCH] Diffuse Shading works (#67) * 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 <93843297+ZacharyH777@users.noreply.github.com> --- Editor/Editor/Editor.cpp | 3 +- Editor/Editor/EditorScene.cpp | 162 +++--------------- Editor/Editor/EditorScene.hpp | 16 +- engine3d/Core/Renderer/Renderer.hpp | 1 + sim_shader_transforms/simple_shader.frag | 8 +- sim_shader_transforms/simple_shader.frag.spv | Bin 864 -> 892 bytes sim_shader_transforms/simple_shader.vert | 58 +++---- sim_shader_transforms/simple_shader.vert.spv | Bin 1500 -> 2356 bytes src/engine3d/Core/GraphicDrivers/Mesh.cpp | 17 +- src/engine3d/Core/Renderer/Renderer.cpp | 100 ++++++++--- .../Vulkan2Showcase/Shaders/VulkanShader.cpp | 33 +--- 11 files changed, 150 insertions(+), 248 deletions(-) diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index 7d8c16d..1d2ae00 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -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(){ diff --git a/Editor/Editor/EditorScene.cpp b/Editor/Editor/EditorScene.cpp index e925225..f3fb4af 100644 --- a/Editor/Editor/EditorScene.cpp +++ b/Editor/Editor/EditorScene.cpp @@ -9,56 +9,6 @@ #include namespace engine3d{ - static Mesh CreateCubeMesh(glm::vec3 offset){ - std::vector 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 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 vb = VertexBuffer::Create(vertices); - Ref ib = IndexBuffer::Create(indices); - // return VertexBuffer::Create(vertices); - return Mesh(vb, ib); - } - EditorScene::EditorScene(){ m_Scene = new Scene(); @@ -67,20 +17,21 @@ 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(); auto& camera_transform = m_CameraObject->SceneGetComponent(); camera_transform.m_Position = {-1.f, -2.f, -20.f}; auto camera = m_CameraObject->SceneGetComponent(); + m_CameraObjects.push_back(m_CameraObject); + // ----------------------------- // Cube 1 Scene object Creation @@ -88,7 +39,7 @@ namespace engine3d{ SceneObject* cube1 = new SceneObject(m_Scene); auto& cube1_transform = cube1->SceneGetComponent(); 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); // ----------------------------- @@ -98,100 +49,41 @@ namespace engine3d{ auto& cube2_transform = cube2->SceneGetComponent(); // 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(); + 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(); - // auto& camera_component = obj->SceneGetComponent(); - // camera_component.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 50.f); - // } } void EditorScene::OnMoveCamUpdate(){ - auto& transform = m_CameraObject->SceneGetComponent(); - auto& camera = m_CameraObject->SceneGetComponent(); + + auto& cameraObject = m_AllSceneObjecs["Cameras"].at(0); + auto& transform = cameraObject->SceneGetComponent(); + auto& camera = cameraObject->SceneGetComponent(); auto cube_transform = m_SceneObjects[0]->SceneGetComponent(); // 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(); - auto& camera = obj->SceneGetComponent(); - - if(glm::dot(rotate, rotate) > std::numeric_limits::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 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::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. @@ -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}; @@ -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().OnUpdate(); - // } - // } }; \ No newline at end of file diff --git a/Editor/Editor/EditorScene.hpp b/Editor/Editor/EditorScene.hpp index c03f4fa..b7f3fc7 100644 --- a/Editor/Editor/EditorScene.hpp +++ b/Editor/Editor/EditorScene.hpp @@ -3,6 +3,7 @@ // #include #include #include +#include namespace engine3d{ class EditorScene{ @@ -15,14 +16,23 @@ namespace engine3d{ void OnMoveCamUpdate(); - std::vector& GetSceneObjects() { return m_SceneObjects; } + // std::vector& GetSceneObjects() { return m_SceneObjects; } - SceneObject* GetCameraObject() const { return m_CameraObject; } + std::unordered_map>& GetAllSceneObjects() { return m_AllSceneObjecs; } + + // SceneObject* GetCameraObject() const { return m_CameraObject; } private: std::vector m_SceneObjects; + std::vector m_PointLightObjects; + std::vector m_CameraObjects; + std::vector m_PointRadioLights; + + + //! @note All scene objects within the scene. + std::unordered_map> 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}; diff --git a/engine3d/Core/Renderer/Renderer.hpp b/engine3d/Core/Renderer/Renderer.hpp index 4e3a83a..86508b3 100644 --- a/engine3d/Core/Renderer/Renderer.hpp +++ b/engine3d/Core/Renderer/Renderer.hpp @@ -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& p_SceneObjects, SceneObject* p_CameraObject); + static void RecordSceneGameObjects(std::unordered_map>& p_AllSceneObjects); static void EndFrame(); template diff --git a/sim_shader_transforms/simple_shader.frag b/sim_shader_transforms/simple_shader.frag index 2c2e18e..08d4ac9 100644 --- a/sim_shader_transforms/simple_shader.frag +++ b/sim_shader_transforms/simple_shader.frag @@ -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(){ diff --git a/sim_shader_transforms/simple_shader.frag.spv b/sim_shader_transforms/simple_shader.frag.spv index c07daa5904668771c6f14aa988b958818417662f..39bdcf54f80f755367e4ee3f11fde6c8b3204fed 100644 GIT binary patch delta 308 zcmaFB_J@s^nMs+Qfq{{Mn}L@>d?N4bSP=#WhJe!I3?Rk82819!2!s?R<`t*q7v%!k zFmXm8+c!TYHODuxq$sljD9#R6&jb|r$xP2ELDIy^z``Il*_Tnj9%dpdP?ZM*E8GTU zARh#f#ei;O5CDs@fYpI)MAir5gY-E7F-#vw451HVKFCENbqyfLyE8B|h)llAxLj8b gYP~Q}3WT|USOMrzQ6S9;#2}|yL-}Hp4>HXM05R_$TL1t6 delta 279 zcmeyv_JEC-nMs+Qfq{{Mn}L@>Vj}PBP*Da3hJe!I3?Rk82819!2$U2h<`t*q7v%!k zFmXm8J2NP?I6tSfBr`t`D9#F2&jb`t&df%b_2Mc9BCq(TmAlQ57C b66XS91)v%+Ak7KHAXiyK`QnrBGR+4754swP diff --git a/sim_shader_transforms/simple_shader.vert b/sim_shader_transforms/simple_shader.vert index 709464b..4978d1c 100644 --- a/sim_shader_transforms/simple_shader.vert +++ b/sim_shader_transforms/simple_shader.vert @@ -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; +} \ No newline at end of file diff --git a/sim_shader_transforms/simple_shader.vert.spv b/sim_shader_transforms/simple_shader.vert.spv index 168989782ffa0df4ed3c52efba55376ada40364f..ef88cd1bb75e3adb558a60aeffaae89e2861412a 100644 GIT binary patch literal 2356 zcmZ9N*-sQf5XKvpWf2iXyboN!8^seuQMnzDMb>z{PByy(PBPAJW(O6Yc=XA{#6QUR zSNUS%_v@WrCibT2s;{cPs_yF1Lhn?`m>zS`95NfGvqnv?5F@PL?q20hWu~1rX70`2 zB4XGSY#=csiemLiKGwn*{j{Vkxi49lc#?{wpkJ@{-%9K=gT|})-sYxPS@-L$mfucy zqri{CIPe=`64cZ1Q$XB6E-sAg(M}^DP*g;|c2PYaPz_P|X;XCDA5xiyxFPM;o%RRp ze*LgnZ(#q`4%xhlRH?ZhAN=dpq*0LGnIG z(%_3s%MH%L*KCJr*o-YVI5jOE#_5~zt4>c&$#PLwzSSsft%dEh7T1HWk7?lBiK4Er zsJmtktfGEp?cIVF8!*$_V;#E|shIz{nM4i0-KvSXb6?EwmG!s9&~F~ewr77Hr$Mad zcRSq7cq^%GuQsD*lI1eT*AWU zPO+VzyUY8zH*9JW&v%1OjWK1BHz5lf{kXJ#?PbXc#dB_K{AJk#nGL>>*?b?w5I>md z^nOw{=K#YV&t?OLzyaqW?Qn31aANcR!H&ru(O#DHNTxHJ8PYHLquPmukN9I2*B>q7 zv5!m0KP&r$cKQVym&X0NzVRXOp3=^_@zWPJ-vF5FsvtxakG^ns#y#SMR!XIJw)KnU5U$gP-?+pZo>w*y`^U<`HzoleQYY9)CDfyDd{0z~nSjH?jLu2mVRrB^FTM$I$2%dt zEP1UQ&Nw9jV>#@Cbg-Trb}_>^YgHW$YQH2QpLqO}k{{y5cUeMhV(5=Ia8<%Pqd)q` zCeHPLLpuHQ7F_=`(!uEsUjAbRV{PiI**B20wgHC19@JGwGh>dv=cJ Q(!q!$pWea#s^YxlAH4FbYybcN literal 1500 zcmYk5+fLL_6o$7m6c7YN?Uj0UjI7%YjxA@^yZAYViwJkd1UIdYC1xUu-Qrn{nP$IQH~CtK6^sM zvguZV#H^UB)qO_47ekw3EJPh1z(<2(rmasDaJ%lMm2p*7AOUsl-CW~tD* zDxMiC+@|HJ{h`j})IK=NcbM4mds~#lG>WG_Hh^!EBvYTKIhmaUzcuN>r1*>t4{T1E z57Xi-%P(|yI6MY<_J#RHZ;Hdgp2S6#Od8%^RfqmjmSnj!z1F42O)pOf`=1V5#dy{C zILn9UhpjxmkF$@I#|}A8I}CncXotZK3@#4i4#u|$ah;6r@HU6ZCK z{+Er-+XJJ|va~OuE`6|9B%CeT?3TLlg6FCjnq7Vn1kXmygXiBF5F**PVLL^x7vHXg-o6BW%-)mmUE%jy6JQhPxINUJdGWCa?7w31Lh=u(IC7u> diff --git a/src/engine3d/Core/GraphicDrivers/Mesh.cpp b/src/engine3d/Core/GraphicDrivers/Mesh.cpp index 10c8cad..23aeab4 100644 --- a/src/engine3d/Core/GraphicDrivers/Mesh.cpp +++ b/src/engine3d/Core/GraphicDrivers/Mesh.cpp @@ -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){ diff --git a/src/engine3d/Core/Renderer/Renderer.cpp b/src/engine3d/Core/Renderer/Renderer.cpp index dfbfcba..0e87c42 100644 --- a/src/engine3d/Core/Renderer/Renderer.cpp +++ b/src/engine3d/Core/Renderer/Renderer.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE @@ -32,8 +33,9 @@ namespace engine3d{ //! @note vk::VulkanModal is how shaders are going to be struct SimplePushConstantData{ glm::mat4 Transform{1.f}; - glm::vec2 iResolution; - alignas(16) glm::vec3 Color; + glm::mat4 ModelMatrix{1.f}; + glm::vec3 LightTransform{1.0, -3.0, -1.0}; + // alignas(16) glm::vec3 Color; }; void Renderer::Initialize(const std::string& p_DebugName){ @@ -166,30 +168,82 @@ namespace engine3d{ //! @note Starts when to start rendering!! vkCmdBindPipeline(current_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Shader->GetGraphicsPipeline()); float delta_time = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; - // ConsoleLogWarn("Delta Time = {:.7}", delta_time); - // auto projection_view = - auto camera_component = p_CameraObject->SceneGetComponent(); //! @note Only for testing purposes for mesh data. - for(auto& obj : p_Objects){ - // auto camera_component = obj->SceneGetComponent(); + // auto point_light_obj = p_Objects[2]; + // auto point_light_position = point_light_obj->SceneGetComponent().m_Position; + + // for(size_t i = 0; i <= p_Objects.size()-1; i++){ + // ConsoleLogWarn("Index (i <= 1) = {}", i); + for(const auto& obj : p_Objects){ + // auto obj = p_Objects[i]; + auto proj_view = camera_component.GetProjection() * camera_component.GetView(); - // obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi()); - // float x = obj->GetRotation().x; - // float y = glm::mod(obj->GetRotation().y + (0.1f * delta_time), glm::two_pi()); - // float x = glm::mod(obj->GetRotation().x + (0.05f * delta_time), glm::two_pi()); - // float z = obj->GetRotation().z; + auto model_matrix = obj->toMat4(); + + SimplePushConstantData push = { + .Transform = proj_view * model_matrix, + .ModelMatrix = model_matrix, + // .LightTransform = point_light_position + }; + + vkCmdPushConstants( + current_cmd_buffer, + g_PipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + 0, + sizeof(SimplePushConstantData), + &push + ); + + auto& vb = obj->GetMesh().GetVertices(); + auto ib = obj->GetMesh().GetIndices(); + vb->Bind(current_cmd_buffer); + + if(ib != nullptr){ + ib->Bind(current_cmd_buffer); + if(ib->HasIndicesPresent()){ + ib->Draw(GetCurrentCommandBuffer()); + } + else{ + vb->Draw(GetCurrentCommandBuffer()); + } + } + else{ + vb->Draw(GetCurrentCommandBuffer()); + } + + } + } + + void Renderer::RecordSceneGameObjects(std::unordered_map>& p_AllSceneObjects){ + + auto current_cmd_buffer = GetCurrentCommandBuffer(); + + //! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i]) + //! @note Starts when to start rendering!! + vkCmdBindPipeline(current_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Shader->GetGraphicsPipeline()); + float delta_time = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; + auto cameraObject = p_AllSceneObjects["Cameras"][0]; + auto camera_component = cameraObject->SceneGetComponent(); + + //! @note Only for testing purposes for mesh data. + // auto point_light_obj = p_Objects[2]; + // auto point_light_position = point_light_obj->SceneGetComponent().m_Position; + auto& position = p_AllSceneObjects["Cameras"][0]->SceneGetComponent().m_Position; + + for(const auto& obj : p_AllSceneObjects.at("SceneObjects")){ + + auto proj_view = camera_component.GetProjection() * camera_component.GetView(); - // glm::vec3 new_position = {x, y, z}; - // new_position = glm::normalize(new_position); - // obj->SetRotation(new_position); + auto model_matrix = obj->toMat4(); SimplePushConstantData push = { - .Transform = proj_view * obj->toMat4(), - // .iResolution = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()}, - // .Color = obj.GetColor(), + .Transform = proj_view * model_matrix, + .ModelMatrix = model_matrix, + .LightTransform = position }; vkCmdPushConstants( @@ -201,19 +255,9 @@ namespace engine3d{ &push ); - // obj->GetModel()->Bind(current_cmd_buffer); - // obj->GetModel()->Draw(current_cmd_buffer); auto& vb = obj->GetMesh().GetVertices(); auto ib = obj->GetMesh().GetIndices(); vb->Bind(current_cmd_buffer); - // ib->Bind(current_cmd_buffer); - - // if(ib->HasIndicesPresent()){ - // ib->Draw(current_cmd_buffer); - // } - // else{ - // vb->Draw(current_cmd_buffer); - // } if(ib != nullptr){ ib->Bind(current_cmd_buffer); diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp index ee91c4e..3edefdc 100644 --- a/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp +++ b/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp @@ -121,22 +121,12 @@ namespace engine3d::vk{ } std::vector VulkanShader::GetVertexAttributeDescriptions(){ - std::vector attribute_description(2); - attribute_description[0] = { - .location = 0, // // layout(location = 0) - .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, - // .offset = 0 - .offset = offsetof(Vertex, Position) - }; - - attribute_description[1] = { - .location = 1, // layout(location = 1) - .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, - .offset = offsetof(Vertex, Color) - }; + std::vector attribute_description{}; + attribute_description.push_back({0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, Position)}); + attribute_description.push_back({1, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, Color)}); + attribute_description.push_back({2, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, Normals)}); + attribute_description.push_back({3, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, Uv)}); return attribute_description; } @@ -176,18 +166,7 @@ namespace engine3d::vk{ .offset = {0, 0}, .extent = {p_Width, p_Height} }; - - // config.PipelineViewportCreateInfo = { - // VkPipelineViewportStateCreateInfo PipelineViewportCreateInfo = { - // .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, - // .pNext = nullptr, - // .flags = 0, - // .viewportCount = 1, - // .pViewports = &config.Viewport, - // .scissorCount = 1, - // .pScissors = &config.Scissor - // }; - + config.PipelineRasterizationCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .pNext = nullptr,