bool HECube::collideWithRay(Ray ray, Ray* reflection, vec3& color, float& reflectivity) { double t = Test_RayCubeIntersect(vec3(ray.origin.x, ray.origin.y, ray.origin.z), vec3(ray.direction.x, ray.direction.y, ray.direction.z), modelMatrix); if(t>0) { color = vec3(0, 0, 1); return true; } return false; //change this }
Node* DisplayClass::recIntersection(glm::vec3 P, glm::vec3 V, Node* n) { *n->t = -1.0; Node* c = NULL; double res; double cT; //Node* ch = NULL; if (n->furniture != NULL) { for (int i = 0; i < n->furniture->primitives->size(); i++) { switch(*n->furniture->primitives->at(i)) { case 0: res = Test_RayCubeIntersect(P, V, *n->furniture->inverses->at(i)); if (res != -1) { if (c == NULL || res < *c->t) { c = n; *c->t = res; glm::vec4 p4(P.x, P.y, P.z, 1.0f); glm::vec4 v4(V.x, V.y, V.z, 0.0f); p4 = *n->furniture->inverses->at(i) * p4; v4 = *n->furniture->inverses->at(i) * v4; glm::vec4 inters = p4 + ((float)res) * v4; glm::vec4 myNorm; bool forward = inters.z > 0.4999 && inters.z < 0.5001; bool backward = inters.z < -0.4999 && inters.z > -0.5001; bool right = inters.x > 0.4999 && inters.x < 0.5001; bool left = inters.x < -0.4999 && inters.x > -0.5001; bool up = inters.y > 0.4999 && inters.y < 0.5001; bool down = inters.y < -0.4999 && inters.y > -0.5001; if (forward) { myNorm.x = 0.0f; myNorm.y = 0.0f; myNorm.z = 1.0f; } else if (backward) { myNorm.x = 0.0f; myNorm.y = 0.0f; myNorm.z = -1.0f; } else if (right) { myNorm.x = 1.0f; myNorm.y = 0.0f; myNorm.z = 0.0f; } else if (left) { myNorm.x = -1.0f; myNorm.y = 0.0f; myNorm.z = 0.0f; } else if (up) { myNorm.x = 0.0f; myNorm.y = 1.0f; myNorm.z = 0.0f; } else if (down) { myNorm.x = 0.0f; myNorm.y = -1.0f; myNorm.z = 0.0f; } myNorm[3] = 0.0f; myNorm = glm::normalize(glm::transpose(*n->furniture->inverses->at(i)) * myNorm); *c->currentWorldTransform = *n->furniture->worldTransforms->at(i); c->normal->x = myNorm.x; c->normal->y = myNorm.y; c->normal->z = myNorm.z; } } break; case 1: res = Test_RayCylinderIntersect(P, V, *n->furniture->inverses->at(i)); if (res != -1) { if (c == NULL || res < *c->t) { c = n; *c->t = res; glm::vec4 p4(P.x, P.y, P.z, 1.0f); glm::vec4 v4(V.x, V.y, V.z, 0.0f); p4 = *n->furniture->inverses->at(i) * p4; v4 = *n->furniture->inverses->at(i) * v4; glm::vec4 inters = p4 + ((float)res) * v4; glm::vec4 myNorm(2.0f * inters.x, 0.0f, 2.0f * inters.z, 0.0f); myNorm = glm::normalize(glm::transpose(*n->furniture->inverses->at(i)) * myNorm); *c->currentWorldTransform = *n->furniture->worldTransforms->at(i); c->normal->x = myNorm.x; c->normal->y = myNorm.y; c->normal->z = myNorm.z; } } break; case 2: res = Test_RaySphereIntersect(P, V, *n->furniture->inverses->at(i)); if (res != -1) { if (c == NULL || res < *c->t) { c = n; *c->t = res; glm::vec4 p4(P.x, P.y, P.z, 1.0f); glm::vec4 v4(V.x, V.y, V.z, 0.0f); p4 = *n->furniture->inverses->at(i) * p4; v4 = *n->furniture->inverses->at(i) * v4; glm::vec4 inters = p4 + ((float)res) * v4; glm::vec4 myNorm(inters.x * 2.0f, inters.y * 2.0f, inters.z * 2.0f, 0.0f); myNorm = glm::normalize(glm::transpose(*n->furniture->inverses->at(i)) * myNorm); *c->currentWorldTransform = *n->furniture->worldTransforms->at(i); c->normal->x = myNorm.x; c->normal->y = myNorm.y; c->normal->z = myNorm.z; } } /*else { glm::vec3 x; }*/ break; } } //do intersection tests, get intersection with minimum t-value } else if (n->shape != NULL) { if (*n->shape->kind < 97 || Test_RayCubeIntersect(P, V, *n->boundTrans) != -1) { glm::vec3 p1; glm::vec3 p2; glm::vec3 p3; glm::vec4 norm(0.0f, 0.0f, 0.0f, 0.0f); double minT = -1; for (int q = 0; q < *n->shape->numVbo; q+=12) { p1.x = n->shape->vbo[q]; p1.y = n->shape->vbo[q + 1]; p1.z = n->shape->vbo[q + 2]; p2.x = n->shape->vbo[q + 4]; p2.y = n->shape->vbo[q + 5]; p2.z = n->shape->vbo[q + 6]; p3.x = n->shape->vbo[q + 8]; p3.y = n->shape->vbo[q + 9]; p3.z = n->shape->vbo[q + 10]; res = Test_RayPolyIntersect(P, V, p1, p2, p3, *n->inv); if (res != -1 && (minT > res || minT == -1)) { minT = res; norm.x = n->shape->nbo[q]; norm.y = n->shape->nbo[q + 1]; norm.z = n->shape->nbo[q + 2]; } } if (minT != -1) { if (c == NULL || *c->t == -1 || res < *c->t) { c = n; *c->t = minT; norm = glm::normalize(glm::transpose(*n->inv) * norm); *c->currentWorldTransform = *n->worldTransform; c->normal->x = norm.x; c->normal->y = norm.y; c->normal->z = norm.z; } } } //loop through triangles and find intersection with minimum t-value } Node* ch = NULL; for (int i = 0; i < n->children->size(); i++) { ch = recIntersection(P, V, n->children->at(i)); if (ch != NULL) { if (c == NULL || *c->t == -1 || *ch->t < *c->t) { c = ch; } } } return c; }