@@ -29,8 +29,9 @@ module Mesh3DModule
29
29
sym:: Any
30
30
color:: Any
31
31
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)
34
35
end
35
36
end
36
37
@@ -96,8 +97,12 @@ module Mesh3DModule
96
97
SDL_SetHint (SDL_HINT_RENDER_SCALE_QUALITY, " 0" )
97
98
end
98
99
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[]
101
106
new (surface, texture, width, height, mode, filter, type, compression, UInt8[], 0 )
102
107
end
103
108
end
@@ -176,18 +181,19 @@ module Mesh3DModule
176
181
filter:: Int = TEXTURE_FILTER_LINEAR,
177
182
type:: Int = TEXTURE_TYPE_DIFFUSE,
178
183
compression:: Int = TEXTURE_COMPRESSION_NONE):: Texture
184
+ println (" Loading texture from: $file_path " )
179
185
# Get file extension
180
186
ext = lowercase (splitext (file_path)[2 ])
181
187
182
188
# Load texture based on format
183
189
surface = if ext == " .bmp"
184
- SDL_LoadBMP (file_path)
190
+ IMG_Load (file_path)
185
191
elseif ext == " .png"
186
- SDL_LoadPNG (file_path)
192
+ IMG_Load (file_path)
187
193
elseif ext == " .jpg" || ext == " .jpeg"
188
- SDL_LoadJPG (file_path)
194
+ IMG_Load (file_path)
189
195
elseif ext == " .dds" # DirectDraw Surface (compressed texture)
190
- SDL_LoadDDS (file_path)
196
+ IMG_Load (file_path)
191
197
else
192
198
error (" Unsupported texture format: $ext " )
193
199
end
@@ -196,6 +202,7 @@ module Mesh3DModule
196
202
error (" Failed to load texture: $file_path " )
197
203
end
198
204
205
+ println (" Surface loaded successfully" )
199
206
texture = Texture (surface, mode, filter, type, compression)
200
207
201
208
# Apply compression if requested
@@ -352,6 +359,10 @@ module Mesh3DModule
352
359
texCoords = Vector {vec3d} ()
353
360
faces = Vector {Vector{Int}} ()
354
361
faceTexCoords = Vector {Vector{Int}} ()
362
+ faceNormals = Vector {Vector{Int}} ()
363
+
364
+ # Track current material
365
+ current_material = " default"
355
366
356
367
while ! eof (f)
357
368
line = readline (f)
@@ -374,6 +385,8 @@ module Mesh3DModule
374
385
# Parse face indices (vertex/texture/normal)
375
386
face_indices = Vector {Int} ()
376
387
face_tex_indices = Vector {Int} ()
388
+ face_normal_indices = Vector {Int} ()
389
+
377
390
for i in 2 : length (s)
378
391
indices = split (s[i], " /" )
379
392
if length (indices) >= 1
@@ -382,14 +395,18 @@ module Mesh3DModule
382
395
if length (indices) >= 2 && ! isempty (indices[2 ])
383
396
push! (face_tex_indices, parse (Int, indices[2 ]))
384
397
end
398
+ if length (indices) >= 3 && ! isempty (indices[3 ])
399
+ push! (face_normal_indices, parse (Int, indices[3 ]))
400
+ end
385
401
end
386
402
push! (faces, face_indices)
387
403
push! (faceTexCoords, face_tex_indices)
404
+ push! (faceNormals, face_normal_indices)
388
405
elseif s[1 ] == " usemtl" # Material
389
- this . mesh . currentMaterial = s[2 ]
406
+ current_material = s[2 ]
390
407
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)
393
410
end
394
411
end
395
412
@@ -412,6 +429,9 @@ module Mesh3DModule
412
429
]
413
430
end
414
431
432
+ # Set the material for this triangle
433
+ tri. material = current_material
434
+
415
435
push! (this. mesh. tris, tri)
416
436
end
417
437
end
@@ -420,6 +440,42 @@ module Mesh3DModule
420
440
return true
421
441
end
422
442
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
+
423
479
function load_from_fbx (this:: Mesh3D , file_path:: String ):: Bool
424
480
# TODO : Implement FBX loading
425
481
# This would require a FBX parsing library
@@ -727,12 +783,15 @@ module Mesh3DModule
727
783
texture = nothing
728
784
if haskey (material. textures, TEXTURE_TYPE_DIFFUSE)
729
785
texture = material. textures[TEXTURE_TYPE_DIFFUSE]
786
+ println (" Using diffuse texture" )
730
787
elseif ! isempty (material. textures)
731
788
texture = first (material. textures)[2 ]
789
+ println (" Using fallback texture" )
732
790
end
733
791
734
792
if texture != = nothing
735
793
tex_coords = [apply_texture_mode (coord, texture) for coord in tex_coords]
794
+ println (" Texture coordinates applied: " , tex_coords)
736
795
end
737
796
738
797
sdl_verts = [
@@ -743,7 +802,20 @@ module Mesh3DModule
743
802
744
803
# Use material texture if available, otherwise use color
745
804
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
747
819
748
820
if JulGame. IS_DEBUG
749
821
SDL_RenderDrawLine (
@@ -864,25 +936,42 @@ module Mesh3DModule
864
936
function create_cube ()
865
937
meshCube = mesh (triangle[
866
938
# 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 )]),
870
943
# 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 )]),
873
948
# 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 )]),
876
953
# 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 )]),
879
958
# 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 )]),
882
963
# 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 )]),
885
968
])
969
+
970
+ # Set material for all triangles
971
+ for tri in meshCube. tris
972
+ tri. material = " default"
973
+ end
974
+
886
975
return meshCube
887
976
end
888
977
end
0 commit comments