Single surface entities

Blitz3D Forums/Blitz3D Programming/Single surface entities

Graphics3D 640,480,16,2

;-----------------------------------------------------------------------
; REQUIRED GLOBALS & TYPES FOR SS_LIB
;-----------------------------------------------------------------------

Global ss_pivot=CreatePivot()
Global ss_large#=1.7*10^38
Type ent
	;vx vy and vz are offsets from x,y,z.
	Field name$,numsurfs,numverts,numtris,vert,tri,vx,vy,vz,layer,layersurf,vertindex
	Field x#,y#,z#,pitch#,yaw#,roll#,sx#,sy#,sz#,r,g,b,a#,visible
	Field nx#,ny#,nz#
End Type
Type layer
	Field mesh,surf,ent
End Type


; COMMANDS

; handle = CreateLayer([alpha#])
; creates a layer and returns the layer handle. This is just a mesh.
; optional alpha true or false will enable alpha support per SS entity.
; However this means you loose your Z sorting.
;
; apply your textures to this as normal.

; handle = CreateEntity( ent , layer )
; transfers an existing entity into the layer and returns a new custom handle
; remember they will share mapping so build your meshes with this in mind.

; using the new custom handle, you can use the following blitz-equivalents:
; SS_PositionEntity ent,x#,y#,z#
; SS_TranslateEntity ent,x#,y#,z#
; SS_MoveEntity ent,x#,y#,z#
; SS_RotateEntity ent,x#,y#,z#
; SS_TurnEntity ent,x#,y#,z#
; SS_ScaleEntity ent,x#,y#,z#
; SS_EntityAlpha ent,alpha#
; SS_EntityColor ent,r,g,b

; SS_EntityX( ent )
; SS_EntityY( ent )
; SS_EntityZ( ent )
; SS_EntityPitch( ent )
; SS_EntityYaw( ent )
; SS_EntityRoll( ent )

;

;-----------------------------------------------------------------------
; simple typical setup example
;-----------------------------------------------------------------------
Global fpsindex,fpstime,fpsfold_millisecs,fpsfps


camera=CreateCamera()
light=CreateLight()
MoveEntity camera,0,0,-10


;sphere=CreateSphere(4)
;HideEntity sphere

cube=CreateCube()
HideEntity cube

; A layer. Transfer as much as you like into it within dx limits.
Global layer2
layer2=SS_CreateLayer()

Type cube_ent
Field model_handle#
End Type
; a single surface cube.;
;c.cube_ent=New cube_ent
mycube = SS_CreateEntity(cube,layer2)
SS_PositionEntity mycube,-2,0,0

;populate scene with single surface spheres for testing (same surf)
;For i=1 To 2
;For j=1 To 2
;c.cube_ent=New cube_ent
;	c\model_handle = SS_CreateEntity(cube,layer2)
;	SS_PositionEntity c\model_handle,i*5,0,j*5

;Next
;Next

;-----------------------------------------------------------------------
; mainloop for testing
;-----------------------------------------------------------------------
While Not KeyHit(1)

	
	RenderWorld
	Text 0,0,TrisRendered()
	Text 0,16,fps()
	Flip 0	

Wend
End








;-----------------------------------------------------------------------
; SS_LIB FUNCTIONS
;-----------------------------------------------------------------------

;-----------------------------------------------------------------------
; Creates a single surface layer. Treat it like a mesh and texture it.

Function SS_CreateLayer(alpha=0)
	l.layer=New layer
	l\mesh=CreateMesh()
	l\surf=CreateSurface(l\mesh)
	AddVertex l\surf,0,0,0
	VertexCoords l\surf,0,0,0,0
	ClearSurface l\surf	
	NameEntity l\mesh,Handle(l)
	l\ent=CreateBank()
	If alpha EntityFX l\mesh,34 Else EntityFX l\mesh,2
	Return l\mesh
End Function

;-----------------------------------------------------------------------
;Function creates a copy of entity in a single surface layer and returns
;the handle, which you can use.

Function SS_CreateEntity(entity,layer)
	;find layer
	l.layer=Object.layer(EntityName(layer))
	;create a virtual entity
	e.ent=New ent
	e\vx=CreateBank()
	e\vy=CreateBank()
	e\vz=CreateBank()	
	e\tri=CreateBank()
	e\name$=Handle(e)
	e\layer=layer
	e\x=EntityX(entity)
	e\x=EntityY(entity)
	e\x=EntityZ(entity)
	e\nx=CreateBank()
	e\ny=CreateBank()
	e\nz=CreateBank()
	e\r=255
	e\g=255
	e\b=255
	e\a#=1
	e\visible=1
	;create the mesh inside the layer & add the data to the virtual entity.
	layersurf=GetSurface(layer,1)
	e\layersurf=layersurf
	e\vertindex=CountVertices(layersurf)
	surf=GetSurface(entity,1)
	;update the virtual mesh
	e\numverts=CountVertices(surf)-1
	e\numtris=CountTriangles(surf)-1
	;add data to layer + virtual mesh
	For i=0 To CountVertices(surf)-1
		AddVertex(layersurf,VertexX(surf,i),VertexY(surf,i),VertexZ(surf,i),VertexU(surf,i),VertexV(surf,i),VertexW(surf,i))
		VertexNormal layersurf,i+e\vertindex,VertexNX(surf,i),VertexNY(surf,i),VertexNZ(surf,i)
		WriteBankFloat(e\vx,VertexX(surf,i))
		WriteBankFloat(e\vy,VertexY(surf,i))
		WriteBankFloat(e\vz,VertexZ(surf,i))
		WriteBankFloat(e\nx,VertexNX(surf,i))
		WriteBankFloat(e\ny,VertexNY(surf,i))
		WriteBankFloat(e\nz,VertexNZ(surf,i))
	Next	
	For i=0 To CountTriangles(surf)-1
		i2=e\vertindex;
		AddTriangle(layersurf,TriangleVertex(surf,i,0)+i2,TriangleVertex(surf,i,1)+i2,TriangleVertex(surf,i,2)+i2)
	Next
	WriteBankInt(l\ent,Handle(e))
	Return Handle(e)
End Function

;-----------------------------------------------------------------------

Function SS_PositionEntity(ent,x#,y#,z#)
	e.ent=Object.ent(ent)
	e\x=x : e\y=y : e\z=z
	RotateEntity ss_pivot,e\pitch,e\yaw,e\roll
	If e\visible=0 Return
	offset=0
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()
		offset=offset+4
	Next		
End Function

;-----------------------------------------------------------------------

Function SS_TranslateEntity(ent,x#,y#,z#)
	e.ent=Object.ent(ent)
	e\x=e\x+x : e\y=e\y+y : e\z=e\z+z
	RotateEntity ss_pivot,e\pitch,e\yaw,e\roll
	If e\visible=0 Return
	offset=0
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()
		offset=offset+4
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_MoveEntity(ent,x#,y#,z#)
	e.ent=Object.ent(ent)
	RotateEntity ss_pivot,e\pitch,e\yaw,e\roll
	TFormVector x,y,z,ss_pivot,0
	e\x=e\x+TFormedX() : e\y=e\y+TFormedY() : e\z=e\z+TFormedZ()
	If e\visible=0 Return
	offset=0
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()		
		offset=offset+4
	Next
End Function

;-----------------------------------------------------------------------
Function SS_TurnEntity(ent,pitch#,yaw#,roll#)
	e.ent=Object.ent(ent)
	RotateEntity ss_pivot,e\pitch#,e\yaw#,e\roll#
	TurnEntity ss_pivot,pitch#,yaw#,roll#
	e\pitch=EntityPitch(ss_pivot)
	e\yaw=EntityYaw(ss_pivot)
	e\roll=EntityRoll(ss_pivot)
	If e\visible=0 Return
	offset=0
	
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()		
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()		
		offset=offset+4
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_RotateEntity(ent,pitch#,yaw#,roll#)
	e.ent=Object.ent(ent)
	RotateEntity ss_pivot,pitch#,yaw#,roll#
	e\pitch=EntityPitch(ss_pivot)
	e\yaw=EntityYaw(ss_pivot)
	e\roll=EntityRoll(ss_pivot)
	If e\visible=0 Return
	offset=0
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()		
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()		
		offset=offset+4
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_TurnEntity2(ent,pitch#,yaw#,roll#)
	e.ent=Object.ent(ent)
	RotateEntity ss_pivot,e\pitch#,e\yaw#,e\roll#
	TurnEntity ss_pivot,pitch#,yaw#,roll#
	e\pitch=EntityPitch(ss_pivot)
	e\yaw=EntityYaw(ss_pivot)
	e\roll=EntityRoll(ss_pivot)
	If e\visible=0 Return
	offset=0
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()		
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()		
		offset=offset+4
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_EntityAlpha(ent,a#)
	e.ent=Object.ent(ent)
	e\a#=a#	
	If e\visible=0 Return
	For i=0 To e\numverts
		VertexColor e\layersurf,i+e\vertindex,e\r,e\g,e\b;,e\a#
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_EntityColor(ent,r,g,b)
	e.ent=Object.ent(ent)
	e\r=r : e\g=g : e\b=b
	If e\visible=0 Return
	For i=0 To e\numverts
		VertexColor e\layersurf,i+e\vertindex,e\r,e\g,e\b;,e\a#
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_HideEntity(ent)
	e.ent=Object.ent(ent)
	If e\visible=0 Return
	For i=0 To e\numverts
		VertexCoords e\layersurf,i+e\vertindex,ss_large,ss_large,ss_large
	Next	
End Function

;-----------------------------------------------------------------------

Function SS_ShowEntity(ent)
	e.ent=Object.ent(ent)
	e\visible=1
	RotateEntity ss_pivot,e\pitch,e\yaw,e\roll
	offset=0
	For i=0 To e\numverts
		TFormPoint ReadBankFloat(e\vx,offset),ReadBankFloat(e\vy,offset),ReadBankFloat(e\vz,offset),ss_pivot,0
		VertexCoords e\layersurf,i+e\vertindex,e\x+TFormedX(),e\y+TFormedY(),e\z+TFormedZ()
		TFormNormal ReadBankFloat(e\nx,offset),ReadBankFloat(e\ny,offset),ReadBankFloat(e\nz,offset),ss_pivot,0
		VertexNormal e\layersurf,i+e\vertindex,TFormedX(),TFormedY(),TFormedZ()
		VertexColor e\layersurf,i+e\vertindex,e\r,e\g,e\b;,e\a#
		offset=offset+4
	Next
End Function

;-----------------------------------------------------------------------

Function SS_EntityX#(ent)
	e.ent=Object.ent(ent)
	Return e\x
End Function

;-----------------------------------------------------------------------

Function SS_EntityY#(ent)
	e.ent=Object.ent(ent)
	Return e\x
End Function

;-----------------------------------------------------------------------

Function SS_EntityZ#(ent)
	e.ent=Object.ent(ent)
	Return e\x
End Function

;-----------------------------------------------------------------------

Function SS_EntityPitch#(ent)
	e.ent=Object.ent(ent)
	Return e\pitch
End Function

;-----------------------------------------------------------------------

Function SS_EntityYaw#(ent)
	e.ent=Object.ent(ent)
	Return e\yaw
End Function
;-----------------------------------------------------------------------

Function SS_EntityRoll#(ent)
	e.ent=Object.ent(ent)
	Return e\roll
End Function

;-----------------------------------------------------------------------

Function WriteBankFloat(bank,value#)
	size=BankSize(bank)
	ResizeBank(bank,size+4)
	PokeFloat bank,size,value#
End Function

;-----------------------------------------------------------------------

Function WriteBankInt(bank,value)
	size=BankSize(bank)
	ResizeBank(bank,size+4)
	PokeInt bank,size,value
End Function

;-----------------------------------------------------------------------

Function ReadBankFloat#(bank,offset)
	Return PeekFloat(bank,offset)
End Function

;-----------------------------------------------------------------------

Function ReadBankInt(bank,offset)
	Return PeekInt(bank,offset)
End Function

;-----------------------------------------------------------------------

Function fps()
	fpsindex=fpsindex+1
	fpstime=fpstime+MilliSecs()-fpsfold_millisecs
	If fpstime=>1000
		fpsfps=fpsindex
		fpstime=0
		fpsindex=0
	EndIf
	fpsfold_millisecs=MilliSecs()
	Return fpsfps
End Function


this will work,
But as soon as you do anything it says 'bb-bank not found'

e.g. uncomment the line where a type instance is created,-error

or,
uncomment the line where a sphere is created- error

I can only guess that blitz is overwriting the bank with the sphere mesh information - so how do you prevent this?

>> as soon as you do anything<<
what exactly are you doing?

Probably the number of vertices is altered, so the size of the bank need to be chanched too?

e.g. uncomment the line where a type instance is created,-error

I have no idea. can you contact the guy who wrote it?

he said he doesnt support it anymore (sniff)