How to know if a triangle is facing the camera?

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

Uncle(Posted 2008) [#1]
Hi,

I've been working with Sswift normal code from the archive...

http://www.blitzbasic.com/codearcs/codearcs.php?code=976

I was hoping that with the normal of the surface, that I would be able to tell if the triangle was in view of the camera. Is this logical or am I barking up the wrong tree? Any clues would be really appreciated as Im new to all this mesh/surface/triangles/vertex business.

Cheers


Ross C(Posted 2008) [#2]
Yeah, i'm sure checking the normal of the triangle is the way to go. I would assume, tforming the normal (TFormNormal) to the cameras co-ords, would allow you to easily check by determining if the z vector is minus or not.

Could be wrong, but i think that's the way it works :o)


Stevie G(Posted 2008) [#3]
This should work ..

;get cameras direction normal
Tformnormal 0,0,1,Camera, 0
CamNx# = tformedx()
CamNy# = tformedy()
CamNz# = tformedz()

;compute dot product of camera normal and tri normal
CdotN# = CamNx*TriNx + CamNY * TriNy + CamNz * TriNz
If CdotN < 0 then
  {This tri is facing the camera}
Endif


I forget whether the CdotN should be < 0 or greater so bear that in mind if it doesn't work as is.

Stevie


Uncle(Posted 2008) [#4]
Thanks both. I seem to be having a problem that my triangle normals are correct. I can calculate the mid point of triangle ok, and I would expect to see my normal coming out 90 to the surface, however the seem to be coming out at an odd angle. I've tried the codes in the archives, but seem to be doing something wrong.

Does anyone have a demo showning the a triangles norm, or a demo showing when a face is visible to the camera? Here's hoping as my head is hurting ;)

Cheers again,


Unc


Stevie G(Posted 2008) [#5]
What do you need this for? Are you using updatenormals - this will average the normals of shared vertices so is wrong?


Uncle(Posted 2008) [#6]
hello, basically Im a using poly which isn't unwelded, so there are a lot of shared verts. I was hoping to try and find out which verts were visible to the camera (for a hidden line routine).

Im still new to this cross product / dot product stuff and still getting my head around it. Anyway when I use a dot product to check visibility with the shared verts, it doesnt always work i.e. I can physically see the vert on the screen before dot product is <=0. Maybe Im doing something wrong here?

So my next idea was to use the normals of each of triangle to check their dot products. If they were in view then I could assume that all the verts that go to make up that triangle are also visible (and therefore I wouldnt need to unweld my model).

HHmm I hope its clear? Thanks for you help on this Stevie G :)


JoeGr(Posted 2008) [#7]
Anyway when I use a dot product to check visibility with the shared verts, it doesnt always work i.e. I can physically see the vert on the screen before dot product is <=0.

1) You should probably unweld and correct the vertex normals first.
2) IIRC you need to point the camera at the vertex you are checking against. Or use a pivot as a 'dummy' camera, parented to the real camera. Then you don't have to bother restoring its rotation afterwards.

Maybe try (2) first and see if that sorts it out.


Uncle(Posted 2008) [#8]
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.




Uncle(Posted 2008) [#9]
ok, nearly got it :)



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 :)


Uncle(Posted 2008) [#10]
Yay! I got it, and it was something stupid... I wasn't recording the projected vertices for the last corner of the triangle correctly. The code read :

CameraProject main_cam,cx,cy,cz
vbx#=ProjectedX()
vby#=ProjectedY()

It should be

CameraProject main_cam,cx,cy,cz
vcx#=ProjectedX()
vcy#=ProjectedY()

Doh! Anyway, this routine works now (fingers crossed). It simply shows all the vertexs which are visible to the camera.

Thanks to everyone for you help on this. Couldnt have done it without you. thats for sure.


JoeGr(Posted 2008) [#11]
Well done. Satisfying, isn't it? Sorry my last post didn't make much sense. I should have looked at your code more carefully. I unweld my mesh and use a vertex normal rather than a face normal in the dot product calculation because that way you don't have to do the cross product stuff that you do to find the face normal. But you seem to have figured out what I meant.

Now get those lines back in and you'll be ready to get cracking on occlusion and depth-sorting. :)