Got mouse and keyboard movement working -- I think

This commit is contained in:
Conál 2024-02-07 10:39:07 -05:00
parent 5df7032caa
commit df334c3941
11 changed files with 65 additions and 27 deletions

View File

@ -82,18 +82,15 @@ void Input::handleMovementKeys(Renderer& ren) {
forward = glm::vec3(glm::sin(ren.cam.theta)*glm::cos(ren.cam.phi), glm::cos(ren.cam.theta), glm::sin(ren.cam.theta)*glm::sin(ren.cam.phi));
else
forward = glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi));
const auto right = glm::cross(forward, glm::vec3(0.0, 1.0, 0.0));
const auto right = glm::cross(glm::vec3(0.0, 1.0, 0.0), forward);
const auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f;
if(glfwGetKey(in, GLFW_KEY_UP)) {
ren.cam.theta -= 0.01;
}
if(glfwGetKey(in, GLFW_KEY_UP)) {
ren.cam.theta -= 0.01;
ren.cam.theta -= 0.02;
}
if(glfwGetKey(in, GLFW_KEY_DOWN)) {
ren.cam.theta += 0.01;
ren.cam.theta += 0.02;
}
if(glfwGetKey(in, GLFW_KEY_LEFT)) {

View File

@ -324,10 +324,10 @@ void Renderer::draw() {
auto sz = win.getDimensions();
uniform_buffer->upload(UniformData{
.view = cam.view(),
.viewport = glm::vec2(viewport.width, viewport.y),
.time = time,
.cam_pos = cam.pos,
.cam_dir = cam.dir(),
.focal_length = 2.0,
});
command_buffer->bind(*pipeline);

View File

@ -58,7 +58,7 @@ struct Renderer {
std::unique_ptr<UI> ui;
Camera cam{ .pos = glm::vec3(0.0, -10.0, 0.0), };
Camera cam{ .pos = glm::vec3(0.0, 0.0, -1.0), };
bool capture_mouse = false;
bool flycam = false;

View File

@ -8,10 +8,6 @@ UniformBuffer::UniformBuffer(vk::PhysicalDevice phys_dev, vk::Device dev) {
vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible
);
Log::debug("Offset of time: %zu\n", offsetof(UniformData, time));
Log::debug("Offset of cam_pos: %zu\n", offsetof(UniformData, cam_pos));
Log::debug("Offset of viewport: %zu\n", offsetof(UniformData, viewport));
}
void UniformBuffer::upload(const UniformData& data) {

View File

@ -12,19 +12,19 @@
/* Uniform:
*
* layout (set = 0, binding = 0) uniform Matrices {
* mat4 view;
* vec2 viewport;
* float time;
* vec3 cam_pos;
* vec3 cam_dir;
* float focal_length;
* };
*
*/
struct UniformData {
glm::mat4 view;
glm::vec2 viewport;
float time;
glm::vec3 cam_pos;
glm::vec3 cam_dir;
float focal_length = 2.0;
};
struct UniformBuffer {

View File

@ -15,7 +15,11 @@ struct Camera {
glm::vec3 pos = glm::vec3(0.0f);
inline glm::vec3 dir() {
return glm::vec3(sin(theta) * cos(phi), cos(theta), sin(theta) * sin(phi));
return glm::normalize(glm::vec3(
sin(theta) * cos(phi),
cos(theta),
sin(theta) * sin(phi)
));
}
inline glm::mat4 view() {

View File

@ -89,9 +89,18 @@ void UI::newFrame() {
ImGui::SetNextWindowBgAlpha(0.5f);
ImGui::Begin("Rendering Info", nullptr);
auto target = info.cam.dir() + info.cam.pos;
ImGui::Text("FPS: %f", info.fps);
ImGui::Text("Time: %f", info.time);
ImGui::Checkbox("Fly Camera", &info.flycam);
ImGui::Text("Target: %f %f %f", target.x, target.y, target.z);
ImGui::SliderFloat("Theta", &info.cam.theta, 0.0, glm::pi<float>());
ImGui::SliderFloat("Phi", &info.cam.phi, 0.0, glm::two_pi<float>());
ImGui::SliderFloat("X", &info.cam.pos.x, -10.0, 10.0);
ImGui::SliderFloat("Y", &info.cam.pos.y, -10.0, 10.0);
ImGui::SliderFloat("Z", &info.cam.pos.z, -10.0, 10.0);
ImGui::End();
}

View File

@ -1,16 +1,43 @@
#version 450 core
layout (set = 0, binding = 0) uniform Matrices {
mat4 view;
vec2 viewport;
float time;
vec3 cam_pos;
vec3 cam_dir;
};
layout (location = 0) in vec2 texCoord;
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 dir;
layout (location = 0) out vec4 fragColor;
float sphere(vec3 p, vec3 center, float r) {
return length(p) - r;
}
float sdf(vec3 pos) {
return sphere(pos, vec3(0.0, 0.0, 10.0), 3.0);
}
float raycast() {
float t = 0.0;
for(int i = 0; i < 64; i++) {
float dt = sdf(pos + dir * t);
if(dt < 0.0001*t)
return t;
else if(dt > 200.0)
return -1;
t += dt;
}
return -1;
}
void main() {
fragColor = vec4(abs(texCoord), abs(cos(time)), 1.0);
float d = raycast();
vec3 col = vec3(1.0-d*0.075);
fragColor = vec4(col, 1.0);
}

Binary file not shown.

View File

@ -2,16 +2,21 @@
layout (location = 0) in vec2 aCoord;
layout (location = 0) out vec2 texCoord;
layout (location = 0) out vec3 pos;
layout (location = 1) out vec3 dir;
layout (set = 0, binding = 0) uniform Matrices {
mat4 view;
vec2 viewport;
float time;
vec3 cam_pos;
vec3 cam_dir;
float focal_length;
};
void main() {
texCoord = aCoord;
vec4 p = view * vec4(aCoord.x * viewport.x/viewport.y, aCoord.y, 0.0, 1.0);
pos = p.xyz;
p-= view * vec4(0.0, 0.0, -focal_length, 1.0);
dir = normalize(p.xyz);
gl_Position = vec4(aCoord, 0.0, 1.0);
}

Binary file not shown.