About Monkey 2 › Forums › Monkey 2 Programming Help › Need help converting model to triangles!
This topic contains 3 replies, has 2 voices, and was last updated by
DruggedBunny
11 months ago.
-
AuthorPosts
-
May 10, 2018 at 1:29 am #14608
I’m really struggling with visualising the whole mesh/vertex/index/triangle thing!
It doesn’t help that I don’t really understand what the indices are, and how they relate to triangles…
So far I have a ModelFromTriangle:Model function (at the top) that takes a model and attempts to return a single triangle from the model’s mesh, but I think even that may be down to poor understanding.
It produces what looks like a cube from a distance (the original gets hidden), but as you approach (A/Z, cursors) it becomes clear I’ve got the wrong end of the stick completely. Moving the model to z=10 even adds a weird black triangle here. Enable the sphere version for an even more bizarre result!
I basically want to see how turning a model into triangles, and then turning those into physics bodies, goes — probably painfully slowly, but ought to look really cool, and worth a try (for my Bust! demo)…
Not taking into account materials or repositioning to match the original at this stage, just trying to recreate the right triangles from the model using a hard-coded material for now…
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202[crayon-5cb9b9d9019ac632067179 inline="true" ]#Import "<std>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Global AppName:String = "My 3D Game"' TODO: Return array of tri models??' Single tri...Function ModelFromTriangle:Model (in_model:Model, index:UInt)Local mesh:Mesh = in_model.MeshLocal tri_model:Model = New Modeltri_model.Material = New PbrMaterial (Color.Red)tri_model.Material.CullMode = CullMode.NoneAssert (index < (mesh.NumIndices - 1) - 2, "index too high") ' Think that's right...Local tri_verts:Vertex3f [] = New Vertex3f [3]' mesh-local co-ords...tri_verts [0] = mesh.GetVertex (index + 0)tri_verts [1] = mesh.GetVertex (index + 1)tri_verts [2] = mesh.GetVertex (index + 2)Local tri_indices:UInt [] = New UInt [3]tri_indices [0] = 0tri_indices [1] = 1tri_indices [2] = 2Local tri_mesh:Mesh = New Mesh (tri_verts, tri_indices)tri_mesh.AddMesh (tri_mesh)tri_mesh.UpdateNormals ()tri_mesh.UpdateTangents ()tri_model.Mesh = tri_meshReturn tri_modelEndClass Game Extends WindowConst SHIFT_BOOST:Float = 5.0Field camera_boost:Float = 1.0' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:Light' Test cube...Field cube:ModelMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)scene = Scene.GetCurrent () ' Important!camera = New Camera' Camera settings...camera.Near = 0.1' Camera position...camera.Move (0, 0, -2)'camera.Viewport = Window.Rectlight = New Lightlight.Move (-10, 10, -10)cube = Model.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5), 1, 1, 1, New PbrMaterial (Color.Aluminum))' cube.Rotate (0, 45, 0)' This is... beautiful!'cube = Model.CreateSphere (0.5, 32, 32, New PbrMaterial (Color.Aluminum))Local ticks:Int = Millisecs ()For Local loop:UInt = 0 Until cube.Mesh.NumIndices - 3 Step 3Print loopLocal test:Model = ModelFromTriangle (cube, loop)test.Move (0, 0, 10)Next' Hide original!cube.Visible = FalsePrint Millisecs () - ticksEndMethod UpdateGame:Void ()EndMethod OnRender (canvas:Canvas) OverrideProcessInput ()UpdateGame ()cube.Rotate (0.0, 1.0, 0.0)' Tell app to draw frame when ready...RequestRender ()' Render scene to canvas (passed to OnRender by mojo), from camera...scene.Render (canvas)canvas.DrawText ("VP:" + camera.Viewport, 20, 20)EndMethod ProcessInput:Void ()If Keyboard.KeyHit (Key.Space) Then light.CastsShadow = Not light.CastsShadowIf Keyboard.KeyDown (Key.LeftShift)camera_boost = SHIFT_BOOSTElsecamera_boost = 1.0EndifIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.A)camera.Move (0.0, 0.0, 0.1 * camera_boost)EndifIf Keyboard.KeyDown (Key.Z)camera.Move (0.0, 0.0, -0.1 * camera_boost)EndifIf Keyboard.KeyDown (Key.Left)camera.Rotate (0.0, 1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Right)camera.Rotate (0.0, -1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Up)camera.Rotate (1.0, 0.0, 0.0, True)EndifIf Keyboard.KeyDown (Key.Down)camera.Rotate (-1.0, 0.0, 0.0, true)EndifEndEndFunction Main ()' Windowed mode...' Local width:Int = 640' Local height:Int = 480' Local flags:WindowFlags = WindowFlags.Center' Full-screen mode (comment out above)...Local width:Int = 1024Local height:Int = 768Local flags:WindowFlags = WindowFlags.Center'FullscreenNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()EndMay 11, 2018 at 8:04 pm #14613You need to get the source vertices via their index:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205#Import "<std>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Global AppName:String = "My 3D Game"' TODO: Return array of tri models??' Single tri...Function ModelFromTriangle:Model (in_model:Model, index:UInt)Local mesh:Mesh = in_model.MeshLocal tri_model:Model = New Modeltri_model.Material = New PbrMaterial (Color.Red)tri_model.Material.CullMode = CullMode.NoneAssert (index + 2 < mesh.NumIndices , "index too high") ' Think that's right...Local indices:=mesh.GetIndices()Local tri_verts:Vertex3f [] = New Vertex3f [3]' mesh-local co-ords...tri_verts [0] = mesh.GetVertex (indices[index + 0])tri_verts [1] = mesh.GetVertex (indices[index + 1])tri_verts [2] = mesh.GetVertex (indices[index + 2])Local tri_indices:UInt [] = New UInt [3]tri_indices [0] = 0tri_indices [1] = 1tri_indices [2] = 2Local tri_mesh:Mesh = New Mesh (tri_verts, tri_indices)' tri_mesh.AddMesh (tri_mesh)tri_mesh.UpdateNormals ()tri_mesh.UpdateTangents ()tri_model.Mesh = tri_meshReturn tri_modelEndClass Game Extends WindowConst SHIFT_BOOST:Float = 5.0Field camera_boost:Float = 1.0' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:Light' Test cube...Field cube:ModelMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)scene = Scene.GetCurrent () ' Important!camera = New Camera' Camera settings...camera.Near = 0.1' Camera position...camera.Move (0, 0, -2)'camera.Viewport = Window.Rectlight = New Lightlight.Move (-10, 10, -10)' cube = Model.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5), 1, 1, 1, New PbrMaterial (Color.Aluminum))' cube.Rotate (0, 45, 0)' This is... beautiful!cube = Model.CreateSphere (0.5, 32, 32, New PbrMaterial (Color.Aluminum))Local ticks:Int = Millisecs ()For Local loop:UInt = 0 Until cube.Mesh.NumIndices Step 3Print loopLocal test:Model = ModelFromTriangle (cube, loop)' test.Move (loop*0.125, 0, 1)test.Move (0, 0, 1)Next' Hide original!cube.Visible = FalsePrint Millisecs () - ticksEndMethod UpdateGame:Void ()EndMethod OnRender (canvas:Canvas) OverrideProcessInput ()UpdateGame ()cube.Rotate (0.0, 1.0, 0.0)' Tell app to draw frame when ready...RequestRender ()' Render scene to canvas (passed to OnRender by mojo), from camera...scene.Render (canvas)canvas.DrawText ("VP:" + camera.Viewport, 20, 20)EndMethod ProcessInput:Void ()If Keyboard.KeyHit (Key.Space) Then light.CastsShadow = Not light.CastsShadowIf Keyboard.KeyDown (Key.LeftShift)camera_boost = SHIFT_BOOSTElsecamera_boost = 1.0EndifIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.A)camera.Move (0.0, 0.0, 0.1 * camera_boost)EndifIf Keyboard.KeyDown (Key.Z)camera.Move (0.0, 0.0, -0.1 * camera_boost)EndifIf Keyboard.KeyDown (Key.Left)camera.Rotate (0.0, 1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Right)camera.Rotate (0.0, -1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Up)camera.Rotate (1.0, 0.0, 0.0, True)EndifIf Keyboard.KeyDown (Key.Down)camera.Rotate (-1.0, 0.0, 0.0, true)EndifEndEndFunction Main ()' Windowed mode...' Local width:Int = 640' Local height:Int = 480' Local flags:WindowFlags = WindowFlags.Center' Full-screen mode (comment out above)...Local width:Int = 1024Local height:Int = 768Local flags:WindowFlags = WindowFlags.Center'FullscreenNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()EndMay 11, 2018 at 10:24 pm #14614Ahhhh… thanks, Simon, that’s awesome, will try take it in, but I can at least get to work now! Thanks again.
May 18, 2018 at 12:25 am #14650Getting somewhere with this, with a lot of hand-waving…
Messy and still needs a lot of work, eg. triangles’ physics bodies are boxes so they don’t settle right (think I will try build rects from them instead), but at least the ball’s disintegration looks sort-of OK. Ideally it would disintegrate as each triangle intersects the ground, instead of the instant the ball touches the ground, but it’s basically doing what I was hoping for.
The triangles seem to disappear initially, but I think it’s because they turn edge-on, being infinitely thin.
Bit slow on WebGL, unsurprisingly.
In real life, triangles would be replaced with pre-generated model chunks.
-
AuthorPosts
You must be logged in to reply to this topic.