# Backface Culling - code included

This topic is 3624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello. I'm trying to add backface culling to my 3d display program. but its not working correctly. It kinda works but not correctly.... if the camera is close to the triangle, you only have to move about 30 degrees around it either way before it disappears. The farther you go the more it seems correct. At a distance, it almost makes the face disappear when it is perpendicular to the camera. Am I doing this right? here is the relevant code: Thanks =)
struct POLYGON
{
float verts[3][3];
float texcoord[3][2];
float normal[3];
bool b_visible;
};

float mag;
float UVCam[3];
float UVNorm[3];
float dot_p;

//loads the normal of the triangle using point 0 as the reference point on the triangle, and segments 0-2 and 0-1 for the cross product.
void Normal(float *_normal, float _v[3][3]) // *_normal points to normal[3] in poly struct, _v[3][3] points to verts[3][3] in poly struct.
{
_normal[0] = (_v[2][1] - _v[0][1]) * (_v[1][2] - _v[0][2]) - (_v[2][2] - _v[0][2]) * (_v[1][1] - _v[0][1]);
_normal[1] = (_v[2][2] - _v[0][2]) * (_v[1][0] - _v[0][0]) - (_v[2][0] - _v[0][0]) * (_v[1][2] - _v[0][2]);
_normal[2] = (_v[2][0] - _v[0][0]) * (_v[1][1] - _v[0][1]) - (_v[2][1] - _v[0][1]) * (_v[1][0] - _v[0][0]);

_normal[0] = _normal[0] + _v[0][0];
_normal[1] = _normal[1] + _v[0][1];
_normal[2] = _normal[2] + _v[0][2];
}
}
// UVCam[] is a unit vector from the camera to the reference point on the polygon, and UVNorm[] is a unit vector to that same point.
// this routine decides which polys are visible or not
void SelectPolys()
{
for(int j = 0; j < n_polygons; j++)
{
UVCam[0] = camera.x - m_polys[j].verts[0][0];
UVCam[1] = camera.y - m_polys[j].verts[0][1];
UVCam[2] = camera.z - m_polys[j].verts[0][2];

mag = fabs(sqrt(UVCam[0] * UVCam[0] + UVCam[1] * UVCam[1] + UVCam[2] * UVCam[2]));

UVCam[0] /= mag;
UVCam[1] /= mag;
UVCam[2] /= mag;

UVNorm[0] = m_polys[j].normal[0] - m_polys[j].verts[0][0];
UVNorm[1] = m_polys[j].normal[1] - m_polys[j].verts[0][1];
UVNorm[2] = m_polys[j].normal[2] - m_polys[j].verts[0][2];

mag = fabs(sqrt(UVNorm[0] * UVNorm[0] + UVNorm[1] * UVNorm[1] + UVNorm[2] * UVNorm[2]));

UVNorm[0] /= mag;
UVNorm[1] /= mag;
UVNorm[2] /= mag;

dot_p = UVNorm[0] * UVCam[0] + UVNorm[1] * UVCam[1] + UVNorm[2] * UVCam[2]; // dot_p is supposed to be the dot product =(

if(dot_p > 0)
m_polys[j].b_visible = true;
else
m_polys[j].b_visible = false;
}

}


EDIT: I have been messing around with the program, and tried putting the polygon at (0,0,0), because it is currently 100 units forward from the origin, and now it seems to work fine. but why is it then that the 100 units forward makes it give the above problem? Shouldn't these lines fix that problem?: //subtracting the reference point on the polygon from the camera vector UVCam[0] = camera.x - m_polys[j].verts[0][0]; UVCam[1] = camera.y - m_polys[j].verts[0][1]; UVCam[2] = camera.z - m_polys[j].verts[0][2]; [Edited by - CPPNick on January 11, 2009 10:05:28 AM]

##### Share on other sites
A normal does not have a "reference point". It is just a direction. It looks like you're adding the first vertex of the triangle to the normal when you compute the normal, and then subtracting it later. It shouldn't be added in the first place.

The dot product should be between the triangle's normal and the direction the camera is facing. The direction from the camera to the first vertex of the triangle is not meaningful here.

##### Share on other sites
Thanks Vorpy... but it turns out my problem was actually that my camera was backwards <=( I was adding to the transformation matrix rather than subtracting...thats why it worked from farther away, because when you get farther away the distance between the polygon and origin becomes negligible.

Thanks anyways.
Nick

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013455
×