How to know if a triangle is facing the camera?

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

Here's the code so far. I can't take any credit for it as its Sswifts more or less. It's close to what Im looking for, but you will see when you turn the mesh, some vertexs appear before they should (this happens with welded and unwelded meshes) - I guess because Im not looking at any normals, and only verts positions.

Ref, your suggestion Joe, Im not sure where I would do that. Does the angle of the camera effect the dot calculation. Sorry Im really new to this.

;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.	
	TFormNormal 0,0,1, main_cam, 0	
	CNx# = TFormedX#()
	CNy# = TFormedY#()
	CNz# = TFormedZ#()
	;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()
			
			; 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#
			
			; 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


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 cube,0,0.1,0
	If KeyDown(205) Then TranslateEntity cube,0.1,0,0
	If KeyDown(203) Then TranslateEntity cube,-0.1,0,0
	If KeyDown(208) Then TranslateEntity cube,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