Added a custom mesh to the example models/mesh_generation
This commit is contained in:
parent
5e9516b94f
commit
0e82ab3a0d
1 changed files with 71 additions and 9 deletions
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -8,7 +10,7 @@ func main() {
|
||||||
screenWidth := int32(1280)
|
screenWidth := int32(1280)
|
||||||
screenHeight := int32(720)
|
screenHeight := int32(720)
|
||||||
|
|
||||||
numModels := 8
|
numModels := 9
|
||||||
|
|
||||||
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation")
|
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[5] = rl.LoadModelFromMesh(rl.GenMeshTorus(0.25, 4, 16, 32))
|
||||||
models[6] = rl.LoadModelFromMesh(rl.GenMeshKnot(1, 2, 16, 128))
|
models[6] = rl.LoadModelFromMesh(rl.GenMeshKnot(1, 2, 16, 128))
|
||||||
models[7] = rl.LoadModelFromMesh(rl.GenMeshPoly(5, 2))
|
models[7] = rl.LoadModelFromMesh(rl.GenMeshPoly(5, 2))
|
||||||
|
models[8] = rl.LoadModelFromMesh(GenMeshCustom())
|
||||||
|
|
||||||
for i := 0; i < numModels; i++ {
|
for i := 0; i < numModels; i++ {
|
||||||
rl.SetMaterialTexture(models[i].Materials, rl.MapDiffuse, texture)
|
rl.SetMaterialTexture(models[i].Materials, rl.MapDiffuse, texture)
|
||||||
|
@ -49,12 +52,17 @@ func main() {
|
||||||
rl.UpdateCamera(&camera, rl.CameraOrbital)
|
rl.UpdateCamera(&camera, rl.CameraOrbital)
|
||||||
|
|
||||||
if rl.IsKeyPressed(rl.KeyUp) {
|
if rl.IsKeyPressed(rl.KeyUp) {
|
||||||
currentModel++
|
currentModel = (currentModel + 1) % numModels // Cycle between the textures
|
||||||
if currentModel >= numModels {
|
}
|
||||||
currentModel = 0
|
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.BeginDrawing()
|
||||||
|
|
||||||
rl.ClearBackground(rl.RayWhite)
|
rl.ClearBackground(rl.RayWhite)
|
||||||
|
@ -66,9 +74,10 @@ func main() {
|
||||||
|
|
||||||
rl.EndMode3D()
|
rl.EndMode3D()
|
||||||
|
|
||||||
rl.DrawRectangle(10, 10, 310, 30, rl.Fade(rl.SkyBlue, 0.5))
|
rl.DrawRectangle(10, 10, 310, 50, rl.Fade(rl.SkyBlue, 0.5))
|
||||||
rl.DrawRectangleLines(10, 10, 310, 30, rl.Fade(rl.DarkBlue, 0.5))
|
rl.DrawRectangleLines(10, 10, 310, 50, rl.Fade(rl.DarkBlue, 0.5))
|
||||||
rl.DrawText("UP ARROW KEY TO CHANGE MODELS", 20, 20, 10, rl.Blue)
|
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"
|
txt := "PLANE"
|
||||||
switch currentModel {
|
switch currentModel {
|
||||||
|
@ -86,6 +95,8 @@ func main() {
|
||||||
txt = "KNOT"
|
txt = "KNOT"
|
||||||
case 7:
|
case 7:
|
||||||
txt = "POLY"
|
txt = "POLY"
|
||||||
|
case 8:
|
||||||
|
txt = "Custom (triangle)"
|
||||||
}
|
}
|
||||||
txtlen := rl.MeasureText(txt, 20)
|
txtlen := rl.MeasureText(txt, 20)
|
||||||
rl.DrawText(txt, screenWidth/2-txtlen/2, 10, 20, rl.DarkBlue)
|
rl.DrawText(txt, screenWidth/2-txtlen/2, 10, 20, rl.DarkBlue)
|
||||||
|
@ -94,9 +105,60 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.UnloadTexture(texture)
|
rl.UnloadTexture(texture)
|
||||||
|
clearCustomMesh(models)
|
||||||
for i := 0; i < numModels; i++ {
|
for i := 0; i < numModels; i++ {
|
||||||
rl.UnloadModel(models[i])
|
rl.UnloadModel(models[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.CloseWindow()
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue