Skip to content

Commit 2187191

Browse files
committed
updates
1 parent 09f8921 commit 2187191

File tree

2 files changed

+116
-27
lines changed

2 files changed

+116
-27
lines changed

src/engine/Component/Mesh3D.jl

Lines changed: 114 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ module Mesh3DModule
2929
sym::Any
3030
color::Any
3131
texCoords::Vector{vec3d}
32-
function triangle(p = [vec3d(0,0,0), vec3d(0,0,0), vec3d(0,0,0)])
33-
new(p, nothing, nothing, [vec3d(0,0,0), vec3d(0,0,0), vec3d(0,0,0)])
32+
material::String
33+
function triangle(p = [vec3d(0,0,0), vec3d(0,0,0), vec3d(0,0,0)], sym = nothing, color = nothing, texCoords = [vec3d(0,0,0), vec3d(0,0,0), vec3d(0,0,0)], material = "default")
34+
new(p, sym, color, texCoords, material)
3435
end
3536
end
3637

@@ -96,8 +97,12 @@ module Mesh3DModule
9697
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0")
9798
end
9899

99-
width = surface.w
100-
height = surface.h
100+
w = Ref{Int32}(0)
101+
h = Ref{Int32}(0)
102+
SDL_QueryTexture(texture, C_NULL, C_NULL, w, h)
103+
104+
width = w[]
105+
height = h[]
101106
new(surface, texture, width, height, mode, filter, type, compression, UInt8[], 0)
102107
end
103108
end
@@ -176,18 +181,19 @@ module Mesh3DModule
176181
filter::Int = TEXTURE_FILTER_LINEAR,
177182
type::Int = TEXTURE_TYPE_DIFFUSE,
178183
compression::Int = TEXTURE_COMPRESSION_NONE)::Texture
184+
println("Loading texture from: $file_path")
179185
# Get file extension
180186
ext = lowercase(splitext(file_path)[2])
181187

182188
# Load texture based on format
183189
surface = if ext == ".bmp"
184-
SDL_LoadBMP(file_path)
190+
IMG_Load(file_path)
185191
elseif ext == ".png"
186-
SDL_LoadPNG(file_path)
192+
IMG_Load(file_path)
187193
elseif ext == ".jpg" || ext == ".jpeg"
188-
SDL_LoadJPG(file_path)
194+
IMG_Load(file_path)
189195
elseif ext == ".dds" # DirectDraw Surface (compressed texture)
190-
SDL_LoadDDS(file_path)
196+
IMG_Load(file_path)
191197
else
192198
error("Unsupported texture format: $ext")
193199
end
@@ -196,6 +202,7 @@ module Mesh3DModule
196202
error("Failed to load texture: $file_path")
197203
end
198204

205+
println("Surface loaded successfully")
199206
texture = Texture(surface, mode, filter, type, compression)
200207

201208
# Apply compression if requested
@@ -352,6 +359,10 @@ module Mesh3DModule
352359
texCoords = Vector{vec3d}()
353360
faces = Vector{Vector{Int}}()
354361
faceTexCoords = Vector{Vector{Int}}()
362+
faceNormals = Vector{Vector{Int}}()
363+
364+
# Track current material
365+
current_material = "default"
355366

356367
while !eof(f)
357368
line = readline(f)
@@ -374,6 +385,8 @@ module Mesh3DModule
374385
# Parse face indices (vertex/texture/normal)
375386
face_indices = Vector{Int}()
376387
face_tex_indices = Vector{Int}()
388+
face_normal_indices = Vector{Int}()
389+
377390
for i in 2:length(s)
378391
indices = split(s[i], "/")
379392
if length(indices) >= 1
@@ -382,14 +395,18 @@ module Mesh3DModule
382395
if length(indices) >= 2 && !isempty(indices[2])
383396
push!(face_tex_indices, parse(Int, indices[2]))
384397
end
398+
if length(indices) >= 3 && !isempty(indices[3])
399+
push!(face_normal_indices, parse(Int, indices[3]))
400+
end
385401
end
386402
push!(faces, face_indices)
387403
push!(faceTexCoords, face_tex_indices)
404+
push!(faceNormals, face_normal_indices)
388405
elseif s[1] == "usemtl" # Material
389-
this.mesh.currentMaterial = s[2]
406+
current_material = s[2]
390407
elseif s[1] == "mtllib" # Material library
391-
# TODO: Load material library
392-
# For now, we'll just ignore it
408+
mtl_path = joinpath(dirname(file_path), s[2])
409+
load_material_library(this, mtl_path)
393410
end
394411
end
395412

@@ -412,6 +429,9 @@ module Mesh3DModule
412429
]
413430
end
414431

432+
# Set the material for this triangle
433+
tri.material = current_material
434+
415435
push!(this.mesh.tris, tri)
416436
end
417437
end
@@ -420,6 +440,42 @@ module Mesh3DModule
420440
return true
421441
end
422442

443+
function load_material_library(this::Mesh3D, mtl_path::String)
444+
if !isfile(mtl_path)
445+
@warn "Material library file not found: $mtl_path"
446+
return
447+
end
448+
449+
f = open(mtl_path, "r")
450+
current_material = nothing
451+
452+
while !eof(f)
453+
line = readline(f)
454+
s = split(line)
455+
456+
if isempty(s)
457+
continue
458+
end
459+
460+
if s[1] == "newmtl"
461+
println("newmtl: ", s[2])
462+
current_material = Material(string(s[2]))
463+
this.mesh.materials[s[2]] = current_material
464+
elseif s[1] == "map_Kd" && current_material !== nothing
465+
# Load texture
466+
texture_path = joinpath(dirname(mtl_path), s[2])
467+
if isfile(texture_path)
468+
texture = load_texture(texture_path)
469+
current_material.textures[TEXTURE_TYPE_DIFFUSE] = texture
470+
else
471+
@warn "Texture file not found: $texture_path"
472+
end
473+
end
474+
end
475+
476+
close(f)
477+
end
478+
423479
function load_from_fbx(this::Mesh3D, file_path::String)::Bool
424480
# TODO: Implement FBX loading
425481
# This would require a FBX parsing library
@@ -727,12 +783,15 @@ module Mesh3DModule
727783
texture = nothing
728784
if haskey(material.textures, TEXTURE_TYPE_DIFFUSE)
729785
texture = material.textures[TEXTURE_TYPE_DIFFUSE]
786+
println("Using diffuse texture")
730787
elseif !isempty(material.textures)
731788
texture = first(material.textures)[2]
789+
println("Using fallback texture")
732790
end
733791

734792
if texture !== nothing
735793
tex_coords = [apply_texture_mode(coord, texture) for coord in tex_coords]
794+
println("Texture coordinates applied: ", tex_coords)
736795
end
737796

738797
sdl_verts = [
@@ -743,7 +802,20 @@ module Mesh3DModule
743802

744803
# Use material texture if available, otherwise use color
745804
texture_ptr = texture !== nothing ? texture.texture : C_NULL
746-
SDL_RenderGeometry(JulGame.Renderer, texture_ptr, sdl_verts, length(sdl_verts), C_NULL, 0)
805+
if texture_ptr != C_NULL
806+
println("Rendering with texture")
807+
else
808+
println("Rendering without texture")
809+
end
810+
811+
# Set the blend mode for proper texture rendering
812+
SDL_SetRenderDrawBlendMode(JulGame.Renderer, SDL_BLENDMODE_BLEND)
813+
814+
# Render the geometry
815+
result = SDL_RenderGeometry(JulGame.Renderer, texture_ptr, sdl_verts, length(sdl_verts), C_NULL, 0)
816+
if result < 0
817+
println("SDL_RenderGeometry failed: ", unsafe_string(SDL_GetError()))
818+
end
747819

748820
if JulGame.IS_DEBUG
749821
SDL_RenderDrawLine(
@@ -864,25 +936,42 @@ module Mesh3DModule
864936
function create_cube()
865937
meshCube = mesh(triangle[
866938
# SOUTH
867-
triangle([ vec3d(0.0, 0.0, 0.0), vec3d(0.0, 1.0, 0.0), vec3d(1.0, 1.0, 0.0)]),
868-
triangle([ vec3d(0.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 0.0, 0.0)]),
869-
triangle([ vec3d(0.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 0.0, 0.0)]),
939+
triangle([ vec3d(0.0, 0.0, 0.0), vec3d(0.0, 1.0, 0.0), vec3d(1.0, 1.0, 0.0)],
940+
nothing, nothing, [vec3d(0.0, 0.0, 0.0), vec3d(0.0, 1.0, 0.0), vec3d(1.0, 1.0, 0.0)]),
941+
triangle([ vec3d(0.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 0.0, 0.0)],
942+
nothing, nothing, [vec3d(0.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 0.0, 0.0)]),
870943
# EAST
871-
triangle([ vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 1.0, 1.0)]),
872-
triangle([ vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 1.0), vec3d(1.0, 0.0, 1.0)]),
944+
triangle([ vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 1.0, 1.0)],
945+
nothing, nothing, [vec3d(0.0, 0.0, 0.0), vec3d(0.0, 1.0, 0.0), vec3d(0.0, 1.0, 1.0)]),
946+
triangle([ vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 1.0), vec3d(1.0, 0.0, 1.0)],
947+
nothing, nothing, [vec3d(0.0, 0.0, 0.0), vec3d(0.0, 1.0, 1.0), vec3d(0.0, 0.0, 1.0)]),
873948
# NORTH
874-
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(1.0, 1.0, 1.0), vec3d(0.0, 1.0, 1.0)]),
875-
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(0.0, 1.0, 1.0), vec3d(0.0, 0.0, 1.0)]),
949+
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(1.0, 1.0, 1.0), vec3d(0.0, 1.0, 1.0)],
950+
nothing, nothing, [vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(0.0, 1.0, 0.0)]),
951+
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(0.0, 1.0, 1.0), vec3d(0.0, 0.0, 1.0)],
952+
nothing, nothing, [vec3d(1.0, 0.0, 0.0), vec3d(0.0, 1.0, 0.0), vec3d(0.0, 0.0, 0.0)]),
876953
# WEST
877-
triangle([ vec3d(0.0, 0.0, 1.0), vec3d(0.0, 1.0, 1.0), vec3d(0.0, 1.0, 0.0)]),
878-
triangle([ vec3d(0.0, 0.0, 1.0), vec3d(0.0, 1.0, 0.0), vec3d(0.0, 0.0, 0.0)]),
954+
triangle([ vec3d(0.0, 0.0, 1.0), vec3d(0.0, 1.0, 1.0), vec3d(0.0, 1.0, 0.0)],
955+
nothing, nothing, [vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 1.0, 1.0)]),
956+
triangle([ vec3d(0.0, 0.0, 1.0), vec3d(0.0, 1.0, 0.0), vec3d(0.0, 0.0, 0.0)],
957+
nothing, nothing, [vec3d(1.0, 0.0, 0.0), vec3d(1.0, 1.0, 1.0), vec3d(1.0, 0.0, 1.0)]),
879958
# TOP
880-
triangle([ vec3d(0.0, 1.0, 0.0), vec3d(0.0, 1.0, 1.0), vec3d(1.0, 1.0, 1.0)]),
881-
triangle([ vec3d(0.0, 1.0, 0.0), vec3d(1.0, 1.0, 1.0), vec3d(1.0, 1.0, 0.0)]),
959+
triangle([ vec3d(0.0, 1.0, 0.0), vec3d(0.0, 1.0, 1.0), vec3d(1.0, 1.0, 1.0)],
960+
nothing, nothing, [vec3d(0.0, 0.0, 0.0), vec3d(0.0, 1.0, 0.0), vec3d(1.0, 1.0, 0.0)]),
961+
triangle([ vec3d(0.0, 1.0, 0.0), vec3d(1.0, 1.0, 1.0), vec3d(1.0, 1.0, 0.0)],
962+
nothing, nothing, [vec3d(0.0, 0.0, 0.0), vec3d(1.0, 1.0, 0.0), vec3d(1.0, 0.0, 0.0)]),
882963
# BOTTOM
883-
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(0.0, 0.0, 1.0), vec3d(0.0, 0.0, 0.0)]),
884-
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(0.0, 0.0, 0.0), vec3d(1.0, 0.0, 0.0)]),
964+
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(0.0, 0.0, 1.0), vec3d(0.0, 0.0, 0.0)],
965+
nothing, nothing, [vec3d(1.0, 0.0, 0.0), vec3d(0.0, 0.0, 0.0), vec3d(0.0, 0.0, 1.0)]),
966+
triangle([ vec3d(1.0, 0.0, 1.0), vec3d(0.0, 0.0, 0.0), vec3d(1.0, 0.0, 0.0)],
967+
nothing, nothing, [vec3d(1.0, 0.0, 0.0), vec3d(0.0, 0.0, 1.0), vec3d(1.0, 0.0, 1.0)]),
885968
])
969+
970+
# Set material for all triangles
971+
for tri in meshCube.tris
972+
tri.material = "default"
973+
end
974+
886975
return meshCube
887976
end
888977
end

src/engine/Component/Sprite.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ module SpriteModule
214214
SDL2.SDL_ClearError()
215215

216216
fullPath = joinpath(BasePath, "assets", "images", imagePath)
217-
this.image = load_image_sdl(this, fullPath, imagePath)
217+
this.image = load_image_sdl(fullPath, imagePath)
218218
error = unsafe_string(SDL2.SDL_GetError())
219219

220220
if !isempty(error) || this.image == C_NULL
@@ -248,7 +248,7 @@ module SpriteModule
248248
Component.set_color(this)
249249
end
250250

251-
function load_image_sdl(this::InternalSprite, fullPath::String, imagePath::String)
251+
function load_image_sdl(fullPath::String, imagePath::String)
252252
if haskey(JulGame.IMAGE_CACHE, get_comma_separated_path(imagePath))
253253
raw_data = JulGame.IMAGE_CACHE[get_comma_separated_path(imagePath)]
254254
rw = SDL2.SDL_RWFromConstMem(pointer(raw_data), length(raw_data))

0 commit comments

Comments
 (0)