PDA

View Full Version : Can you declare glLoadObjects with a array?



shanedudddy2
01-13-2003, 01:05 PM
Hope you guys understand this.
Okay I need to be able to make every triangle running through this loop selectable by the mouse. How would I do this??
I know how to use glLoadName and the selection and feedback system.
Please some hints or pointers
THANKS!!

for (int i= 0; i<tris.size();i++)
{


glVertex3f(verts[tris[i].ix[2]].x,verts[tris[i].ix[2]].y,verts[tris[i].ix[2]].z);
glVertex3f(2.0f,0.0f,0.0f);
glVertex3f(5.0f,0.0f,0.0f);

};
glEnd();

Robbo
01-14-2003, 12:49 AM
Best approach depends on the number of triangles in the object. You could push an identifier per triangle, but that would be a little grim.

Alternatively, you can exchange your loop for some kind of ray-triangle intersection test - depending on the number of objects it would still be slow, but you don't have much choice without some kind of spatial partitioning system in there:

for (int i= 0; i<tris.size();i++)
{
TestIntersection(RayOrigin, RayDirection, vertex1, vertex2, vertex3
};

shanedudddy2
01-14-2003, 08:14 PM
It looks that way!
I have been looking for everything on ray intersection and can't find anything.
Could you please help and explain how to create the ray and intersection with my code example.
Thanks.

shanedudddy2
01-15-2003, 01:35 AM
Please, please, please, please!!

Robbo
01-15-2003, 02:56 AM
The first thing you need to do is generate a ray from the mouse cursor position into the scene. This is done by unprojecting the mouse position. This is a function from my most recent project that does a similar thing. Note that the mistake most often made is to get the ray with the incorrect matrix on the stack. Be sure to execute this function after executing your current camera modelview and projection matrix and not a local matrix for any scene objects.

//
// Get the ray from the cursor into the scene - no selection occurs - this is just a utility method.
//

void CSelectionBuffer :: GetRay ( int MouseX, int MouseY, CVertex3D& p1, CVector3D& p2, float z )

{
//
// Finally, we need to reverse transform the values we have back into the world to get correct world x, y, z.
//

GLdouble Projection [16], Model [16];
GLint Viewport [4];

//
// Now get the matrices and viewport.
//

glGetDoublev ( GL_PROJECTION_MATRIX, Projection );
glGetDoublev ( GL_MODELVIEW_MATRIX, Model );
glGetIntegerv( GL_VIEWPORT, Viewport );

//
// We will need to invert mouse y (because its top left whereas our window is bottom left origin).
//

double Invert_m_y = static_cast<double> ( Viewport [3] ) - static_cast<double> ( MouseY );

HITRECORD h1, h2;

//
// Inverse transform the given point - note its important that the viewing transforms are setup as they were rendered.
//

gluUnProject ( static_cast<double> ( MouseX ), // Mouse x.
Invert_m_y, // Inverted mouse y.
0.0, // The z depth result we got from selection.
Model, // Model view matrix.
Projection, // Projection matrix.
Viewport, // Viewport co-ordinates.
&h1.x, // Final result x.
&h1.y, // Final result y.
&h1.z ); // Final result z.

gluUnProject ( static_cast<double> ( MouseX ), // Mouse x.
Invert_m_y, // Inverted mouse y.
1.0, // The z depth result we got from selection.
Model, // Model view matrix.
Projection, // Projection matrix.
Viewport, // Viewport co-ordinates.
&h2.x, // Final result x.
&h2.y, // Final result y.
&h2.z ); // Final result z.

//
// Copy results across ( from double to float ).
//

p1.v [0] = static_cast<float> ( h1.x );
p1.v [1] = static_cast<float> ( h1.y );
p1.v [2] = static_cast<float> ( h1.z );

p2.v [0] = static_cast<float> ( h2.x );
p2.v [1] = static_cast<float> ( h2.y );
p2.v [2] = static_cast<float> ( h2.z );

//
// Turn inverse transformed vertex into a unit vector.
//

if ( ( p2 - p1 ) == 0 )
{
return;
}
else
{
p2 = ( p2 - p1 ).Normalized ();
}
}


Next you need to find the intersection of an infinite ray and a triangle (lots of tuts on the net all over, search on ray\triangle intersection) - you will need to check all intersections and use the intersection thats closest to you(distance from intersection point to camera eyepoint).

Thats more or less it I guess.

knackered
01-15-2003, 03:53 AM
I feel sorry for you - here's a nice tutorial:- http://geometryalgorithms.com/Archive/algorithm_0105/algorithm_0105.htm#Segment-Triang le (http://geometryalgorithms.com/Archive/algorithm_0105/algorithm_0105.htm#Segment-Triangle)

BTW, you really shouldn't be doing things in quite the way rob suggests (no offense, rob). Doing things like reading the current modelview and projection matrices from the driver is just damn messy. Using a graphics API specific helper library to do what is essentially an easy maths operation is silly.
You should be maintaining the matrices (world/projection/nodeLocal/nodeAbsolute) in your own classes/structs. Then querying stuff like this is fast and more self contained, and less reliant on a particular rendering api - and you can do it whereever you are in your app, and not just when there's a current render context (which would be invalid in another thread, for instance).
I hate it when people use DX datatypes/helpers just as much as when people use GL datatypes and helpers!.
Just add an unprojectvec function to your matrix class or whatever, and upload your own system memory matrices as and when GL needs them.
Mark my words, even though I talk a lot of sh*t sometimes, in this case I'm talking sense - you will regret basing your renderer on a specific API.

[This message has been edited by knackered (edited 01-15-2003).]