How to know if a triangle is facing the camera?

Blitz3D Forums/Blitz3D Programming/How to know if a triangle is facing the camera?

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.

ok, nearly got it :)

;setup graphics
Graphics3D 1024, 768, 0, 2
SetBuffer BackBuffer()

;enable wireframe	
WireFrame 1

;create camera	
Global main_cam = CreateCamera()
MoveEntity main_cam, 0, 0, -5

;create cube	
Global cube = CreateSphere(4)

Global surfaces,tris,verts

Function plotvertex(x,y,col)
	If col=1 Then 
		Color 0,255,0
	Else
		Color 255,0,0
	EndIf
	Oval x,y,3,3
End Function


Function Hidden(mesh)
	; Calculate the normal which indicates the direction the camera is pointing.	
	;count the surfaces of the mesh
	surfaces=CountSurfaces(mesh)
	
	;loop through all the surfaces
	For surfcount=1 To surfaces
		;count the number of triangles on the surfaces
		s=GetSurface(mesh,surfcount)
		tris=CountTriangles(s)
		verts=CountVertices(s)
		For t=0 To tris-1
			
			;get the corners of each triangle
			corner1=TriangleVertex(s,t,0)
			corner2=TriangleVertex(s,t,1)
			corner3=TriangleVertex(s,t,2)
			
			; get vertex information for corner 1
			TFormPoint VertexX(s,corner1),VertexY(s,corner1),VertexZ(s,corner1),mesh,0
			ax#=TFormedX()
			ay#=TFormedY()
			az#=TFormedZ()
			CameraProject main_cam,ax,ay,az
			vax#=ProjectedX()	
			vay#=ProjectedY()
			
			; get vertex information for corner 2
			TFormPoint VertexX(s,corner2),VertexY(s,corner2),VertexZ(s,corner2),mesh,0
			bx#=TFormedX()
			by#=TFormedY()
			bz#=TFormedZ()
			CameraProject main_cam,bx,by,bz
			vbx#=ProjectedX()	
			vby#=ProjectedY()
			
			; get vertex information for corner 3
			TFormPoint VertexX(s,corner3),VertexY(s,corner3),VertexZ(s,corner3),mesh,0
			cx#=TFormedX()
			cy#=TFormedY()
			cz#=TFormedZ()
			CameraProject main_cam,cx,cy,cz
			vbx#=ProjectedX()	
			vby#=ProjectedY()
			
			; find mid point of triangle and plot it
			mx# = (ax+bx+cx) / 3
			my# = (ay+by+cy) / 3
			mz# = (az+bz+cz) / 3
			
			; Triangle 1
			; Get the vectors for two edges of the triangle.
			Px# = Ax#-Bx#
			Py# = Ay#-By#
			Pz# = Az#-Bz#
			
			Qx# = Bx#-Cx#
			Qy# = By#-Cy#
			Qz# = Bz#-Cz#
			
			; Compute their cross product to get the face normal.
			FNx# = Py#*Qz# - Pz#*Qy#
			FNy# = Pz#*Qx# - Px#*Qz#
			FNz# = Px#*Qy# - Py#*Qx#
			
			PositionEntity campivot,EntityX(main_cam,1),EntityY(main_cam,1),EntityZ(main_cam,1)
			PositionEntity temppivot,mx,my,mz
			PointEntity campivot,temppivot
			TFormNormal 0,0,1,campivot,0
			cnx#=TFormedX()
			cny#=TFormedY()
			cnz#=TFormedZ()
			
			; Compute the dot product of the face and camera normal.
			DP# = FNx#*CNx# + FNy#*CNy# + FNz#*CNz#
			
			; Is this face facing towards the camera, if so then draw its verts?
			If DP# <0
				plotvertex(vax,vay,1)
				plotvertex(vbx,vby,1)
				plotvertex(vcx,vcy,1)
			EndIf	
		Next
	Next
End Function

Global campivot=CreatePivot()
Global temppivot=CreatePivot()
fps=0
oldfps=0
fpstime=MilliSecs()+1000

While Not KeyDown(1)
	; fps stuff
	If MilliSecs()>fpstime Then
		fps=oldfps
		oldfps=0
		fpstime=MilliSecs()+1000
	Else
		oldfps=oldfps+1
	EndIf
	; main stuff
	If MouseDown(1) Then TurnEntity cube,0,0.4,0
	If MouseDown(2) Then TurnEntity cube,0.4,0,0
	If KeyDown(200) Then TranslateEntity main_cam,0,0.1,0
	If KeyDown(205) Then TranslateEntity main_cam,0.1,0,0
	If KeyDown(203) Then TranslateEntity main_cam,-0.1,0,0
	If KeyDown(208) Then TranslateEntity main_cam,0,-0.1,0
	RenderWorld()
	Hidden(cube)
	Color 255,255,255
	Text 10,10,"Surfaces : "+surfaces
	Text 10,25,"Triangles : "+tris
	Text 10,40,"Vertices : "+verts
	Text 930,10,"FPS : "+fps
	Text 10,750,"Left / Right Mouse Button to rotate, and cursors to move. "
	Flip 
	
Wend


There are now only a couple of point which should be highlight, which aren't. Not sure why. Anyone got an ideas? Im hoping its something stupid that I have forgotten to do.

Cheers :)