glm::f32 tmin = 1000.0f;
for (int i = 0; i < frame.objects.size(); i++)
{
VObject *object = frame.objects[i];
glm::mat4 model = object->model;
model *= object->scale;
glm::mat4 mpvInverse = glm::inverse(model * projection * this->camera->GetViewMatrix());
glm::vec4 origin = mpvInverse * glm::vec4(
(x - glm::f32(screenWidth / 2.0f)) / glm::f32(screenWidth / 2.0f), (glm::f32(screenHeight / 2.0f) - y) / glm::f32(screenHeight / 2.0f), -1.0f, 1.0f);
glm::vec4 dir = mpvInverse * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
glm::vec3 O = glm::vec3(origin.x, origin.y, origin.z);
glm::vec3 D = glm::normalize(glm::vec3(dir.x, dir.y, dir.z));
for (int j = 0; j<object->numVertices; j += 3)
{
glm::vec3 K = glm::vec3(object->vertices[j]);
glm::vec3 L = glm::vec3(object->vertices[j + 1]);
glm::vec3 M = glm::vec3(object->vertices[j + 2]);
// Compute vectors E, F, and G
glm::vec3 E = K - M;
glm::vec3 F = L - M;
glm::vec3 G = O - M;
// Compute the vector containing t and the coordinates k and l
glm::vec3 tkl = 1.0f / glm::dot(glm::cross(D, F), E) *
glm::vec3(glm::dot(glm::cross(G, E), F),
glm::dot(glm::cross(D, F), G),
glm::dot(glm::cross(G, E), D));
// Check if t and the intersection point (k, l) are acceptable
if (tkl.x < tmin && tkl.y > 0.0f && tkl.z > 0.0f && (tkl.y + tkl.z) < 1.0f)
{
tmin = tkl.x;
}
}
}
if (tmin < 1000.f)
{
MessageBox(NULL, "hit", "", 0);
return;
}