Texture loading, alpha-discard shader, dummy torches

This commit is contained in:
Nefrace 2024-04-20 01:14:24 +03:00
parent 16d9c5479e
commit cd412f8249
7 changed files with 127 additions and 10 deletions

BIN
assets/sword.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 B

BIN
assets/torch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

3
run.sh Executable file
View File

@ -0,0 +1,3 @@
!#/bin/bash
odin run src

View File

@ -9,7 +9,6 @@ Entity :: struct {
position : Vec3, position : Vec3,
velocity : Vec3, velocity : Vec3,
size : Vec3, size : Vec3,
update : proc(entity: ^Entity, delta: f32),
} }
PAL_INT :: [6]u32 { PAL_INT :: [6]u32 {
@ -23,6 +22,9 @@ PAL_INT :: [6]u32 {
PAL := [6]rl.Color{} PAL := [6]rl.Color{}
CAMERA : ^rl.Camera3D
TEXTURES : Textures
main :: proc() { main :: proc() {
for v, i in PAL_INT { for v, i in PAL_INT {
PAL[i] = rl.GetColor(v) PAL[i] = rl.GetColor(v)
@ -30,6 +32,11 @@ main :: proc() {
rl.InitWindow(800, 400, "By The Fire") rl.InitWindow(800, 400, "By The Fire")
rl.SetTargetFPS(60) 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]) img := rl.GenImageChecked(64, 64, 8, 8, PAL[1], PAL[2])
defer rl.UnloadImage(img) defer rl.UnloadImage(img)
@ -41,27 +48,65 @@ main :: proc() {
defer rl.UnloadMesh(plane) defer rl.UnloadMesh(plane)
mat := rl.LoadMaterialDefault() mat := rl.LoadMaterialDefault()
rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex) rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex)
player_load_resources()
defer player_unload_resources()
player := player_create() player := player_create()
CAMERA = &(player.camera)
rl.DisableCursor() rl.DisableCursor()
torches := [10]Torch{}
for &v, i in torches {
torches[i] = Torch{position = Vec3{f32(i) * 2, 2, 0}}
}
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
using rl using rl
player_update(&player, GetFrameTime()) player_update(&player, GetFrameTime())
BeginDrawing() BeginDrawing()
BeginMode3D(player.camera) BeginMode3D(player.camera)
BeginShaderMode(discard_shader)
ClearBackground(PAL[0]) ClearBackground(PAL[0])
rlPushMatrix() rlPushMatrix()
rlRotatef(45, 0, 1, 0) rlRotatef(45, 0, 1, 0)
DrawMesh(plane, mat, rl.Matrix(1)) DrawMesh(plane, mat, rl.Matrix(1))
rlPopMatrix() rlPopMatrix()
player_draw(&player) player_draw(&player)
for &v in torches {
torch_draw(&v)
}
EndShaderMode()
EndMode3D() EndMode3D()
EndDrawing() 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;
}
`

View File

@ -1,6 +1,7 @@
package main package main
import "core:math" import "core:math"
import "core:fmt"
import rl "vendor:raylib" import rl "vendor:raylib"
Player :: struct { Player :: struct {
@ -14,6 +15,27 @@ Player :: struct {
camera: rl.Camera3D, 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 { player_create :: proc() -> Player {
return Player { return Player {
size = {0.5, 2, 0.5}, 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 velocity.xz = Vector3MoveTowards(velocity, motion * 100, 100 * delta).xz
position += velocity * delta 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) bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0,1,0}, direction)
camera.position = position + cameraOffset + bob_position camera.position = position + cameraOffset + bob_position
@ -73,16 +97,19 @@ player_draw :: proc(using player: ^Player) {
rlPushMatrix() rlPushMatrix()
offset := position + cameraOffset + bob_position offset := position + cameraOffset + (bob_position / 2)
rlTranslatef(offset.x, offset.y, offset.z) rlTranslatef(offset.x, offset.y, offset.z)
rlRotatef(math.to_degrees(player.direction), 0, 1, 0) rlRotatef(math.to_degrees(player.direction), 0, 1, 0)
rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0) rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0)
rlTranslatef(-1, -0.3, 1) rlTranslatef(-1, -0.3, 1)
rlScalef(0.5, 0.5, 0.5) rlTranslatef(math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)) * bob_factor * 0.3, 0, 0)
DrawCube({0, 0, 0}, 1,1,1, PAL[4]) rlRotatef(-90, 1, 0, 0)
rlTranslatef(0, 0, 1) rlRotatef(-90, 0, 0, 1)
rlScalef(0.4, 0.4, 1) rlRotatef(210, 0, 1, 0)
DrawCube({0, 0, 0}, 1,1,1, PAL[4]) 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() rlPopMatrix()
} }

28
src/resloader.odin Normal file
View File

@ -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)
}

14
src/torch.odin Normal file
View File

@ -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)
}