🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

(NEWBIE) Help ov visible faces on a rotating cube

Started by
1 comment, last by KappaMic 23 years, 12 months ago
I want to change the bitmaps (on tut6) when the faces are not visible. How can i know if aface is visible or not? Many thanks! A simple routine would be good...
Advertisement
quote: Original post by KappaMic

I want to change the bitmaps (on tut6) when the faces are not visible.
How can i know if aface is visible or not?
Many thanks!

A simple routine would be good...





I''m not sure if OpenGL supports it, but there is a geometric way to do it. If you''re familiar with the cross product, it''s not too hard. This is somewhat general (because I haven''t seen the tutorial) but it should work.

First, if we have three points from a face (A,B,C) with B being a corner that connects to the other two, we calculate two directional vectors U and V:

B--C U = C - B
/ / V = A - B
A--D

Now that we''ve got U and V, we take the cross product, U x V to get vector N.

N = U x V

or

Nx = Uy*Vz - Uz*Vy;
Ny = Uz*Vx - Ux*Vz;
Nz = Ux*Vy - Vy*Vx;

The order U x V or V x U depends on whether the points on the cube are plotted clockwise or counter clockwise - you''ll have to try them to both to see which works.

Once you have N, you can calculate the plane equation for the face

a*x + b*y + c*z + d = 0
a = Nx, b = Ny, c = Nz.

Because we know Nx, Ny, and Nz, and we also have the x,y,z coords of a point on the face can calculate D like this:

d = -(N . B)
Where N is the normal vector (that we calculated) and B is one of the points on the face.

Now, when we put the location of the camera into the equation (the point we''ll call M) We get a*Mx + b*My + c*Mz + d - if the result is GREATER than zero, then the face is visible, if it''s negative, the face is invisible. If it is zero, then we''re actually seeing the plane from the side (which, because it has no thickness, makes it invisible).

That''s a lot of work for each polygon each frame. My guess is that there IS a way to do it with OpenGL. I think (although I haven''t tried it) that using glGetfv(CURRENT_NORMAL, myNormal) will get the normal used in the calculations for the last face drawn. That would save you the trouble of doing the cross product, which is the most expensive computation. If you find a better way, let me know at: hawking@neteze.com

Good luck
AC
quote: Original post by KappaMic

I want to change the bitmaps (on tut6) when the faces are not visible.
How can i know if aface is visible or not?
Many thanks!

A simple routine would be good...





I''m not sure if OpenGL supports it, but there is a geometric way to do it. If you''re familiar with the cross product, it''s not too hard. This is somewhat general (because I haven''t seen the tutorial) but it should work.

First, if we have three points from a face (A,B,C) with B being a corner that connects to the other two, we calculate two directional vectors U and V:

B--C U = C - B
/ / V = A - B
A--D

Now that we''ve got U and V, we take the cross product, U x V to get vector N.

N = U x V

or

Nx = Uy*Vz - Uz*Vy;
Ny = Uz*Vx - Ux*Vz;
Nz = Ux*Vy - Vy*Vx;

The order U x V or V x U depends on whether the points on the cube are plotted clockwise or counter clockwise - you''ll have to try them to both to see which works.

Once you have N, you can calculate the plane equation for the face

a*x + b*y + c*z + d = 0
a = Nx, b = Ny, c = Nz.

Because we know Nx, Ny, and Nz, and we also have the x,y,z coords of a point on the face can calculate D like this:

d = -(N . B)
Where N is the normal vector (that we calculated) and B is one of the points on the face.

Now, when we put the location of the camera into the equation (the point we''ll call M) We get a*Mx + b*My + c*Mz + d - if the result is GREATER than zero, then the face is visible, if it''s negative, the face is invisible. If it is zero, then we''re actually seeing the plane from the side (which, because it has no thickness, makes it invisible).

That''s a lot of work for each polygon each frame. My guess is that there IS a way to do it with OpenGL. I think (although I haven''t tried it) that using glGetfv(CURRENT_NORMAL, myNormal) will get the normal used in the calculations for the last face drawn. That would save you the trouble of doing the cross product, which is the most expensive computation. If you find a better way, let me know at: hawking@neteze.com

Good luck
AC

This topic is closed to new replies.

Advertisement