From 0e82ab3a0d57bdcc0af6775ad93c695816860638 Mon Sep 17 00:00:00 2001 From: Per Hultqvist Date: Mon, 11 Nov 2024 08:53:10 +0100 Subject: [PATCH] Added a custom mesh to the example models/mesh_generation --- examples/models/mesh_generation/main.go | 80 ++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/examples/models/mesh_generation/main.go b/examples/models/mesh_generation/main.go index d7c7da1..555277c 100644 --- a/examples/models/mesh_generation/main.go +++ b/examples/models/mesh_generation/main.go @@ -1,6 +1,8 @@ package main import ( + "unsafe" + rl "github.com/gen2brain/raylib-go/raylib" ) @@ -8,7 +10,7 @@ func main() { screenWidth := int32(1280) screenHeight := int32(720) - numModels := 8 + numModels := 9 rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation") @@ -33,6 +35,7 @@ func main() { models[5] = rl.LoadModelFromMesh(rl.GenMeshTorus(0.25, 4, 16, 32)) models[6] = rl.LoadModelFromMesh(rl.GenMeshKnot(1, 2, 16, 128)) models[7] = rl.LoadModelFromMesh(rl.GenMeshPoly(5, 2)) + models[8] = rl.LoadModelFromMesh(GenMeshCustom()) for i := 0; i < numModels; i++ { rl.SetMaterialTexture(models[i].Materials, rl.MapDiffuse, texture) @@ -49,12 +52,17 @@ func main() { rl.UpdateCamera(&camera, rl.CameraOrbital) if rl.IsKeyPressed(rl.KeyUp) { - currentModel++ - if currentModel >= numModels { - currentModel = 0 - } + currentModel = (currentModel + 1) % numModels // Cycle between the textures + } + if rl.IsKeyPressed(rl.KeyDown) { + // Adding numModels here is necessary to avoid a crash + // where the golang % (modulus) operator, doesn't work as + // one might expect for negative numbers. + currentModel = (currentModel + numModels - 1) % numModels // Cycle between the textures + } + if rl.IsMouseButtonPressed(rl.MouseButtonLeft) { + currentModel = (currentModel + 1) % numModels // Cycle between the textures } - rl.BeginDrawing() rl.ClearBackground(rl.RayWhite) @@ -66,9 +74,10 @@ func main() { rl.EndMode3D() - rl.DrawRectangle(10, 10, 310, 30, rl.Fade(rl.SkyBlue, 0.5)) - rl.DrawRectangleLines(10, 10, 310, 30, rl.Fade(rl.DarkBlue, 0.5)) - rl.DrawText("UP ARROW KEY TO CHANGE MODELS", 20, 20, 10, rl.Blue) + rl.DrawRectangle(10, 10, 310, 50, rl.Fade(rl.SkyBlue, 0.5)) + rl.DrawRectangleLines(10, 10, 310, 50, rl.Fade(rl.DarkBlue, 0.5)) + rl.DrawText("UP/DOWN ARROW KEY OR LEFT MOUSE", 20, 20, 10, rl.Blue) + rl.DrawText("BUTTON TO CHANGE MODELS", 20, 40, 10, rl.Blue) txt := "PLANE" switch currentModel { @@ -86,6 +95,8 @@ func main() { txt = "KNOT" case 7: txt = "POLY" + case 8: + txt = "Custom (triangle)" } txtlen := rl.MeasureText(txt, 20) rl.DrawText(txt, screenWidth/2-txtlen/2, 10, 20, rl.DarkBlue) @@ -94,9 +105,60 @@ func main() { } rl.UnloadTexture(texture) + clearCustomMesh(models) for i := 0; i < numModels; i++ { rl.UnloadModel(models[i]) } rl.CloseWindow() } + +func clearCustomMesh(models []rl.Model) { + // For some reason the custom model + // (id = 8) panics when unloading it. + // So we clear the mesh for it manually + // here. + models[8].Meshes.Vertices = nil + models[8].Meshes.Normals = nil + models[8].Meshes.Texcoords = nil +} + +// GenMeshCustom generates a simple triangle mesh from code +func GenMeshCustom() rl.Mesh { + mesh := rl.Mesh{ + TriangleCount: 1, + VertexCount: 3, + } + + var vertices, normals, texcoords []float32 + + // 3 vertices + vertices = addCoord(vertices, 0, 0, 0) + vertices = addCoord(vertices, 1, 0, 2) + vertices = addCoord(vertices, 2, 0, 0) + mesh.Vertices = unsafe.SliceData(vertices) + + // 3 normals + normals = addCoord(normals, 0, 1, 0) + normals = addCoord(normals, 0, 1, 0) + normals = addCoord(normals, 0, 1, 0) + mesh.Normals = unsafe.SliceData(normals) + + // 3 texcoords + texcoords = addCoord(texcoords, 0, 0) + texcoords = addCoord(texcoords, 0.5, 1) + texcoords = addCoord(texcoords, 1, 0) + mesh.Texcoords = unsafe.SliceData(texcoords) + + // Upload mesh data from CPU (RAM) to GPU (VRAM) memory + rl.UploadMesh(&mesh, false) + + return mesh +} + +func addCoord(slice []float32, values ...float32) []float32 { + for _, value := range values { + slice = append(slice, value) + } + return slice +}