Implemented simple terrain generation

This commit is contained in:
Lorenzo Torres 2025-08-13 21:16:42 +02:00
parent fb9607b2b1
commit c0b8d021d4
16 changed files with 825 additions and 32 deletions

View file

@ -16,11 +16,14 @@ device: vk.Device,
render_pass: vk.RenderPass,
swapchain: vk.Swapchain,
graphics_pipeline: vk.GraphicsPipeline,
terrain_pipeline: vk.TerrainPipeline,
current_frame: u32,
mesh: Mesh,
transforms: std.ArrayList(math.Transform),
previous_time: std.time.Instant,
current_image: usize,
terrain_vertex: vk.Buffer,
terrain_index: vk.Buffer,
pub fn init(allocator: Allocator, instance_handle: vk.c.VkInstance, surface_handle: vk.c.VkSurfaceKHR) !Self {
const instance: vk.Instance = .{ .handle = instance_handle };
@ -41,13 +44,32 @@ pub fn init(allocator: Allocator, instance_handle: vk.c.VkInstance, surface_hand
const mesh = try pipeline_builder.addMesh("assets/models/cube.glb");
var graphics_pipeline = try pipeline_builder.build(swapchain, render_pass, vertex_shader, fragment_shader);
// TODO: I think the renderer shouldn't have to interact with buffers. I think the API should change to
// something along the lines of
// renderer.begin()
// renderer.render(triangle);
// renderer.render(some_other_thing);
// ...
// renderer.submit()
const terrain_vertex_shader = try device.initShader("terrain_vert");
defer device.deinitShader(terrain_vertex_shader);
const terrain_fragment_shader = try device.initShader("terrain_frag");
defer device.deinitShader(terrain_fragment_shader);
var terrain_pipeline = try vk.TerrainPipeline.init(graphics_pipeline, terrain_vertex_shader, terrain_fragment_shader);
const perlin: math.PerlinNoise = .{ .seed = 54321 };
const heightmap = try allocator.alloc(u32, 700 * 700);
defer allocator.free(heightmap);
for (0..700) |x| {
for (0..700) |y| {
const scale = 0.01;
var pixel = (perlin.fbm(@as(f64, @floatFromInt(x)) * scale, @as(f64, @floatFromInt(y)) * scale, 8, 3, 0.5) * 3);
pixel = std.math.pow(f64, pixel, 1.2);
const gray: u32 = @intFromFloat(pixel * 255);
const color: u32 = (255 << 24) | (gray << 16) | (gray << 8) | gray;
heightmap[x*700 + y] = color;
}
}
const terrain_vertex, const terrain_index = try Mesh.terrain(allocator, device, 700, 700, 10);
const heightmap_texture = try Texture.fromBytes(@alignCast(@ptrCast(heightmap)), device, 700, 700);
//const heightmap_texture = try Texture.init("assets/textures/heightmap.png", device);
try terrain_pipeline.setHeightmap(device, heightmap_texture);
const texture = try Texture.init("assets/textures/container.png", device);
const diffuse = try Texture.init("assets/textures/container_specular.png", device);
@ -79,6 +101,7 @@ pub fn init(allocator: Allocator, instance_handle: vk.c.VkInstance, surface_hand
try transforms.append(math.Transform.init(.{0.0, 0.5, 1.0}, .{0.5, 0.5, 0.5}, .{0.0, 0.0, 0.0}));
try transforms.append(math.Transform.init(.{0.0, 0.0, 0.0}, .{0.5, 0.5, 0.5}, .{0.0, 0.0, 0.0}));
return .{
.instance = instance,
.surface = surface,
@ -87,11 +110,14 @@ pub fn init(allocator: Allocator, instance_handle: vk.c.VkInstance, surface_hand
.render_pass = render_pass,
.swapchain = swapchain,
.graphics_pipeline = graphics_pipeline,
.terrain_pipeline = terrain_pipeline,
.current_frame = 0,
.transforms = transforms,
.previous_time = try std.time.Instant.now(),
.mesh = mesh,
.current_image = undefined,
.terrain_vertex = terrain_vertex,
.terrain_index = terrain_index,
};
}
@ -121,11 +147,19 @@ pub fn begin(self: *Self) !void {
try self.device.resetCommand(self.current_frame);
try self.device.beginCommand(self.current_frame);
self.render_pass.begin(self.swapchain, self.device, image, self.current_frame);
self.current_image = image;
}
pub fn beginGraphics(self: *Self) !void {
self.graphics_pipeline.bind(self.device, self.current_frame);
self.device.bindVertexBuffer(self.graphics_pipeline.vertex_buffer, self.current_frame);
self.device.bindIndexBuffer(self.graphics_pipeline.index_buffer, self.current_frame);
self.device.bindDescriptorSets(self.graphics_pipeline, self.current_frame, 0);
self.current_image = image;
}
pub fn beginTerrain(self: *Self) !void {
self.terrain_pipeline.bind(self.device, self.current_frame);
self.device.bindTerrainSets(self.terrain_pipeline, self.current_frame);
}
pub fn setLightCount(self: *Self, count: u32) void {
@ -144,7 +178,5 @@ pub fn end(self: *Self) !void {
self.current_frame = (self.current_frame + 1) % 2;
self.device.waitIdle();
}
self.device.waitIdle();
}