1
0

to hell with bloom, too complicated.

This commit is contained in:
Rokas Puzonas 2024-01-14 20:38:37 +02:00
parent 74c38c8af8
commit c4c28f3b7a
8 changed files with 25 additions and 5249 deletions

View File

@ -19,10 +19,6 @@ pub fn build(b: *std.Build) !void {
.source_file = .{ .path = "libs/zgltf/src/main.zig" },
}));
exe.addAnonymousModule("opengl", .{
.source_file = .{ .path = "libs/gl3v3.zig" }
});
// Provide filenames of all files in 'src/ROMs' to program as options
{
var files = std.ArrayList([]const u8).init(b.allocator);
@ -41,7 +37,7 @@ pub fn build(b: *std.Build) !void {
exe.addOptions("options", options);
}
raylib.addTo(b, exe, target, optimize);
raylib.addTo(b, exe, target, optimize, .{});
{
var build_models_step = b.step("models", "Export .blend files");

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@ const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const StringList = std.ArrayList([]const u8);
const max_lights: u32 = 3;
const max_lights: u32 = 2;
allocator: Allocator,
@ -33,6 +33,7 @@ raylib_chip: *RaylibChip,
chip_sound: rl.Sound,
screen_texture: rl.RenderTexture2D,
rom: ?ROM = null,
camera: rl.Camera3D,
pub fn genSinWave(wave: *rl.Wave, frequency: f32) void {
assert(wave.sampleSize == 16); // Only 16 bits are supported
@ -157,8 +158,6 @@ pub fn init(allocator: Allocator) !Self {
errdefer emulator.deinit();
emulator.setShader(shader);
lights[2].set_point(rl.GREEN, emulator.get_power_light_position(), 0.3);
return Self {
.allocator = allocator,
.emulator = emulator,
@ -169,7 +168,14 @@ pub fn init(allocator: Allocator) !Self {
.raylib_chip = raylib_chip,
.chip_sound = chip_sound,
.screen_texture = screen_texture,
.powered = powered
.powered = powered,
.camera = rl.Camera3D{
.position = rl.Vector3.new(0, 0, -10),
.target = rl.Vector3.new(0.0, 0.0, 0.0),
.up = rl.Vector3.new(0.0, 1.0, 0.0),
.fovy = 45.0,
.projection = rl.CameraProjection.CAMERA_PERSPECTIVE,
},
};
}
@ -213,7 +219,7 @@ pub fn toggle_power(self: *Self) void {
fn updateCamera(self: *Self, dt: f32) void {
const mouse_delta = rl.GetMouseDelta();
const camera = &self.ctx.camera;
const camera = &self.camera;
const emulator = &self.emulator;
if (rl.IsWindowResized()) {
@ -288,11 +294,11 @@ fn updateCamera(self: *Self, dt: f32) void {
pub fn update(self: *Self, dt: f32) void {
self.updateCamera(dt);
const camera = &self.ctx.camera;
const camera = &self.camera;
const cameraPos = [3]f32{ camera.position.x, camera.position.y, camera.position.z };
rl.SetShaderValue(self.shader, self.shader.locs.?[@intFromEnum(rl.ShaderLocationIndex.SHADER_LOC_VECTOR_VIEW)], &cameraPos, .SHADER_UNIFORM_VEC3);
const ray = rl.GetMouseRay(rl.GetMousePosition(), self.ctx.camera);
const ray = rl.GetMouseRay(rl.GetMousePosition(), self.camera);
if (self.emulator.isOverPowerSwitch(ray)) {
if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_LEFT)) {
self.toggle_power();
@ -304,8 +310,6 @@ pub fn update(self: *Self, dt: f32) void {
self.raylib_chip.update(dt);
}
self.lights[2].set_enabled(self.powered.*);
rl.BeginTextureMode(self.screen_texture);
{
self.raylib_chip.render();
@ -362,18 +366,13 @@ pub fn update(self: *Self, dt: f32) void {
}
pub fn draw(self: *Self) void {
const emissiveLoc = rl.GetShaderLocation(self.shader, "emissive");
const enabled = true;
const disabled = false;
rl.ClearBackground(rl.Color{ .r = 33, .g = 33, .b = 33 });
rl.BeginShaderMode(self.shader);
{
rl.BeginMode3D(self.camera);
self.emulator.draw();
rl.SetShaderValue(self.shader, emissiveLoc, &enabled, .SHADER_UNIFORM_INT);
rl.DrawSphere(self.emulator.power_light_position, 0.015, rl.RED);
rl.rlDrawRenderBatchActive();
rl.SetShaderValue(self.shader, emissiveLoc, &disabled, .SHADER_UNIFORM_INT);
rl.EndMode3D();
}
rl.EndShaderMode();
}

View File

@ -8,8 +8,6 @@ const RaylibChip = @import("raylib-chip.zig");
const MainScene = @import("./main-scene.zig");
const listROMs = @import("./roms.zig").listROMs;
const TestScene = @import("test-scene.zig");
fn megabytes(amount: usize) usize {
return amount * 1024 * 1024;
}
@ -30,11 +28,11 @@ pub fn main() anyerror!void {
rl.SetTargetFPS(60);
var scene = try TestScene.init(allocator);
var scene = try MainScene.init(allocator);
defer scene.deinit();
// const roms = comptime listROMs();
// scene.set_rom(roms[3]);
const roms = comptime listROMs();
scene.set_rom(roms[3]);
const font_size = 24;
const font_ttf_default_numchars = 95; // TTF font generation default charset: 95 glyphs (ASCII 32..126)
@ -44,6 +42,9 @@ pub fn main() anyerror!void {
while (!rl.WindowShouldClose()) {
var dt = rl.GetFrameTime();
scene.update(dt);
rl.BeginDrawing();
scene.draw();
rl.EndDrawing();
}
}

View File

@ -89,7 +89,7 @@ pub fn render(self: *const Self) void {
for (0..self.chip.display_height) |y| {
for (0..self.chip.display_width) |x| {
if (self.chip.display_get(@intCast(x), @intCast(y))) {
rl.DrawPixel(@intCast(x), @intCast(y), self.on_color);
rl.DrawRectangle(@intCast(x), @intCast(y), 1, 1, self.on_color);
}
}
}

View File

@ -1,39 +0,0 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
uniform vec2 renderSize;
// const vec2 size = vec2(800, 450); // Framebuffer size
const float samples = 7.0; // Pixels per axis; higher = bigger glow, worse performance
const float quality = 5.0; // Defines size factor: Lower = smaller glow, better quality
void main()
{
vec4 sum = vec4(0);
vec2 sizeFactor = vec2(1)/renderSize*quality;
// Texel color fetching from texture sampler
vec4 source = texture(texture0, fragTexCoord);
const int range = (int(samples) - 1)/2; // should be = (samples - 1)/2;
for (int x = -range; x <= range; x++)
{
for (int y = -range; y <= range; y++)
{
sum += texture(texture0, fragTexCoord + vec2(x, y)*sizeFactor);
}
}
// Calculate final fragment color
finalColor = ((sum/(samples*samples)) + source)*colDiffuse;
}

View File

@ -1,24 +0,0 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
// Output vertex attributes (to fragment shader)
out vec2 fragTexCoord;
out vec4 fragColor;
void main()
{
// Send vertex attributes to fragment shader
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;
// Calculate final vertex position
gl_Position = mvp*vec4(vertexPosition, 1.0);
}

View File

@ -1,116 +0,0 @@
const Self = @This();
const rl = @import("raylib");
const gl = @import("opengl");
const std = @import("std");
const Allocator = std.mem.Allocator;
const ShaderCode = @import("./shader-code.zig");
const Light = @import("./light.zig");
const max_lights = 1;
const SHADER_LOC_VECTOR_VIEW = @intFromEnum(rl.ShaderLocationIndex.SHADER_LOC_VECTOR_VIEW);
shader: rl.Shader,
bloom_shader: rl.Shader,
frame_buffer: rl.RenderTexture2D,
camera: rl.Camera3D,
lights: [max_lights]Light,
cube: rl.Model,
pub fn init(allocator: Allocator) !Self {
const main_shader_code = try ShaderCode.init(allocator, max_lights);
defer main_shader_code.deinit();
const shader = rl.LoadShaderFromMemory(main_shader_code.vertex, main_shader_code.fragment);
errdefer rl.UnloadShader(shader);
if (shader.id == rl.rlGetShaderIdDefault()) {
return error.CompileShader;
}
const bloom_shader = rl.LoadShaderFromMemory(@embedFile("shaders/default_vs.glsl"), @embedFile("shaders/bloom_fs.glsl"));
errdefer rl.UnloadShader(bloom_shader);
if (bloom_shader.id == rl.rlGetShaderIdDefault()) {
return error.CompileShader;
}
const frame_buffer = rl.LoadRenderTexture(rl.GetScreenWidth(), rl.GetScreenHeight());
errdefer rl.UnloadRenderTexture(frame_buffer);
var lights: [max_lights]Light = undefined;
for (0..max_lights) |i| {
lights[i] = Light.init(shader, i);
}
Light.init(shader, 0).set_point(rl.RED, rl.Vector3{ .x = 1, .y = 1, .z = 0.5 }, 2.0);
shader.locs.?[SHADER_LOC_VECTOR_VIEW] = rl.GetShaderLocation(shader, "viewPos");
const ambientLoc = rl.GetShaderLocation(shader, "ambient");
rl.SetShaderValue(shader, ambientLoc, &[4]f32{ 0.6, 0.6, 1, 1.0 }, .SHADER_UNIFORM_VEC4);
return Self{
.shader = shader,
.bloom_shader = bloom_shader,
.frame_buffer = frame_buffer,
.camera = rl.Camera3D{
.position = rl.Vector3.new(0, 0, -10),
.target = rl.Vector3.new(0.0, 0.0, 0.0),
.up = rl.Vector3.new(0.0, 1.0, 0.0),
.fovy = 45.0,
.projection = rl.CameraProjection.CAMERA_PERSPECTIVE,
},
.lights = lights,
.cube = rl.LoadModelFromMesh(rl.GenMeshCube(1.0, 1.0, 1.0))
};
}
pub fn deinit(self: Self) void {
rl.UnloadShader(self.shader);
rl.UnloadRenderTexture(self.frame_buffer);
}
pub fn update(self: *Self, dt: f32) void {
const screen_width = rl.GetScreenWidth();
const screen_height = rl.GetScreenHeight();
if (self.frame_buffer.texture.width != screen_width or self.frame_buffer.texture.height != screen_height) {
rl.UnloadRenderTexture(self.frame_buffer);
self.frame_buffer = rl.LoadRenderTexture(screen_width, screen_height);
}
rl.UpdateCamera(&self.camera, .CAMERA_THIRD_PERSON);
const cameraPos = [3]f32{ self.camera.position.x, self.camera.position.y, self.camera.position.z };
rl.SetShaderValue(self.shader, self.shader.locs.?[SHADER_LOC_VECTOR_VIEW], @ptrCast(&cameraPos), .SHADER_UNIFORM_VEC3);
_ = dt;
}
pub fn draw(self: *Self) void {
rl.BeginTextureMode(self.frame_buffer);
{
rl.ClearBackground(.{ .r = 33, .g = 33, .b = 33, .a = 255 });
rl.BeginMode3D(self.camera);
rl.BeginShaderMode(self.shader);
self.cube.materials.?[0].shader = self.shader;
rl.DrawModel(self.cube, rl.Vector3Zero(), 1.0, rl.WHITE);
rl.EndShaderMode();
rl.EndMode3D();
}
rl.EndTextureMode();
const screen_width = rl.GetScreenWidth();
const screen_height = rl.GetScreenHeight();
const screen_rect = rl.Rectangle{ .x = 0, .y = 0, .width = @floatFromInt(screen_width), .height = @floatFromInt(screen_height) };
const loc = rl.GetShaderLocation(self.bloom_shader, "renderSize");
rl.SetShaderValue(self.bloom_shader, loc, &[2]f32{ @floatFromInt(screen_width), @floatFromInt(screen_height) }, .SHADER_UNIFORM_VEC2);
rl.BeginDrawing();
rl.BeginShaderMode(self.bloom_shader);
rl.DrawTextureRec(self.frame_buffer.texture, screen_rect, rl.Vector2.zero(), rl.WHITE);
rl.EndShaderMode();
rl.EndDrawing();
}