Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example fixed models/obj_loading #467

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions examples/models/obj_loading/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package main

import (
"path/filepath"
"slices"
"unsafe"

"github.com/gen2brain/raylib-go/raylib"
)

var supportedFileTypes = []string{
".obj",
".gltf",
".glb",
".vox",
".iqm",
".m3d",
}

func main() {
screenWidth := int32(800)
screenHeight := int32(450)
Expand All @@ -24,26 +37,75 @@ func main() {

position := rl.NewVector3(0.0, 0.0, 0.0) // Set model position

meshes := unsafe.Slice(obj.Meshes, obj.MeshCount)
bounds := rl.GetMeshBoundingBox(meshes[0]) // Set model bounds

selected := false

rl.DisableCursor()
rl.SetTargetFPS(60)

for !rl.WindowShouldClose() {
rl.BeginDrawing()
// Update
rl.UpdateCamera(&camera, rl.CameraFirstPerson)

// Load new models/textures on drag&drop
if rl.IsFileDropped() {
droppedFiles := rl.LoadDroppedFiles()

if len(droppedFiles) == 1 { // Only support one file dropped
if slices.Contains(supportedFileTypes, filepath.Ext(droppedFiles[0])) { // Model file formats supported
rl.UnloadModel(obj) // Unload previous model
obj = rl.LoadModel(droppedFiles[0]) // Load new model
rl.SetMaterialTexture(obj.Materials, rl.MapDiffuse, texture) // Set current map diffuse texture

meshes = unsafe.Slice(obj.Meshes, obj.MeshCount)
bounds = rl.GetMeshBoundingBox(meshes[0])

// TODO: Move camera position from target enough distance to visualize model properly
} else if filepath.Ext(droppedFiles[0]) == ".png" { // Texture file formats supported
// Unload current model texture and load new one
rl.UnloadTexture(texture)
texture = rl.LoadTexture(droppedFiles[0])
rl.SetMaterialTexture(obj.Materials, rl.MapDiffuse, texture) // Set current map diffuse texture
}
}

rl.UnloadDroppedFiles() // Unload file paths from memory
}

// Select model on mouse click
if rl.IsMouseButtonPressed(rl.MouseButtonLeft) {
// Check collision between ray and box
if rl.GetRayCollisionBox(rl.GetMouseRay(rl.GetMousePosition(), camera), bounds).Hit {
selected = !selected
} else {
selected = false
}
}

rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)

rl.BeginMode3D(camera)

rl.DrawModel(obj, position, 1.0, rl.White) // Draw 3d model with texture
rl.DrawGrid(20, 10.0) // Draw a grid

rl.DrawGrid(20, 10.0) // Draw a grid
if selected {
rl.DrawBoundingBox(bounds, rl.Green) // Draw selection box
}

rl.EndMode3D()

rl.DrawText("Drag & drop model to load mesh/texture", 10, screenHeight-20, 10, rl.Gray)
if selected {
rl.DrawText("Model selected!", screenWidth-110, 10, 10, rl.Green)
}
rl.DrawText("(c) Castle 3D model by Alberto Cano", screenWidth-200, screenHeight-20, 10, rl.Gray)

rl.DrawFPS(10, 10)
rl.EndDrawing()
}

rl.UnloadTexture(texture) // Unload texture
rl.UnloadModel(obj) // Unload model

Expand Down
Loading