Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ray-triangle intersection and glTranslate()?

Started by littlejedi Mar 24, 2005 at 7:11 AM 6 replies 1.1k views
Original Post
littlejedi
littlejedi
I'm an beginner. With referrence to the algorithm of ray-triangle intersection by Moller,Trumbore(the most common one used) I wrote the code in my OpenGL program in visual c++, but it just doesn't work. I know nothing is wrong with the algorithm(http://www.graphics.cornell.edu/pubs/1997/MT97.html) Someone tell me the algorithm need to modify(the ray's coordinate should be modified) if glTranslate is used. But I don't know them in detail. Could someone explain it to me?? It's very important for my program. Many thanks!!
James Trotter
James Trotter
Quote:
Original post by littlejedi
I'm an beginner.
With referrence to the algorithm of ray-triangle intersection by Moller,Trumbore(the most common one used)
I wrote the code in my OpenGL program in visual c++, but it just doesn't work.


This is helping neither you nor me. You will need to post your code. Also, it would help if you specify in what you mean by "it just doesn't work". What exactly is wrong?

Quote:

Someone tell me the algorithm need to modify(the ray's coordinate should be modified) if glTranslate is used. But I don't know them in detail.
Could someone explain it to me??


You haven't given enough information for anyone to help you. I also have problems understanding what exactly it is you're asking. Try to formulate your query in a clearer manner.
littlejedi
littlejedi
Here's an example code.They are not my code, but it can tell you what I'm trying to ask.

bool IntersectTriangle() // ray-triangle intersection, as I mentioned above
{
GLfloat edge1[3];
GLfloat edge2[3];

edge1[0]=V1[0]-V0[0];
edge1[1]=V1[1]-V0[1];
edge1[2]=V1[2]-V0[2];

edge2[0]=V2[0]-V0[0];
edge2[1]=V2[1]-V0[1];
edge2[2]=V2[2]-V0[2];

GLfloat dir[3];
dir[0]=g_farxyz[0]-g_nearxyz[0];
dir[1]=g_farxyz[1]-g_nearxyz[1];
dir[2]=g_farxyz[2]-g_nearxyz[2];

GLfloat w = (GLfloat)sqrt((double)pow(dir[0],2.0)+(double)pow(dir[1],2.0)+(double)pow(dir[2],2.0));
dir[0] /= w;
dir[1] /= w;
dir[2] /= w;

GLfloat pvec[3];
pvec[0]= dir[1]*edge2[2] - dir[2]*edge2[1];
pvec[1]= dir[2]*edge2[0] - dir[0]*edge2[2];
pvec[2]= dir[0]*edge2[1] - dir[1]*edge2[0];

GLfloat det ;
det = edge1[0]*pvec[0]+edge1[1]*pvec[1]+edge1[2]*pvec[2];

GLfloat tvec[3];
if( det > 0 )
{

tvec[0] = g_nearxyz[0] - V0[0];
tvec[1] = g_nearxyz[1] - V0[1];
tvec[2] = g_nearxyz[2] - V0[2];

}
else
{

tvec[0] = V0[0] - g_nearxyz[0];
tvec[1] = V0[1] - g_nearxyz[1];
tvec[2] = V0[2] - g_nearxyz[2];
det = -det ;

}

if( det < 0.0001f ) return false;


GLfloat u ;
u = tvec[0]*pvec[0]+ tvec[1]*pvec[1]+ tvec[2]*pvec[2];

if( u < 0.0f || u > det ) return false;

GLfloat qvec[3];
qvec[0]= tvec[1]*edge1[2] - tvec[2]*edge1[1];
qvec[1]= tvec[2]*edge1[0] - tvec[0]*edge1[2];
qvec[2]= tvec[0]*edge1[1] - tvec[1]*edge1[0];


GLfloat v;
v = dir[0]*qvec[0]+dir[1]*qvec[1]+dir[2]*qvec[2];
if( v < 0.0f || u + v > det ) return false;

GLfloat t = edge2[0]*qvec[0]+edge2[1]*qvec[1]+edge2[2]*qvec[2];
GLfloat fInvDet = 1.0f / det;
t *= fInvDet;
u *= fInvDet;
v *= fInvDet;
return true;

}

void pick(GLfloat xpos,GLfloat ypos)//Get Selection Ray from the screen
{
xpos,ypos;
GLint viewport[4];
GLdouble mvmatrix[16],projmatrix[16];
GLint realy;
GLdouble wx,wy,wz;

glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);

realy = viewport[3]-(GLint)ypos -1;
gluUnProject((GLdouble)xpos,(GLdouble)realy,0.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);

g_nearxyz[0] = (GLfloat)wx;
g_nearxyz[1] = (GLfloat)wy;
g_nearxyz[2] = (GLfloat)wz;////

gluUnProject((GLdouble)xpos,(GLdouble)realy,1.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);

g_farxyz[0] = (GLfloat)wx;
g_farxyz[1] = (GLfloat)wy;
g_farxyz[2] = (GLfloat)wz;////

g_color = 0.0;
if(IntersectTriangle()) g_color=1.0;

}
GLfloat V0[3]={1.0,0.0,-1.0 };
GLfloat V1[3]={0.0,1.0,-1.0 };
GLfloat V2[3]={0.0,0.0,-2.0 };


Void DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(g_color,0.0,1.0);
glVertex3fv(V0);//
glVertex3fv(V1);
glVertex3fv(V2);
glEnd();
SwapBuffers(hDC);
}
}

There's a mark in the end, it said: if GLTRANSALTE() or this kind of function is used, THE SELECTIONR RAY SHOULD BE MODIFIED..

That's what I don't understand. If you will help me, plz tell me!!
Many thanks again!
littlejedi
littlejedi
But how to modify the selection ray?
Somehow, I'm always puzzled by this kind or that kind of coordinate system or matrix :(
James Trotter
James Trotter
I'm sorry if this doesn't help you directly. I haven't looked through that code for the problem, because it's badly (not at all?) commented, and written in a generally untidy manner. Many parts of the code could be simplified by, for instance, making use of classes or structs for organizing things. A vector class with a few basic functions (overloaded arithmetic operators +,-,*,/, vector product and scalar product, etc. You've got them all in there, but they're hard-coded each place you're using them!) would help you greatly.

Anyway, I think that the real problem might be that you have (as it appears to me) a fairly weak understanding of co-ordinate systems, matrix transformations, and how it all fits together. I'm just pointing out that it's not simple stuff to learn and understand, but when you do understand it, everything becomes much more simple. A book on linear algebra is a good place to start.

If I'm mistaken, then I apologize, and retract the preceding statements.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.