MrPickle

04-18-2008, 04:40 AM

I am calling gluUnProject(); with the mouse's x coords and viewport[3]- mouse's y coord twice to get a starting and end point for my ray then subtracting the starting point from the end point and normalizing it to get the direction of the ray but the ray's just going off where it wants.

The greenline is the ray and the red circle is where I clicked.

http://i159.photobucket.com/albums/t156/RTBPhotoslol/RayFront.png

http://i159.photobucket.com/albums/t156/RTBPhotoslol/RaySide.png

As you can see the ray is very high up to where I clicked. It also moves along the x axis the furthur I click from the center.

When the ray intersects a triangle it's ment to make the triangle go red. As you can see the triangle the ray did intersect didn't go red. I have to get extremely close for the faces to go red but the ray never passes through the faces that are going red nor is the face that goes red under my mouse.

Here's my code:

void Project(int x, int y)

{

GLdouble model_view[16];

GLdouble projection[16];

GLint viewport[4];

GLdouble NearX, NearY, NearZ, FarX, FarY, FarZ;

Vector Dir, Edge1, Edge2, H, S, Q;

float a, f, u, v, t;

Triangle *Current;

glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

glGetDoublev(GL_PROJECTION_MATRIX, projection);

glGetIntegerv(GL_VIEWPORT, viewport);

gluUnProject(x, viewport[3]-y, 0.0f, model_view, projection, viewport, &NearX, &NearY, &NearZ);

gluUnProject(x, viewport[3]-y, 1.0f, model_view, projection, viewport, &FarX, &FarY, &FarZ);

Near.x = NearX; Near.y = NearY; Near.z = NearZ;

Far.x = FarX; Far.y = FarY; Far.z = FarZ;

Dir = VectorSub(Far, Near);

Dir = VectorNorm(Dir);

for(int i = 1; i < Object.getTriangleCount(); i++)

{

Current = &Object.getTriangle(i);

Edge1 = VectorSub(Object.getVertex(Current->v[1]).Pos, Object.getVertex(Current->v[0]).Pos);

Edge2 = VectorSub(Object.getVertex(Current->v[2]).Pos, Object.getVertex(Current->v[0]).Pos);

H = VectorCross(Dir, Edge2);

a = VectorDot(Edge1, H);

if(a > -0.00001f)

continue;

f = 1.0f/a;

S = VectorSub(Near, Object.getVertex(Current->v[0]).Pos);

u = f * VectorDot(S, H);

if(u < -0.0f || u > 1.0f)

continue;

Q = VectorCross(S, Edge1);

v = f * VectorDot(Dir, Q);

if(v < -0.0f || u + v > 1.0f)

continue;

t = f * VectorDot(Edge2, Q);

if(t > 0.0f)

Object.setFaceColour(i, 1.0, 0.0, 0.0);

}

}

Here are my vector functions:

Vector VectorSub(Vector VecA, Vector VecB)

{

Vector Sub;

Sub.x = VecA.x - VecB.x;

Sub.y = VecA.y - VecB.y;

Sub.z = VecA.z - VecB.z;

return Sub;

}

float VectorDot(Vector VecA, Vector VecB)

{

float Dot;

Dot = (VecA.x*VecB.x) + (VecA.y*VecB.y) + (VecA.z*VecB.z);

return Dot;

}

Vector VectorCross(Vector VecA, Vector VecB)

{

Vector CrossProduct;

CrossProduct.x = (VecA.y*VecB.z)-(VecA.z*VecB.y);

CrossProduct.y = (VecB.z*VecA.x)-(VecB.x*VecA.z);

CrossProduct.z = (VecA.x*VecB.y)-(VecA.y*VecB.x);

return CrossProduct;

}

Vector VectorNorm(Vector VecA)

{

Vector Norm;

float DotProduct, NormFactor;

DotProduct = VectorDot(VecA, VecA);

NormFactor = sqrt(DotProduct);

Norm.x = VecA.x/NormFactor;

Norm.y = VecA.y/NormFactor;

Norm.z = VecA.z/NormFactor;

return Norm;

}

The greenline is the ray and the red circle is where I clicked.

http://i159.photobucket.com/albums/t156/RTBPhotoslol/RayFront.png

http://i159.photobucket.com/albums/t156/RTBPhotoslol/RaySide.png

As you can see the ray is very high up to where I clicked. It also moves along the x axis the furthur I click from the center.

When the ray intersects a triangle it's ment to make the triangle go red. As you can see the triangle the ray did intersect didn't go red. I have to get extremely close for the faces to go red but the ray never passes through the faces that are going red nor is the face that goes red under my mouse.

Here's my code:

void Project(int x, int y)

{

GLdouble model_view[16];

GLdouble projection[16];

GLint viewport[4];

GLdouble NearX, NearY, NearZ, FarX, FarY, FarZ;

Vector Dir, Edge1, Edge2, H, S, Q;

float a, f, u, v, t;

Triangle *Current;

glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

glGetDoublev(GL_PROJECTION_MATRIX, projection);

glGetIntegerv(GL_VIEWPORT, viewport);

gluUnProject(x, viewport[3]-y, 0.0f, model_view, projection, viewport, &NearX, &NearY, &NearZ);

gluUnProject(x, viewport[3]-y, 1.0f, model_view, projection, viewport, &FarX, &FarY, &FarZ);

Near.x = NearX; Near.y = NearY; Near.z = NearZ;

Far.x = FarX; Far.y = FarY; Far.z = FarZ;

Dir = VectorSub(Far, Near);

Dir = VectorNorm(Dir);

for(int i = 1; i < Object.getTriangleCount(); i++)

{

Current = &Object.getTriangle(i);

Edge1 = VectorSub(Object.getVertex(Current->v[1]).Pos, Object.getVertex(Current->v[0]).Pos);

Edge2 = VectorSub(Object.getVertex(Current->v[2]).Pos, Object.getVertex(Current->v[0]).Pos);

H = VectorCross(Dir, Edge2);

a = VectorDot(Edge1, H);

if(a > -0.00001f)

continue;

f = 1.0f/a;

S = VectorSub(Near, Object.getVertex(Current->v[0]).Pos);

u = f * VectorDot(S, H);

if(u < -0.0f || u > 1.0f)

continue;

Q = VectorCross(S, Edge1);

v = f * VectorDot(Dir, Q);

if(v < -0.0f || u + v > 1.0f)

continue;

t = f * VectorDot(Edge2, Q);

if(t > 0.0f)

Object.setFaceColour(i, 1.0, 0.0, 0.0);

}

}

Here are my vector functions:

Vector VectorSub(Vector VecA, Vector VecB)

{

Vector Sub;

Sub.x = VecA.x - VecB.x;

Sub.y = VecA.y - VecB.y;

Sub.z = VecA.z - VecB.z;

return Sub;

}

float VectorDot(Vector VecA, Vector VecB)

{

float Dot;

Dot = (VecA.x*VecB.x) + (VecA.y*VecB.y) + (VecA.z*VecB.z);

return Dot;

}

Vector VectorCross(Vector VecA, Vector VecB)

{

Vector CrossProduct;

CrossProduct.x = (VecA.y*VecB.z)-(VecA.z*VecB.y);

CrossProduct.y = (VecB.z*VecA.x)-(VecB.x*VecA.z);

CrossProduct.z = (VecA.x*VecB.y)-(VecA.y*VecB.x);

return CrossProduct;

}

Vector VectorNorm(Vector VecA)

{

Vector Norm;

float DotProduct, NormFactor;

DotProduct = VectorDot(VecA, VecA);

NormFactor = sqrt(DotProduct);

Norm.x = VecA.x/NormFactor;

Norm.y = VecA.y/NormFactor;

Norm.z = VecA.z/NormFactor;

return Norm;

}