|
|
@ -10,6 +10,7 @@ const createAndWriteBuffer = Renderer.createAndWriteBuffer; |
|
|
|
/// Describes the layout of each vertex that a primitive is made of. |
|
|
|
/// Describes the layout of each vertex that a primitive is made of. |
|
|
|
pub const VertexData = struct { |
|
|
|
pub const VertexData = struct { |
|
|
|
position: [3]f32, |
|
|
|
position: [3]f32, |
|
|
|
|
|
|
|
uv: [2]f32, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/// Contains the data to render a primitive (3D shape or model). |
|
|
|
/// Contains the data to render a primitive (3D shape or model). |
|
|
@ -43,8 +44,8 @@ pub fn createPrimitive( |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn vert(x: f32, y: f32, z: f32) VertexData { |
|
|
|
fn vert(x: f32, y: f32, z: f32, tx: f32, ty: f32) VertexData { |
|
|
|
return .{ .position = .{ x, y, z } }; |
|
|
|
return .{ .position = .{ x, y, z }, .uv = .{ tx, ty } }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn createTrianglePrimitive(length: f32) PrimitiveData { |
|
|
|
pub fn createTrianglePrimitive(length: f32) PrimitiveData { |
|
|
@ -60,9 +61,9 @@ pub fn createTrianglePrimitive(length: f32) PrimitiveData { |
|
|
|
// / \ |
|
|
|
// / \ |
|
|
|
// 1-----2 |
|
|
|
// 1-----2 |
|
|
|
&.{ |
|
|
|
&.{ |
|
|
|
vert(@sin(a0) * radius, @cos(a0) * radius, 0.0), |
|
|
|
vert(@sin(a0) * radius, @cos(a0) * radius, 0.0, 0.5, 0.0), |
|
|
|
vert(@sin(a1) * radius, @cos(a1) * radius, 0.0), |
|
|
|
vert(@sin(a1) * radius, @cos(a1) * radius, 0.0, 0.0, 1.0), |
|
|
|
vert(@sin(a2) * radius, @cos(a2) * radius, 0.0), |
|
|
|
vert(@sin(a2) * radius, @cos(a2) * radius, 0.0, 1.0, 1.0), |
|
|
|
}, |
|
|
|
}, |
|
|
|
// Vertices have to be specified in counter-clockwise, |
|
|
|
// Vertices have to be specified in counter-clockwise, |
|
|
|
// so the "front" of the triangle is facing the right way. |
|
|
|
// so the "front" of the triangle is facing the right way. |
|
|
@ -83,10 +84,10 @@ pub fn createSquarePrimitive(width: f32) PrimitiveData { |
|
|
|
// 1---3 |
|
|
|
// 1---3 |
|
|
|
&.{ |
|
|
|
&.{ |
|
|
|
// zig fmt: off |
|
|
|
// zig fmt: off |
|
|
|
vert(-half_width, -half_width, 0.0), |
|
|
|
vert(-half_width, -half_width, 0.0, 0.0, 0.0), |
|
|
|
vert(-half_width, half_width, 0.0), |
|
|
|
vert(-half_width, half_width, 0.0, 0.0, 1.0), |
|
|
|
vert( half_width, -half_width, 0.0), |
|
|
|
vert( half_width, -half_width, 0.0, 1.0, 0.0), |
|
|
|
vert( half_width, half_width, 0.0), |
|
|
|
vert( half_width, half_width, 0.0, 1.0, 1.0), |
|
|
|
// zig fmt: on |
|
|
|
// zig fmt: on |
|
|
|
}, |
|
|
|
}, |
|
|
|
// ... but it has to be split up into 2 triangles. |
|
|
|
// ... but it has to be split up into 2 triangles. |
|
|
@ -127,35 +128,35 @@ pub fn createCubePrimitive(width: f32) PrimitiveData { |
|
|
|
// zig fmt: off |
|
|
|
// zig fmt: off |
|
|
|
&.{ |
|
|
|
&.{ |
|
|
|
// Right (+X) |
|
|
|
// Right (+X) |
|
|
|
vert( half_width, half_width, -half_width), |
|
|
|
vert( half_width, half_width, -half_width, 0.0, 0.0), |
|
|
|
vert( half_width, -half_width, -half_width), |
|
|
|
vert( half_width, -half_width, -half_width, 0.0, 1.0), |
|
|
|
vert( half_width, half_width, half_width), |
|
|
|
vert( half_width, half_width, half_width, 1.0, 0.0), |
|
|
|
vert( half_width, -half_width, half_width), |
|
|
|
vert( half_width, -half_width, half_width, 1.0, 1.0), |
|
|
|
// Left (-X) |
|
|
|
// Left (-X) |
|
|
|
vert(-half_width, half_width, half_width), |
|
|
|
vert(-half_width, half_width, half_width, 0.0, 0.0), |
|
|
|
vert(-half_width, -half_width, half_width), |
|
|
|
vert(-half_width, -half_width, half_width, 0.0, 1.0), |
|
|
|
vert(-half_width, half_width, -half_width), |
|
|
|
vert(-half_width, half_width, -half_width, 1.0, 0.0), |
|
|
|
vert(-half_width, -half_width, -half_width), |
|
|
|
vert(-half_width, -half_width, -half_width, 1.0, 1.0), |
|
|
|
// Top (+Y) |
|
|
|
// Top (+Y) |
|
|
|
vert( half_width, half_width, -half_width), |
|
|
|
vert( half_width, half_width, -half_width, 0.0, 0.0), |
|
|
|
vert( half_width, half_width, half_width), |
|
|
|
vert( half_width, half_width, half_width, 0.0, 1.0), |
|
|
|
vert(-half_width, half_width, -half_width), |
|
|
|
vert(-half_width, half_width, -half_width, 1.0, 0.0), |
|
|
|
vert(-half_width, half_width, half_width), |
|
|
|
vert(-half_width, half_width, half_width, 1.0, 1.0), |
|
|
|
// Bottom (-Y) |
|
|
|
// Bottom (-Y) |
|
|
|
vert(-half_width, -half_width, -half_width), |
|
|
|
vert(-half_width, -half_width, -half_width, 1.0, 0.0), |
|
|
|
vert(-half_width, -half_width, half_width), |
|
|
|
vert(-half_width, -half_width, half_width, 0.0, 0.0), |
|
|
|
vert( half_width, -half_width, -half_width), |
|
|
|
vert( half_width, -half_width, -half_width, 1.0, 1.0), |
|
|
|
vert( half_width, -half_width, half_width), |
|
|
|
vert( half_width, -half_width, half_width, 0.0, 1.0), |
|
|
|
// Front (+Z) |
|
|
|
// Front (+Z) |
|
|
|
vert( half_width, half_width, half_width), |
|
|
|
vert( half_width, half_width, half_width, 0.0, 0.0), |
|
|
|
vert( half_width, -half_width, half_width), |
|
|
|
vert( half_width, -half_width, half_width, 0.0, 1.0), |
|
|
|
vert(-half_width, half_width, half_width), |
|
|
|
vert(-half_width, half_width, half_width, 1.0, 0.0), |
|
|
|
vert(-half_width, -half_width, half_width), |
|
|
|
vert(-half_width, -half_width, half_width, 1.0, 1.0), |
|
|
|
// Back (-Z) |
|
|
|
// Back (-Z) |
|
|
|
vert(-half_width, half_width, -half_width), |
|
|
|
vert(-half_width, half_width, -half_width, 0.0, 0.0), |
|
|
|
vert(-half_width, -half_width, -half_width), |
|
|
|
vert(-half_width, -half_width, -half_width, 0.0, 1.0), |
|
|
|
vert( half_width, half_width, -half_width), |
|
|
|
vert( half_width, half_width, -half_width, 1.0, 0.0), |
|
|
|
vert( half_width, -half_width, -half_width), |
|
|
|
vert( half_width, -half_width, -half_width, 1.0, 1.0), |
|
|
|
}, |
|
|
|
}, |
|
|
|
&.{ |
|
|
|
&.{ |
|
|
|
0, 1, 2, 3, 2, 1, // Right |
|
|
|
0, 1, 2, 3, 2, 1, // Right |
|
|
@ -175,26 +176,26 @@ pub fn createPyramidPrimitive(width: f32) PrimitiveData { |
|
|
|
// zig fmt: off |
|
|
|
// zig fmt: off |
|
|
|
&.{ |
|
|
|
&.{ |
|
|
|
// Right |
|
|
|
// Right |
|
|
|
vert( 0.0, half_width, 0.0), |
|
|
|
vert( 0.0, half_width, 0.0, 0.5, 0.0), |
|
|
|
vert( half_width, -half_width, -half_width), |
|
|
|
vert( half_width, -half_width, -half_width, 0.0, 1.0), |
|
|
|
vert( half_width, -half_width, half_width), |
|
|
|
vert( half_width, -half_width, half_width, 1.0, 1.0), |
|
|
|
// Left |
|
|
|
// Left |
|
|
|
vert( 0.0, half_width, 0.0), |
|
|
|
vert( 0.0, half_width, 0.0, 0.5, 0.0), |
|
|
|
vert(-half_width, -half_width, half_width), |
|
|
|
vert(-half_width, -half_width, half_width, 0.0, 1.0), |
|
|
|
vert(-half_width, -half_width, -half_width), |
|
|
|
vert(-half_width, -half_width, -half_width, 1.0, 1.0), |
|
|
|
// Front |
|
|
|
// Front |
|
|
|
vert( 0.0, half_width, 0.0), |
|
|
|
vert( 0.0, half_width, 0.0, 0.5, 0.0), |
|
|
|
vert( half_width, -half_width, half_width), |
|
|
|
vert( half_width, -half_width, half_width, 0.0, 1.0), |
|
|
|
vert(-half_width, -half_width, half_width), |
|
|
|
vert(-half_width, -half_width, half_width, 1.0, 1.0), |
|
|
|
// Back |
|
|
|
// Back |
|
|
|
vert( 0.0, half_width, 0.0), |
|
|
|
vert( 0.0, half_width, 0.0, 0.5, 0.0), |
|
|
|
vert(-half_width, -half_width, -half_width), |
|
|
|
vert(-half_width, -half_width, -half_width, 0.0, 1.0), |
|
|
|
vert( half_width, -half_width, -half_width), |
|
|
|
vert( half_width, -half_width, -half_width, 1.0, 1.0), |
|
|
|
// Bottom |
|
|
|
// Bottom |
|
|
|
vert(-half_width, -half_width, -half_width), |
|
|
|
vert(-half_width, -half_width, -half_width, 0.0, 0.0), |
|
|
|
vert(-half_width, -half_width, half_width), |
|
|
|
vert(-half_width, -half_width, half_width, 0.0, 1.0), |
|
|
|
vert( half_width, -half_width, -half_width), |
|
|
|
vert( half_width, -half_width, -half_width, 1.0, 0.0), |
|
|
|
vert( half_width, -half_width, half_width), |
|
|
|
vert( half_width, -half_width, half_width, 1.0, 1.0), |
|
|
|
}, |
|
|
|
}, |
|
|
|
&.{ |
|
|
|
&.{ |
|
|
|
0, 1, 2, // Right |
|
|
|
0, 1, 2, // Right |
|
|
|