Fixed index generation issue, debugging tessellation issue

This commit is contained in:
Conál 2024-02-05 12:26:00 -05:00
parent 8e2559cf26
commit 9582baf2d2
14 changed files with 34 additions and 18 deletions

View File

@ -339,7 +339,8 @@ void Renderer::draw() {
uniform_buffer->upload(UniformData {
.mvp = p * cam.view(),
.view = cam.view(),
.proj = p,
.time = time,
.cam_pos = cam.pos,
.viewport = glm::vec2(viewport.width, viewport.y),

View File

@ -73,6 +73,6 @@ struct Renderer {
float speed = 1.0;
bool running = true;
float tess_factor;
float tess_factor = 5.0f;
float tess_edge_size = 20.0f;
};

View File

@ -10,7 +10,8 @@
#include <memory>
struct UniformData {
glm::mat4 mvp;
glm::mat4 view;
glm::mat4 proj;
float time;
glm::vec3 cam_pos;
glm::vec2 viewport;

View File

@ -27,7 +27,7 @@ struct Texture {
.binding = binding,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment,
.stageFlags = vk::ShaderStageFlagBits::eFragment | vk::ShaderStageFlagBits::eTessellationEvaluation,
.pImmutableSamplers = nullptr,
};
}

View File

@ -33,7 +33,7 @@ Terrain::Terrain(vk::PhysicalDevice phys_dev, vk::Device dev, Texture& tex) : ph
.pos = glm::vec3(
2.0f * x + 1.0f - patch_size,
0.0f,
2.0f * y * 1.0f - patch_size),
2.0f * y + 1.0f - patch_size),
.uv = glm::vec2(static_cast<float>(x)/patch_size, static_cast<float>(y) / patch_size) * uv_scale,
});
@ -88,7 +88,7 @@ Terrain::Terrain(vk::PhysicalDevice phys_dev, vk::Device dev, Texture& tex) : ph
for (auto x = 0_u32; x < w; x++)
for (auto y = 0_u32; y < w; y++) {
auto idx = x + y * w * 4;
auto idx = (x + y * w) * 4;
indices[idx] = x+y*patch_size;
indices[idx+1] = indices[idx] + patch_size;
indices[idx+2] = indices[idx+1] + 1;

View File

@ -92,8 +92,8 @@ void UI::newFrame() {
ImGui::Text("FPS: %f", info.fps);
ImGui::Text("Time: %f", info.time);
ImGui::Checkbox("Fly Camera", &info.flycam);
ImGui::SliderFloat("Tessellation Factor: %f", &info.tess_factor, 0.0, 20.0);
ImGui::SliderFloat("Edge Size: %f", &info.tess_edge_size, 0.0, 40.0);
ImGui::SliderFloat("Tessellation Factor", &info.tess_factor, 0.0, 20.0);
ImGui::SliderFloat("Edge Size", &info.tess_edge_size, 0.0, 40.0);
ImGui::End();
}

View File

@ -6,9 +6,13 @@ layout (location = 2) in vec3 pos;
layout (location = 0) out vec4 FragColor;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
mat4 view;
mat4 proj;
float time;
vec3 cam_pos;
vec2 viewport;
float tess_factor;
float tess_edge_size;
};
layout (set = 0, binding = 1) uniform sampler2D tex;

Binary file not shown.

View File

@ -1,7 +1,8 @@
#version 450 core
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
mat4 view;
mat4 proj;
float time;
vec3 cam_pos;
vec2 viewport;
@ -24,14 +25,14 @@ layout (location = 2) out vec3 _pos[4];
float screen_space_tess(vec4 p0, vec4 p1) {
/* calc midpoint + distance btw the two points */
vec4 mid = (p0+p1) / 2.0;
vec4 mid = (p0+p1) * 0.5;
float r = distance(p0, p1) / 2.0;
vec4 v0 = mvp * mid;
vec4 v0 = view * mid;
/* projevt into clip spaace */
vec4 clip0 = mvp * (v0 - vec4(r, 0.0, 0.0, 0.0));
vec4 clip1 = mvp * (v0 + vec4(r, 0.0, 0.0, 0.0));
vec4 clip0 = proj * (v0 - vec4(r, 0.0, 0.0, 0.0));
vec4 clip1 = proj * (v0 + vec4(r, 0.0, 0.0, 0.0));
clip0 /= clip0.w;
clip1 /= clip1.w;

Binary file not shown.

View File

@ -1,7 +1,8 @@
#version 450 core
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
mat4 view;
mat4 proj;
float time;
vec3 cam_pos;
vec2 viewport;
@ -36,7 +37,10 @@ void main() {
/* not displacing yet */
vec4 pos1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
_pos = mix(pos1, pos2, gl_TessCoord.y).xyz;
gl_Position = mvp * mix(pos1, pos2, gl_TessCoord.y);
vec4 fpos = mix(pos1, pos2, gl_TessCoord.y);
fpos.y += textureLod(heightmap, _texCoord, 0.0).r;
_pos = fpos.xyz;
gl_Position = proj * view * fpos;
}

Binary file not shown.

View File

@ -8,8 +8,13 @@ layout (location = 1) out vec2 texCoord;
layout (location = 2) out vec3 pos;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
mat4 view;
mat4 proj;
float time;
vec3 cam_pos;
vec2 viewport;
float tess_factor;
float tess_edge_size;
};
void main() {

Binary file not shown.