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
|
// Compute O and D in object coordinates
glm::vec4 origin = mvp_inverse * glm::vec4(
(x-half_width)/half_width, (half_height-y)/half_height, -1, 1);
glm::vec4 dir = mvp_inverse * glm::vec4(0, 0, 1, 0);
O = glm::vec3(origin.x, origin.y, origin.z);
D = glm::normalize(glm::vec3(dir.x, dir.y, dir.z));
// Iterate through the figures in the model
tmin = 1000.0;
for(i=0; i<num_objects; i++) {
data_array = (float*)geom_vec[i].map["POSITION"].data;
// Iterate through the triangles in the figure
for(j=0; j<geom_vec[i].index_count; j+=3) {
index = geom_vec[i].indices[j]*3;
// Read the first point of Triangle KLM
K = glm::vec3(data_array[index],
data_array[index+1],
data_array[index+2]);
// Read the second point of Triangle KLM
index = geom_vec[i].indices[j+1]*3;
L = glm::vec3(data_array[index],
data_array[index+1],
data_array[index+2]);
// Read the third point of Triangle KLM
index = geom_vec[i].indices[j+2]*3;
M = glm::vec3(data_array[index],
data_array[index+1],
data_array[index+2]);
// Compute vectors E, F, and G
E = K - M;
F = L - M;
G = O - M;
// Compute the vector containing t and the coordinates k and l
tkl = 1/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[i] && tkl.y > 0.0f && tkl.z > 0.0f && (tkl.y + tkl.z) < 1) {
tmin = tkl.x;
sphere = i;
}
}
} |
Partager