Drawing triangles quickly

Blitz3D Forums/Blitz3D Programming/Drawing triangles quickly

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

In which case use vertexcolor .... you can also use the optional alpha parameter if you set entityfx to 2+32.

First unweld the mesh if you haven't already done so to ensure you can colour each individual triangle it's own color. I have a function which does this if you need it. Next use a function which calculates the triangle normal rather than the averaged shared vertex normal ( as per updatenormals ) function ..

Function MESHnormals( mesh )

	For su = 1 To CountSurfaces(mesh )
		s = GetSurface( mesh , su )

		For t = 0 To CountTriangles( s )-1
			v0 = TriangleVertex( s, t, 0 )
			v1 = TriangleVertex( s, t, 1 )
			v2 = TriangleVertex( s, t, 2 )
			ax# = VertexX( s, v1 ) - VertexX( s, v0 )
			ay# = VertexY( s, v1 ) - VertexY( s, v0 )	
			az# = VertexZ( s, v1 ) - VertexZ( s, v0 )	
			bx# = VertexX( s, v2 ) - VertexX( s, v1 )
			by# = VertexY( s, v2 ) - VertexY( s, v1 )	
			bz# = VertexZ( s, v2 ) - VertexZ( s, v1 )	
			Nx# = ( ay * bz ) - ( az * by )
			Ny# = ( az * bx ) - ( ax * bz )
			Nz# = ( ax * by ) - ( ay * bx )
			Ns# = Sqr( Nx * Nx + Ny*Ny + Nz*Nz ) 
			Nx = Nx / Ns
			Ny = Ny / Ns
			Nz = Nz / Ns
			
			For v = v0 To v2
				VertexNormal s, v, Nx, Ny, Nz 
			Next
		Next
	Next

End Function


Then use a function like so ... by passing both the mesh and the camera entities.

Function TRIScolor( Mesh , Camera )

	;set mesh to vertexcolor
	EntityFX Mesh, 2

	;get cameras direction normal
	TFormNormal 0,0,1,Camera, 0
	CamNx# = TFormedX()
	CamNy# = TFormedY()
	CamNz# = TFormedZ()

	For su = 1 To CountSurfaces( Mesh )
		s = GetSurface( Mesh , su )
		For t = 0 To CountTriangles( s ) - 1
		
			;get first vertex in triangle - normals should be same on each
			v0 = TriangleVertex( s, t, 0 )
			TriNX# = VertexNX( s, v0 )
			TriNy# = VertexNY( s, v0 )
			TriNz# = VertexNZ( s, v0 )			 
			
			;compute dot product of camera normal and tri normal
			CdotN# = CamNx*TriNx + CamNY * TriNy + CamNz * TriNz
			
			If CdotN < 0 Then
			
				;triangle facing camera - color red
				For v = v0 To v0 + 2
					VertexColor s, v, 255,0,0
				Next
			
			Else
			
				;triangle not facing camera - color white
				For v = v0 To v0 + 2
					VertexColor s, v, 255,255,255
				Next
			
			EndIf

		Next
	Next
	
End Function


Stevie