About Monkey 2 › Forums › Monkey 2 Programming Help › terrain – Release and Debug mojo3d diferent behaviour.
This topic contains 5 replies, has 3 voices, and was last updated by
impixi
1 year, 9 months ago.
-
AuthorPosts
-
July 7, 2017 at 10:00 pm #9172
I modified the terrain test and added my own terrain generator. I put the start code in a function so it can be called to create a new terrain by pressing space. But I am getting different results with emscripten en wasm in release and debug mode. In release mode the terrain seems not to be cleared? The terrain seems to grow every time you press space, In debug mode it does not seem to do this. In the desktop mode (release and debug) I am also getting different types of maps.
Did I stumble on a bug or am I not doing something I should do?
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Global mapsize:Int=512Class MyWindow Extends WindowField _scene:SceneField _fog:FogEffectField _camera:CameraField _light:LightField _material:MaterialField _terrain:TerrainMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )startgame()End MethodMethod OnRender( canvas:Canvas ) OverrideRequestRender()Fly( _camera,Self )_scene.Render( canvas,_camera )If Keyboard.KeyReleased(Key.Space) Then startgame()canvas.DrawText( "Width="+Width+", Height="+Height+", FPS="+App.FPS,0,0 )canvas.DrawText("Cursor up/down/left/right a/z and Left mouse button - space = new map.",0,20)End MethodMethod startgame()If _terrain Then _terrain.Destroy()If _camera Then _camera.Destroy()If _light Then _light.Destroy()If _material Then _material.Discard()If _scene Then_scene.Terrains.Clear()End If_scene = Scene.GetCurrent()_fog=New FogEffect( Color.Sky,480,512 )'create camera'_camera=New Camera_camera.Near=1_camera.Far=512_camera.Move( 0,66,0 )'create light'_light=New Light_light.RotateX( Pi/2 ) 'aim directional light 'down' - Pi/2=90 degrees._material = New PbrMaterial( Color.Brown,1,0.5 )_material.ScaleTextureMatrix( 32,32 )Local heightMap:= New Pixmap(mapsize,mapsize)heightMap = makeheightmap()_terrain=New Terrain( heightMap,New Boxf( -mapsize,0,-mapsize,mapsize,64,mapsize ),_material )heightMap.Discard()End MethodEnd'' This function creates a pixmap with a random map' inside it for the use of the mojo3d terrain.''Function makeheightmap:Pixmap()SeedRnd(Millisecs()) 'Different seed is different mapLocal pm:Pixmappm = New Pixmap(mapsize,mapsize)' This is a lambda function that increases the' color on the pixmap by a bit.Local myrect:=Lambda(x1:int,y1:Int,w:Int,h:Int)Local inc:Float=Rnd(-0.1,0.1)For Local y2:=y1 Until y1+hFor Local x2:=x1 Until x1+wIf x2>=0 And x2<mapsize And y2>=0 And y2<mapsizeLocal mc:Colormc = pm.GetPixel(x2,y2)Local r:Float=mc.r + incLocal g:Float=mc.g + incLocal b:Float=mc.b + incIf r>1 Then r=1If g>1 Then g=1If b>1 Then b=1If r<0 Then r=0If g<0 Then g=0If b<0 Then b=0pm.SetPixel(x2,y2,New Color(r,g,b))End IfNextNextEnd Lambda' This lambda takes one input coordinate where' it takes a color and next and below it also a color' this then is avaraged(divided by 3) and put back' on the pixmap(blurring/smoothing it)'Local blur:=Lambda(x1:Int,y1:Int)Local c1:Color = pm.GetPixel(x1,y1)Local c2:Color = pm.GetPixel(x1+1,y1)Local c3:Color = pm.GetPixel(x1,y1+1)Local nr:Float=(c1.r+c2.r+c3.r)/3pm.SetPixel(x1,y1,New Color(nr,nr,nr))End Lambda' Here we create a mapFor Local i:=0 Until (mapsize*mapsize)/500Local x:Int=Rnd(-50,mapsize)Local y:Int=Rnd(-50,mapsize)Local w:Int=Rnd(5,mapsize/4)Local h:Int=Rnd(5,mapsize/4)myrect(x,y,w,h)NextFor Local i:=0 Until (mapsize*mapsize)*3blur(Rnd(1,mapsize-1),Rnd(1,mapsize-1))NextReturn pmEnd Function' Taken from the mojo3d testFunction Fly( entity:Entity,view:View )If Keyboard.KeyDown( Key.Up )entity.RotateX( .1 )Else If Keyboard.KeyDown( Key.Down )entity.RotateX( -.1 )EndifIf Keyboard.KeyDown( Key.Q )entity.RotateZ( .1 )Else If Keyboard.KeyDown( Key.W )entity.RotateZ( -.1 )EndifIf Keyboard.KeyDown( Key.Left )entity.RotateY( .1,True )Else If Keyboard.KeyDown( Key.Right )entity.RotateY( -.1,True )EndifIf Mouse.ButtonDown( MouseButton.Left )If Mouse.X<view.Width/3entity.RotateY( .1,True )Else If Mouse.X>view.Width/3*2entity.RotateY( -.1,True )Elseentity.Move( New Vec3f( 0,0,.1 ) )EndifEndifIf Keyboard.KeyDown( Key.A )entity.MoveZ( .5 ) '( New Vec3f( 0,0,.1 ) )Else If Keyboard.KeyDown( Key.Z )entity.MoveZ( -.5 ) '( New Vec3f( 0,0,-.1 ) )EndifEnd FunctionFunction Main()New AppInstanceNew MyWindowApp.Run()EndJuly 8, 2017 at 12:54 am #9174Bugs, I’d say. I’m seeing similar results from my own terrain generator (quite different than yours).
But remember Mojo3d is still a WIP Alpha, so much of it is not “production” ready yet.
July 9, 2017 at 11:42 pm #9208Had me going for a while, but the main problem here is that pixmaps are not cleared by default.
Add a pm.Clear( Color.Black ) after the New Pixmap(…).
July 10, 2017 at 1:36 am #9217I just discovered my bug was forgetting to clear the scene’s Terrains stack, so on each runtime call to my generate function a new terrain model was created. It looked like an addition to the existing terrain model… FFS, I’m making a lot of rookie mistakes lately I’m just about ready to quit coding forever…
July 10, 2017 at 1:42 am #9219The scene Terrain stack is internal and may yet change – using Entity.Destroy() is the correct way to remove something from a scene (assuming it works!).
There are many internal/experimental features like this. When in doubt, consult the docs, all the internal stuff should be hidden.
It’s probably about time to add a real ‘Internal’ directive to the language…
FFS, I’m making a lot of rookie mistakes lately I’m just about ready to quit coding forever…
Well, it’s not like any of this is particularly well docced!
July 10, 2017 at 2:17 am #9221using Entity.Destroy() is the correct way to remove something from a scene (assuming it works!).
Just tried Destroy() and it does indeed work for terrain objects. Thanks for the tip.
-
AuthorPosts
You must be logged in to reply to this topic.