Original Post
Hi, I made a Frustum class to go with my octree/quadtree view culling.
The constructor is called in main()
However, when I quit, VC++ 08 express gives me an error
"The stack around the variable 'frustum' was corrupted"
Also, even though this frustum class is not currently in use, it is affecting the drawing of some meshes - probably because of the stack corruption. I have no idea of how to stop this happening, can anyone help me out?
PS. I'm pretty sure my maths is correct, but if not, could you point out where? Thanks a lot
struct Frustum
{
D3DXVECTOR3 Normals[6];
float D[0];
Frustum(D3DXMATRIX viewProj)
{
D3DXVECTOR3 corners[8] =
{
D3DXVECTOR3(-1, -1, 0), // bottom left front
D3DXVECTOR3( 1, -1, 0), // top left front
D3DXVECTOR3(-1, 1, 0), // bottom right front
D3DXVECTOR3( 1, 1, 0), // top right front
D3DXVECTOR3(-1, -1, 1),
D3DXVECTOR3( 1, -1, 1),
D3DXVECTOR3(-1, 1, 1),
D3DXVECTOR3( 1, 1, 1),
};
D3DXVECTOR3 worldCorners[8];
// Corners are currently in projection space. To get the corners in world-space, transform by inverse of view proj
D3DXMATRIX inverseViewProj;
float determinant;
D3DXMatrixInverse(&inverseViewProj, &determinant, &viewProj);
// transform the proj space corners into world space
D3DXVec3TransformCoordArray(worldCorners, sizeof(D3DXVECTOR3), corners, sizeof(D3DXVECTOR3), &inverseViewProj, 8);
// get some edges to be used
// edgeA_B is constructed as (corners[A] - corners)
D3DXVECTOR3 edge5_1 = worldCorners[5] - worldCorners[1];
D3DXVECTOR3 edge0_1 = worldCorners[0] - worldCorners[1];
D3DXVECTOR3 edge3_1 = worldCorners[1] - worldCorners[1];
D3DXVECTOR3 edge4_6 = worldCorners[4] - worldCorners[6];
D3DXVECTOR3 edge2_6 = worldCorners[2] - worldCorners[6];
D3DXVECTOR3 edge7_6 = worldCorners[6] - worldCorners[6];
// get the normals to the planes by crossing edges on those planes. all normals point outwards
D3DXVec3Cross(&Normals[FF_Left], &edge0_1, &edge5_1);
D3DXVec3Cross(&Normals[FF_Right], &edge2_6, &edge7_6);
D3DXVec3Cross(&Normals[FF_Top], &edge5_1, &edge3_1);
D3DXVec3Cross(&Normals[FF_Bottom], &edge4_6, &edge2_6);
D3DXVec3Cross(&Normals[FF_Front], &edge3_1, &edge0_1);
D3DXVec3Cross(&Normals[FF_Back], &edge7_6, &edge4_6);
// get the D values. r.n = d
D[FF_Left] = D3DXVec3Dot(&worldCorners[0], &Normals[FF_Left]);
D[FF_Right] = D3DXVec3Dot(&worldCorners[3], &Normals[FF_Right]);
D[FF_Top] = D3DXVec3Dot(&worldCorners[1], &Normals[FF_Top]);
D[FF_Bottom] = D3DXVec3Dot(&worldCorners[2], &Normals[FF_Bottom]);
D[FF_Front] = D3DXVec3Dot(&worldCorners[0], &Normals[FF_Front]);
D[FF_Back] = D3DXVec3Dot(&worldCorners[7], &Normals[FF_Back]);
// normalize all planes so D represents distance to origin
for (int i = 0; i < 6; i++)
{
float len = D3DXVec3Length(&Normals);
Normals /= len;
D /= len;
}
}
};
The constructor is called in main()
D3DXMATRIX world, view, proj;
<snip initialisation>
Frustum frustum(world * view * proj);
However, when I quit, VC++ 08 express gives me an error
"The stack around the variable 'frustum' was corrupted"
Also, even though this frustum class is not currently in use, it is affecting the drawing of some meshes - probably because of the stack corruption. I have no idea of how to stop this happening, can anyone help me out?
PS. I'm pretty sure my maths is correct, but if not, could you point out where? Thanks a lot