예제 #1
0
        public static Matrix4By4 Transpose(Matrix4By4 matrix)
        {
            Vector firstRow  = new Vector(matrix.m.v1.x, matrix.m.v2.x, matrix.m.v3.x);
            Vector secondRow = new Vector(matrix.m.v1.y, matrix.m.v2.y, matrix.m.v3.y);
            Vector thirdRow  = new Vector(matrix.m.v1.z, matrix.m.v2.z, matrix.m.v3.z);

            return(new Matrix4By4(new Matrix(firstRow, secondRow, thirdRow), new Vector(0, 0, 0)));
        }
예제 #2
0
        public static Matrix4By4 ZRotation(double rotationAngleThroughZAxis)
        {
            Matrix4By4 rm =
                new Matrix4By4(new Matrix(
                                   new Vector(Math.Cos(Algebra.convertToRad(rotationAngleThroughZAxis)), Math.Sin(Algebra.convertToRad(rotationAngleThroughZAxis)), 0),
                                   new Vector((-1) * Math.Sin(Algebra.convertToRad(rotationAngleThroughZAxis)), Math.Cos(Algebra.convertToRad(rotationAngleThroughZAxis)), 0),
                                   new Vector(0, 0, 1)),
                               new Vector());

            return(rm);
        }
예제 #3
0
 public void AddRotationThroughAllAxisToTransformationMatrix(double rotationAngleThroughXAxis, double rotationAngleThroughYAxis, double rotaionAngleThroughZAxis)
 {
     transformMatrix         = Matrix4By4.ZRotation(rotaionAngleThroughZAxis) * Matrix4By4.YRotation(rotationAngleThroughYAxis) * Matrix4By4.XRotation(rotationAngleThroughXAxis) * transformMatrix;
     inverseTransformMatrix *= Matrix4By4.XRotation(-rotationAngleThroughXAxis) * Matrix4By4.YRotation(-rotationAngleThroughYAxis) * Matrix4By4.ZRotation(-rotaionAngleThroughZAxis);
 }
예제 #4
0
 public void AddScaleToTransformationMatrix(Vector scale)
 {
     transformMatrix         = new Matrix4By4(new Matrix(new Vector(scale.x, 0, 0), new Vector(0, scale.y, 0), new Vector(0, 0, scale.z)), new Vector());
     inverseTransformMatrix *= new Matrix4By4(new Matrix(new Vector((1 / scale.x), 0, 0), new Vector(0, (1 / scale.y), 0), new Vector(0, 0, (1 / scale.z))), new Vector());
 }
예제 #5
0
        /*
         * // to find the reflected ray after reflection
         * public virtual Vector ReflectedRay(Vector direction, Vector normal) {
         *  Vector reflectionRay = direction - 2 * (normal * direction) * normal;
         *  return reflectionRay;
         * }*/

        public void AddTranslationToTranformationMatrix(Vector translate)
        {
            transformMatrix         = new Matrix4By4(new Matrix(), translate) * transformMatrix;
            inverseTransformMatrix *= new Matrix4By4(new Matrix(), (-1) * translate);
        }
예제 #6
0
        SColor TraceRay(Ray ray, Scene scene, int rayDepth)
        {
            int maximumNumberOfReflections = 10;

            if (rayDepth >= maximumNumberOfReflections)
            {
                return(new SColor(0, 0, 0, 0));
            }

            SColor shapeColor = new SColor();

            SColor reflectionColor = new SColor();

            SColor averageReflectionColor = new SColor();

            Shape closestShape = scene.shapes[0]; // rendering multiple objects so searching for the closest shape to render

            double closestT = double.MaxValue;    // shape which has closest T need to be rendered at first



            foreach (Shape shape in scene.shapes)
            {
                //transforming the ray origin along with the given tranformation matrices
                // origin is transformed like the point tranformation whereas ray direction is transformed like the vector transformation

                Vector newOrigin    = FindNewPoint(shape.inverseTransformMatrix, ray.origin);
                Vector newDirection = (shape.inverseTransformMatrix * ray.direction).Normalize();

                double t = shape.DoesIntersect(newOrigin, newDirection); // passing transformed origin and transformed direction
                if ((t < closestT) && (t >= 0.0001))
                {
                    closestT     = t;
                    closestShape = shape;
                }
            }

            Vector closeOrigin    = FindNewPoint(closestShape.inverseTransformMatrix, ray.origin);
            Vector closeDirection = (closestShape.inverseTransformMatrix * ray.direction).Normalize();


            if (closestShape.DoesIntersect(closeOrigin, closeDirection) >= 0) // checking whether the ray hits the sphere or not
            {
                Vector point = closeOrigin + (closeDirection * closestT);     // point of intersection

                SColor color               = closestShape.material.ColorAtPoint(point);
                double ambient             = closestShape.material.ambient;
                double specularCoefficient = closestShape.material.specularCoefficient;

                SColor lightContribution = new SColor();


                //transforming the point of intersection according to the transformation matrices
                point = FindNewPoint(closestShape.transformMatrix, point);

                Vector normal = closestShape.NormalAtPoint(point);

                // also transforming the normal of the shapes according to the transformation matrix
                normal = Matrix4By4.Transpose(closestShape.inverseTransformMatrix) * normal;

                // for multiple lighting
                foreach (Light light in scene.lights)
                {
                    // for also checking shadows
                    Vector shadowRayDirection = (light.location - point).Normalize(); // calculating the direction of the ray of the shadow

                    // at first assigning inShadow variable boolean value false so that we can check whether any shape is in shadow or not
                    bool inShadow = false;

                    foreach (Shape shape in scene.shapes)
                    {
                        //checking whether each shape is in shadow or not
                        // also checking whether the distance from the point of intersection and the direction of the ray is greater than the ray intersection distance

                        //transforming the ray origin along with the given tranformation matrices
                        Vector transformedShadowRayDirection = (shape.inverseTransformMatrix * shadowRayDirection).Normalize();
                        if (shape.DoesIntersect(point + (normal * 0.5), transformedShadowRayDirection) >= 0 && Math.Abs((light.location - point).Magnitude()) > shape.DoesIntersect(position, closeDirection))
                        {
                            inShadow = true;
                            break; // giving break point if it's in in shadow
                        }
                    }
                    if (inShadow)
                    {
                        continue; // if it's in shadow than continuing that is casting a shadow
                    }

                    Vector lightToPoint   = point - light.location;     // position vector from light to point of intersection
                    Vector lightDriection = lightToPoint.Normalize();   // and then normalizing it to get the direction of that vector
                    double cosineAngle    = -(lightDriection * normal); // finding the scalar value which gives the light intensity


                    if (cosineAngle < 0)
                    {
                        cosineAngle = 0;
                    }

                    // for specular highlight
                    Vector cameraToIntersectionPoint = (position - point).Normalize();
                    Vector reflectedRayDirection     = (closestShape.material.ReflectedRay(ray.origin, ray.direction, closestShape).direction).Normalize();



                    //averageReflectionColor = reflectionColor * (1 / rayDepth);
                    //color = averageReflectionColor + color;


                    double specularFactor = Math.Pow((cameraToIntersectionPoint * reflectedRayDirection), closestShape.material.smoothness);
                    if (specularFactor < 0)
                    {
                        specularFactor = 0;
                    }


                    if (cosineAngle >= 0 || specularFactor >= 0)
                    {
                        lightContribution = lightContribution + (light.lightColor * color * (cosineAngle * closestShape.material.diffuseCoefficient + specularFactor * closestShape.material.specularCoefficient) * light.Intensity);
                    }
                }

                /*
                 * if (rayDepth < maximumNumberOfReflections)
                 * {
                 *  Ray reflectedRay = closestShape.material.ReflectedRay(closeOrigin, closeDirection, closestShape);
                 *  reflectionColor = TraceRay(reflectedRay, scene, rayDepth + 1); // recursive method
                 * }
                 */
                shapeColor = lightContribution + (color * ambient);
            }


            return(shapeColor + closestShape.material.reflectionCoefficient * reflectionColor);
        }
예제 #7
0
        //creating the method for multiplying matrix 4by4 and point of intersection
        Vector FindNewPoint(Matrix4By4 matrix, Vector point)
        {
            Vector vector1 = matrix * point;

            return(vector1 + matrix.v1);
        }