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