About Monkey 2 › Forums › Monkey 2 Programming Help › mojo3d:Chunks creation problems (memory access violation)
This topic contains 5 replies, has 3 voices, and was last updated by
Mark Sibly
1 year ago.
-
AuthorPosts
-
March 25, 2018 at 5:21 am #14128
I am kind of stuck with this. I changed my createcube method to having more vertices so I can put textures on them but then I got these errors thrown every time I got more or less in the center of the maps.
I modified a example to get these errors thrown again. It seems that there is some kind of limit? in the amount of meshes that can be created? When I recreated the models without recreating the messes I could fill the screen until the fps got to 0. I have been at it all night and maybe there is something really simple wrong.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class MyWindow Extends WindowField _scene:Scene'The size of the chunks (Minecraft has 16x128*16 chunks)Field chunkwidth:Int=16Field chunkheight:Int=64Field chunkdepth:Int=16Field _camera:CameraField _light:LightField _model:ModelField _rectCanvas:CanvasField _rectImage:ImageMethod New()'get scene'_scene=Scene.GetCurrent()'create camera'_camera=New Camera_camera.Near=.1_camera.Far=100_camera.Move( 0,0,-35 )'create light'_light=New Light_light.Move(110,110,120)_light.PointAt(New Vec3f(0,0,0))' _light.RotateX( 90 ) 'aim directional light downwards - 90 degrees.'Here we create our chunk arrayLocal chunk:Int[,,] = New Int[chunkwidth,chunkheight,chunkdepth]For Local z:Int=0 Until chunkdepthFor Local y:Int=0 Until chunkheightFor Local x:Int=0 Until chunkwidthchunk[x,y,z] = 1NextNextNextFor Local z:Int=0 Until chunkdepthFor Local y:Int=0 Until chunkheightFor Local x:Int=2 Until chunkwidth-2chunk[x,y,z] = Int(Rnd(0,2))If Rnd() <.7 Then chunk[x,y,z]=0NextNextNext'Here we create our chunk meshes to many chunks here -4 to 4 gives'memory acces errorFor Local x2:Int=-4 To 4For Local z2:Int=-4 To 4Local chunkmesh:= New Mesh()For Local z:Int=0 Until chunkdepthFor Local y:Int=0 Until chunkheightFor Local x:Int=0 Until chunkwidthIf chunk[x,y,z] <> 0 ThenLocal sides:Bool[] = New Bool[6]If z-1>=0 And chunk[x,y,z-1] <> 0 Then sides[0] = False Else sides[0] = TrueIf x-1>=0 And chunk[x-1,y,z] <> 0 Then sides[3] = False Else sides[3] = TrueIf z+1<chunkdepth And chunk[x,y,z+1] <> 0 Then sides[1] = False Else sides[1] = TrueIf x+1<chunkwidth And chunk[x+1,y,z] <> 0 Then sides[2] = False Else sides[2] = TrueIf y+1<chunkheight And chunk[x,y+1,z] <> 0 Then sides[4] = False Else sides[4] = TrueIf y-1>=0 And chunk[x,y-1,z] <> 0 Then sides[5] = False Else sides[5] = TrueLocal mesh2:=createcube(x*2,y*2,z*2,sides)chunkmesh.AddMesh(mesh2)endifNextNextNext' Here we create our model containing the chunkchunkmesh.UpdateNormals()_model=New Model_model.Mesh=chunkmesh_model.Materials = _model.Materials.Resize(chunkmesh.NumMaterials)Local sm:= New PbrMaterial()_rectImage=New Image( 256, 256 )_rectCanvas=New Canvas( _rectImage )sm.ColorTexture = _rectImage.Texturesm.ColorTexture.Flags = TextureFlags.FilterMipmap'sm.CullMode=CullMode.None_model.Materials[chunkmesh.NumMaterials - 1] = sm_model.Move(x2*2*chunkwidth,0,z2*2*chunkdepth)NextNextEnd MethodMethod OnRender( canvas:Canvas ) OverrideRequestRender()RenderTexture()controls()_scene.Update()_scene.Render( canvas,_camera )canvas.DrawText( "Width="+Width+", Height="+Height+", FPS="+App.FPS,0,0 )If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()End Method'sides (0-front,1=left,2=back,3=right,4=top,5=bottom)'x,y,z is location in the chunkMethod createcube:Mesh(x:Float=0,y:Float=0,z:Float=0,sides:Bool[])'create cube mesh'Local vertices:=New Vertex3f[24]'frontvertices[0].position=New Vec3f( -1+x, 1+y,-1+z )'left front topvertices[1].position=New Vec3f( 1+x, 1+y,-1+z )'right front topvertices[2].position=New Vec3f( 1+x,-1+y,-1+z )'right front bottomvertices[3].position=New Vec3f( -1+x,-1+y,-1+z )'left front bottom'backvertices[4].position=New Vec3f( 1+x, 1+y, 1+z )'right back topvertices[5].position=New Vec3f( -1+x, 1+y, 1+z )'left back topvertices[6].position=New Vec3f( -1+x,-1+y, 1+z )'left back bottomvertices[7].position=New Vec3f( 1+x,-1+y, 1+z )'right back bottom'rightvertices[8].position=New Vec3f( 1+x, 1+y,-1+z )'right front topvertices[9].position=New Vec3f( 1+x, 1+y, 1+z )'right back topvertices[10].position=New Vec3f( 1+x,-1+y, 1+z )'right back bottomvertices[11].position=New Vec3f( 1+x,-1+y,-1+z )'right front bottom'leftvertices[12].position=New Vec3f( -1+x, 1+y, 1+z )'left back topvertices[13].position=New Vec3f( -1+x, 1+y,-1+z )'left front topvertices[14].position=New Vec3f( -1+x,-1+y,-1+z )'left front bottomvertices[15].position=New Vec3f( -1+x,-1+y, 1+z )'left back bottom'topvertices[16].position=New Vec3f( -1+x, 1+y, 1+z )'left back topvertices[17].position=New Vec3f( 1+x, 1+y, 1+z )'right back topvertices[18].position=New Vec3f( 1+x, 1+y,-1+z )'right front topvertices[19].position=New Vec3f( -1+x, 1+y,-1+z )'left front top'bottomvertices[20].position=New Vec3f( -1+x,-1+y,-1+z )'left front bottomvertices[21].position=New Vec3f( 1+x,-1+y,-1+z )'right front bottomvertices[22].position=New Vec3f( 1+x,-1+y, 1+z )'right back bottomvertices[23].position=New Vec3f( -1+x,-1+y, 1+z )'left back bottom' Texture coordinates represent coordinates within the image, where' 0,0=top left, 1,0=top right, 1,1=bottom right, 0,1=bottom left'front texturevertices[0].texCoord0 = New Vec2f(0,0)vertices[1].texCoord0 = New Vec2f(1,0)vertices[2].texCoord0 = New Vec2f(1,1)vertices[3].texCoord0 = New Vec2f(0,1)'back texturevertices[4].texCoord0 = New Vec2f(0,0)vertices[5].texCoord0 = New Vec2f(1,0)vertices[6].texCoord0 = New Vec2f(1,1)vertices[7].texCoord0 = New Vec2f(0,1)'right texturevertices[8].texCoord0 = New Vec2f(0,0)vertices[9].texCoord0 = New Vec2f(1,0)vertices[10].texCoord0 = New Vec2f(1,1)vertices[11].texCoord0 = New Vec2f(0,1)'left texturevertices[12].texCoord0 = New Vec2f(0,0)vertices[13].texCoord0 = New Vec2f(1,0)vertices[14].texCoord0 = New Vec2f(1,1)vertices[15].texCoord0 = New Vec2f(0,1)'top texturevertices[16].texCoord0 = New Vec2f(0,0)vertices[17].texCoord0 = New Vec2f(1,0)vertices[18].texCoord0 = New Vec2f(1,1)vertices[19].texCoord0 = New Vec2f(0,1)'bottom texturevertices[20].texCoord0 = New Vec2f(0,0)vertices[21].texCoord0 = New Vec2f(1,0)vertices[22].texCoord0 = New Vec2f(1,1)vertices[23].texCoord0 = New Vec2f(0,1)'Local indices:=New UInt[36]Local cnt:Int=0'frontIf sides[0]indices[cnt]=0;cnt+=1indices[cnt]=1;cnt+=1indices[cnt]=2;cnt+=1indices[cnt]=0;cnt+=1indices[cnt]=2;cnt+=1indices[cnt]=3;cnt+=1End if'back sideIf sides[1]indices[cnt]=4;cnt+=1indices[cnt]=5;cnt+=1indices[cnt]=6;cnt+=1indices[cnt]=4;cnt+=1indices[cnt]=6;cnt+=1indices[cnt]=7;cnt+=1End If'right sideIf sides[2]indices[cnt]=8;cnt+=1indices[cnt]=9;cnt+=1indices[cnt]=10;cnt+=1indices[cnt]=8;cnt+=1indices[cnt]=10;cnt+=1indices[cnt]=11;cnt+=1End if' left sideIf sides[3]indices[cnt]=12;cnt+=1indices[cnt]=13;cnt+=1indices[cnt]=14;cnt+=1indices[cnt]=12;cnt+=1indices[cnt]=14;cnt+=1indices[cnt]=15;cnt+=1End If'top sideIf sides[4]indices[cnt]=16;cnt+=1indices[cnt]=17;cnt+=1indices[cnt]=18;cnt+=1indices[cnt]=16;cnt+=1indices[cnt]=18;cnt+=1indices[cnt]=19;cnt+=1End If'bottom sideIf sides[5]indices[cnt]=20;cnt+=1indices[cnt]=21;cnt+=1indices[cnt]=22;cnt+=1indices[cnt]=20;cnt+=1indices[cnt]=22;cnt+=1indices[cnt]=23;cnt+=1End IfReturn New Mesh( vertices,indices )End MethodMethod controls()If Keyboard.KeyDown(Key.W) Then _camera.Move(0,0,.5)If Keyboard.KeyDown(Key.S) Then _camera.Move(0,0,-.5)If Keyboard.KeyDown(Key.A) Then _camera.Move(-.5,0,0)If Keyboard.KeyDown(Key.D) Then _camera.Move(.5,0,0)If Keyboard.KeyDown(Key.Up) Then _camera.Rotate(1,0,0)If Keyboard.KeyDown(Key.Down) Then _camera.Rotate(-1,0,0)If Keyboard.KeyDown(Key.Left) Then _camera.Rotate(0,1,0)If Keyboard.KeyDown(Key.Right) Then _camera.Rotate(0,-1,0)End MethodMethod RenderTexture()If Not _rectCanvas Then_rectCanvas=New Canvas( _rectImage )Endif'This should be orange with white text on'But since I'm drawing something in the top left corner -'I'm just getting that top left pixel on the entire rectangle_rectCanvas.Clear( Color.Blue )_rectCanvas.Color = Color.White_rectCanvas.DrawText( "Hello World", Rnd(8,12), 8 )_rectCanvas.Color = Color.Orange_rectCanvas.DrawRect( 50, 50 , 200 ,90) 'White in the top left_rectCanvas.Flush()End MethodEnd ClassFunction Main()New AppInstanceNew MyWindowApp.Run()EndMarch 25, 2018 at 3:33 pm #14130That code doesn’t seem to crash for me, though I did get a crash on the web demo once.
I notice you’re using an older version of mojo3d, though, as the current version of Scene.Render doesn’t take a camera parameter, so it might be worth upgrading to see if that resolves it.
March 25, 2018 at 3:46 pm #14131I uploaded a new emscripten version yesterday evening I think after also getting a crash. Maybe that fixed it.
Yes, I am using the current itch.io version.
As for the new monkey version. I think I will need to learn how to update it from the github. Patreon asks for a creditcard so I can not get the 1.09 version that way.
Maybe I can manually download the mojo3d files and rebuild? Wil try that first.
March 25, 2018 at 4:29 pm #14132Ok, I dowloaded a zip file from the monkey2 develop branch and unpacked it on my last monkey install. I rebuilt the desktop modules and the error has gone away.
March 25, 2018 at 4:41 pm #14133Yes, it’s crashing here if I set chunk sizes to 16, 128, 16 but it is also using about 2G memory which is the 32 bit limit and is likely to be causing problems (somehow). This shouldn’t be an issue on 64 bit windows really, although it will be on emscripten which is only 32 bit. You can check how much memory the app is using by starting task manager and watching the process memory usage growing.
Part of this is because the mojo3d vertex format is quite ‘heavyweight’ – 80 bytes per vertex. Something like minecraft really needs a very lightweight vertex format but you can’t do that in mojo3d yet. Also, Mesh really needs a Compact() method of some kind as meshes will currently be consuming about 2x what they need to be due to the internal stacks.
I’ll keep looking into this, but there is realistically likely to be some kind of 2G limit for monkey2 apps at the moment. It does also seem to be consuming more memory than I would have thought, which I’ll also look into.
Note that building meshes with the mesh=mesh+mesh2 will be very ineffecient. Each time you add a mesh, the old mesh must be copied, which ‘grows’ the output mesh, which gets incremental slower and slower etc. You’ll probably get much better performance if you build everything into an array first and just build the mesh once.
March 25, 2018 at 5:05 pm #14134Also, note that your code is creating *every* cube face here, even the hidden ones! The way you’ve done the sides[i] check effectively means that hidden sides will end up with ‘null’ indices (or possibly even incorrect indices) but the sides are actually still there and consuming memory and will even be ‘rendered’ by the gpu, although nothing willl be drawn because they are degenerate.
Here’s a version that skips sides properly and now works on my machine without crashing. Again, the more efficient way to do this would be to just writing the entire thing into one big array and create the mesh all at once, but this is an improvement!
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136'sides (0-front,1=left,2=back,3=right,4=top,5=bottom)'x,y,z is location in the chunkMethod createcube:Mesh(x:Float=0,y:Float=0,z:Float=0,sides:Bool[])'create cube mesh'Local vertices:=New Vertex3f[24],nv:=0Local indices:=New UInt[36],ni:=0'frontIf sides[0]vertices[nv+0].position=New Vec3f( -1+x, 1+y,-1+z )'left front topvertices[nv+1].position=New Vec3f( 1+x, 1+y,-1+z )'right front topvertices[nv+2].position=New Vec3f( 1+x,-1+y,-1+z )'right front bottomvertices[nv+3].position=New Vec3f( -1+x,-1+y,-1+z )'left front bottomvertices[nv+0].texCoord0 = New Vec2f(0,0)vertices[nv+1].texCoord0 = New Vec2f(1,0)vertices[nv+2].texCoord0 = New Vec2f(1,1)vertices[nv+3].texCoord0 = New Vec2f(0,1)indices[ni+0]=nv+0indices[ni+1]=nv+1indices[ni+2]=nv+2indices[ni+3]=nv+0indices[ni+4]=nv+2indices[ni+5]=nv+3nv+=4ni+=6Endif'backIf sides[1]vertices[nv+0].position=New Vec3f( 1+x, 1+y, 1+z )'right back topvertices[nv+1].position=New Vec3f( -1+x, 1+y, 1+z )'left back topvertices[nv+2].position=New Vec3f( -1+x,-1+y, 1+z )'left back bottomvertices[nv+3].position=New Vec3f( 1+x,-1+y, 1+z )'right back bottomvertices[nv+0].texCoord0 = New Vec2f(0,0)vertices[nv+1].texCoord0 = New Vec2f(1,0)vertices[nv+2].texCoord0 = New Vec2f(1,1)vertices[nv+3].texCoord0 = New Vec2f(0,1)indices[ni+0]=nv+0indices[ni+1]=nv+1indices[ni+2]=nv+2indices[ni+3]=nv+0indices[ni+4]=nv+2indices[ni+5]=nv+3nv+=4ni+=6Endif'rightIf sides[2]vertices[nv+0].position=New Vec3f( 1+x, 1+y,-1+z )'right front topvertices[nv+1].position=New Vec3f( 1+x, 1+y, 1+z )'right back topvertices[nv+2].position=New Vec3f( 1+x,-1+y, 1+z )'right back bottomvertices[nv+3].position=New Vec3f( 1+x,-1+y,-1+z )'right front bottomvertices[nv+0].texCoord0 = New Vec2f(0,0)vertices[nv+1].texCoord0 = New Vec2f(1,0)vertices[nv+2].texCoord0 = New Vec2f(1,1)vertices[nv+3].texCoord0 = New Vec2f(0,1)indices[ni+0]=nv+0indices[ni+1]=nv+1indices[ni+2]=nv+2indices[ni+3]=nv+0indices[ni+4]=nv+2indices[ni+5]=nv+3nv+=4ni+=6Endif'leftIf sides[3]vertices[nv+0].position=New Vec3f( -1+x, 1+y, 1+z )'left back topvertices[nv+1].position=New Vec3f( -1+x, 1+y,-1+z )'left front topvertices[nv+2].position=New Vec3f( -1+x,-1+y,-1+z )'left front bottomvertices[nv+3].position=New Vec3f( -1+x,-1+y, 1+z )'left back bottomvertices[nv+0].texCoord0 = New Vec2f(0,0)vertices[nv+1].texCoord0 = New Vec2f(1,0)vertices[nv+2].texCoord0 = New Vec2f(1,1)vertices[nv+3].texCoord0 = New Vec2f(0,1)indices[ni+0]=nv+0indices[ni+1]=nv+1indices[ni+2]=nv+2indices[ni+3]=nv+0indices[ni+4]=nv+2indices[ni+5]=nv+3nv+=4ni+=6Endif'topIf sides[4]vertices[nv+0].position=New Vec3f( -1+x, 1+y, 1+z )'left back topvertices[nv+1].position=New Vec3f( 1+x, 1+y, 1+z )'right back topvertices[nv+2].position=New Vec3f( 1+x, 1+y,-1+z )'right front topvertices[nv+3].position=New Vec3f( -1+x, 1+y,-1+z )'left front topvertices[nv+0].texCoord0 = New Vec2f(0,0)vertices[nv+1].texCoord0 = New Vec2f(1,0)vertices[nv+2].texCoord0 = New Vec2f(1,1)vertices[nv+3].texCoord0 = New Vec2f(0,1)indices[ni+0]=nv+0indices[ni+1]=nv+1indices[ni+2]=nv+2indices[ni+3]=nv+0indices[ni+4]=nv+2indices[ni+5]=nv+3nv+=4ni+=6Endif'bottomIf sides[5]vertices[nv+0].position=New Vec3f( -1+x,-1+y,-1+z )'left front bottomvertices[nv+1].position=New Vec3f( 1+x,-1+y,-1+z )'right front bottomvertices[nv+2].position=New Vec3f( 1+x,-1+y, 1+z )'right back bottomvertices[nv+3].position=New Vec3f( -1+x,-1+y, 1+z )'left back bottomvertices[nv+0].texCoord0 = New Vec2f(0,0)vertices[nv+1].texCoord0 = New Vec2f(1,0)vertices[nv+2].texCoord0 = New Vec2f(1,1)vertices[nv+3].texCoord0 = New Vec2f(0,1)indices[ni+0]=nv+0indices[ni+1]=nv+1indices[ni+2]=nv+2indices[ni+3]=nv+0indices[ni+4]=nv+2indices[ni+5]=nv+3nv+=4ni+=6Endifvertices=vertices.Slice( 0,nv )indices=indices.Slice( 0,ni )Return New Mesh( vertices,indices )End Method -
AuthorPosts
You must be logged in to reply to this topic.