public static bool ModelRayIntersection(WW3DModel model, Point3D rayOrig, Vector3D rayDir, out Point3D hitPos, out Vector3D hitSurfaceNormal, out double rayLength)
        {
            rayLength = double.MaxValue;
            hitPos = new Point3D();
            hitSurfaceNormal = new Vector3D();

            var points = model.TriangleList();
            var indices = model.IndexList();

            for (int i = 0; i < indices.Length/3; ++i) {
                Point3D pos;
                Vector3D surfaceNormal;
                double distance;
                if (!TriangleRayIntersect(points[indices[i * 3 + 0]], points[indices[i * 3 + 1]], points[indices[i * 3 + 2]], rayOrig, rayDir, out pos, out surfaceNormal, out distance)) {
                    continue;
                }
                if (distance < rayLength) {
                    hitPos = pos;
                    hitSurfaceNormal = surfaceNormal;
                    rayLength = distance;
                }
            }

            return rayLength != double.MaxValue;
        }
Example #2
0
        public static bool ModelRayIntersection(WW3DModel model, Point3D rayOrig, Vector3D rayDir, out Point3D hitPos, out Vector3D hitSurfaceNormal, out double rayLength)
        {
            rayLength        = double.MaxValue;
            hitPos           = new Point3D();
            hitSurfaceNormal = new Vector3D();

            var points  = model.TriangleList();
            var indices = model.IndexList();

            for (int i = 0; i < indices.Length / 3; ++i)
            {
                Point3D  pos;
                Vector3D surfaceNormal;
                double   distance;
                if (!TriangleRayIntersect(points[indices[i * 3 + 0]], points[indices[i * 3 + 1]], points[indices[i * 3 + 2]], rayOrig, rayDir, out pos, out surfaceNormal, out distance))
                {
                    continue;
                }
                if (distance < rayLength)
                {
                    hitPos           = pos;
                    hitSurfaceNormal = surfaceNormal;
                    rayLength        = distance;
                }
            }

            return(rayLength != double.MaxValue);
        }
Example #3
0
        private void DrawModel(WW3DModel model, Matrix3D modelWorldMatrix, Brush brush)
        {
            var modelProjectionMatrix = modelWorldMatrix * mWorldProjectionMatrix;

            var pointArray = model.TriangleList();
            var indexArray = model.IndexList();

            for (int i = 0; i < indexArray.Length / 3; ++i)
            {
                {
                    Point3D p0 = Point3D.Multiply(pointArray[indexArray[i * 3 + 0]], modelProjectionMatrix);
                    Point3D p1 = Point3D.Multiply(pointArray[indexArray[i * 3 + 1]], modelProjectionMatrix);
                    Point3D p2 = Point3D.Multiply(pointArray[indexArray[i * 3 + 2]], modelProjectionMatrix);
                    AddNewLine(p0, p1, brush);
                    AddNewLine(p1, p2, brush);
                    AddNewLine(p2, p0, brush);
                }
#if false
                // 法線のデバッグ表示
                Point3D po0   = pointArray[indexArray[i * 3 + 0]];
                Point3D po1   = pointArray[indexArray[i * 3 + 1]];
                Point3D po2   = pointArray[indexArray[i * 3 + 2]];
                var     edge0 = po1 - po0;
                var     edge1 = po2 - po1;
                var     n     = Vector3D.CrossProduct(edge0, edge1);
                n.Normalize();
                var     center = new Point3D((po0.X + po1.X + po2.X) / 3, (po0.Y + po1.Y + po2.Y) / 3, (po0.Z + po1.Z + po2.Z) / 3);
                Point3D pN0    = Point3D.Multiply(center, modelProjectionMatrix);
                Point3D pN1    = Point3D.Multiply(center + n * 0.1, modelProjectionMatrix);
                AddNewLine(pN0, pN1, brush);

                // 面の番号表示
                TextBlock tb = new TextBlock();
                tb.Text       = i.ToString();
                tb.Foreground = brush;
                mCanvas.Children.Add(tb);
                var textPos = ScaleToCanvas(pN0);
                Canvas.SetLeft(tb, textPos.X);
                Canvas.SetTop(tb, textPos.Y);
#endif
            }
        }
Example #4
0
        private void DrawModel(WW3DModel model, Matrix3D modelWorldMatrix, Brush brush)
        {
            var modelProjectionMatrix = modelWorldMatrix * mWorldProjectionMatrix;

            var pointArray = model.TriangleList();
            var indexArray = model.IndexList();
            for (int i = 0; i < indexArray.Length / 3; ++i) {
                {
                    Point3D p0 = Point3D.Multiply(pointArray[indexArray[i * 3 + 0]], modelProjectionMatrix);
                    Point3D p1 = Point3D.Multiply(pointArray[indexArray[i * 3 + 1]], modelProjectionMatrix);
                    Point3D p2 = Point3D.Multiply(pointArray[indexArray[i * 3 + 2]], modelProjectionMatrix);
                    AddNewLine(p0, p1, brush);
                    AddNewLine(p1, p2, brush);
                    AddNewLine(p2, p0, brush);
                }
            #if false
                // 法線のデバッグ表示
                Point3D po0 = pointArray[indexArray[i * 3 + 0]];
                Point3D po1 = pointArray[indexArray[i * 3 + 1]];
                Point3D po2 = pointArray[indexArray[i * 3 + 2]];
                var edge0 = po1 - po0;
                var edge1 = po2 - po1;
                var n = Vector3D.CrossProduct(edge0, edge1);
                n.Normalize();
                var center = new Point3D((po0.X + po1.X + po2.X)/3, (po0.Y + po1.Y + po2.Y)/3, (po0.Z + po1.Z + po2.Z)/3);
                Point3D pN0 = Point3D.Multiply(center, modelProjectionMatrix);
                Point3D pN1 = Point3D.Multiply(center+n*0.1, modelProjectionMatrix);
                AddNewLine(pN0, pN1, brush);

                // 面の番号表示
                TextBlock tb = new TextBlock();
                tb.Text = i.ToString();
                tb.Foreground = brush;
                mCanvas.Children.Add(tb);
                var textPos = ScaleToCanvas(pN0);
                Canvas.SetLeft(tb, textPos.X);
                Canvas.SetTop(tb, textPos.Y);
            #endif
            }
        }