1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
bool gcapture(POpengl opengl,bool click, int mx, int my)
{
bool bret = false;
PTVERTEX ret = NULL;
RECT rc;
GetClientRect(opengl->hWnd, &rc);
int half_width = rc.right / 2;
int half_height = rc.bottom / 2;
int x = mx;
int y = my;
float tmin = 10000.0;
// Compute O and D in object coordinates
HANDLE hiterator_1 = opengl->hrefcapture;
while (hiterator_1 != NULL)
{
PHANDLE_PLAY pp = (PHANDLE_PLAY)Stack_Next(hiterator_1, true); //Stack_Next Fonctionne mais fonction privé
if (pp->capture == false) //il est égal à true
continue;
glm::mat4 mvp_inverse;
mvp_inverse = glm::inverse(pp->lastmat); //lastmat= matrice général
/*glm::vec4 origin = mvp_inverse * glm::vec4(
(x - half_width) / half_width, (half_height - y) / half_height, -1, 1);*/
glm::vec4 origin = glm::vec4(1,camera.cameraPos);
glm::vec4 dir = mvp_inverse * glm::vec4(0, 0, 1, 0);
glm::vec3 O = glm::vec3(origin.x, origin.y, origin.z);
glm::vec3 D = glm::normalize(glm::vec3(dir.x, dir.y, dir.z));
Ray ray;
ray.Origin = origin;
ray.Direction = D;
HANDLE hiterator_2 = pp->hlist;
while (hiterator_2 != NULL)
{
PINTERNALVTX pp = (PINTERNALVTX)Stack_Next(hiterator_2, true); //Stack_Next Fonctionne
// Read the first point of Triangle KLM
glm::vec3 K = glm::vec3(pp->vv.x[0], pp->vv.y[0], pp->vv.z[0]);;
// Read the second point of Triangle KLM
glm::vec3 L = glm::vec3(pp->vv.x[1], pp->vv.y[1], pp->vv.z[1]);;
// Read the third point of Triangle KLM
glm::vec3 M = glm::vec3(pp->vv.x[2], pp->vv.y[2], pp->vv.z[2]);;
// Compute vectors E, F, and G
glm::vec3 centroid = K;
//#error toremove
//if (IntersectTriangle(ray, K, L, M))
/*glm::vec3 N = glm::cross(L - K, M - K);
float k0 = glm::dot(N, K);
float ks = glm::dot(N, L) - k0;
float kt = glm::dot(N, M) - k0;
glm::vec3 result = (kt * S - ks * T) / (kt - ks);*/
glm::vec3 intersect;
bool result = glm::intersectRayTriangle(ray.Origin, ray.Direction, K, L, M, intersect);
if (result)
{
float dd = sqrt(sqr(origin.x - intersect.x) + sqr(origin.y - intersect.y) + sqr(origin.z - intersect.z));
if (dd < tmin)
{
tmin = dd;
ret = &pp->vv;
}
}
}
}
if (opengl->drhint != NULL)
{
bret=opengl->drhint(opengl->param,click, ret)==1;
}
return bret;
} |
Partager