예제 #1
0
        internal override object run()
        {
            // so we loop  through all objects in the scene:
            for (int i = 0; i < scene.members.Count; i++)
            {
                // and find everything.
                FSceneMember member = scene.members[i];

                if (member is FPolygon)
                {
                    throw new Exception("Polygons should not be in scenes! Perhaps this rule is dumb, but that's not my discresion.");
                }

                if (member is FCamera || member is FLightSource)
                {
                    //Console.WriteLine(((FCamera)member).position.X);
                    URS.Add(member);
                    continue;
                }

                if (member is FObject)
                {
                    FObject fobject = (FObject)member; // member is now an FObject in the eyes of the law!

                    for (int j = 0; j < fobject.template.polygons.Count; j++)
                    {
                        FPolygon poly = new FPolygon(fobject.template.polygons[j].verticies, fobject.template.polygons[j].color);

                        for (int k = 0; k < poly.verticies.Length; k++)
                        {
                            poly.verticies[k] = Vector3.Add(poly.verticies[k], fobject.position);
                        }

                        URS.Add(poly);
                    }
                    continue;
                }

                throw new Exception("An unknown sceneMember has been given to basicReferenceCreator! how did this happen???");
            }
            //cool.

            return(base.run());
        }
예제 #2
0
        internal override object run()
        {
            //check if multiple cameras are active
            //This variable checks for cameras in the scene if it is aready set then there is multiple cameras in the scene if
            //at the end of this is still -1 then there aren't any both cases we would return an error

            int camera = -1;

            for (int x = 0; x < URS.Count; x++)
            {
                if (URS[x] is FCamera)
                {
                    if (((FCamera)URS[x]).active)
                    {
                        if (camera == -1)
                        {
                            camera = x;
                            Fuzzy3D.activeCamera = (FCamera)URS[camera];
                        }
                        else
                        {
                            //Throwing an error that I found in an example of how to use throw as I am rather new to it
                            throw new IndexOutOfRangeException();
                        }
                    }
                }
            }
            if (camera == -1)
            {
                throw new IndexOutOfRangeException();
            }

            Vector3 cam    = ((FCamera)URS[camera]).position;
            double  camRot = ((FCamera)URS[camera]).Rotation;

            //Looping through the URS to set to the LRS
            //Console.WriteLine("Before Translation: " + URS.Count);
            List <FSceneMember> MRS = new List <FSceneMember>();

            for (int y = 0; y < URS.Count; y++)
            {
                if (URS[y] is FPolygon)
                {
                    Vector3[] prev = ((FPolygon)URS[y]).verticies;
                    Vector3[] next = { Vector3.Subtract(prev[0], cam), Vector3.Subtract(prev[1], cam), Vector3.Subtract(prev[2], cam) };

                    MRS.Add(new FPolygon(next, ((FPolygon)URS[y]).color, ((FPolygon)URS[y]).surfaceNormal));
                    continue;
                }
                if (URS[y] is FCamera)
                {
                    //Console.WriteLine("WHat the f**k!");

                    Vector3 prev = ((FCamera)URS[y]).position;
                    MRS.Add(new FCamera(Vector3.Subtract(prev, cam), ((FCamera)URS[y]).FOV));
                    continue;
                }

                throw new Exception("Transformer does not currently handle Lightsources!");
            }

            // Do the same but for rotation.
            // Note that this must occur after translation; It is dependent on it.

            for (int y = 0; y < MRS.Count; y++)
            {
                if (MRS[y] is FPolygon)
                {
                    Vector3[] prev = ((FPolygon)MRS[y]).verticies;
                    Vector3[] next = { Rotate2D(prev[0], camRot), Rotate2D(prev[1], camRot), Rotate2D(prev[2], camRot) };


                    // Now we check about surface normal
                    if (((FPolygon)MRS[y]).surfaceNormal == new Vector3(0, 0, 0))
                    {
                        LRS.Add(new FPolygon(next, ((FPolygon)MRS[y]).color));
                    }
                    else
                    {
                        FPolygon poly = new FPolygon(next, ((FPolygon)MRS[y]).color, ((FPolygon)MRS[y]).surfaceNormal);

                        Console.WriteLine("Y: " + y + " Vector: " + poly.surfaceNormal.X + ", " + poly.surfaceNormal.Y + ", " + poly.surfaceNormal.Z + " Angle: " + Math.Abs(Math.Acos(poly.surfaceNormal.X)));
                        if (Math.Abs(Math.Acos(poly.surfaceNormal.X)) > Math.PI / 2 & !Double.IsNaN(Math.Abs(Math.Acos(poly.surfaceNormal.X))))
                        {
                            LRS.Add(poly);
                        }
                    }


                    continue;
                }
                if (MRS[y] is FCamera)
                {
                    Vector3 prev = ((FCamera)MRS[y]).position;

                    LRS.Add(new FCamera(Rotate2D(prev, camRot), ((FCamera)MRS[y]).FOV));

                    continue;
                }

                throw new Exception("Transformer does not currently handle: " + URS[y].GetType());
            }
            //Console.WriteLine("After Rotation: " + LRS.Count);
            return(base.run());
        }
예제 #3
0
        internal override object run()
        {
            // first of all, make the screen black:
            for (int x = 0; x < ScreenState.GetLength(0); x++)
            {
                for (int y = 0; y < ScreenState.GetLength(1); y++)
                {
                    ScreenState[x, y] = Color.Black;
                }
            }
            List <Line> drawn_lines = new List <Line>();

            //Loop through all objects in the LRS
            for (int i = 0; i < LRS.Count; i++)
            {
                //Console.WriteLine(LRS.Count);
                //If an object is a polygon
                if (LRS[i] is FPolygon)
                {
                    FPolygon poly = (FPolygon)LRS[i];
                    for (int j = 0; j < poly.verticies.Length; j++)
                    {
                        bool cancel = false;
                        // get the 2 vector3s we will be drawing the line from.
                        Vector2 A = poly.screenVerticies[j];
                        Vector2 B;
                        if (j == poly.verticies.Length - 1)
                        {
                            B = poly.screenVerticies[0];
                        }
                        else
                        {
                            B = poly.screenVerticies[j + 1];
                        }
                        if (A.X == -1 || B.Y == -1)
                        {
                            continue;
                        }
                        for (int g = 0; g < drawn_lines.Count; g++)
                        {
                            //Check if the points are the same as one previously drawn
                            if (A == drawn_lines[g].first && B == drawn_lines[g].second)
                            {
                                cancel = true;
                            }
                            else if (A == drawn_lines[g].second && B == drawn_lines[g].first)
                            {
                                cancel = true;
                            }
                        }
                        //Dont run the rest of the code if the line has already been drawn
                        if (!cancel)
                        {
                            if (A.X == B.X)
                            {
                                // okay, vertical line.
                                for (int vertY = (int)((A.Y < B.Y) ? A.Y : B.Y); vertY < (int)((A.Y > B.Y) ? A.Y : B.Y); vertY++)
                                {
                                    try
                                    {
                                        ScreenState[(int)A.X, vertY] = linecolor;
                                    }
                                    catch
                                    {
                                        //Console.WriteLine("Caught something... don't know what, or why, but it's in wireframe.");
                                        // meh, do nothing
                                        break;
                                    }
                                }
                            }
                            //Bresenham( (int)(A.X<B.X?A.X:B.X), (int)(A.X < B.X ? A.Y : B.Y), (int)(A.X > B.X ? A.X : B.X), (int)(A.X > B.X ? A.Y : B.Y));
                            Bresenham((int)A.X, (int)A.Y, (int)B.X, (int)B.Y);
                            int x = (int)A.X;
                            int y = (int)A.Y;
                            if (x >= 0 & y >= 0 & x < ScreenState.GetLength(1) & y < ScreenState.GetLength(1))
                            {
                                ScreenState[(int)x, (int)y] = Color.White;
                            }
                            x = (int)B.X;
                            y = (int)B.Y;
                            if (x >= 0 & y >= 0 & x < ScreenState.GetLength(1) & y < ScreenState.GetLength(1))
                            {
                                ScreenState[(int)x, (int)y] = Color.White;
                            }
                            //Record the line we just drew
                            drawn_lines.Add(new Line(A, B));
                        }
                    }
                }
            }


            return(base.run());
        }
예제 #4
0
        internal override object run()
        {
            // first of all, make the screen black:
            for (int x = 0; x < ScreenState.GetLength(0); x++)
            {
                for (int y = 0; y < ScreenState.GetLength(1); y++)
                {
                    ScreenState[x, y] = Color.Black;
                }
            }

            for (int i = 0; i < LRS.Count; i++)
            {
                if (LRS[i] is FPolygon)
                {
                    FPolygon poly = (FPolygon)LRS[i];
                    for (int j = 0; j < poly.verticies.Length; j++)
                    {
                        // get the 2 vector3s we will be drawing the line from.
                        Vector2 A = poly.screenVerticies[j];
                        Vector2 B;
                        if (j == poly.verticies.Length - 1)
                        {
                            B = poly.screenVerticies[0];
                        }
                        else
                        {
                            B = poly.screenVerticies[j + 1];
                        }
                        if (A.X == -1 || B.Y == -1)
                        {
                            continue;
                        }
                        if (A.X == B.X)
                        {
                            // okay, vertical line.
                            for (int vertY = (int)((A.Y < B.Y)?A.Y:B.Y); vertY < (int)((A.Y > B.Y) ? A.Y : B.Y); vertY++)
                            {
                                ScreenState[(int)A.X, vertY] = linecolor;
                            }
                        }
                        //Bresenham( (int)(A.X<B.X?A.X:B.X), (int)(A.X < B.X ? A.Y : B.Y), (int)(A.X > B.X ? A.X : B.X), (int)(A.X > B.X ? A.Y : B.Y));
                        Bresenham((int)A.X, (int)A.Y, (int)B.X, (int)B.Y);
                        int x = (int)A.X;
                        int y = (int)A.Y;
                        if (x >= 0 & y >= 0 & x < ScreenState.GetLength(1) & y < ScreenState.GetLength(1))
                        {
                            ScreenState[(int)x, (int)y] = Color.White;
                        }
                        x = (int)B.X;
                        y = (int)B.Y;
                        if (x >= 0 & y >= 0 & x < ScreenState.GetLength(1) & y < ScreenState.GetLength(1))
                        {
                            ScreenState[(int)x, (int)y] = Color.White;
                        }
                    }
                }
            }


            return(base.run());
        }
예제 #5
0
        internal override object run()
        {
            // first of all, make the screen black:
            for (int x = 0; x < screenState.GetLength(0); x++)
            {
                for (int y = 0; y < screenState.GetLength(1); y++)
                {
                    screenState[x, y] = Color.Black;
                }
            }

            double slope = Fuzzy3D.activeCamera.FOV;

            for (int i = 0; i < LRS.Count; i++)
            {
                if (LRS[i] is FPolygon)
                {
                    FPolygon poly = (FPolygon)LRS[i];
                    // now, we need to do the things!
                    // The HORROR!
                    // So, for each point in the poly:
                    for (int j = 0; j < poly.verticies.Length; j++)
                    {
                        // for each point, uhh...

                        /*Okay, so, let me lay out my understanding:
                         * For each vertice in each polygon:
                         * Determine the x value, and based on the cameras slope, get it’s 2D coords from the square defined by the slope,
                         * Ie, one the upper left corner would be defined as, y=slopex, z=slopex, given that the viewport is square. LowerRight is the negative of both, and so on.
                         * Take these coordinates and convert them into the full ( or small) sized coordinates of the viewport
                         * This should be easy, actually:
                         *          I. for the x position: x(relative to the top left corner of the viewport)/width=realX/screenWidth
                         *              Just solve for realx and it’s done.
                         * You now have the proper positioning of every vertice on the screen.
                         */

                        Vector3 vert = poly.verticies[j];
                        if (vert.X <= 0)
                        {
                            // if it is behind the camera, it cannot be seen.
                            ((FPolygon)LRS[i]).screenVerticies = new Vector2[3] {
                                new Vector2(-1, -1), new Vector2(-1, -1), new Vector2(-1, -1)
                            };
                            continue;
                        }

                        // Now we find the size of the viewport.
                        // which requires the camera's FOV...
                        // Start by grabing the upper left corner:
                        // notice, we're implicitly implying that the viewport is square.
                        Vector2 viewportOrigin = new Vector2(-(float)slope * vert.X, -(float)slope * vert.X);
                        double  viewportSize   = (float)slope * vert.X * 2; // should be zero!

                        Vector2 locationOnViewport = Vector2.Subtract(new Vector2(vert.Z, -vert.Y), viewportOrigin);


                        //okay, easy peasy, now just to get that to the screen
                        int x = (int)Math.Round((double)(locationOnViewport.X / viewportSize) * (screenState.GetLength(1)));
                        int y = (int)Math.Round((double)(locationOnViewport.Y / viewportSize) * (screenState.GetLength(1)));

                        ((FPolygon)LRS[i]).screenVerticies[j] = new Vector2(x, y);
                        if (x >= 0 & y >= 0 & x < screenState.GetLength(1) & y < screenState.GetLength(1))
                        {
                            screenState[x, y] = Color.White;
                        }
                    }
                }
            }


            return(base.run());
        }