ray3 CameraOrthographic::getRay( const vec2& uv ) const { // Your code goes here. // Create a ray3( ray origin point, ray direction ). return ray3( vec3( 0,0,0 ), vec3( 0,0,1 ) ); }
ray3 CameraPerspective::getRay( const vec2& uv ) const { // Your code goes here. // Create a ray3( ray origin point, ray direction ). // Since you will be testing for intersections along the ray, // you may want to start the ray on the film plane so that objects // between the eye() and the film plane aren't drawn. return ray3( vec3( 0,0,0 ), vec3( 0,0,1 ) ); }
TEST(RayTest, equality) { Geom::Point3D origin(0, 0, 0); Geom::Vector3D direction(1, 1, 1); Geom::Ray ray1(origin, direction); Geom::Ray ray2(origin, direction); EXPECT_EQ(ray1, ray2); Geom::Ray ray3(origin, Geom::Vector3D(2, 3, 2)); EXPECT_NE(ray1, ray3); }
void tst_QRay3D::compare() { Qt3DRender::RayCasting::QRay3D ray1(QVector3D(10, 20, 30), QVector3D(-3, -4, -5)); Qt3DRender::RayCasting::QRay3D ray2(QVector3D(10, 20, 30), QVector3D(1.5f, 2.0f, 2.5f)); Qt3DRender::RayCasting::QRay3D ray3(QVector3D(0, 20, 30), QVector3D(-3, -4, -5)); QVERIFY(ray1 == ray1); QVERIFY(!(ray1 != ray1)); QVERIFY(qFuzzyCompare(ray1, ray1)); QVERIFY(ray1 != ray2); QVERIFY(!(ray1 == ray2)); QVERIFY(!qFuzzyCompare(ray1, ray2)); QVERIFY(ray1 != ray3); QVERIFY(!(ray1 == ray3)); QVERIFY(!qFuzzyCompare(ray1, ray3)); }
int main(int argc, char *argv[]) { Vector orig(0,0,0); double radius = 1; Ray ray1(Vector(-2, 1, 0), Vector(1,0,0)); Ray ray2(Vector(-2, 1, 0), Vector(-1,0,0)); Ray ray3(Vector(0, 0.5f, 0), Vector(1,0,0)); Sphere sphere(orig, radius); ASSERT(sphere.intersect(ray1), true, "No intersection"); ASSERT(sphere.intersect(ray2), false, "Error in intersection"); ASSERT(sphere.intersect(ray3), false, "Origin is inside the Sphere"); REPORT(); return ERROR_COUNT; }
int CDistanceFuncGridModel<T>::BruteForceInnerPointsStatic(const C3DModel &model, const Vector3<T> &vQuery) { //In this variable we count the number on intersections int nIntersections = 0; for(unsigned int i=0;i<model.m_vMeshes.size();i++) { const C3DMesh& mesh=model.m_vMeshes[i]; Ray3<T> ray3(vQuery,VECTOR3(0.9,0.8,0.02) ); CDynamicArray<TriFace>::const_iterator faceIter; //Get the bounding box of the 3d model const AABB3<T> &rBox = mesh.GetBox(); //Get the mesh if(!rBox.isPointInside(vQuery)) continue; //reset the number of intersection to zero for the current subobject nIntersections=0; for(faceIter=mesh.m_pFaces.begin();faceIter!=mesh.m_pFaces.end();faceIter++) { TriFace tri=*faceIter; //We loop through all triangular faces of the // model. This variable will hold the current face Triangle3<T> tri3(mesh.GetVertices()[tri[0]],mesh.GetVertices()[tri[1]],mesh.GetVertices()[tri[2]]); //init our intersector CIntersectorRay3Tri3<T> intersector(ray3, tri3); //test for intersection if(intersector.Intersection()) nIntersections++; }//end for faces //we finished looping through the faces of the subobject //look if the point is inside the subobject //if the number of intersection is even //we return false else true if(!(nIntersections % 2 == 0)) return 1; }//end for meshes //The query point is not inside of any of the //submeshes return 0; }//end BruteForceInnerPoints
TEST(TestRaycast, TestPlaneIntersection) { FTRaycast ray(glm::vec3(0, 2, 0), glm::vec3(1, 3, 1)); // Test Standard glm::vec3 intersection; EXPECT_TRUE(ray.intersectsPlane(glm::vec3(2,1,-4), glm::vec3(3,2,1), intersection)); EXPECT_EQ(intersection, glm::vec3(2, 8, 2)); // Test parrallel no intersection intersection = glm::vec3(23, 5, 78); FTRaycast ray2(glm::vec3(1, 4, 0), glm::vec3(1, 2, 1)); EXPECT_FALSE(ray2.intersectsPlane(glm::vec3(2, 1, -4), glm::vec3(3, 2, 1), intersection)); EXPECT_EQ(intersection, glm::vec3(23, 5, 78)); // Test parallel with intersection FTRaycast ray3(glm::vec3(2, 8, 2), glm::vec3(1, 2, 1)); EXPECT_TRUE(ray3.intersectsPlane(glm::vec3(2, 1, -4), glm::vec3(3, 2, 1), intersection)); EXPECT_EQ(intersection, glm::vec3(3,2,1)); }
Vec3f RayTracer::traceRay(Ray &ray, float tmin, int bounces, float weight, float indexOfRefraction, Hit &hit) const { //printf("当前已有光线:\n"); //RayTree::Print(); Vec3f canswer; if (bounces > max_bounces) return Vec3f(0.0f, 0.0f, 0.0f); Camera *camera = sceneParser->getCamera(); Group *group = sceneParser->getGroup(); int num_lights = sceneParser->getNumLights(); Vec3f cambient = sceneParser->getAmbientLight(); //原来最后是这里出了问题,一旦碰到有转换的物体,那么hit带出来的值是 //转换后的视线看到的值,而非本来视线看到的值 //所以解决方案是:距离不变,根据距离重新计算焦点 if (group->intersect(ray, hit, tmin))//撞到了 { if (is_view_ray) { RayTree::SetMainSegment(ray, 0, hit.getT()); is_view_ray = false; } Vec3f cobject = hit.getMaterial()->getDiffuseColor(); Vec3f hitPoint = hit.getIntersectionPoint(); //环境光部分 canswer = cambient * cobject; Vec3f clight;//光的颜色 Vec3f light_dir;//指向光的方向 Vec3f normal_dir = hit.getNormal();//交点法线向量 float distolight;//距离光源的距离 for (int i = 0; i < num_lights; i++) { Light *light = sceneParser->getLight(i); //light_dir : the direction to the light // 该方法用于获得指向光的方向,光的颜色,和到达光的距离 // 第一个参数传递的是焦点信息 light->getIllumination(hitPoint, light_dir, clight, distolight); Ray ray2(hitPoint, light_dir); Vec3f init_normal(0, 0, 0); Hit hit2(distolight, NULL, init_normal); //阴影检测 if (shadow) { if (group->intersect(ray2, hit2, tmin)){ RayTree::AddShadowSegment(ray2, 0, hit2.getT()); continue; } RayTree::AddShadowSegment(ray2, 0, hit2.getT()); } //cpixel = cambient * cobject + SUMi [ clamped(Li . N) * clighti * cobject ] //返回局部光 canswer = canswer + hit.getMaterial()->Shade(ray, hit, light_dir, clight); } //printf("当前已有光线:\n"); //RayTree::Print(); //反射光 Material *material = hit.getMaterial(); Vec3f rc = material->getReflectiveColor(); if (rc.r() > 0 && rc.g() > 0 && rc.b() > 0) { Vec3f mirrorDir; Vec3f incoming = ray.getDirection(); mirrorDir = mirrorDirection(normal_dir, incoming); // The ray weight is simply multiplied by the magnitude of the reflected color Ray ray3(hitPoint, mirrorDir); Vec3f init_normal(0, 0, 0); Hit hit3(distolight, NULL, init_normal); //忘记乘以本身的反射光系数%………… canswer += traceRay(ray3, tmin, bounces + 1, weight*rc.Length(), indexOfRefraction, hit3)*rc; if (bounces + 1 < max_bounces) RayTree::AddReflectedSegment(ray3, 0, hit3.getT()); } //printf("当前已有光线:\n"); //RayTree::Print(); //从这里开始还都存在问题!!!!! //折射光 Vec3f transmitted; Vec3f tc = material->getTransparentColor(); float index = material->getIndexOfRefraction(); if (tc.r() > 0 && tc.g() > 0 && tc.b() > 0) { Vec3f init_normal(0, 0, 0); Hit hit4(distolight, NULL, init_normal); //在判断折射光的存在之后,要考虑光线的位置:物体内还是物体外 //这里根据normal和incoming的点积来判断 Vec3f incoming = ray.getDirection(); float judge = normal_dir.Dot3(incoming); if (judge < 0)//光线在外 { if (transmittedDirection(normal_dir, incoming, 1, index, transmitted)) { Ray ray4(hitPoint, transmitted); canswer += traceRay(ray4, tmin, bounces+1, weight*rc.Length(), index, hit4)*tc; RayTree::AddTransmittedSegment(ray4, 0, hit4.getT()); } } else//光线在内 { normal_dir.Negate(); if (transmittedDirection(normal_dir, incoming, index, 1, transmitted)) { Ray ray4(hitPoint, transmitted); canswer += traceRay(ray4, tmin, bounces+1, weight*rc.Length(), 1, hit4)*tc; RayTree::AddTransmittedSegment(ray4, 0, hit4.getT()); } } } //printf("当前已有光线:\n"); //RayTree::Print(); } else canswer = sceneParser->getBackgroundColor(); canswer.Clamp(); return canswer; }