Skip to content

Commit

Permalink
graphics: Implement drawCuboidPbr3D.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Jun 22, 2022
1 parent 711a93f commit 02335cd
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
111 changes: 109 additions & 2 deletions graphics/src/backend/gpu/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,111 @@ pub const Graphics = struct {
}
}

pub fn drawCuboidPbr3D(self: *Self, xform: Transform, material: graphics.Material) void {
self.batcher.beginTexPbr3D(self.white_tex, self.cur_cam_world_pos);
const cur_mvp = self.batcher.mvp;
// Create temp mvp.
const vp = self.view_transform.getAppliedTransform(self.cur_proj_transform);
self.batcher.beginMvp(vp);

// Compute normal matrix for lighting.
self.batcher.normal = xform.toRotationMat();

self.batcher.model_idx = self.batcher.mesh.cur_mats_buf_size;
self.batcher.mesh.addMatrix(xform.mat);

self.batcher.material_idx = self.batcher.mesh.cur_materials_buf_size;
self.batcher.mesh.addMaterial(material);

self.ensureUnusedBatchCapacity(8, 36);
const vert_start = self.batcher.mesh.getNextIndexId();
var vert: TexShaderVertex = undefined;
vert.setColor(Color.White);
vert.setUV(0, 0);
// far top-left
vert.setXYZ(-0.5, 0.5, -0.5);
vert.setNormal(comptime Vec3.init(-1, 1, -1).normalize());
self.batcher.mesh.addVertex(&vert);
// far top-right
vert.setXYZ(0.5, 0.5, -0.5);
vert.setNormal(comptime Vec3.init(1, 1, -1).normalize());
self.batcher.mesh.addVertex(&vert);
// far bottom-right
vert.setXYZ(0.5, -0.5, -0.5);
vert.setNormal(comptime Vec3.init(1, -1, -1).normalize());
self.batcher.mesh.addVertex(&vert);
// far bottom-left
vert.setXYZ(-0.5, -0.5, -0.5);
vert.setNormal(comptime Vec3.init(-1, -1, -1).normalize());
self.batcher.mesh.addVertex(&vert);
// near top-left
vert.setXYZ(-0.5, 0.5, 0.5);
vert.setNormal(comptime Vec3.init(-1, 1, 1).normalize());
self.batcher.mesh.addVertex(&vert);
// near top-right
vert.setXYZ(0.5, 0.5, 0.5);
vert.setNormal(comptime Vec3.init(1, 1, 1).normalize());
self.batcher.mesh.addVertex(&vert);
// near bottom-right
vert.setXYZ(0.5, -0.5, 0.5);
vert.setNormal(comptime Vec3.init(1, -1, 1).normalize());
self.batcher.mesh.addVertex(&vert);
// near bottom-left
vert.setXYZ(-0.5, -0.5, 0.5);
vert.setNormal(comptime Vec3.init(-1, -1, 1).normalize());
self.batcher.mesh.addVertex(&vert);

// far face
self.batcher.mesh.addIndex(vert_start);
self.batcher.mesh.addIndex(vert_start+1);
self.batcher.mesh.addIndex(vert_start+2);
self.batcher.mesh.addIndex(vert_start);
self.batcher.mesh.addIndex(vert_start+2);
self.batcher.mesh.addIndex(vert_start+3);

// left face
self.batcher.mesh.addIndex(vert_start);
self.batcher.mesh.addIndex(vert_start+3);
self.batcher.mesh.addIndex(vert_start+4);
self.batcher.mesh.addIndex(vert_start+4);
self.batcher.mesh.addIndex(vert_start+3);
self.batcher.mesh.addIndex(vert_start+7);

// right face
self.batcher.mesh.addIndex(vert_start+1);
self.batcher.mesh.addIndex(vert_start+5);
self.batcher.mesh.addIndex(vert_start+2);
self.batcher.mesh.addIndex(vert_start+5);
self.batcher.mesh.addIndex(vert_start+6);
self.batcher.mesh.addIndex(vert_start+2);

// near face
self.batcher.mesh.addIndex(vert_start+4);
self.batcher.mesh.addIndex(vert_start+7);
self.batcher.mesh.addIndex(vert_start+5);
self.batcher.mesh.addIndex(vert_start+5);
self.batcher.mesh.addIndex(vert_start+7);
self.batcher.mesh.addIndex(vert_start+6);

// bottom face
self.batcher.mesh.addIndex(vert_start+7);
self.batcher.mesh.addIndex(vert_start+3);
self.batcher.mesh.addIndex(vert_start+2);
self.batcher.mesh.addIndex(vert_start+2);
self.batcher.mesh.addIndex(vert_start+6);
self.batcher.mesh.addIndex(vert_start+7);

// top face
self.batcher.mesh.addIndex(vert_start);
self.batcher.mesh.addIndex(vert_start+4);
self.batcher.mesh.addIndex(vert_start+1);
self.batcher.mesh.addIndex(vert_start+1);
self.batcher.mesh.addIndex(vert_start+4);
self.batcher.mesh.addIndex(vert_start+5);

self.batcher.beginMvp(cur_mvp);
}

pub fn drawScene3D(self: *Self, xform: Transform, scene: graphics.GLTFscene) void {
for (scene.mesh_nodes) |id| {
const node = scene.nodes[id];
Expand Down Expand Up @@ -1923,12 +2028,14 @@ pub const Graphics = struct {
const cur_mvp = self.batcher.mvp;
// Create temp mvp.
const vp = self.view_transform.getAppliedTransform(self.cur_proj_transform);
const mvp = xform.getAppliedTransform(vp);
self.batcher.beginMvp(mvp);
self.batcher.beginMvp(vp);

// Compute normal matrix for lighting.
self.batcher.normal = xform.toRotationUniformScaleMat();

self.batcher.model_idx = self.batcher.mesh.cur_mats_buf_size;
self.batcher.mesh.addMatrix(xform.mat);

self.batcher.material_idx = self.batcher.mesh.cur_materials_buf_size;
self.batcher.mesh.addMaterial(mesh.material);

Expand Down
6 changes: 6 additions & 0 deletions graphics/src/backend/gpu/vertex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub const TexShaderVertex = packed struct {
self.color_a = @intToFloat(f32, color.channels.a) / 255;
}

pub fn setNormal(self: *Self, normal: stdx.math.Vec3) void {
self.norm_x = normal.x;
self.norm_y = normal.y;
self.norm_z = normal.z;
}

pub fn setUV(self: *Self, u: f32, v: f32) void {
self.uv_x = u;
self.uv_y = v;
Expand Down
15 changes: 15 additions & 0 deletions graphics/src/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,13 @@ pub const Graphics = struct {
}
}

pub fn drawCuboidPbr3D(self: *Self, xform: Transform, material: Material) void {
switch (Backend) {
.OpenGL, .Vulkan => gpu.Graphics.drawCuboidPbr3D(&self.impl, xform, material),
else => unsupported(),
}
}

pub fn drawScene3D(self: *Self, xform: Transform, scene: GLTFscene) void {
switch (Backend) {
.OpenGL, .Vulkan => gpu.Graphics.drawScene3D(&self.impl, xform, scene),
Expand Down Expand Up @@ -2101,4 +2108,12 @@ pub const Material = struct {
emissivity: f32,
roughness: f32,
metallic: f32,

pub fn initDefault() Material {
return .{
.emissivity = 0,
.metallic = 0,
.roughness = 0,
};
}
};

0 comments on commit 02335cd

Please sign in to comment.