Code archives/3D Graphics - Mesh/Faster vertice manipulation

This code has been declared by its author to be Public Domain code.

Download source code

Faster vertice manipulation by Damien Sturdy2005
1> Thanks to BlackD for the idea!!!!


In your code, replace vertexcolor, vertextexcoord and tertexcoord commands with vertexcolor2, vertextexcoord2 and tertexcoord2. Before rendering, use UPDATEMESHES().

After a few tests, i can tell you that if you are NOT doing much with a mesh other than updating it, you can actually lose speed using this technique. If you are updating a mesh which is pickable, and are picking, you get a massive increase.
Graphics3D 640,480,16,2
light=CreateLight()

mesh=CreateSphere(8)
Global dst#
camera=CreateCamera()
EntityFX mesh,3
MoveEntity camera,0,20,-30
PointEntity camera,mesh

EntityPickMode mesh,2
sf=GetSurface(mesh,1)

ms=MilliSecs()
For n=1 To 2500
	vert=Rand(0,CountVertices(sf))
	VertexCoords(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
	VertexTexCoords(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
	LinePick(0,0,0,1,1,1)
Next
ms2=MilliSecs()
normalupdate=ms2-ms

ms=MilliSecs()
For n=1 To 2500
	vert=Rand(0,CountVertices(sf))
	VertexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
	VertexTexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
	LinePick(0,0,0,1,1,1)
Next
updatemeshes()
ms2=MilliSecs()
Speedupdate=ms2-ms


WireFrame 1

Repeat
	If mode=1 Then
		For n=1 To 1000
			vert=Rand(0,CountVertices(sf))
			VertexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			VertexTexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			LinePick(0,0,0,1,1,1)
		Next
		updatemeshes
	Else
		For n=1 To 1000
			vert=Rand(0,CountVertices(sf))
			VertexCoords(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			VertexTexCoords(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			LinePick(0,0,0,1,1,1)
		Next
EndIf

MoveEntity mesh,KeyDown(203)-KeyDown(205),0,KeyDown(200)-KeyDown(208)
TurnEntity mesh,1,1,1
RenderWorld
Text 0,0,dst
Text 0,20,"Normal update:"+normalupdate
Text 0,32,"Speed update:"+speedupdate

Text 0,50,"Hit space to toggle mode"
If KeyHit(57) Then mode=(mode+1) Mod 2
If mode=0 Then txt$="Normal" Else txt$="Speed"
Text 0,62,"Current mode:"+txt$

Flip
Until KeyDown(1)
End
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                 Functions                   ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Type vertexupd
     Field commandindex
     Field surface,index,x#,y#,z#
     Field u#,v#,w#,coordset
     Field r#,g#,b#,a#
End Type

Function VertexCoords2(surface,index,x#,y#,z#)
Local t.vertexupd
t.vertexupd=New vertexupd
t\commandindex=1
t\surface=surface
t\index=index
t\x=x
t\y=y
t\z=z
End Function

Function VertexTexCoords2(surface,index,u#,v#,w#=0,coord_set=0)
Local t.vertexupd
t.vertexupd=New vertexupd
t\commandindex=2
t\surface=surface
t\index=index
t\u#=u
t\v#=v
t\w#=w
t\coordset=coord_set
End Function

Function VertexColor2(surface,index,r#,g#,b#,a#=1)
Local t.vertexupd
t.vertexupd=New vertexupd
t\commandindex=3
t\surface=surface
t\index=index
t\r=r
t\g=g
t\b=b
t\a=a
End Function

Function updatemeshes()
Local t.vertexupd
Local dogrouped=1
For t.vertexupd=Each vertexupd
Select t\commandindex
Case 1
VertexCoords t\surface,t\index,t\x,t\y,t\z
Case 2
VertexTexCoords t\surface,t\index,t\u,t\v,t\w,t\coordset
Case 3
VertexColor t\surface,t\index,t\r,t\g,t\b,t\a
End Select
Delete t
Next
End Function

Comments

octothorpe2005
Keep in mind that this:

		For n=1 To 1000
			vert=Rand(0,CountVertices(sf))
			VertexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			VertexTexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			LinePick(0,0,0,1,1,1)
		Next
		updatemeshes


is equivalent to this:

		For n=1 To 1000
			LinePick(0,0,0,1,1,1)
		Next
		For n=1 To 1000
			vert=Rand(0,CountVertices(sf))
			VertexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
			VertexTexCoords2(sf,vert,Rand(-10,10),Rand(-10,10),Rand(-10,10))
		Next
		updatemeshes


The increase in speed presumably happens because LinePick has some overhead when the entities it's concerned with change. An alternate (and marginally faster) optimization would be to perform all your LinePicks before you start moving vertices around.


Pedro2005
Ok there is an increase of the performance .

But why do you do "LinePick(0,0,0,1,1,1)" ? What is the interest ? I don't undertsand in this particular case.

it is just to demonstrate the improvement ? But what is the interset If we never use linepick in this way ?

Thanks to clarify

:)


Damien Sturdy2005
If you never use linepick, then you may not need the code :)

Octothorpe.

Yes its equivalent, However, Calling Linepick after updating a mesh forces the mesh to be uploaded to the card. Thats where there's a major improvement :)


Code Archives Forum