/// <summary> /// Computes the complete intersection of the given ray with the object. /// </summary> /// <param name="p0">Ray origin.</param> /// <param name="p1">Ray direction vector.</param> /// <returns>Sorted list of intersection records.</returns> public override LinkedList <Intersection> Intersect(Vector3d p0, Vector3d p1) { // !!!{{ TODO: add your accelerated intersection code here if (mesh == null || mesh.Triangles < 1) { return(null); } List <Intersection> result = null; Intersection i; for (int id = 0; id < mesh.Triangles; id++) { Vector3 a, b, c; mesh.GetTriangleVertices(id, out a, out b, out c); Vector2d uv; double t = Geometry.RayTriangleIntersection(ref p0, ref p1, ref a, ref b, ref c, out uv); if (Double.IsInfinity(t)) { continue; } if (result == null) { result = new List <Intersection>(); } // Compile the 1st Intersection instance: i = new Intersection(this); i.T = t; i.Enter = i.Front = true; i.CoordLocal = p0 + i.T * p1; // Tmp data object TmpData tmp = new TmpData(); tmp.face = id; tmp.uv = uv; Vector3 ba = b - a; // temporary value for flat shading Vector3 ca = c - a; Vector3.Cross(ref ba, ref ca, out tmp.normal); i.SolidData = tmp; result.Add(i); if (!ShellMode) { continue; } // Compile the 2nd Intersection instance: i = new Intersection(this); i.T = t + 1.0e-4; i.Enter = i.Front = false; i.CoordLocal = p0 + i.T * p1; // Tmp data object TmpData tmp2 = new TmpData(); tmp2.face = id; tmp2.uv = uv; tmp2.normal = -tmp.normal; i.SolidData = tmp2; result.Add(i); } if (result == null) { return(null); } // Finalizing the result: sort the result list result.Sort(); return(new LinkedList <Intersection>(result)); // !!!}} }
private void RenderScene(SceneBrep scene) { if (scene != null && scene.Triangles > 0) { if (scene.HasNormals() && scene.Normals > 0) { GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < scene.Triangles; ++i) { int v1, v2, v3; scene.GetTriangleVertices(i, out v1, out v2, out v3); GL.Normal3(scene.GetNormal(v1)); GL.Vertex3(scene.GetVertex(v1)); GL.Normal3(scene.GetNormal(v2)); GL.Vertex3(scene.GetVertex(v2)); GL.Normal3(scene.GetNormal(v3)); GL.Vertex3(scene.GetVertex(v3)); } GL.End(); } else { GL.End(); GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < scene.Triangles; ++i) { Vector3 v1, v2, v3; scene.GetTriangleVertices(i, out v1, out v2, out v3); GL.Vertex3(v1); GL.Vertex3(v2); GL.Vertex3(v3); } GL.End(); } } else // color cube (JB) { GL.Begin(PrimitiveType.Quads); GL.Color3(0.0f, 1.0f, 0.0f); // Set The Color To Green GL.Vertex3(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top) GL.Vertex3(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) GL.Vertex3(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) GL.Vertex3(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) GL.Color3(1.0f, 0.5f, 0.0f); // Set The Color To Orange GL.Vertex3(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom) GL.Vertex3(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom) GL.Vertex3(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom) GL.Vertex3(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom) GL.Color3(1.0f, 0.0f, 0.0f); // Set The Color To Red GL.Vertex3(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) GL.Vertex3(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) GL.Vertex3(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front) GL.Vertex3(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front) GL.Color3(1.0f, 1.0f, 0.0f); // Set The Color To Yellow GL.Vertex3(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back) GL.Vertex3(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back) GL.Vertex3(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back) GL.Vertex3(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back) GL.Color3(0.0f, 0.0f, 1.0f); // Set The Color To Blue GL.Vertex3(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) GL.Vertex3(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left) GL.Vertex3(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left) GL.Vertex3(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left) GL.Color3(1.0f, 0.0f, 1.0f); // Set The Color To Violet GL.Vertex3(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right) GL.Vertex3(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) GL.Vertex3(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right) GL.Vertex3(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right) GL.End(); } }