QOL improvements, add ground plane, add adjustable cam speed

This commit is contained in:
sergeypdev 2024-02-12 16:24:07 +04:00
parent 117977b873
commit 70ff9c17b5
4 changed files with 51 additions and 21 deletions

2
assets/plane.mtl Normal file
View File

@ -0,0 +1,2 @@
# Blender 4.0.2 MTL File: 'None'
# www.blender.org

15
assets/plane.obj Normal file
View File

@ -0,0 +1,15 @@
# Blender 4.0.2
# www.blender.org
mtllib plane.mtl
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
vn -0.0000 1.0000 -0.0000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
s 0
f 1/1/1 2/2/1 4/3/1 3/4/1

View File

@ -196,9 +196,10 @@ pub const FreeLookCamera = struct {
pos: Vec3 = Vec3.new(0, 0, -1),
pitch: f32 = 0,
yaw: f32 = 0,
move_speed: f32 = 0.5,
view_matrix: Mat4 = Mat4.identity(),
pub fn update(self: *FreeLookCamera, move: Vec3, look: Vec2) void {
pub fn update(self: *FreeLookCamera, dt: f32, move: Vec3, look: Vec2) void {
self.yaw += look.x();
self.pitch += look.y();
// First rotate pitch, then yaw
@ -211,7 +212,7 @@ pub const FreeLookCamera = struct {
const movement = left.scale(-move.x()).add(forward.scale(move.y())).add(up.scale(move.z()));
self.pos = self.pos.add(movement);
self.pos = self.pos.add(movement.scale(self.move_speed * dt));
self.view_matrix = Mat4.lookAt(self.pos, self.pos.add(forward), Vec3.up());
}
@ -432,11 +433,18 @@ export fn game_init(global_allocator: *std.mem.Allocator) void {
.rotate = .{ .axis = Vec3.up(), .rate = -60 },
});
// Plane
_ = g_mem.world.addEntity(.{
.flags = .{ .mesh = true },
.transform = .{ .scale = Vec3.one().scale(100) },
.mesh = .{ .handle = a.Meshes.plane },
});
// 10 bunnies
{
for (0..10) |i| {
_ = g_mem.world.addEntity(.{
.transform = .{ .pos = Vec3.new(@floatFromInt(i * 1), 0, 0) },
.transform = .{ .pos = Vec3.new(@as(f32, @floatFromInt(i)) * 0.3, -0.03, 0) },
.flags = .{ .mesh = true },
.mesh = .{ .handle = a.Meshes.bunny },
@ -490,6 +498,11 @@ export fn game_update() bool {
gmem.mouse_focus = true;
}
},
c.SDL_MOUSEWHEEL => {
if (gmem.mouse_focus) {
gmem.free_cam.move_speed = @max(gmem.free_cam.move_speed + event.wheel.preciseY * 0.1, 0);
}
},
c.SDL_KEYUP, c.SDL_KEYDOWN => {
const pressed = event.key.state == c.SDL_PRESSED;
switch (event.key.keysym.scancode) {
@ -518,7 +531,7 @@ export fn game_update() bool {
c.SDL_SCANCODE_SPACE => {
gmem.input_state.up = pressed;
},
c.SDL_SCANCODE_LSHIFT => {
c.SDL_SCANCODE_LCTRL => {
gmem.input_state.down = pressed;
},
else => {},
@ -544,7 +557,6 @@ export fn game_update() bool {
gmem.delta_time = @as(f32, @floatFromInt((now - gmem.last_frame_time))) / @as(f32, @floatFromInt(gmem.performance_frequency));
gmem.last_frame_time = now;
const MOVEMENT_SPEED = 0.5;
if (gmem.input_state.forward) {
//const y = &move.data[1];
move.yMut().* += 1;
@ -565,9 +577,8 @@ export fn game_update() bool {
move.zMut().* -= 1;
}
move = move.scale(MOVEMENT_SPEED * gmem.delta_time);
gmem.free_cam.update(move, look.scale(0.008));
// TODO: make this an entity
gmem.free_cam.update(gmem.delta_time, move, look.scale(0.008));
// RENDER
// gl.fenceSync(_condition: GLenum, _flags: GLbitfield)
@ -704,7 +715,6 @@ export fn game_update() bool {
}
c.SDL_GL_SwapWindow(ginit.window);
// DwmFlush();
// const vblank_event: D3DKMT_WAITFORVERTICALBLANKEVENT = .{
// .hAdapter = 0,
// .hDevice = ginit.syswm_info.info.win.hdc.*,

View File

@ -5,22 +5,24 @@ const Handle = @import("assets").Handle;
pub const Meshes = struct {
pub const bunny = Handle.Mesh{ .id = 1 };
pub const sphere = Handle.Mesh{ .id = 7 };
pub const plane = Handle.Mesh{ .id = 2 };
pub const sphere = Handle.Mesh{ .id = 8 };
};
pub const Shaders = struct {
pub const frag = Handle.Shader{ .id = 2 };
pub const @"mesh.frag" = Handle.Shader{ .id = 3 };
pub const @"mesh.vert" = Handle.Shader{ .id = 5 };
pub const vert = Handle.Shader{ .id = 6 };
pub const frag = Handle.Shader{ .id = 3 };
pub const @"mesh.frag" = Handle.Shader{ .id = 4 };
pub const @"mesh.vert" = Handle.Shader{ .id = 6 };
pub const vert = Handle.Shader{ .id = 7 };
};
pub const ShaderPrograms = struct {
pub const mesh = Handle.ShaderProgram{ .id = 4 };
pub const mesh = Handle.ShaderProgram{ .id = 5 };
};
pub const asset_paths = [_][]const u8{
"assets\\bunny.mesh",
"assets\\plane.mesh",
"assets\\shaders\\frag.glsl",
"assets\\shaders\\mesh.frag.glsl",
"assets\\shaders\\mesh.prog",
@ -31,11 +33,12 @@ pub const asset_paths = [_][]const u8{
pub const asset_path_to_asset_id = std.ComptimeStringMap(u32, .{
.{ "assets\\bunny.mesh", 1 },
.{ "assets\\shaders\\frag.glsl", 2 },
.{ "assets\\shaders\\mesh.frag.glsl", 3 },
.{ "assets\\shaders\\mesh.prog", 4 },
.{ "assets\\shaders\\mesh.vert.glsl", 5 },
.{ "assets\\shaders\\vert.glsl", 6 },
.{ "assets\\sphere.mesh", 7 },
.{ "assets\\plane.mesh", 2 },
.{ "assets\\shaders\\frag.glsl", 3 },
.{ "assets\\shaders\\mesh.frag.glsl", 4 },
.{ "assets\\shaders\\mesh.prog", 5 },
.{ "assets\\shaders\\mesh.vert.glsl", 6 },
.{ "assets\\shaders\\vert.glsl", 7 },
.{ "assets\\sphere.mesh", 8 },
});