From cd412f8249ec6912480260061452dbb91b556d78 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Sat, 20 Apr 2024 01:14:24 +0300 Subject: [PATCH] Texture loading, alpha-discard shader, dummy torches --- assets/sword.png | Bin 0 -> 288 bytes assets/torch.png | Bin 0 -> 551 bytes run.sh | 3 +++ src/main.odin | 51 ++++++++++++++++++++++++++++++++++++++++++--- src/player.odin | 41 +++++++++++++++++++++++++++++------- src/resloader.odin | 28 +++++++++++++++++++++++++ src/torch.odin | 14 +++++++++++++ 7 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 assets/sword.png create mode 100644 assets/torch.png create mode 100755 run.sh create mode 100644 src/resloader.odin create mode 100644 src/torch.odin diff --git a/assets/sword.png b/assets/sword.png new file mode 100644 index 0000000000000000000000000000000000000000..6ae1baf71fa08f31ad9637e6d36926e4fd3fd4f5 GIT binary patch literal 288 zcmeAS@N?(olHy`uVBq!ia0vp^{6MV2!3HG5_9pBHQjEnx?oJHr&dIz4a&CFLIEHw1 zCMO(VzH&A8-{Z;0SAYC|{ri7UGlpe?ZJlNek&l<3Uj0$(fXk%GZEb0Os^Xta>-TR@ z*SO`w!E^kA8VlRX&6=BL3P>zj=*zLez(64Wh|=oamBswZi+Alh{r__ICCRjNzdW|| zzRdi?vrN%gQevX7Phq#hio$d2L^);`T5^l;0IH7RS2kmP^-Ax5@Es+IZ?+Xj4gf*H z`)h_Gz6K4(TfPHD9xPF4zb;y_xN8Dqg{igXiGy?fCZ#z^eau?6bADuj=M}bo0Z5)QFu0)5Hg>FVdQ&MBb@049@ocmMzZ literal 0 HcmV?d00001 diff --git a/assets/torch.png b/assets/torch.png new file mode 100644 index 0000000000000000000000000000000000000000..f3fafc224a95dc4b0cb06f9ee26313b22dea8ca5 GIT binary patch literal 551 zcmV+?0@(eDP)Px$;Ymb6RCt{2Tf0sJF%X;}nl$ha^l4~m`33$Ug6hhA#g(Z*C#6c2U!Wx=P5S%; zX;PZXkuknkB+stSJ5`jNZoK35?C$9T2;y|uxzl0iB-d~wNv;7iDdKqN?-nm2YY`#! z!OtI_0RZRYCjc1{=trRI%uD!zm=YPKY`aUPGOb3E z2;4)Y6HEmv5j;Q^U`SnO)cz2~KM|q|uv#x?T{~P?jbk4LGeI_?Hc$k(#@N_qm>qFI zb=A22X6yEwtxNsJS{LmI^ckQB+X(5V6N(UVAHXKm2I~VPNHxL6VJ4ZCL4~j~$RY|; zzpn~Rau(1E8-`d!fX*slCdeiPtQA&;;sv;sByW3Znu+2wK(q?PkT62gH$+mHbRRIZ p$DiaoLSqMr1E+d$;l$Zbj002ovPDHLkV1oR@=Fk8D literal 0 HcmV?d00001 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..6ca510b --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +!#/bin/bash + +odin run src diff --git a/src/main.odin b/src/main.odin index b7af8dd..a03451f 100644 --- a/src/main.odin +++ b/src/main.odin @@ -9,7 +9,6 @@ Entity :: struct { position : Vec3, velocity : Vec3, size : Vec3, - update : proc(entity: ^Entity, delta: f32), } PAL_INT :: [6]u32 { @@ -23,6 +22,9 @@ PAL_INT :: [6]u32 { PAL := [6]rl.Color{} +CAMERA : ^rl.Camera3D +TEXTURES : Textures + main :: proc() { for v, i in PAL_INT { PAL[i] = rl.GetColor(v) @@ -30,6 +32,11 @@ main :: proc() { rl.InitWindow(800, 400, "By The Fire") rl.SetTargetFPS(60) + + TEXTURES = load_textures() + defer unload_textures(&TEXTURES) + + discard_shader = rl.LoadShaderFromMemory(nil, alpha_discard) img := rl.GenImageChecked(64, 64, 8, 8, PAL[1], PAL[2]) defer rl.UnloadImage(img) @@ -41,27 +48,65 @@ main :: proc() { defer rl.UnloadMesh(plane) mat := rl.LoadMaterialDefault() rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex) - + player_load_resources() + defer player_unload_resources() player := player_create() + CAMERA = &(player.camera) rl.DisableCursor() + torches := [10]Torch{} + for &v, i in torches { + torches[i] = Torch{position = Vec3{f32(i) * 2, 2, 0}} + } + for !rl.WindowShouldClose() { using rl player_update(&player, GetFrameTime()) BeginDrawing() BeginMode3D(player.camera) + BeginShaderMode(discard_shader) ClearBackground(PAL[0]) rlPushMatrix() rlRotatef(45, 0, 1, 0) DrawMesh(plane, mat, rl.Matrix(1)) rlPopMatrix() - + player_draw(&player) + for &v in torches { + torch_draw(&v) + } + + + EndShaderMode() EndMode3D() EndDrawing() } } + +discard_shader : rl.Shader + +alpha_discard :: ` +#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; + +void main() +{ + vec4 texelColor = texture(texture0, fragTexCoord); + if (texelColor.a == 0.0) discard; + finalColor = texelColor * fragColor * colDiffuse; +} +` diff --git a/src/player.odin b/src/player.odin index c936cca..b565e5f 100644 --- a/src/player.odin +++ b/src/player.odin @@ -1,6 +1,7 @@ package main import "core:math" +import "core:fmt" import rl "vendor:raylib" Player :: struct { @@ -14,6 +15,27 @@ Player :: struct { camera: rl.Camera3D, } +player_sword_tex : rl.Texture +player_sword_mesh : rl.Mesh +player_sword_mat : rl.Material + +player_load_resources :: proc() { + player_sword_tex = TEXTURES["sword.png"] // rl.LoadTexture("sword.png") + fmt.println(TEXTURES) + fmt.println(player_sword_tex) + rl.SetTextureFilter(player_sword_tex, rl.TextureFilter.POINT) + player_sword_mesh = rl.GenMeshPlane(1.5, 3.6, 1, 1) + player_sword_mat = rl.LoadMaterialDefault() + player_sword_mat.shader = discard_shader + rl.SetMaterialTexture(&player_sword_mat, rl.MaterialMapIndex.ALBEDO, player_sword_tex) +} + +player_unload_resources :: proc() { + rl.UnloadMaterial(player_sword_mat) + //rl.UnloadTexture(player_sword_tex) + rl.UnloadMesh(player_sword_mesh) +} + player_create :: proc() -> Player { return Player { size = {0.5, 2, 0.5}, @@ -56,7 +78,9 @@ player_update :: proc(using player: ^Player, delta: f32) { velocity.xz = Vector3MoveTowards(velocity, motion * 100, 100 * delta).xz position += velocity * delta - bob_position = Vec3{math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)), math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3 + + //bob_position = Vec3{math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)), math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3 + bob_position = Vec3{0, math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3 bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0,1,0}, direction) camera.position = position + cameraOffset + bob_position @@ -73,16 +97,19 @@ player_draw :: proc(using player: ^Player) { rlPushMatrix() - offset := position + cameraOffset + bob_position + offset := position + cameraOffset + (bob_position / 2) rlTranslatef(offset.x, offset.y, offset.z) rlRotatef(math.to_degrees(player.direction), 0, 1, 0) rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0) rlTranslatef(-1, -0.3, 1) - rlScalef(0.5, 0.5, 0.5) - DrawCube({0, 0, 0}, 1,1,1, PAL[4]) - rlTranslatef(0, 0, 1) - rlScalef(0.4, 0.4, 1) - DrawCube({0, 0, 0}, 1,1,1, PAL[4]) + rlTranslatef(math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)) * bob_factor * 0.3, 0, 0) + rlRotatef(-90, 1, 0, 0) + rlRotatef(-90, 0, 0, 1) + rlRotatef(210, 0, 1, 0) + rlScalef(0.3, 0.3, 0.3) + DrawMesh(player_sword_mesh, player_sword_mat, rl.Matrix(1)) + // DrawCube({0, 0, 0}, 1,1,1, PAL[4]) + // DrawCube({0, 0, 0}, 1,1,1, PAL[4]) rlPopMatrix() } diff --git a/src/resloader.odin b/src/resloader.odin new file mode 100644 index 0000000..8f886a0 --- /dev/null +++ b/src/resloader.odin @@ -0,0 +1,28 @@ +package main + +import rl "vendor:raylib" +import "core:slice" +import "core:path/filepath" + +Textures :: map[string]rl.Texture +tex_filepaths : rl.FilePathList + +load_textures :: proc() -> Textures { + result : Textures = make(map[string]rl.Texture) + + tex_filepaths := rl.LoadDirectoryFiles("./assets") + + for i : u32 = 0; i < tex_filepaths.count; i += 1 { + item := tex_filepaths.paths[i] + name := filepath.base(string(item)) + result[name] = rl.LoadTexture(item) + } + return result +} + +unload_textures :: proc(textures: ^Textures) { + for i, v in textures { + rl.UnloadTexture(v) + } + rl.UnloadDirectoryFiles(tex_filepaths) +} diff --git a/src/torch.odin b/src/torch.odin new file mode 100644 index 0000000..8ff8361 --- /dev/null +++ b/src/torch.odin @@ -0,0 +1,14 @@ +package main + +import rl "vendor:raylib" + +Torch :: struct { + using entity: Entity, +} + +torch_texture : rl.Texture + + +torch_draw :: proc(torch: ^Torch) { + rl.DrawBillboard(CAMERA^, TEXTURES["torch.png"], torch.position, 4, rl.WHITE) +}