examples/gkurve: calculate barycentric vertex coordinates in shader

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-05-14 16:26:39 -07:00 committed by Stephen Gutekanst
parent dae283734f
commit e35b86ad25
2 changed files with 10 additions and 8 deletions

View file

@ -12,13 +12,12 @@ const glfw = @import("glfw");
pub const Vertex = struct { pub const Vertex = struct {
pos: @Vector(4, f32), pos: @Vector(4, f32),
uv: @Vector(2, f32), uv: @Vector(2, f32),
bary: @Vector(3, f32) = .{ 0, 0, 0 },
}; };
// Simple triangle // Simple triangle
pub const vertices = [_]Vertex{ pub const vertices = [_]Vertex{
.{ .pos = .{ 0, 250, 0, 1 }, .uv = .{ 0.5, 1 }, .bary = .{ 0, 0, 1 } }, .{ .pos = .{ 0, 250, 0, 1 }, .uv = .{ 0.5, 1 } },
.{ .pos = .{ -250, -250, 0, 1 }, .uv = .{ 0, 0 }, .bary = .{ 1, 0, 0 } }, .{ .pos = .{ -250, -250, 0, 1 }, .uv = .{ 0, 0 } },
.{ .pos = .{ 250, -250, 0, 1 }, .uv = .{ 1, 0 }, .bary = .{ 0, 1, 0 } }, .{ .pos = .{ 250, -250, 0, 1 }, .uv = .{ 1, 0 } },
}; };
// TODO: Need to ask Ayush about this, ideally we have a square window in this example because it // TODO: Need to ask Ayush about this, ideally we have a square window in this example because it
@ -73,7 +72,6 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
const vertex_attributes = [_]gpu.VertexAttribute{ const vertex_attributes = [_]gpu.VertexAttribute{
.{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 }, .{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 },
.{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 }, .{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 },
.{ .format = .float32x3, .offset = @offsetOf(Vertex, "bary"), .shader_location = 2 },
}; };
const vertex_buffer_layout = gpu.VertexBufferLayout{ const vertex_buffer_layout = gpu.VertexBufferLayout{
.array_stride = @sizeOf(Vertex), .array_stride = @sizeOf(Vertex),

View file

@ -12,14 +12,18 @@ struct VertexOut {
@stage(vertex) fn main( @stage(vertex) fn main(
@builtin(instance_index) instanceIdx: u32, @builtin(instance_index) instanceIdx: u32,
@builtin(vertex_index) vertex_index: u32,
@location(0) position: vec4<f32>, @location(0) position: vec4<f32>,
@location(1) uv: vec2<f32>, @location(1) uv: vec2<f32>,
@location(2) bary: vec3<f32>,
) -> VertexOut { ) -> VertexOut {
var output : VertexOut; var output : VertexOut;
output.position_clip = ubos[instanceIdx].matrix * position; output.position_clip = ubos[instanceIdx].matrix * position;
output.frag_uv = uv; output.frag_uv = uv;
output.frag_bary = bary; output.frag_bary = vec3<f32>(
f32(vertex_index % 3u == 1u),
f32(vertex_index % 3u == 2u),
f32(vertex_index % 3u == 0u),
);
output.instance_index = instanceIdx; output.instance_index = instanceIdx;
return output; return output;
} }