renderer-mvp/post-processing #56
70
src/main.zig
70
src/main.zig
|
@ -6,8 +6,9 @@ const ona = @import("ona");
|
|||
|
||||
const Actors = struct {
|
||||
instances: coral.stack.Sequential(ona.gfx.Point2D) = .{.allocator = coral.heap.allocator},
|
||||
quad_mesh_2d: ona.gfx.Handle = .none,
|
||||
body_texture: ona.gfx.Handle = .none,
|
||||
quad_mesh_2d: ?*ona.gfx.Handle = null,
|
||||
body_texture: ?*ona.gfx.Handle = null,
|
||||
render_texture: ?*ona.gfx.Handle = null,
|
||||
};
|
||||
|
||||
const Player = struct {
|
||||
|
@ -24,8 +25,21 @@ pub fn main() !void {
|
|||
|
||||
fn load(display: coral.Write(ona.gfx.Display), actors: coral.Write(Actors), assets: coral.Write(ona.gfx.Assets)) !void {
|
||||
display.res.width, display.res.height = .{1280, 720};
|
||||
actors.res.body_texture = try assets.res.open_file(coral.files.bundle, "actor.bmp");
|
||||
actors.res.quad_mesh_2d = try assets.res.open_quad_mesh_2d(@splat(1));
|
||||
actors.res.body_texture = try assets.res.create_from_file(coral.files.bundle, "actor.bmp");
|
||||
actors.res.quad_mesh_2d = try assets.res.create_quad_mesh_2d(@splat(1));
|
||||
|
||||
actors.res.render_texture = try assets.res.context.create(.{
|
||||
.texture = .{
|
||||
.format = .rgba8,
|
||||
|
||||
.access = .{
|
||||
.render = .{
|
||||
.width = display.res.width,
|
||||
.height = display.res.height,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
try actors.res.instances.push_grow(.{0, 0});
|
||||
}
|
||||
|
@ -34,27 +48,47 @@ fn exit(actors: coral.Write(Actors)) void {
|
|||
actors.res.instances.deinit();
|
||||
}
|
||||
|
||||
fn render(queue: ona.gfx.Queue, actors: coral.Write(Actors)) !void {
|
||||
for (actors.res.instances.values) |instance| {
|
||||
try queue.commands.append(.{
|
||||
.instance_2d = .{
|
||||
.mesh_2d = actors.res.quad_mesh_2d,
|
||||
.texture = actors.res.body_texture,
|
||||
fn render(queue: ona.gfx.Queue, actors: coral.Write(Actors), display: coral.Read(ona.gfx.Display)) !void {
|
||||
try queue.commands.append(.{.target = .{
|
||||
.texture = actors.res.render_texture.?,
|
||||
.clear_color = .{0, 0, 0, 0},
|
||||
.clear_depth = 0,
|
||||
}});
|
||||
|
||||
.transform = .{
|
||||
.origin = instance,
|
||||
.xbasis = .{64, 0},
|
||||
.ybasis = .{0, 64},
|
||||
},
|
||||
for (actors.res.instances.values) |instance| {
|
||||
try queue.commands.append(.{.instance_2d = .{
|
||||
.mesh_2d = actors.res.quad_mesh_2d.?,
|
||||
.texture = actors.res.body_texture.?,
|
||||
|
||||
.transform = .{
|
||||
.origin = instance,
|
||||
.xbasis = .{64, 0},
|
||||
.ybasis = .{0, 64},
|
||||
},
|
||||
});
|
||||
}});
|
||||
}
|
||||
|
||||
try queue.commands.append(.{.target = .{
|
||||
.clear_color = null,
|
||||
.clear_depth = null,
|
||||
}});
|
||||
|
||||
try queue.commands.append(.{.instance_2d = .{
|
||||
.mesh_2d = actors.res.quad_mesh_2d.?,
|
||||
.texture = actors.res.render_texture.?,
|
||||
|
||||
.transform = .{
|
||||
.origin = .{@floatFromInt(display.res.width / 2), @floatFromInt(display.res.height / 2)},
|
||||
.xbasis = .{@floatFromInt(display.res.width), 0},
|
||||
.ybasis = .{0, @floatFromInt(display.res.height)},
|
||||
},
|
||||
}});
|
||||
}
|
||||
|
||||
fn update(player: coral.Read(Player), actors: coral.Write(Actors), mapping: coral.Read(ona.act.Mapping)) !void {
|
||||
actors.res.instances.values[0] += .{
|
||||
mapping.res.axis_strength(player.res.move_x),
|
||||
mapping.res.axis_strength(player.res.move_y),
|
||||
mapping.res.axis_strength(player.res.move_x) * 10,
|
||||
mapping.res.axis_strength(player.res.move_y) * 10,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ pub const Assets = struct {
|
|||
};
|
||||
};
|
||||
|
||||
pub fn open_file(self: *Assets, storage: coral.files.Storage, path: []const u8) (OpenError || Format.Error)!Handle {
|
||||
pub fn create_from_file(self: *Assets, storage: coral.files.Storage, path: []const u8) (OpenError || Format.Error)!*Handle {
|
||||
defer {
|
||||
const max_cache_size = 536870912;
|
||||
|
||||
|
@ -40,16 +40,16 @@ pub const Assets = struct {
|
|||
continue;
|
||||
}
|
||||
|
||||
return self.context.open(try format.file_desc(&self.staging_arena, storage, path));
|
||||
return self.context.create(try format.file_desc(&self.staging_arena, storage, path));
|
||||
}
|
||||
|
||||
return .none;
|
||||
return error.FormatUnsupported;
|
||||
}
|
||||
|
||||
pub fn open_quad_mesh_2d(self: *Assets, extents: Point2D) OpenError!Handle {
|
||||
pub fn create_quad_mesh_2d(self: *Assets, extents: Point2D) OpenError!*Handle {
|
||||
const width, const height = extents / @as(Point2D, @splat(2));
|
||||
|
||||
return self.context.open(.{
|
||||
return self.context.create(.{
|
||||
.mesh_2d = .{
|
||||
.indices = &.{0, 1, 2, 0, 2, 3},
|
||||
|
||||
|
@ -66,6 +66,23 @@ pub const Assets = struct {
|
|||
|
||||
pub const Color = @Vector(4, f32);
|
||||
|
||||
pub const Command = union (enum) {
|
||||
instance_2d: Instance2D,
|
||||
target: Target,
|
||||
|
||||
pub const Instance2D = struct {
|
||||
texture: *Handle,
|
||||
mesh_2d: *Handle,
|
||||
transform: Transform2D,
|
||||
};
|
||||
|
||||
pub const Target = struct {
|
||||
texture: ?*Handle = null,
|
||||
clear_color: ?Color,
|
||||
clear_depth: ?f32,
|
||||
};
|
||||
};
|
||||
|
||||
pub const Desc = union (enum) {
|
||||
texture: Texture,
|
||||
mesh_2d: Mesh2D,
|
||||
|
@ -81,13 +98,19 @@ pub const Desc = union (enum) {
|
|||
};
|
||||
|
||||
pub const Texture = struct {
|
||||
data: []const coral.io.Byte,
|
||||
width: u16,
|
||||
format: Format,
|
||||
access: Access,
|
||||
|
||||
pub const Access = enum {
|
||||
static,
|
||||
pub const Access = union (enum) {
|
||||
static: struct {
|
||||
width: u16,
|
||||
data: []const coral.io.Byte,
|
||||
},
|
||||
|
||||
render: struct {
|
||||
width: u16,
|
||||
height: u16,
|
||||
},
|
||||
};
|
||||
|
||||
pub const Format = enum {
|
||||
|
@ -109,17 +132,7 @@ pub const Display = struct {
|
|||
clear_color: Color = colors.black,
|
||||
};
|
||||
|
||||
pub const Handle = enum (usize) {
|
||||
none,
|
||||
_,
|
||||
|
||||
pub fn index(self: Handle) ?usize {
|
||||
return switch (self) {
|
||||
.none => null,
|
||||
_ => @intFromEnum(self) - 1,
|
||||
};
|
||||
}
|
||||
};
|
||||
pub const Handle = opaque {};
|
||||
|
||||
pub const Input = union (enum) {
|
||||
key_up: Key,
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -72,10 +72,14 @@ pub fn bmp_file_desc(
|
|||
|
||||
return .{
|
||||
.texture = .{
|
||||
.width = pixel_width,
|
||||
.data = pixels,
|
||||
.format = .rgba8,
|
||||
.access = .static,
|
||||
|
||||
.access = .{
|
||||
.static = .{
|
||||
.width = pixel_width,
|
||||
.data = pixels,
|
||||
},
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
@header const Vec2 = @Vector(2, f32)
|
||||
@header pub const Vec2 = @Vector(2, f32)
|
||||
@header pub const Mat4 = [4]@Vector(4, f32)
|
||||
@ctype vec2 Vec2
|
||||
@ctype mat4 Mat4
|
||||
|
||||
@vs vs
|
||||
in vec2 mesh_xy;
|
||||
|
@ -12,8 +14,8 @@ in vec4 instance_tint;
|
|||
in float instance_depth;
|
||||
in vec4 instance_rect;
|
||||
|
||||
uniform Screen {
|
||||
vec2 screen_size;
|
||||
uniform Projection {
|
||||
mat4 projection;
|
||||
};
|
||||
|
||||
out vec4 color;
|
||||
|
@ -22,13 +24,10 @@ out vec2 uv;
|
|||
void main() {
|
||||
// Calculate the world position of the vertex
|
||||
const vec2 world_position = instance_origin + mesh_xy.x * instance_xbasis + mesh_xy.y * instance_ybasis;
|
||||
|
||||
// Convert world position to normalized device coordinates (NDC)
|
||||
// Assuming the screen coordinates range from (0, 0) to (screen_size.x, screen_size.y)
|
||||
const vec2 ndc_position = (vec2(world_position.x, -world_position.y) / screen_size) * 2.0 - vec2(1.0, -1.0);
|
||||
const vec2 projected_position = (projection * vec4(world_position, 0, 1)).xy;
|
||||
|
||||
// Set the position of the vertex in clip space
|
||||
gl_Position = vec4(ndc_position, instance_depth, 1.0);
|
||||
gl_Position = vec4(projected_position, instance_depth, 1.0);
|
||||
color = instance_tint;
|
||||
|
||||
// Calculate the width and height from left, top, right, bottom configuration
|
Loading…
Reference in New Issue