Combining objects to one surface for speed?

Blitz3D Forums/Blitz3D Programming/Combining objects to one surface for speed?

RexRhino(Posted 2003) [#1]
OK, I am doing some optimizations on my 3D engine... There are places where I have many meshs, and can probably combine them into one... of course, just doing addmesh seems to make no difference in speed whatsoever... but I have heard that combining the models manually so that they are all on one surface actually does speed things up.

Is this true? I haven't found a function in the code archives or on blitzcoder to combine meshs to one surface, so before I spend an afternoon coding one myself, it would be good to get some input.

Also, what are some other tricks for squeezing out a few more frames per second.


DJWoodgate(Posted 2003) [#2]
I believe that if meshes have different surface properties (texure, color, alpha etc) then those surfaces can not be combined into one surface and retain those distinct properties. Addmesh will it seems optimise surfaces that are painted with the same brush into single surfaces as shown below (I keep hearing people say addmesh does not combine surfaces, when it clearly does, so I suspect this a feature that Mark snuck in at some point). There is of course a vert limit on surfaces and I would assume that if a surface is already full then addmesh will make a new one. I have heard suggested that this sort of optimisation will be carried out on loaded meshes automatically which seems reasonable (Personally I think it should be optional). As to more fps, texture size will hit fillrate, so reducing texture size may well help on low end cards.

BTW I have noticed definate speedups when using addmesh and reducing the surface count, but you will need to consider other factors, like use of entityautofade and portaling techniques in deciding exactly what meshes you combine. You might want to throw some LOD into the mix as well.

Graphics3D 640,480,0,2

texture1=CreateTexture(128,128)
SetBuffer TextureBuffer(texture1)
Color 128,128,128 Rect 0,0,128,128
Color 128,0,0 Rect 0,0,64,64 Rect 64,64,64,64

texture2=CreateTexture(128,128)
SetBuffer TextureBuffer(texture2)
Color 128,128,0 Rect 0,0,128,128
Color 128,0,128 Rect 0,0,64,64 Rect 64,64,64,64

texture3=CreateTexture(128,128)
SetBuffer TextureBuffer(texture3)
Color 255,128,255 Rect 0,0,128,128
Color 128,0,255 Rect 0,0,64,64 Rect 64,64,64,64

; create 3 brushes
brush1=CreateBrush(255,255,255)		BrushTexture brush1,texture1
brush2=CreateBrush(128,128,128)		BrushTexture brush2,texture2
brush3=CreateBrush(64,64,64)		BrushTexture brush3,texture3

; paintmesh with these brushes
; three solid cones (6 surfaces)
obj1=CreateCone(18)			PaintMesh obj1,brush1
obj2=CreateCone(18)			PaintMesh obj2,brush2
obj3=CreateCone(18)			PaintMesh obj3,brush3

; three solid cylinders (6 surfaces)
obj4=CreateCylinder(18)		PaintMesh obj4,brush1
obj5=CreateCylinder(18)		PaintMesh obj5,brush2
obj6=CreateCylinder(18)		PaintMesh obj6,brush3

; three spheres (3 surfaces)
obj7=CreateSphere(18)		PaintMesh obj7,brush1
obj8=CreateSphere(18)		PaintMesh obj8,brush2
obj9=CreateSphere(18)		PaintMesh obj9,brush3

; combined by addmesh into 3 surfaces
; (surfaces painted with the same brush will be combined)
mesh=CreateMesh()
PositionMesh obj1,-3,3,0	AddMesh obj1,mesh	FreeEntity obj1
PositionMesh obj2,0,3,0		AddMesh obj2,mesh	FreeEntity obj2
PositionMesh obj3,3,3,0		AddMesh obj3,mesh	FreeEntity obj3
PositionMesh obj4,-3,0,0	AddMesh obj4,mesh	FreeEntity obj4
PositionMesh obj5,0,0,0		AddMesh obj5,mesh	FreeEntity obj5
PositionMesh obj6,3,0,0		AddMesh obj6,mesh	FreeEntity obj6
PositionMesh obj7,-3,-3,0	AddMesh obj7,mesh	FreeEntity obj7
PositionMesh obj8,0,-3,0	AddMesh obj8,mesh	FreeEntity obj8
PositionMesh obj9,3,-3,0	AddMesh obj9,mesh	FreeEntity obj9

SetBuffer BackBuffer()
Color 255,255,255

camera=CreateCamera()
PositionEntity camera,0,0,-6
light=CreateLight()
RotateEntity light,45,-45,0

Repeat

	RenderWorld
	
	Text 0,0,TrisRendered()
	For i=1 To CountSurfaces(mesh)
		Text 0,i*12,CountTriangles(GetSurface(mesh,i))
	Next
	If keyhit(57) Then EntityColor mesh,128,128,128
	
Flip
Until KeyDown(1)