Help with selecting polygons and using the mouse in 3d
I'm trying to get something simple up and running just to learn how to work with the mouse in 3d. In 2d its no problem but I cant figure out how to use it in 3d. What Im doing is, I made a grid that extends out down the z axis. I want to highlight the square the mouse is over when you move the mouse. You can also move your view point and move in all 8 directions. Its in opengl, I've looked at the selection and picking functions but they dont seem too useful for this, unless I give a name to every polygon on the grid and that doesnt seem to be a good idea. So Im trying windows now. Any advice or help with how to deal with the 3d aspect?
Are you looking for something like this?
http://nehe.gamedev.net/data/articles/article.asp?article=13
http://nehe.gamedev.net/data/articles/article.asp?article=13
Thanks thats perfect, I guess I'll have to use glu now. If someone has any other ways Id like to hear those too.
I assume ur using DirectX then, ummm, look at the SDK Pick example, if that doesn't help, look at the functions,
D3DXIntersect();
D3DXIntersectSubset();
These are how its done,
and google is pretty useful as well.
ace
D3DXIntersect();
D3DXIntersectSubset();
These are how its done,
and google is pretty useful as well.
ace
Im using opengl. For some reason the nehe article isnt giving me what I want. The z coordinate is always -999 and doesnt change, the x and y sometimes jump to 0 for no reason I can find. I'll play around with it more and see what I can do.
I'd actually suggest THIS nehe tutorial:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32.
It covers picking polygons with the mouse (see the function "void Selection( void )"). Note that instead of returning a 3d coordinate, it returns the index number of the polygon you clicked, which IMHO is easier to use.
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32.
It covers picking polygons with the mouse (see the function "void Selection( void )"). Note that instead of returning a 3d coordinate, it returns the index number of the polygon you clicked, which IMHO is easier to use.
I use both, but the ray picking is faster if you need the ploy in front, but I am doing tile games. So sometimes I need the ploy farthest away then I use object selection. for object selection I like this tutorial
http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg3.htm
Object selection
http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg3.htm
Object selection
I'm doing it in software. Creating a ray from the mouse coordinate, and then intersecting it with objects/triangles. It's not working 100% yet, but enough to be useable. If you're interested I could post a snippet later.
I got the code from article 13 to work, I forgot to turn on depth testing...Doh! But i think I'm going to convert somehow or way to code that functions more like picking because it will let me select the polygon better. I eventually want to be able to manipulate the grid, moving the vertices and etc. Thanks all for the help!
I got this fully functional code for selecting does a ray intersect a triangle, I searched hard to get it work right :)
// Compiled With MSVC++6.0// REQUIRED FOR PICKING#define EPSILON 0.000001#define CROSS(dest,v1,v2) dest.x = v1.y * v2.z -v1.z * v2.y; dest.y = v1.z * v2.x -v1.x * v2.z; dest.z = v1.x * v2.y -v1.y * v2.x;#define DOT(v1,v2) (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z)#define SUB(dest,v1,v2) dest.x=v1.x-v2.x; dest.y=v1.y-v2.y; dest.z=v1.z-v2.z; /* code rewritten to do tests on the sign of the determinant *//* the division is at the end in the code */bool intersect_triangle1(VERTEX orig,VERTEX dir,VERTEX vert0,VERTEX vert1, VERTEX vert2){ double t,u,v; VERTEX edge1, edge2, tvec, pvec, qvec; double det,inv_det; /* find vectors for two edges sharing vert0 */ SUB(edge1, vert1, vert0); SUB(edge2, vert2, vert0); /* begin calculating determinant - also used to calculate U parameter */ CROSS(pvec, dir, edge2); /* if determinant is near zero, ray lies in plane of triangle */ det = DOT(edge1, pvec); if (det > EPSILON) { /* calculate distance from vert0 to ray origin */ SUB(tvec, orig, vert0); /* calculate U parameter and test bounds */ u = DOT(tvec, pvec); if (u < 0.0 || u > det) return false; /* prepare to test V parameter */ CROSS(qvec, tvec, edge1); /* calculate V parameter and test bounds */ v = DOT(dir, qvec); if (v < 0.0 || u + v > det) return false; } else if(det < -EPSILON) { /* calculate distance from vert0 to ray origin */ SUB(tvec, orig, vert0); /* calculate U parameter and test bounds */ u = DOT(tvec, pvec); if (u > 0.0 || u < det) return false; /* prepare to test V parameter */ CROSS(qvec, tvec, edge1); /* calculate V parameter and test bounds */ v = DOT(dir, qvec) ; if (v > 0.0 || u + v < det) return false; } else return false; /* ray is parallell to the plane of the triangle */ inv_det = 1.0 / det; /* calculate t, ray intersects triangle */ t = DOT(edge2, qvec) * inv_det; (u) *= inv_det; (v) *= inv_det; return true;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement