diff --git a/shaders/shader.vert b/shaders/shader.vert index 026661c..251f6dd 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -6,6 +6,8 @@ layout(location = 2) in vec2 inUV; layout(binding = 0) uniform UniformBufferObject { mat4 model; + mat4 view; + mat4 projection; } ubo; layout(location = 0) out vec3 fragColor; @@ -23,6 +25,6 @@ vec3 colors[3] = vec3[] ( ); void main() { - gl_Position = ubo.model * vec4(inPosition, 1.0); - fragColor = inNormal; + gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0); + fragColor = clamp(inNormal, vec3(0.0), vec3(1.0)); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cdc3ebf..9a56715 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,8 +31,8 @@ struct Vertex { struct UniformBufferObject { glm::mat4 model; - //glm::mat4 view; - //glm::mat4 proj; + glm::mat4 view; + glm::mat4 proj; }; VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, @@ -158,6 +158,9 @@ private: std::vector> vertices; std::vector> indices; std::vector worldMatrices; + glm::mat4x4 viewMatrix; + glm::mat4x4 projectionMatrix; + std::vector vertexBuffer; std::vector vertexBufferMemory; std::vector indexBuffer; @@ -294,6 +297,10 @@ private: worldMatrices.push_back(transform); } + if (node.camera == 0) { + viewMatrix = glm::inverse(transform); + } + processNodes(node.children, transform); } }; @@ -357,6 +364,27 @@ private: throw std::runtime_error("Failed to load GLTF: " + err); } + viewMatrix = glm::mat4x4(1); + projectionMatrix = glm::mat4x4(1); + //glm::perspectiveFovLH(60.f, static_cast(WIDTH), static_cast(HEIGHT), 0.001f, 1000.f); + if (model.cameras.size() > 0) + { + const auto& camera = model.cameras[0]; + if (camera.type == "perspective") + { + projectionMatrix = glm::perspectiveRH(camera.perspective.yfov, camera.perspective.aspectRatio, camera.perspective.znear, camera.perspective.zfar); + } + if (camera.type == "orthographic") + { + auto right = camera.orthographic.xmag / 2.f; + auto left = - right; + auto top = camera.orthographic.ymag / 2.f; + auto bottom = - top; + projectionMatrix = glm::orthoRH(left, right, bottom, top, camera.orthographic.znear, camera.orthographic.zfar); + } + } + projectionMatrix[1][1] *= -1.0f; + processNodes(model.scenes[0].nodes, glm::mat4x4(1)); std::cout << "Loaded " << vertices.size() << " vertices, " @@ -1204,6 +1232,8 @@ private: { UniformBufferObject ubo{}; ubo.model = worldMatrices[i]; + ubo.view = viewMatrix; + ubo.proj = projectionMatrix; memcpy(uniformBuffersMapped[i], &ubo, sizeof(ubo)); }