CreateStaticGeometry() - dimensions

Everything related to 3D programming
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

CreateStaticGeometry() - dimensions

Post by Bananenfreak »

Heyho,

I want to discuss about the 3 values in WU you have to fill in to CreateStaticGeometry().
In the documentation, the values are the dimensions of the static geometry itself.

In my mind, static geometry has no size to give, it's endless. BUT there is one thing you can choose with the dimensions: The size of regions.
http://www.ogre3d.org/docs/api/1.9/clas ... a076fd8a69
1 static geometry can have multiple regions. A Region is rendered or not. No half things :D
So it's important to set the right regionsize.

So what do you think? Is the documentation wrong or is the way I'm thinking the wrong path?
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: CreateStaticGeometry() - dimensions

Post by applePi »

Hi Bananenfreak, from testing the official example StaticGeometry.pl i am inclined to believe that the Width, Height, Length in CreateStaticGeometry(#StaticGeometry, Width, Height, Length, EnableShadows) are just a signs , and they inflated or contracted as necessary to a limited value (more about this later), this is seems like the relation between CreateMesh and createEntity, look this example adapted from the official example, change the Width, Height, Length to 10000 or 1000 or 10 and it will accept the size of 3000 X 300 X 3000 cube (ground) and the other cube 200, 1000, 200. we can determine the size of the staticgeometry components exactly
but it seems there is a some ratio: this will be accepted, but change 3000 to 30000 and error will result

Code: Select all

CreateStaticGeometry(0, 10, 10, 10, #True)
    AddStaticGeometryEntity(0, EntityID(0), 0, 0, 0, 3000,  300, 3000, 0, 0, 0)        
    AddStaticGeometryEntity(0, EntityID(0), 0, 700, 0,  200, 1000, 200, 0, 0, 0)
but change Width, Height, Length in CreateStaticGeometry to 10000 and then you can use 30000 for the cube. so there is some ratio

Code: Select all

CreateStaticGeometry(0, 10000, 1000, 10000, #True)
    AddStaticGeometryEntity(0, EntityID(0), 0, 0, 0, 30000,  300, 30000, 0, 0, 0)        
    AddStaticGeometryEntity(0, EntityID(0), 0, 700, 0,  200, 1000, 200, 0, 0, 0)
Do these experiments using this code

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Static Geometry
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 10, Yaw.f, Pitch.f

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts",#PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    WorldShadows(#PB_Shadow_Additive)
    
    AmbientColor(0)
        
    ; node for Light and Billboard (Sun)
    CreateNode(0, 0, 3000, 0)
    
    ;Create light
    CreateLight(0, RGB(90, 105, 132), 0, 3000, 0)
    AttachNodeObject(0, LightID(0))
    
    ; Create flare
    GetScriptMaterial(0, "Scene/burst")
    CreateBillboardGroup(0, MaterialID(0), 2048, 2048)
    AddBillboard(0, 0, 3000, 0)
    AttachNodeObject(0, BillboardGroupID(0))
    
    
    ; Static geometry
    ;
    
    ; Create Entity
    CreateCube(0, 1)
    CreateEntity(0, MeshID(0), #PB_Material_None)
        
    ; Create Static geometry
    CreateStaticGeometry(0, 10, 10, 10, #True)
    AddStaticGeometryEntity(0, EntityID(0), 0, 0, 0, 3000,  300, 3000, 0, 0, 0)        
    AddStaticGeometryEntity(0, EntityID(0), 0, 700, 0,  200, 1000, 200, 0, 0, 0)
      
    
    ; Build the static geometry
    BuildStaticGeometry(0)
   
    FreeEntity(0)

    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 2000, 2000, 2000, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraRange (0, 2, 5000)
    CameraFOV   (0, 90)
    CameraBackColor(0, RGB(90, 105, 132))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        Yaw   = -MouseDeltaX() * 0.05
        Pitch = -MouseDeltaY() * 0.05
      EndIf
      
      If ExamineKeyboard()
              
        If KeyboardPushed(#PB_Key_Up)    
          MoveCamera(0,  0, 0, -2 * Boost)
        ElseIf KeyboardPushed(#PB_Key_Down)
          MoveCamera(0,  0, 0,  2 * Boost)
        EndIf 
  
        If KeyboardPushed(#PB_Key_Left)  
          MoveCamera(0, -2 * Boost, 0, 0) 
        ElseIf KeyboardPushed(#PB_Key_Right)
          MoveCamera(0,  2 * Boost, 0, 0)
        EndIf 
  
      EndIf
           
      ; Sun
      nx = 10000 * Cos(ElapsedMilliseconds() / 2500)
      nz = 10000 * Sin(ElapsedMilliseconds() / 2500)   
      MoveNode(0, nx, 3000, nz, #PB_Absolute)
      
      RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: CreateStaticGeometry() - dimensions

Post by DK_PETER »

Changing the Geo dimension doesn't really do much other than what ApplePi described.

However..I find the use of a Static Geometry quite limiting and slow.

I've made an example, which illustrates the limitations..

Picture:

Image

Here's how to do it:

You need a mesh: Any kind..

Create these three folders: Mesh , material and texture

Copy the mesh to the mesh folder.

Copy the images below to the texture folder:

Image Image Image

Here's the material script you must save to the material folder (for the tree): filename: tree.material

Code: Select all

material "leafs"
{

	technique leafs_technique
	{
		pass
		{
			ambient 0.00784314 0.423529 0.12549 1
			diffuse 0.00784314 0.423529 0.12549 1
			specular 0.252 0.252 0.252 0.28 102
			emissive 0.31 0.31 0.31 1
		}
                texture_unit
		{
		       texture branch.jpg
		       colour_op add
		}
	}
}
material "matTreeBark"
{
	technique matTreeBark_technique
	{
		pass matTreeBark_standard
		{
			ambient 0.478431 0.317647 0.231373 1
			diffuse 0.478431 0.317647 0.231373 1
			specular 0 0 0 0 25.5
			emissive 0 0 0 1
		}
	}
}
Lastly...Here's the main code:

EDITED 2 Times: Leafs flies more realistic and clouds looks pretty decent now.
No more editing. :-)

Code: Select all

;Example: Static Geo Forest versus Forest by DK_PETER
;-----------------------------------------------------------
;Building a static geometry is actually quite slow
;and in this example there are no gains whatsoever.
;Actually it's quite faster without using Geo.
;Example below:
;Load time with static geometry:  47 secs.
;Load time Without static geometry: 3 sec.
;Framerate drops frequently when using static geometry.. which is quite odd.
;Without static geometry it stays 99.7% of the time at 60 fps. 
;-----------------------------------------------------------------------------
; Tested with: GFX: Gigabyte 770 GTX | Window 7 64 bit | 3 GHz. Quad Core AMD.
;-----------------------------------------------------------------------------

EnableExplicit
UseJPEGImageDecoder()
UsePNGImageDecoder()

If InitEngine3D() = 0
  MessageRequester("Error", "Can't initialize Engine")
  End
EndIf
If InitSprite() = 0
  MessageRequester("Error", "Can't initialize Screen")
  End
EndIf
If InitKeyboard() = 0
  MessageRequester("Error", "Can't initialize Keyboard")
  End
EndIf
If InitMouse() = 0
  MessageRequester("Error", "Can't initialize Mouse")
  End
EndIf

Structure _Object
  id.i
  ms.i
  ma.i
  tx.i
  tx_extra.i 
  x.f
  y.f
  z.f
  time.i
EndStructure

Structure _Objects
  Cloud._Object
  ground._Object
  Light.i
  Geo.i
  cam.i
  BaseTree._Object
  Leafs._Object[3]
  List tree._Object()
EndStructure

Declare.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
Declare.i BuildTrees(WithGeo.i = #False)

Global StartTime.i, ret.i
Global ob._Objects, Events.i, x.i, y.i, Lightpos.i = 1
Global KeyX.f, KeyY.f, KeyZ.f

OpenWindow(0, 0, 0, 1024, 768, "Forest", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)

Add3DArchive("mesh", #PB_3DArchive_FileSystem)
Add3DArchive("material", #PB_3DArchive_FileSystem)
Add3DArchive("texture", #PB_3DArchive_FileSystem)
Parse3DScripts()

ob\cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(ob\cam, 500, 1, 500)

ob\Light = CreateLight(#PB_Any, $00BD41, 0, 5, 0,#PB_Light_Directional)
LightDirection(ob\Light, -0.2, -1, 0)


With ob\ground
  \tx = LoadTexture(#PB_Any, "2.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  ScaleMaterial(\ma, 0.01, 0.01)
  \ms = CreatePlane(#PB_Any, 2000, 2000, 80, 80, 1, 1)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
EndWith

With ob\BaseTree
  \ma = GetScriptMaterial(#PB_Any, "leafs")
  \ms = LoadMesh(#PB_Any, "tree1.mesh")
  \id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None)
EndWith

With ob\Cloud
  \tx_extra = CreateTexture(#PB_Any, 1024, 1024)
  StartDrawing(TextureOutput(\tx_extra))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  For x = 0 To 6000
    Circle(Random(1020, 5), Random(1020,5) ,Random(2,1), RGBA(255, 255, 255, Random(255,30)))
  Next x 
  StopDrawing()
  \tx = LoadTexture(#PB_Any, "1.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx_extra))
  MaterialBlendingMode(\ma, #PB_Material_Color)
  AddMaterialLayer(\ma, TextureID(\tx),#PB_Material_Add)
  AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Modulate)
  RotateMaterial(\ma, 90, #PB_Material_Fixed, 1)
  ScaleMaterial(\ma, 0.005, 0.005, 0)
  ScaleMaterial(\ma, 0.1, 0.1, 1)
  ScaleMaterial(\ma, 0.1, 0.1, 2)
  ScrollMaterial(\ma, 0.02, -0.01, #PB_Material_Animated, 1)
  ScrollMaterial(\ma,  -0.01, 0.01, #PB_Material_Animated, 2)
  \ms = CreatePlane(#PB_Any, 400, 400, 10, 10, 1, 1)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 100, 0)
  RotateEntity(\id, -180,0, 0)
  ScaleEntity(\id, 10, 1, 10)
EndWith

For x = 0 To 2
  With ob\Leafs[x]
    \id = CreateParticleEmitter(#PB_Any, 600, 1, 600, #PB_Particle_Box, 0, 1, 0)
    \tx = LoadTexture(#PB_Any, "leaf.png")
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialBlendingMode(\ma, #PB_Material_AlphaBlend)
    RotateMaterial(\ma, randomf(-2.3,2.3), #PB_Material_Animated)
    ParticleMaterial(\id, MaterialID(\ma))
    ParticleSize(\id, 2+x, 2+x)
    ParticleEmitterDirection(\id, Randomf(-1,1), 0, Randomf(-1,1))
    ParticleEmissionRate(\id, Random(300, 100))
    ParticleVelocity(\id, 0.1, 5)
    ParticleSpeedFactor(\id, 1.1)
  EndWith
Next x
;******************************************************************************

ret = BuildTrees(#False)             ;Set to #True for building with static geo

;******************************************************************************
For x = 0 To 2
  ob\Leafs[x]\time = ElapsedMilliseconds()
Next x

Repeat
  
  Repeat
    Events = WindowEvent()
  Until Events = 0
  
  ExamineMouse()
  
  RotateCamera(ob\cam, MouseDeltaY() * 0.05, -MouseDeltaX() * 0.05, 0, #PB_Relative)
  
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Left)
    KeyX = -0.3
  ElseIf KeyboardPushed(#PB_Key_Right)
    KeyX = 0.3
  Else
    KeyX = 0
  EndIf
  
  If KeyboardPushed(#PB_Key_Up)
    KeyY = -0.3
  ElseIf KeyboardPushed(#PB_Key_Down)
    KeyY = 0.3
  Else
    KeyY = 0
  EndIf
  MoveCamera(ob\cam, KeyX, 0, KeyY)
  MoveCamera(ob\cam, CameraX(ob\cam), 1, CameraZ(ob\cam), #PB_Absolute) ;lock it at 1
  MoveParticleEmitter(ob\Leafs\id, CameraX(ob\cam), CameraY(ob\cam), CameraZ(ob\cam), #PB_Absolute)
  For x = 0 To 2
    With ob\Leafs[x]
      If ElapsedMilliseconds() - \time > Random(800, 200)
        ParticleEmitterDirection(\id, randomf(-1,1), randomf(-0.1,0.1), Randomf(-1,1))  ;Edited the (y-dir) -  made the leafs fly more 'realistic'
        \time = ElapsedMilliseconds()
      EndIf
    EndWith
  Next x
  RenderWorld()
  SetWindowTitle(0, Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)))
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)

Procedure.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
  If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
  ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure

Procedure.i BuildTrees(WithGeo.i = #False)
  StartTime = ElapsedMilliseconds()
  If WithGeo = #True
    ob\Geo = CreateStaticGeometry(#PB_Any, 1000, 500, 1000, 1)
    ;ob\Geo = CreateStaticGeometry(#PB_Any, 10, 50, 10, 1) ;Works just as well
    For y = 0 To 2600 Step 100
      For x = 0 To 2600 Step 100
        AddElement(ob\tree())
        With ob\tree()
          \ma = GetScriptMaterial(#PB_Any, "leafs")
          \ms = LoadMesh(#PB_Any, "tree1.mesh")
          \id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None)
          AddStaticGeometryEntity(ob\Geo, EntityID(\id), x + RandomF(70,0), 0, y + randomf(70, 0), 3, 3, 3)
        EndWith
      Next x
    Next y
    BuildStaticGeometry(ob\Geo)
  Else
    For y = 0 To 2000 Step 100
      For x = 0 To 2000 Step 100
        AddElement(ob\tree())
        With ob\tree()
          \ma = GetScriptMaterial(#PB_Any, "leafs")
          \ms = LoadMesh(#PB_Any, "tree1.mesh")
          \id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None,x + RandomF(70,0), 0, y + randomf(70, 0))
          ScaleEntity(\id, 3, 3, 3)
        EndWith
      Next x
    Next y
  EndIf
  Debug (ElapsedMilliseconds()-StartTime) / 1000
  ProcedureReturn #True
EndProcedure
Last edited by DK_PETER on Fri Sep 04, 2015 12:18 pm, edited 1 time in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: CreateStaticGeometry() - dimensions

Post by applePi »

Hi DK_PETER, it happened i was using an old computer searching my old files, when i run your code it stay 7 seconds , it seems to me the loading of tree mesh consume the time, and if we replaced \ms = LoadMesh(#PB_Any, "tree1.mesh") with \ms = CreateCube(#PB_Any, 2) in the 3 cases in the file then the time is 0 . it is a nice and useful example and a showcase of using staticGeometry.
it seems morphologically that CreateStaticGeometry resemble CreatMesh and AddStaticGeometryEntity resembles AttachEntityObject() and the two are making the physics poor. but they are convenient in other than physics projects .
i will do more tests
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: CreateStaticGeometry() - dimensions

Post by DK_PETER »

Hey ApplePi
it seems to me the loading of tree mesh consume the time, and if we replaced \ms = LoadMesh(#PB_Any, "tree1.mesh") with \ms = CreateCube(#PB_Any, 2) in the 3 cases in the file then the time is 0.
Using simple cubes decreases the load time considerably. But since most graphics dependant meshes are in the details (to have any real value), cubes have a limited functionality. :-)
Nevertheless..Replacing a detailed tree mesh with the cube certainly speeds up the creation and it is finihed instantly.

What bothers me is, that using the tree.mesh is really fast without static geo and the framerate is also very high, while this changes drastically when using static geo....(at least for me, that is).
it seems morphologically that CreateStaticGeometry resemble CreatMesh and AddStaticGeometryEntity resembles AttachEntityObject() and the two are making the physics poor
Yes...is certainly does resembles that. For simple things and tests the static geo does serve a genuine purpose. When going for critical things and details, one must take another road.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: CreateStaticGeometry() - dimensions

Post by Bananenfreak »

Thank you for your posts to this subject.

Sadly, I habe no Computer to test it (now).

Static geometry slows down programs? Hmm, in my case, 1025 trees are rendered. In summary 4,800,000 tris and ~100 batches. I think this can be done by a graphicscard without having problems.
But I get only ~7.6FPS.
I have to test it without Static geometry.
Image
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: CreateStaticGeometry() - dimensions

Post by PMV »

There is something wrong ...
With StaticGeometry, my GraphicCard goes up to 100%.
With 4Million triangles and up to 340 batches.

Without StaticGeometry, my GraphicCard is around 36%.
With 4 Million triangles and up to 769 batches.

At first, Static Geometry does what it should, it reduces the batches.
But there is a big overhead, somewhere in this case .... strange.


Additional:
At first, you have your loop from 2600 for static geo and 2000 without.
Second you use EnableShadow = 1, even so it seems to do nothing.
Third the values for with/ depth/ high at CreateStaticGeo() are important.

They set the LOD i think, with 100/ 100/ 100 i have the same triangle-count
as without static geometry, but with 2000, 100, 2000 i will have the double amount
(over 7million) and over 400 batches. So my test result is with 100/ 100/ 100.

Show the triangles and batches in title, too: :)

Code: Select all

  SetWindowTitle(0, Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) + " | " + Str(Engine3DStatus(#PB_Engine3D_NbRenderedTriangles)) + " | " + Str(Engine3DStatus(#PB_Engine3D_NbRenderedBatches)))
And static geometry works only for the same mesh, so i would not load
the same mesh over and over again (even so ogre will see that its the same mesh),
but just load the mesh one time and use this mesh for all entities.
And as the entity is not used internally but recreated, you can free
the entity after adding it to the geometry. :)

@Comtois
Do you know, why static geometry has such a high overhead in this case?

MFG PMV
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: CreateStaticGeometry() - dimensions

Post by DK_PETER »

There is something wrong ...
With StaticGeometry, my GraphicCard goes up to 100%.
With 4Million triangles and up to 340 batches.

Without StaticGeometry, my GraphicCard is around 36%.
With 4 Million triangles and up to 769 batches.

At first, Static Geometry does what it should, it reduces the batches.
But there is a big overhead, somewhere in this case .... strange.
I agree.
At first, you have your loop from 2600 for static geo and 2000 without.
Second you use EnableShadow = 1, even so it seems to do nothing.
Again, I agee. 2600 vs 2000 was a typoe..Not that the change would differ that much.

And static geometry works only for the same mesh, so i would not load
the same mesh over and over again (even so ogre will see that its the same mesh),
I would 'not' load the same mesh over and over again...But it shows the load speed issue.

Concerning the static geometry only supports a single mesh type...........Really??

Image
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: CreateStaticGeometry() - dimensions

Post by Samuel »

Here's a few things you can try to speed things up.

First try making your screen resolutions a little smaller and see if that speeds things up. If you get a major increase in FPS then it's most likely not your batch count messing things up (which is the whole purpose of static geometry).

Make sure all your static meshes use the same material. Static geometry merges meshes together, but it can't do that if each material is unique. Well, technically it will add unique materials, but it will be even slower then not using static geometry because they lose their per mesh culling.

Good

Code: Select all

GetScriptMaterial(#Material, "")

For CTR = 1 to 1000
  LoadMesh(CTR, "")
  CreateEntity(#PB_Any, MeshID(CTR), #Material)
  AddStaticGeometryEntity()
Next
Slower

Code: Select all

For CTR = 1 to 1000
  GetScriptMaterial(CTR, "")
  LoadMesh(CTR, "")
  CreateEntity(#PB_Any, MeshID(CTR), CTR)
  AddStaticGeometryEntity()
Next
Another problem that can arise is a vertex count bottleneck because static geometry can increase the amount of vertices.
The same goes for your VRAM. You may need to decrease texture size because the huge meshes being sent to your graphic card might be exceeding the memory limit.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: CreateStaticGeometry() - dimensions

Post by DK_PETER »

@Samuel
I do know how to increase performance considerably, but I always appreciate your input.

The above example was simply meant to show that there is an 'overhead' somewhere at some
point, when using Static geometry. Not the best example, but I made my point, even if I
scaled the entities up and as PMV stated: Loaded each entity seperately.
(Which is possible and usable if you wish to add many different entities to the static geo).

I've made some changes to the next example.

Here's the download link for the next example: https://www.dropbox.com/s/let03mwqrlc6l ... 2.zip?dl=0
There are two meshes, several images and sound.
Too much to put here. This is my last static geo example.

Here's the new main code, though:

Code: Select all

; Example3: Static Geo town by DK_PETER
;----------------------------------------------------------------------------
; A MORE OPTIMIZED VERSION
;-----------------------------------------------------------------------------
; In the previous example, I didn't keep everything small, but entities were
; scaled up considerably. This was to show, that there is something going on,
; which I can't figure out. Loading time was increased considerably, while
; omitting the Static Geo decreased the loading time considerably.
;-----------------------------------------------------------------------------
; In a 'real world' example, the proper way is to keep everthing VERY small.
; Simply scale everything down and you'll see that the speed is drastically improved.
;-----------------------------------------------------------------------------
; In this example I'll do just that.
; Note: Only the leaf texture size has been decreased
; and we're adding two entities to the static geometry.
; Remember: Physics + STATIC GEO won't work. 
; But that's not the purpose of the static geometry anyway. :-)
;-----------------------------------------------------------------------------
; I've added a church/courthouse besides the tree and attached some bells sounds to the courthouses/churches.
; This time the performance is good. Less that 4 seconds and YES..It can be done even better.
; Without Static geo - it's blazingly fast (Instant creation 0 seconds).
;-----------------------------------------------------------------------------
; Tested with: GFX: Gigabyte 770 GTX | Window 7 64 bit | 3 GHz. Quad Core AMD.
;-----------------------------------------------------------------------------

EnableExplicit
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()

If InitEngine3D() = 0
  MessageRequester("Error", "Can't initialize Engine")
  End
EndIf
If InitSprite() = 0
  MessageRequester("Error", "Can't initialize Screen")
  End
EndIf
If InitKeyboard() = 0
  MessageRequester("Error", "Can't initialize Keyboard")
  End
EndIf
If InitMouse() = 0
  MessageRequester("Error", "Can't initialize Mouse")
  End
EndIf
If InitSound() = 0
  MessageRequester("Error", "Can't initialize Sound")
  End
EndIf

Structure _Object
  id.i
  ms.i
  ma.i
  tx.i
  x.f
  y.f
  z.f
  time.i
EndStructure

Structure _Bellsound
  snd.i  
  nod.i
EndStructure

Structure _Objects
  Cloud._Object
  ground._Object
  Light.i
  Leafs._Object[3]
  church._Object
  tree._Object
EndStructure

Structure MainVars
  Geo.i
  cam.i  
  ret.i
  Events.i
  KeyX.f
  KeyY.f
  KeyZ.f
  tx_extra.i
  FinishedCreationTime.i
EndStructure

Declare.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
Declare.i BuildTown(WithGeo.i = #True)

Global ob._Objects, m.MainVars, x.i, y.i, NewList sn._Bellsound()

OpenWindow(0, 0, 0, 1024, 768, "ChurchTown", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)

Add3DArchive("mesh", #PB_3DArchive_FileSystem)
Add3DArchive("material", #PB_3DArchive_FileSystem)
Add3DArchive("texture", #PB_3DArchive_FileSystem)
Add3DArchive("sound", #PB_3DArchive_FileSystem)

Parse3DScripts()

m\cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(m\cam, 130, 1, 130)

ob\Light = CreateLight(#PB_Any, $CEFFFF41, 0, 5, 0,#PB_Light_Directional)
LightDirection(ob\Light, -0.2, -1, 0)


With ob\ground
  \tx = LoadTexture(#PB_Any, "2.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  ScaleMaterial(\ma, 0.01, 0.01)
  \ms = CreatePlane(#PB_Any, 400, 400, 10, 10, 1, 1)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
EndWith

With ob\Cloud
  m\tx_extra = CreateTexture(#PB_Any, 1024, 1024)
  StartDrawing(TextureOutput(m\tx_extra))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  For x = 0 To 6000
    Circle(Random(1020, 5), Random(1020,5) ,Random(2,1), RGBA(255, 255, 255, Random(255,30)))
  Next x
  StopDrawing()
  \tx = LoadTexture(#PB_Any, "1.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(m\tx_extra))
  MaterialBlendingMode(\ma, #PB_Material_Color)
  AddMaterialLayer(\ma, TextureID(\tx),#PB_Material_Add)
  AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Modulate)
  RotateMaterial(\ma, 90, #PB_Material_Fixed, 1)
  ScaleMaterial(\ma, 0.005, 0.005, 0)
  ScaleMaterial(\ma, 0.1, 0.1, 1)
  ScaleMaterial(\ma, 0.1, 0.1, 2)
  ScrollMaterial(\ma, 0.02, -0.01, #PB_Material_Animated, 1)
  ScrollMaterial(\ma,  -0.01, 0.01, #PB_Material_Animated, 2)
  \ms = CreatePlane(#PB_Any, 400, 400, 10, 10, 1, 1)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 100, 0)
  RotateEntity(\id, -180,0, 0)
  ScaleEntity(\id, 10, 1, 10)
EndWith

For x = 0 To 2
  With ob\Leafs[x]
    \id = CreateParticleEmitter(#PB_Any, 100, 1, 100, #PB_Particle_Box, 0, 1, 0)
    \tx = LoadTexture(#PB_Any, "leaf.png")
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialBlendingMode(\ma, #PB_Material_AlphaBlend)
    RotateMaterial(\ma, randomf(-3, 3), #PB_Material_Animated)
    ParticleMaterial(\id, MaterialID(\ma))
    ParticleSize(\id, 0.1 + RandomF(1,0.2) , 0.1 + RandomF(1,0.2))
    ParticleEmitterDirection(\id, Randomf(-1,1), 0, Randomf(-1,1))
    ParticleEmissionRate(\id, Random(100, 50))
    ParticleVelocity(\id, 0.1, 5)
    ParticleSpeedFactor(\id, 1.2)
  EndWith
Next x

;******************************************************************************
m\ret = BuildTown(#False)    ;With Geo or without...
                             ;******************************************************************************

For x = 0 To 2
  ob\Leafs[x]\time = ElapsedMilliseconds()
Next x

Repeat
  
  Repeat
    m\Events = WindowEvent()
  Until m\Events = 0
  
  ExamineMouse()
  
  RotateCamera(m\cam, MouseDeltaY() * 0.05, -MouseDeltaX() * 0.05, 0, #PB_Relative)
  
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Left)
    m\KeyX = -0.1
  ElseIf KeyboardPushed(#PB_Key_Right)
    m\KeyX = 0.1
  Else
    m\KeyX = 0
  EndIf
  
  If KeyboardPushed(#PB_Key_Up)
    m\KeyY = -0.1
  ElseIf KeyboardPushed(#PB_Key_Down)
    m\KeyY = 0.1
  Else
    m\KeyY = 0
  EndIf
  MoveParticleEmitter(ob\Leafs\id, CameraX(m\cam), CameraY(m\cam), CameraZ(m\cam), #PB_Absolute)
  For x = 0 To 2
    With ob\Leafs[x]
      If ElapsedMilliseconds() - \time > Random(800, 200)
        ParticleEmitterDirection(\id, randomf(-1,1), randomf(-0.1,0.1), Randomf(-1,1))  ;Edited the (y-dir) -  made the leafs fly more 'realistic'
        \time = ElapsedMilliseconds()
      EndIf
    EndWith
  Next x
  
  SoundListenerLocate(CameraX(m\cam), CameraY(m\cam), CameraZ(m\cam))
  
  MoveCamera(m\cam, m\KeyX, 0, m\KeyY)
  
  RenderWorld()
  SetWindowTitle(0, Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) + " | " + Str(Engine3DStatus(#PB_Engine3D_NbRenderedTriangles)) + " | " + Str(Engine3DStatus(#PB_Engine3D_NbRenderedBatches))) 
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)

Procedure.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
  If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
  ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure

Procedure.i BuildTown(WithGeo.i = #True)
  Protected newval.i = 0, newx.f, newy.f, BaseSound.i
  Protected NewList newEnt.i()
  BaseSound = LoadSound3D(#PB_Any, "churchbells.wav")
  
  m\FinishedCreationTime = ElapsedMilliseconds()
  
  With ob\tree
    \ma = GetScriptMaterial(#PB_Any, "leafs")
    \ms = LoadMesh(#PB_Any, "tree1.mesh")
    \id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None)
  EndWith
  With ob\church
    \ma = GetScriptMaterial(#PB_Any, "_52bac0e2.dds")
    \ms = LoadMesh(#PB_Any, "courtbuilding.mesh")
    \id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None)
  EndWith
  
  ;CONCERNING BELLS
  ;This is waaay too much but here I attach a bell to each courthouse/church
  If WithGeo = #True
    m\Geo = CreateStaticGeometry(#PB_Any, 250, 50, 250, 0)
    For y = 0 To 200 Step 20
      For x = 0 To 200 Step 20
        If newval = 0
          newx = RandomF(8,0) : newy = randomf(8, 0)
          AddStaticGeometryEntity(m\Geo, EntityID(ob\tree\id), x + newx, 0, y + newy, 0.2, 0.2, 0.2, 0, 0, 0)
          newval = 1
        Else
          newx = RandomF(8,0) : newy = randomf(8, 0)
          AddStaticGeometryEntity(m\Geo, EntityID(ob\church\id), x + newx, 0, y + newy, 0.1, 0.1, 0.1, 0, 0, 0)
          AddElement(sn()) 
          sn()\nod = CreateNode(#PB_Any, x + newx, 1,  y + newy)
          sn()\snd = LoadSound3D(#PB_Any, "churchbells.wav")
          SoundRange3D(sn()\snd, 0, 12)
          AttachNodeObject(sn()\nod, SoundID3D(sn()\snd))
          PlaySound3D(sn()\snd, #PB_Sound3D_Loop)
          newval = 0
        EndIf
      Next x
    Next y
    BuildStaticGeometry(m\Geo)
  Else  ;Without Static geo...And now you can do everything - physics - raycasting a.s.f...
    For y = 0 To 200 Step 20
      For x = 0 To 200 Step 20
        If newval = 0
          newx = RandomF(8,0) : newy = randomf(8, 0)
          AddElement(newent())
          newEnt() = CopyEntity(ob\tree\id, #PB_Any)
          ScaleEntity(newEnt(), 0.2, 0.2, 0.2)
          MoveEntity(newEnt(), x + newx, 0, y + newy)
          newval = 1
        Else
          newx = RandomF(8,0) : newy = randomf(8, 0)
          newEnt() = CopyEntity(ob\church\id, #PB_Any)
          ScaleEntity(newEnt(), 0.1, 0.1, 0.1)
          MoveEntity(newEnt(), x + newx, 0, y + newy)
          AddElement(sn()) 
          sn()\nod = CreateNode(#PB_Any, x + newx, 1,  y + newy)
          sn()\snd = LoadSound3D(#PB_Any, "churchbells.wav")
          SoundRange3D(sn()\snd, 0, 12)
          AttachNodeObject(sn()\nod, SoundID3D(sn()\snd))
          PlaySound3D(sn()\snd, #PB_Sound3D_Loop)
          newval = 0
        EndIf
      Next x
    Next y
  EndIf
  Debug (ElapsedMilliseconds() - m\FinishedCreationTime) / 1000
  ;Free basic objects - if one wants to in this example
  FreeEntity(ob\church\id)
  FreeEntity(ob\tree\id)
  ProcedureReturn #True
EndProcedure
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: CreateStaticGeometry() - dimensions

Post by Bananenfreak »

The overhead comes from another place.
The CPU sends Drawcalls for each batch you have.
With static geometry the CPU sends less drawcalls, but the GPU has to render same thing as without static geometry (Not exactly). So the CPU can send more drawcalls, but the GPU can't take more. Your bottleneck is the CPU.
Without static geometry, the CPU have to send many small drawcalls, so your GPU gets bored.
For more, ask Google with "limitation GPU CPU" or something similar. But be aware, there are many "false doctors" out there.
Look what your CPU does with and without static geometry.
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: CreateStaticGeometry() - dimensions

Post by applePi »

i like the leafs in the wind, and the eerie clouds in the sky, also the way churches bells ring, certainly DK_PETER are celebrating the new PB 5.40 (in a few days) from now , now i am running my more advanced pc i5 with Geforce 520, it is considered poor graphics card but it is usable. in windows xp.32 it is may be 1 second to launch the scene, the fps when i was looking to empty terrain it increased to 30 or more but looking to too much churches it dropped to about 15. in windows 7 the situation are more speedy, sometimes the fps = 75. certainly more advanced Graphics cards will gives better results.
Thanks
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: CreateStaticGeometry() - dimensions

Post by DK_PETER »

applePi wrote:i like the leafs in the wind, and the eerie clouds in the sky, also the way churches bells ring, certainly DK_PETER are celebrating the new PB 5.40 (in a few days) from now , now i am running my more advanced pc i5 with Geforce 520, it is considered poor graphics card but it is usable. in windows xp.32 it is may be 1 second to launch the scene, the fps when i was looking to empty terrain it increased to 30 or more but looking to too much churches it dropped to about 15. in windows 7 the situation are more speedy, sometimes the fps = 75. certainly more advanced Graphics cards will gives better results.
Thanks
Anytime applePi. :-)
Good info..And yes..I'm looking forward to 5.40, but I'm not in any haste.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Post Reply