Shading Terrains?

Blitz3D Forums/Blitz3D Programming/Shading Terrains?

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.

When I said post the code - I meant something which I can run without other include files / media etc..

btw. Native spotlights are going to light hidden poly's so this isn't what you want.

The function I gave you could be easily amended to do a check on all 3 vertices of the triangle.

Function TRIScolor( Mesh , Viewer )

	;get viewers direction normal
	TFormNormal 0,0,1,Viewer, 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 vertices in triangle - normals should be same on each
			v0 = TriangleVertex( s, t, 0 )
			v1 = TriangleVertex( s, t, 1 )
			v2 = TriangleVertex( s, t, 2 )
			
			;get triangle normal in world space coords
			TFormVector VertexNX( s, v0 ), VertexNY( s, v0 ) , VertexNZ( s, v0 ) , Mesh , 0
			TriNx# = TFormedX() 
			TriNy# = TFormedY() 
			TriNz# = TFormedZ() 			 
			
			;compute dot product of camera normal and tri normal
			CdotN# = CamNx * TriNx + CamNY * TriNy + CamNz * TriNz
			
			;default rgb to white
			tR = 255
			tG = 255
			tB = 255
			
			If CdotN <= .000001 Then

				;this triangle is facing camera so check whether any of the vertices are obscured
				For v = v0 To v2
					;get vertex position in world space coords
					TFormPoint VertexX( s, v ) , VertexY( s, v ) , VertexZ( s, v ), mesh, 0
					;get direction vector from vertex position to viewer
					Dx# = TFormedX() - EntityX( viewer, 1 )
					Dy# = TFormedY() - EntityY( viewer, 1 )
					Dz# = TFormedZ() - EntityZ( viewer, 1 )
					;do a linepick from the viewer to the vertex
					Picked = LinePick( EntityX( viewer , 1 ) , EntityY( viewer , 1 ) , EntityZ( viewer , 1 ) , Dx, Dy , Dz )
					;the linepick hit something so this tri is obscured
					If PickedTime() < 1.0
						tR = 255
						tG = 0
						tB = 0
						Exit
					EndIf
				Next					

			EndIf
	
			;color this triangle
			For v = v0 To v2
				VertexColor s, v, tR, tG , tB
			Next

		Next
	Next
	
End Function


Does this work for you?

Stevie