LOD code

Blitz3D Forums/Blitz3D Programming/LOD code

AdsAdamJ(Posted 2005) [#1]
Hi, long time lurker here!

I've been programming since I was a little kid (I'm 24 now), but I'm quite new to working with 3D. This morning I was working on some Level-Of-Detail code for a game I'm planning, and this is what I came up with:

Type obj_data
	Field obj				; Object centre
	Field obj_scale#		; Scale of object
	Field range_near		; 'Near' range
	Field range_far			; 'Far' range
	Field objLOD_0			; Closest object
	Field objLOD_1			; Mid-range object
	Field objLOD_2			; Distant object
	Field LOD2_visible		; Override the checking on LOD2 visibility
End Type

......... insert code here

Function check_LOD(temp.obj_data, cam_temp)
	Local dist = 0
	dist = EntityDistance(cam_temp, temp\obj)
	
	If dist < test\range_near Then
		ShowEntity temp\objLOD_0
		HideEntity temp\objLOD_1
		If temp\LOD2_visible = False Then HideEntity temp\objLOD_2		; If LOD2 visibility is true, never hide entity
	End If
	
	If dist >= test\range_near And dist < test\range_far Then
		HideEntity temp\objLOD_0
		ShowEntity temp\objLOD_1
		If temp\LOD2_visible = False Then HideEntity temp\objLOD_2		; If LOD2 visibility is true, never hide entity
	End If

	If dist >= test\range_far Then
		HideEntity temp\objLOD_0
		HideEntity temp\objLOD_1
		ShowEntity temp\objLOD_2										; No checking here, always visible at this range
	End If
End Function


Opinions? Any way I can improve on this? At the moment it's not too bad. I created a random 'galaxy' with 5000 entities, and I racked up no more than 1000-2000 tris on average. I set the highest level of LOD (objLOD_2) as a sprite to cut down on the number of tris rendered.

Also, how do you create the code boxes? I have no idea! Thanks. :)


KuRiX(Posted 2005) [#2]
To create codebox just put your code between
[ codebox ]
code here
[ /codebox ]

Without the spaces of course.

Regarding your LOD. Perhaps you could use the Automatic Culling Blitz System, where all entities far from the camera (see entityrange) are automatically hidden, and also the object behind the camera...


AdsAdamJ(Posted 2005) [#3]
Cheers!

I might work the for-next loop to ignore anything behind the camera and out of the camera view range. While the entities are not rendered, they are still being run through the if statements. Thanks for the thought!


AdsAdamJ(Posted 2005) [#4]
Works pretty well:


(can I not post pictures until I get a certain post count?)

Just over 1000 tris, 2000 stars. I'm going to use this in my Simcity clone hopefully so I can have quite a few buildings with out melting the graphics card.


big10p(Posted 2005) [#5]
All the forum tags are here, Adam:
http://www.blitzbasic.com/faq/faq_entry.php?id=2


AdsAdamJ(Posted 2005) [#6]
Thanks!


Stevie G(Posted 2005) [#7]
With such a low poly count you really don't need to be too concerned about LOD. The system you have would work well for high poly scenes ( 3 versions of the same structure ) but I would be inclined to code without this kind of optomisation until I really needed it.

I guess it does no harm to have this featue implemented at the outset incase it proves a knightmare to implement after the fact.


Banshee(Posted 2005) [#8]
i'll keep this ultra-terse as I just typed this once and lost it. If I recall my target poly count using the original 3DFX card was 8000 pollies per scene, and that was in DBC which is a slower language than B3D. So 2000 really is nothing to worry about optimising, however.

A small enhancement that could make a big difference if this code is used by a few thousand objects can make a big difference, so think about conditionally excluding some of those if checks.

Lets assume the most common object range is the mid distance

if midDistance=true
   ;hide/show
else
   if nearDistance=true
      ;hide/show
   else
      ;hide/show far distance
   endIf
endIf


Secondly i've heard it suggested although not yet personally tested that hiding and showing an entity isn't a very fast command (fast enough not to notice any slowdown in general use, but not that fast if used multiple times). It might therefore be wise to test if an entity is already hidden before hiding it, and vice versa

if not temp\shown=2
   hideentity temp\currentObject
   showentity temp\LodObject
   temp\shown=2
endIf



Matty(Posted 2005) [#9]
I've found on my older PC that for animated entities it is actually faster to free an entity instead of hiding it and copy from a master entity when it needs to be shown again. Don't really know why but it seems to be that way.