Okay, figured out input, for real this time

This commit is contained in:
Conál 2024-02-08 23:32:51 -05:00
parent df334c3941
commit d945f49fca
8 changed files with 51 additions and 49 deletions

View File

@ -77,14 +77,17 @@ void Input::handleMovementKeys(Renderer& ren) {
if (ImGui::GetIO().WantCaptureKeyboard)
return;
auto dir = ren.cam.dir();
glm::vec3 forward;
if (ren.flycam)
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));
forward = dir;
else
forward = glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi));
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;
forward = glm::normalize(glm::vec3(dir.x, 0.0, dir.z));
const auto right = glm::normalize(glm::cross(forward, glm::vec3(0.0, 1.0, 0.0)));
const auto up = glm::normalize(glm::cross(right, forward));
const auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f;
if(glfwGetKey(in, GLFW_KEY_UP)) {
ren.cam.theta -= 0.02;

View File

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

View File

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

View File

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

View File

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

Binary file not shown.

View File

@ -2,21 +2,16 @@
layout (location = 0) in vec2 aCoord;
layout (location = 0) out vec3 pos;
layout (location = 1) out vec3 dir;
layout (location = 0) out vec2 pos;
layout (set = 0, binding = 0) uniform Matrices {
mat4 view;
vec2 viewport;
vec3 cam_pos;
float time;
float focal_length;
};
vec4 viewport;
vec3 cam_dir;
};
void main() {
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);
pos = vec2(aCoord.x * viewport.x / viewport.y, aCoord.y);
}

Binary file not shown.