Exemplo n.º 1
0
        public Utilities.Color CastRay1(Ray cameraRay, Scene currentScene, ref int depth)
        {
            Utilities.Color newColor = new Utilities.Color();

            if (depth > rs.MaxDepth)
            {
                return(Utilities.Color.Background);
            }

            RayHit hit = Trace(currentScene, cameraRay);

            if (hit.isHit)
            {
                Ray shadowRay = new Utilities.Ray();
                shadowRay.Origin    = hit.hitPoint + hit.normal;
                shadowRay.Direction = Vector3.Normalize(currentScene.Lights[0].Position - shadowRay.Origin);

                RayHit shadowHit = Trace(currentScene, shadowRay);
                if (!shadowHit.isHit)
                {
                    newColor = Utilities.Color.Set(1, 0, 0);
                }
            }

            return(newColor);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Renders the current scene to referenced bitmap image in PictureBox
        /// </summary>
        public void RenderScene()
        {
            Camera SceneCamera = Camera.Create(new Vector3(3, 3, 3), new Vector3(0, 1, 0));     // TO DO: Get this from scene file

            Utilities.Color pixelColor = new Utilities.Color();                                 // Used to set pixel color in rendered image
            Ray             newRay     = new Ray();

            Stopwatch RenderTimer = new Stopwatch();

            RenderTimer.Start();

            // Set image scale and aspect ratio
            double scale            = Math.Tan(MathExtensions.DegreesToRadians(rs.FieldOfView * 0.5));
            double imageAspectRatio = rs.ImageWidth / rs.ImageHeight;

            // Create camera rotation matrix and set ray origin
            newRay.Origin = SceneCamera.position;
            Matrix4x4 CameraToWorld = Matrix4x4.LookAt(SceneCamera.position, SceneCamera.target);

            // For each pixel in the image, cast a ray from camera into world space
            for (int y = 0; y < rs.ImageHeight; y++)
            {
                for (int x = 0; x < rs.ImageWidth; x++)
                {
                    // Generate ray direction
                    double px    = (2 * (x + 0.5) / (double)rs.ImageWidth - 1) * imageAspectRatio * scale;
                    double py    = (1 - 2 * (y + 0.5) / (double)rs.ImageHeight) * scale;
                    int    depth = rs.MaxDepth;
                    newRay.Direction = Matrix4x4.MultiplyVector(new Vector3(px, py, -1), CameraToWorld);
                    newRay.Direction = Vector3.Normalize(newRay.Direction);
                    newRay.Distance  = double.PositiveInfinity;
                    pixelColor       = CastRay(newRay, CurrentScene, ref depth);

                    RenderImage.SetPixel(x, y, pixelColor.ToDrawingColor());
                    if (x == 0)
                    {
                        RenderBox.Refresh();
                    }
                }
            }

            RenderBox.Invalidate();
            RenderTimer.Stop();

            rs.RenderTime = RenderTimer.Elapsed.ToString();
        }
Exemplo n.º 3
0
        public Utilities.Color CastRay(Ray ray, Scene scene, ref int depth)
        {
            Utilities.Color newColor = new Utilities.Color();

            Utilities.Color AmbientColor  = new Utilities.Color(0.1, 0.1, 0.1);
            Utilities.Color SpecularColor = new Utilities.Color(1.0, 1.0, 1.0);

            if (depth > rs.MaxDepth)
            {
                return(Utilities.Color.Background);
            }

            RayHit hit = Trace(scene, ray);

            if (hit.isHit)
            {
                Vector3 hitPoint = hit.hitPoint;
                Vector3 Normal   = scene.SceneObjects[hit.HitObjectID].Mesh.GetNormal(hit.index);
                Vector2 st       = new Vector2();   //St Coordinates

                //GetSurfaceProperties
                Vector3 temp = hitPoint;

                //Get Material Type
                Material mat = scene.SceneObjects[hit.HitObjectID].Material;

                //Default
                if (mat.Type == 0)
                {
                    newColor = mat.MainColor;
                }

                //Flat Shading
                if (mat.Type == 1)
                {
                    newColor = mat.MainColor * Math.Max(0, Vector3.Dot(Normal, -ray.Direction));
                }

                //Smooth Shading - Need to vertex normals
                if (mat.Type == 2)
                {
                    Mesh    mesh = scene.SceneObjects[hit.HitObjectID].Mesh;
                    Vector3 n0   = mesh.normals[mesh.triangles[hit.index]];
                    Vector3 n1   = mesh.normals[mesh.triangles[hit.index + 1]];
                    Vector3 n2   = mesh.normals[mesh.triangles[hit.index + 2]];

                    Normal  = n0 * (1 - hit.uv.x - hit.uv.y);
                    Normal += (n1 * hit.uv.x);
                    Normal += (n2 * hit.uv.y);

                    newColor = mat.MainColor * Math.Max(0, Vector3.Dot(Normal, -ray.Direction));
                }

                //Diffuse
                if (mat.Type == 3)
                {
                    Utilities.Color ambient = (AmbientColor * mat.MainColor);

                    Utilities.Color textureColor = Utilities.Color.Set(0.0, 0.0, 0.0);
                    Utilities.Color vertexColor  = Utilities.Color.Set(0.0, 0.0, 0.0);

                    double LightIntesity = 0;
                    double inShadow      = 1;

                    Utilities.Color LightColor = Utilities.Color.Set(0.0, 0.0, 0.0);

                    for (int i = 0; i < scene.Lights.Count; i++)
                    {
                        //RayHit shadowHit = new RayHit();
                        //Ray shadowRay = new Ray();
                        //shadowRay.Origin = hit.hitPoint + (hit.normal * rs.Bias);
                        //shadowRay.Direction = Vector3.Normalize(scene.Lights[i].Position - shadowRay.Origin);

                        //shadowHit = Trace(scene, shadowRay);

                        //if (shadowHit.isHit == false)
                        //{
                        //inShadow = 1;
                        Vector3 L        = -scene.Lights[i].Direction;
                        double  distance = Vector3.Magnitude(scene.Lights[i].Position - hit.hitPoint);
                        double  dist     = 1 / (distance * distance);
                        double  cosTheta = MathExtensions.Clamp(Vector3.Dot(Normal, L), 0.0, 1.0);
                        Vector3 R        = Vector3.Reflect(-L, Normal);
                        double  cosAlpha = MathExtensions.Clamp(Vector3.Dot(Normal, R), 0.0, 1.0);
                        LightColor += (scene.Lights[i].LightColor * scene.Lights[i].Intensity * cosTheta * dist);
                        // }
                    }

                    if (inShadow == 1)
                    {
                        if (mat.MainTexture.PixelMap != null)
                        {
                            //Texture Coordinates
                            Vector2 st0 = scene.SceneObjects[hit.HitObjectID].Mesh.uv[scene.SceneObjects[hit.HitObjectID].Mesh.triangles[hit.index]];
                            Vector2 st1 = scene.SceneObjects[hit.HitObjectID].Mesh.uv[scene.SceneObjects[hit.HitObjectID].Mesh.triangles[hit.index + 1]];
                            Vector2 st2 = scene.SceneObjects[hit.HitObjectID].Mesh.uv[scene.SceneObjects[hit.HitObjectID].Mesh.triangles[hit.index + 2]];
                            Vector2 tex = st0 * (1 - hit.uv.x - hit.uv.y) + st1 * hit.uv.x + st2 * hit.uv.y;

                            //Vertex Color
                            Utilities.Color vc0 = scene.SceneObjects[hit.HitObjectID].Mesh.colors[scene.SceneObjects[hit.HitObjectID].Mesh.triangles[hit.index]];
                            Utilities.Color vc1 = scene.SceneObjects[hit.HitObjectID].Mesh.colors[scene.SceneObjects[hit.HitObjectID].Mesh.triangles[hit.index + 1]];
                            Utilities.Color vc2 = scene.SceneObjects[hit.HitObjectID].Mesh.colors[scene.SceneObjects[hit.HitObjectID].Mesh.triangles[hit.index + 2]];
                            vertexColor = vc0 * (1 - hit.uv.x - hit.uv.y) + vc1 * hit.uv.x + vc2 * hit.uv.y;

                            int tx = (int)(tex.x * mat.MainTexture.Width);
                            int ty = (int)(tex.y * mat.MainTexture.Height);
                            textureColor = mat.MainTexture.GetPixel(tx, ty);
                        }
                    }

                    Utilities.Color diffuse = vertexColor * textureColor * LightColor;
                    ////Utilities.Color specular = SpecularColor * scene.Lights[0].LightColor * scene.Lights[0].Intensity * Math.Pow(cosAlpha, 5) * dist;
                    newColor = ambient + diffuse;            //ambient + diffuse + SpecularColor;
                }
            }
            else
            {
                newColor = Utilities.Color.Background;
            }

            return(newColor);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Load bitmap image with fileName and return as texture
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Texture LoadBitmap(string fileName)
        {
            FileStream   fileStream    = File.Open(fileName, FileMode.Open); // Open file stream
            BinaryReader br            = new BinaryReader(fileStream);       // Create binary file reader
            Texture      loadedTexture = new Texture();

            // Bitmap information
            char[] bitmapHeader  = new char[2];     // File information in BMP
            int    bitmapDataPos = 0;               // Position in file where data begins
            int    bitmapWidth   = 0;               // Bitmap image width
            int    bitmapHeight  = 0;               // Bitmap image height
            int    bitmapSize    = 0;               // Bitmap Width * Bitmap Height * color byte size

            byte[] bitmapData;                      // Pixel data in bitmap image

            // Begin reading file
            bitmapHeader[0] = br.ReadChar();
            bitmapHeader[1] = br.ReadChar();

            // Validate bitmap file
            if (bitmapHeader[0] == 'B' || bitmapHeader[1] == 'M')
            {
                br.ReadInt32();                 //2-5
                br.ReadInt32();                 //6-9
                bitmapDataPos = br.ReadInt32(); //10-13
                br.ReadInt32();                 //14-17
                bitmapWidth  = br.ReadInt32();  //18-21
                bitmapHeight = br.ReadInt32();  //22-25
                br.ReadInt16();                 //26-27
                br.ReadInt16();                 //28-29
                br.ReadInt32();                 //30-33
                bitmapSize = br.ReadInt32();    //34-37
                br.ReadInt32();                 //38-41
                br.ReadInt32();                 //42-45
                br.ReadInt32();                 //46-49
                br.ReadInt32();                 //50-53

                // Get pixel data
                bitmapData = new byte[bitmapSize];

                for (int j = 0; j < bitmapSize; j++)
                {
                    bitmapData[j] = br.ReadByte();
                }

                //Create Texture
                loadedTexture = new Texture((int)bitmapWidth, (int)bitmapHeight);
                int dataIndex = 0;
                for (int y = 0; y < bitmapHeight; y++)
                {
                    for (int x = 0; x < bitmapWidth; x++)
                    {
                        double          r        = (double)(bitmapData[dataIndex + 2] / 255.0);
                        double          g        = (double)(bitmapData[dataIndex + 1] / 255.0);
                        double          b        = (double)(bitmapData[dataIndex] / 255.0);
                        Utilities.Color newColor = new Utilities.Color(r, g, b);
                        loadedTexture.SetPixel(x, y, newColor);
                        dataIndex += 3;
                    }
                }
            }

            br.Close();
            fileStream.Close();

            return(loadedTexture);
        }
Exemplo n.º 5
0
 public static extern void UIColorPicker3([MarshalAs(UnmanagedType.LPStr)] string text, [In, Out] ref Utilities.Color color);
Exemplo n.º 6
0
 public static extern void UIColoredText([MarshalAs(UnmanagedType.LPStr)] string text, Utilities.Color color);