Example #1
0
        public static GameObject CreateCube(Vector3 position, Vector3 scale, Quaternion rotation, Texture texture,
                                            ShaderProgram program, Vector2 tiling, Vector2 offset, Texture tesS = null, int mass = -1)
        {
            GameObject box = new GameObject(position, "Box");

            box.Scale    = scale;
            box.Rotation = rotation;
            Mesh cube = Prefabs.Cube;
            LitMeshRendererComponent mr = new LitMeshRendererComponent(program, cube, texture, 1, false);

            if (tesS != null)
            {
                tesS.TexType = TextureType.Specular;
                mr.Textures  = new[] { mr.Textures[0], tesS };
            }

            mr.Tiling = tiling;
            mr.Offset = offset;
            box.AddComponent(mr);
            Vector3  bounds = scale * 2;
            Collider coll;

            if (mass == -1)
            {
                coll = new Collider(new Box(Vector3.Zero, bounds.X, bounds.Y, bounds.Z), "physics");
            }
            else
            {
                coll = new Collider(new Box(Vector3.Zero, bounds.X, bounds.Y, bounds.Z, mass), "physics");
            }

            coll.PhysicsCollider.Material = new Material(0.1f, 0.1f, 0.1f);
            box.AddComponent(coll);
            return(box);
        }
Example #2
0
        protected override void InitializeScene()
        {
            Add(DebugConsoleComponent.CreateConsole());
            Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.DegreesToRadians(75f), //Field of View Vertical
                16f / 9f,                         //Aspect Ratio
                0.1f,                             //Near Plane
                1000f);                           //Far Plane

            BasicCamera bc = new BasicCamera(proj, Vector3.Zero);

            Add(bc);                                                     //Adding the BasicCamera(That is a gameobject under the hood) to the scene to receive events
            SetCamera(bc);                                               //Sets the Camera as the "active" camera that the scene will be rendered from.

            GameObject box = new GameObject(-Vector3.UnitZ * 4, "Box");  //Creating a new Empty GameObject
            LitMeshRendererComponent lmr = new LitMeshRendererComponent( //Creating a Renderer Component
                DefaultFilepaths.DefaultLitShader,                       //The OpenGL Shader used(Unlit and Lit shaders are provided)
                Prefabs.Cube,                                            //The Mesh that is going to be used by the MeshRenderer
                TextureLoader.ColorToTexture(Color.Red),                 //Diffuse Texture to put on the mesh
                1);                                                      //Render Mask (UI = 1 << 30)

            box.AddComponent(lmr);                                       //Attaching the Renderer to the GameObject
            box.AddComponent(new RotateSelfComponent());                 //Adding a component that rotates the Object on the Y-Axis
            Add(box);                                                    //Adding the Object to the Scene.
        }
Example #3
0
        protected override void Update(float deltaTime)
        {
            if (ObjectUnderMouse(Owner.LocalPosition, out var hit))
            {
                LitMeshRendererComponent lmr = hit.Key.Owner.GetComponent <LitMeshRendererComponent>();
                if (Last == null)
                {
                    Last    = lmr;
                    LastTex = GetTexture(lmr);

                    ApplyTexture(lmr, HitTex);
                }
                else if (lmr != Last)
                {
                    ApplyTexture(Last, LastTex);

                    Last    = lmr;
                    LastTex = GetTexture(lmr);

                    ApplyTexture(lmr, HitTex);
                }
            }
            else if (Last != null)
            {
                ApplyTexture(Last, LastTex);
                Last    = null;
                LastTex = null;
            }
        }
Example #4
0
        protected override void InitializeScene()
        {
            Add(DebugConsoleComponent.CreateConsole());
            Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.DegreesToRadians(75f), //Field of View Vertical
                16f / 9f,                         //Aspect Ratio
                0.1f,                             //Near Plane
                1000f);                           //Far Plane

            BasicCamera bc = new BasicCamera(proj, Vector3.Zero);

            Add(bc);       //Adding the BasicCamera(That is a gameobject under the hood) to the scene to receive events
            SetCamera(bc); //Sets the Camera as the "active" camera that the scene will be rendered from.



            int          texWidth  = 128;    //Width of the input texture
            int          texHeight = 128;    //Height of the input texture
            MemoryBuffer buffer    =         //Creating the Input Buffer
                                     Clapi.CreateEmpty <byte>(
                Clapi.MainThread,            //We use the Main thread instance
                texWidth * texHeight * 4,    //The image size in bytes
                MemoryFlag.ReadWrite);       //We want to read and write to the texture

            Interpreter i = new Interpreter( //Creating an interpreter instance
                Clapi.MainThread,            //We use the main thread
                "assets/filter/red.fl",      //The file to execute
                DataTypes.Uchar1,            //The Data type of our input buffer
                buffer,                      //The buffer
                texWidth,                    //Width
                texHeight,                   //Height
                1,                           //Depth, for images always 1
                4,                           //Channel count(BGRA)
                "assets/kernel/",            //Directory for all kernels
                true);                       //the "brk" statement is ignored.

            do
            {
                i.Step(); //Step through the Instructions one by one until the script terminated.
            } while (!i.Terminated);


            //Create a texture from the output.
            Texture tex = TextureLoader.BytesToTexture(i.GetResult <byte>(), texWidth, texHeight);


            GameObject box = new GameObject(-Vector3.UnitZ * 4, "Box");  //Creating a new Empty GameObject
            LitMeshRendererComponent lmr = new LitMeshRendererComponent( //Creating a Renderer Component
                DefaultFilepaths.DefaultLitShader,                       //The OpenGL Shader used(Unlit and Lit shaders are provided)
                Prefabs.Cube,                                            //The Mesh that is going to be used by the MeshRenderer
                tex,                                                     //Diffuse Texture to put on the mesh
                1);                                                      //Render Mask (UI = 1 << 30)

            box.AddComponent(lmr);                                       //Attaching the Renderer to the GameObject
            box.AddComponent(new RotatingComponent());                   //Adding a component that rotates the Object on the Y-Axis
            Add(box);                                                    //Adding the Object to the Scene.
        }
Example #5
0
        protected override void InitializeScene()
        {
            Add(DebugConsoleComponent.CreateConsole());
            BasicCamera bc =
                new BasicCamera(
                    Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(75), 16 / 9f, 0.1f, 1000f),
                    new Vector3(0, 5, 7)); //Creating a Basic Camera

            SetCamera(bc);
            Add(bc);
            bc.AddComponent(new MouseTrackerComponent());

            GameObject box = new GameObject(Vector3.UnitX * 3, "Box");
            LitMeshRendererComponent boxlmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader,
                                                                           Prefabs.Cube,
                                                                           TextureLoader.ColorToTexture(Color.Red), 1);

            box.AddComponent(boxlmr);
            Add(box);

            GameObject box2 = new GameObject(Vector3.UnitX * -3, "Box");
            LitMeshRendererComponent box2lmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader,
                                                                            Prefabs.Cube,
                                                                            TextureLoader.ColorToTexture(Color.Red), 1);

            box2.AddComponent(box2lmr);
            Add(box2);

            //Creating the Collider Shapes
            Entity boxShape = new Box(
                Physics.BEPUutilities.Vector3.Zero,
                2f,
                2f,
                2f);

            Entity box2Shape = new Box(
                Physics.BEPUutilities.Vector3.Zero,
                2f,
                2f,
                2f);


            //Creating A physics layer to be able to control which objects are meant to collide with each other
            int raycastLayerID = LayerManager.RegisterLayer("raycast", new Layer(1, 1));

            //Creating the Components for the Physics Engine
            //Note: There are different ways to get the LayerID than storing it.
            Collider boxCollider  = new Collider(boxShape, raycastLayerID);
            Collider box2Collider = new Collider(box2Shape, LayerManager.LayerToName(raycastLayerID));


            //Adding the Components
            box.AddComponent(boxCollider);
            box2.AddComponent(box2Collider);
            //Making the Camera LookAt the origin
            bc.LookAt(Vector3.Zero);
        }
Example #6
0
        //Taken from Raycasting Example

        #region Raycast

        private void ApplyTexture(LitMeshRendererComponent lmr, Texture tex)
        {
            for (int i = 0; i < lmr.Textures.Length; i++)
            {
                if (lmr.Textures[i].TexType == TextureType.Diffuse || lmr.Textures[i].TexType == TextureType.None)
                {
                    lmr.Textures[i] = tex;
                    return;
                }
            }
        }
Example #7
0
 private Texture GetTexture(LitMeshRendererComponent lmr)
 {
     for (int i = 0; i < lmr.Textures.Length; i++)
     {
         if (lmr.Textures[i].TexType == TextureType.Diffuse || lmr.Textures[i].TexType == TextureType.None)
         {
             return(lmr.Textures[i]);
         }
     }
     return(null);
 }
Example #8
0
        protected override void InitializeScene()
        {
            creator       = BufferCreator.CreateWithBuiltInTypes();
            iset          = FLInstructionSet.CreateWithBuiltInTypes(CLAPI.MainThread, "assets/kernel/");
            checkPipeline = FLProgramCheckBuilder.CreateDefaultCheckBuilder(iset, creator);
            parser        = new FLParser(iset, creator);
            checkPipeline.Attach(parser, true);

            Add(DebugConsoleComponent.CreateConsole());
            Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.DegreesToRadians(75f), //Field of View Vertical
                16f / 9f,                         //Aspect Ratio
                0.1f,                             //Near Plane
                1000f);                           //Far Plane

            BasicCamera bc = new BasicCamera(proj, Vector3.Zero);

            Add(bc);       //Adding the BasicCamera(That is a gameobject under the hood) to the scene to receive events
            SetCamera(bc); //Sets the Camera as the "active" camera that the scene will be rendered from.


            GameObject box = new GameObject(-Vector3.UnitZ * 4, "Box");  //Creating a new Empty GameObject
            LitMeshRendererComponent lmr = new LitMeshRendererComponent( //Creating a Renderer Component
                DefaultFilepaths.DefaultLitShader,                       //The OpenGL Shader used(Unlit and Lit shaders are provided)
                Prefabs.Cube,                                            //The Mesh that is going to be used by the MeshRenderer
                tex,                                                     //Diffuse Texture to put on the mesh
                1);                                                      //Render Mask (UI = 1 << 30)

            box.AddComponent(lmr);                                       //Attaching the Renderer to the GameObject
            box.AddComponent(new RotateSelfComponent());                 //Adding a component that rotates the Object on the Y-Axis
            Add(box);                                                    //Adding the Object to the Scene.


            FLBuffer buffer =
                new FLBuffer(TextureLoader.TextureToMemoryBuffer(CLAPI.MainThread, tex, "BufferForFLProgram"), 128,
                             128);


            FLProgram program = parser.Process(new FLParserInput("assets/filter/red.fl")).Initialize(iset);


            program.Run(CLAPI.MainThread, buffer, true);

            FLBuffer result = program.GetActiveBuffer(false);

            byte[] dat = CLAPI.ReadBuffer <byte>(CLAPI.MainThread, result.Buffer, (int)result.Buffer.Size);
            //Create a texture from the output.
            TextureLoader.Update(tex, dat, 128, 128);
            result.Dispose();
        }
Example #9
0
        private GameObject CreateBox(Vector3 position, Texture tex)
        {
            GameObject box = new GameObject(position, "Box");            //Creating a new Empty GameObject
            LitMeshRendererComponent lmr = new LitMeshRendererComponent( //Creating a Renderer Component
                DefaultFilepaths.DefaultLitShader,                       //The OpenGL Shader used(Unlit and Lit shaders are provided)
                Prefabs.Cube,                                            //The Mesh that is going to be used by the MeshRenderer
                tex,                                                     //Diffuse Texture to put on the mesh
                1);                                                      //Render Mask (UI = 1 << 30)

            //Adding a collider for raycasting
            box.AddComponent(new Collider(new Box(Byt3.Engine.Physics.BEPUutilities.Vector3.Zero, 2, 2, 2), "raycast"));

            box.AddComponent(lmr); //Attaching the Renderer to the GameObject
            Owner.Scene.Add(box);  //Adding the Object to the Scene.
            return(box);
        }
Example #10
0
        protected override void InitializeScene()
        {
            Add(DebugConsoleComponent.CreateConsole());
            Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.DegreesToRadians(75f), //Field of View Vertical
                16f / 9f,                         //Aspect Ratio
                0.1f,                             //Near Plane
                1000f);                           //Far Plane

            BasicCamera bc = new BasicCamera(proj, Vector3.UnitY * 7);

            bc.Rotate(Vector3.UnitX, MathHelper.DegreesToRadians(-90));
            Add(bc);                                                                  //Adding the BasicCamera(That is a gameobject under the hood) to the scene to receive events
            SetCamera(bc);                                                            //Sets the Camera as the "active" camera that the scene will be rendered from.
            bc.AddComponent(new AudioListener());
            GameObject boxContainer      = new GameObject("Container");               //Empty Container at origin
            GameObject box               = new GameObject(-Vector3.UnitZ * 6, "Box"); //Creating a new Empty GameObject
            LitMeshRendererComponent lmr = new LitMeshRendererComponent(              //Creating a Renderer Component
                DefaultFilepaths.DefaultLitShader,                                    //The OpenGL Shader used(Unlit and Lit shaders are provided)
                Prefabs.Cube,                                                         //The Mesh that is going to be used by the MeshRenderer
                TextureLoader.ColorToTexture(Color.Red),                              //Diffuse Texture to put on the mesh
                1);                                                                   //Render Mask (UI = 1 << 30)

            box.AddComponent(lmr);                                                    //Attaching the Renderer to the GameObject

            AudioSourceComponent asc = new AudioSourceComponent();

            AudioLoader.TryLoad("assets/sound.wav", out AudioFile file);
            asc.Clip    = file;
            asc.Looping = true;
            asc.Play();
            asc.UpdatePosition = true; //Enable 3D Tracking the Gameobjects movements and apply it to the audio source
            asc.Gain           = 0.6f;

            box.AddComponent(asc);
            boxContainer.AddComponent(new RotatingComponent());
            boxContainer.Add(box); //Adding the Object to the Scene.
            Add(boxContainer);
        }
Example #11
0
        private static GameObject[] CreateBounds(int width, int height, ShaderProgram program)
        {
            Texture boundsTex  = TextureLoader.ParameterToTexture(512, 512, "BoundsTex");
            Texture boundsSpec = TextureLoader.ParameterToTexture(512, 512, "BoundsSpec");

            TextureGenerator.CreateBoundsTexture(boundsTex, boundsSpec);
            boundsSpec.TexType = TextureType.Specular;

            GameObject[] ret = new GameObject[4];
            GameObject   obj;

            for (int i = 0; i < 4; i++)
            {
                LitMeshRendererComponent lmr = new LitMeshRendererComponent(program, Prefabs.Cube, boundsTex, 1);
                lmr.Textures = new[] { boundsSpec, boundsTex };
                if (i == 0)
                {
                    obj = new GameObject("BoundsLeft");
                    obj.LocalPosition = new Vector3(-width / 2f, 1, 0);
                    Collider c = new Collider(new Box(Vector3.Zero, 1, 1024, height), "physics");
                    c.PhysicsCollider.Material = new Material(0.1f, 0.1f, 0.1f);
                    obj.AddComponent(c);
                    obj.Scale  = new Vector3(1, 512, height / 2f);
                    lmr.Tiling = new Vector2(width, 512);
                }
                else if (i == 1)
                {
                    obj = new GameObject("BoundsRight");
                    obj.LocalPosition = new Vector3(width / 2f, 1, 0);
                    Collider c = new Collider(new Box(Vector3.Zero, 1, 1024, height), "physics");
                    c.PhysicsCollider.Material = new Material(0.1f, 0.1f, 0.1f);
                    obj.AddComponent(c);
                    obj.Scale  = new Vector3(1, 512, height / 2f);
                    lmr.Tiling = new Vector2(width, 512);
                }
                else if (i == 2)
                {
                    obj = new GameObject("BoundsTop");
                    obj.LocalPosition = new Vector3(0, 1, -height / 2f);
                    Collider c = new Collider(new Box(Vector3.Zero, width, 1024, 1), "physics");
                    c.PhysicsCollider.Material = new Material(0.1f, 0.1f, 0.1f);
                    obj.AddComponent(c);
                    obj.Scale  = new Vector3(width / 2f, 512, 1);
                    lmr.Tiling = new Vector2(width, 512);
                }
                else
                {
                    obj = new GameObject("BoundsBottom");
                    obj.LocalPosition = new Vector3(0, 1, height / 2f);
                    Collider c = new Collider(new Box(Vector3.Zero, width, 1024, 1), "physics");
                    c.PhysicsCollider.Material = new Material(0.1f, 0.1f, 0.1f);
                    obj.AddComponent(c);
                    obj.Scale  = new Vector3(width / 2f, 512, 1);
                    lmr.Tiling = new Vector2(width, 512);
                }

                obj.AddComponent(lmr);


                ret[i] = obj;
            }

            return(ret);
        }
Example #12
0
        protected override void InitializeScene()
        {
            Add(DebugConsoleComponent.CreateConsole());
            BasicCamera bc =
                new BasicCamera(
                    Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(75), 16 / 9f, 0.1f, 1000f),
                    new Vector3(0, 5, 30)); //Creating a Basic Camera

            SetCamera(bc);
            Add(bc);

            //Creating a Box that is meant to fall down on the kinetic box
            GameObject box = new GameObject(Vector3.UnitZ * -3 + Vector3.UnitY * 2, "Box");
            LitMeshRendererComponent lmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader, Prefabs.Cube,
                                                                        TextureLoader.ColorToTexture(Color.Red), 1);

            box.AddComponent(lmr);
            Add(box);

            //Creating a Kinetic Box that will rotate slowly
            GameObject kinetic = new GameObject(Vector3.UnitZ * -3 + Vector3.UnitY * -2, "Box");
            LitMeshRendererComponent kineticlmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader,
                                                                               Prefabs.Cube,
                                                                               TextureLoader.ColorToTexture(Color.Green), 1);

            kinetic.AddComponent(kineticlmr);
            kinetic.AddComponent(new RotateKineticBodyComponent());
            Add(kinetic);

            //A Large sphere that will act as a ground
            GameObject ground = new GameObject(Vector3.UnitZ * -3 + Vector3.UnitY * -1, "Box");
            LitMeshRendererComponent groundlmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader,
                                                                              Prefabs.Sphere,
                                                                              TextureLoader.ColorToTexture(Color.Blue), 1);

            ground.AddComponent(groundlmr);
            Add(ground);
            ground.Scale         = new Vector3(20, 20, 20);
            ground.LocalPosition = Physics.BEPUutilities.Vector3.UnitY * -25;

            //Creating the Collider Shapes
            Entity boxShape = new Box(
                Physics.BEPUutilities.Vector3.Zero,
                2f,
                2f,
                2f,
                1f);

            Entity kineticShape = new Box(
                Physics.BEPUutilities.Vector3.Zero,
                2f,
                2f,
                2f,
                1f);

            Entity groundShape = new Sphere(
                Vector3.Zero,
                20f);
            //Note: Not specifying the mass when creating makes the shape a static shape that is really cheap computatinally

            //Ground(Sphere) and the falling box is going to have 0 friction and maximum bounciness.
            Material groundPhysicsMaterial = new Material(0, 0, 1f);
            Material boxPhysicsMaterial    = new Material(0, 0, 1f);

            //Creating A physics layer to be able to control which objects are meant to collide with each other
            int physicsLayerID = LayerManager.RegisterLayer("physics", new Layer(1, 1));

            //Creating the Components for the Physics Engine
            //Note: There are different ways to get the LayerID than storing it.
            Collider boxCollider     = new Collider(boxShape, physicsLayerID);
            Collider groundCollider  = new Collider(groundShape, "physics");
            Collider kineticCollider = new Collider(kineticShape, LayerManager.LayerToName(physicsLayerID));

            //Final Collider Setup
            //Kinetic becomes Kinetic
            kineticCollider.PhysicsCollider.BecomeKinematic();
            //Adding the Physics Materials
            boxCollider.PhysicsCollider.Material    = boxPhysicsMaterial;
            groundCollider.PhysicsCollider.Material = groundPhysicsMaterial;

            //Adding the Components
            box.AddComponent(boxCollider);
            kinetic.AddComponent(kineticCollider);
            ground.AddComponent(groundCollider);

            //Making the Camera LookAt the origin
            bc.LookAt(Vector3.Zero);
        }
Example #13
0
        protected override void Update(float deltaTime)
        {
            if (ObjectUnderMouse(Owner.LocalPosition, out KeyValuePair <Collider, RayHit> hit)
                ) //We Check where we clicked on
            {
                AiNode node = hit.Key.Owner.GetComponent <AiNode>();
                if (node != null)
                {
                    if (Input.GetKey(Key.S)) //Setting the Start Point
                    {
                        LitMeshRendererComponent lmr =
                            node.Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, purpleTex);
                        if (startNode != null && startNode != node)
                        {
                            ApplyTexture(startNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                         startNode.Walkable ? greenTex : redTex);
                        }

                        startNode = node;
                    }
                    else if (Input.GetKey(Key.E)) //Setting the End Point
                    {
                        LitMeshRendererComponent lmr =
                            node.Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, purpleTex);
                        if (endNode != null && endNode != node)
                        {
                            ApplyTexture(endNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                         endNode.Walkable ? greenTex : redTex);
                        }

                        endNode = node;
                    }
                }
            }

            //When Start and end Point is defined and space is pressed we calculate the path
            if (startNode != null && endNode != null && Input.GetKey(Key.Space))
            {
                if (path != null) //First Clean the Old path, and reset the textures
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        LitMeshRendererComponent lmr =
                            (path[i] as AiNode).Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, path[i].Walkable ? greenTex : redTex);
                    }
                }

                //This line is doing the A*
                path = AStarResolver.FindPath(startNode, endNode, out bool foundPath);


                if (foundPath) //If there exists a path from start to end, we will make it visible
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        LitMeshRendererComponent lmr =
                            (path[i] as AiNode).Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, purpleTex);
                    }
                }
            }

            //Escape Resets the Nodes color and removes the starting points
            if (Input.GetKey(Key.Escape))
            {
                if (startNode != null)
                {
                    ApplyTexture(startNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                 startNode.Walkable ? greenTex : redTex);
                }

                if (endNode != null)
                {
                    ApplyTexture(endNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                 endNode.Walkable ? greenTex : redTex);
                }

                startNode = endNode = null;
                if (path != null)
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        LitMeshRendererComponent lmr =
                            (path[i] as AiNode).Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, path[i].Walkable ? greenTex : redTex);
                    }
                }

                path = null;
            }
        }
Example #14
0
        protected override void InitializeScene()
        {
            creator       = BufferCreator.CreateWithBuiltInTypes();
            iset          = FLInstructionSet.CreateWithBuiltInTypes(CLAPI.MainThread, "assets/kernel/");
            checkPipeline = FLProgramCheckBuilder.CreateDefaultCheckBuilder(iset, creator);
            parser        = new FLParser(iset, creator);
            checkPipeline.Attach(parser, true);
            Mesh plane = MeshLoader.FileToMesh("assets/models/plane.obj");

            Texture texQuad   = TextureLoader.ParameterToTexture(1024, 1024, "FLDisplayTextureQuad");
            Texture texSphere = TextureLoader.ParameterToTexture(1024, 1024, "FLDisplayTextureSphere");

            GameObject objSphere = new GameObject(new Vector3(1, 1, 0), "SphereDisplay");

            LitMeshRendererComponent sphereLmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader,
                                                                              Prefabs.Sphere,
                                                                              texSphere, 1);

            objSphere.AddComponent(sphereLmr);
            sphereLmr.Textures = new[]
            { sphereLmr.Textures[0], DefaultFilepaths.DefaultTexture };

            objSphere.AddComponent(new RotatingComponent());

            GameObject objQuad = new GameObject(new Vector3(-1, 1, 0), "QuadDisplay");
            LitMeshRendererComponent quadLmr = new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader, plane,
                                                                            texQuad, 1);

            objQuad.AddComponent(quadLmr);
            quadLmr.Textures = new[]
            { quadLmr.Textures[0], DefaultFilepaths.DefaultTexture };

            objQuad.Rotate(new Vector3(1, 0, 0), MathHelper.DegreesToRadians(45));

            GameObject sourceCube = new GameObject(new Vector3(0, 10, 10), "Light Source");

            sourceCube.AddComponent(new LightComponent());
            sourceCube.AddComponent(new RotateAroundComponent {
                Slow = 0.15f
            });
            sourceCube.AddComponent(new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader, Prefabs.Cube,
                                                                 TextureLoader.ColorToTexture(Color.White), 1));

            GameObject uiText = new GameObject(new Vector3(0), "UIText");

            uiText.AddComponent(new FlGeneratorComponent(new List <LitMeshRendererComponent>
            {
                sphereLmr, quadLmr
            },
                                                         512,
                                                         512, true));

            Add(sourceCube);
            Add(uiText);
            Add(DebugConsoleComponent.CreateConsole());
            Add(objSphere);
            Add(objQuad);

            GameObject bgObj = new GameObject(Vector3.UnitY * -3, "BG")
            {
                Scale = new Vector3(25, 1, 25)
            };


            bgObj.AddComponent(new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader, Prefabs.Cube,
                                                            GenerateGroundTexture(), 1));
            Add(bgObj);


            BasicCamera mainCamera =
                new BasicCamera(
                    Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(75f),
                                                         GameEngine.Instance.Width / (float)GameEngine.Instance.Height, 0.01f, 1000f), Vector3.Zero);

            object mc = mainCamera;

            EngineConfig.LoadConfig("assets/configs/camera_fldemo.xml", ref mc);


            Add(mainCamera);
            SetCamera(mainCamera);

            GameObject camContainer = new GameObject("CamContainer");

            BasicCamera inPicCam =
                new BasicCamera(
                    Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(75f),
                                                         GameEngine.Instance.Width / (float)GameEngine.Instance.Height, 0.01f, 1000f), Vector3.Zero);

            inPicCam.Rotate(new Vector3(1, 0, 0), MathHelper.DegreesToRadians(0));
            inPicCam.Translate(new Vector3(0, 2, 4));
            inPicCam.AddComponent(new RotateAroundComponent());
            GameObject zeroPoint = new GameObject("Zero");

            Add(zeroPoint);
            LookAtComponent comp = new LookAtComponent();

            comp.SetTarget(zeroPoint);
            inPicCam.AddComponent(comp);
            Add(inPicCam);


            splitCam = new RenderTarget(inPicCam, 1, Color.FromArgb(0, 0, 0, 0))
            {
                MergeType = RenderTargetMergeType.Additive,
                ViewPort  = new Rectangle(0, 0, (int)(GameEngine.Instance.Width * 0.3f),
                                          (int)(GameEngine.Instance.Height * 0.3f))
            };

            Add(camContainer);
            GameEngine.Instance.AddRenderTarget(splitCam);
        }
Example #15
0
        protected override void InitializeScene()
        {
            Add(DebugConsoleComponent.CreateConsole());
            Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.DegreesToRadians(75f), //Field of View Vertical
                16f / 9f,                         //Aspect Ratio
                0.1f,                             //Near Plane
                1000f);                           //Far Plane

            BasicCamera bc = new BasicCamera(proj, Vector3.Zero);

            Add(bc);       //Adding the BasicCamera(That is a gameobject under the hood) to the scene to receive events
            SetCamera(bc); //Sets the Camera as the "active" camera that the scene will be rendered from.


            //Image size in bytes(Width * Height * ChannelCount)
            int imageSize = 512 * 512 * 4;

            //Creating a Kernel Database that will load all the Kernels contained in the asset directory
            KernelDatabase db = new KernelDatabase(Clapi.MainThread, "assets/test_kernel/", DataTypes.Uchar1);

            //We try to get the kernel_red from the file assets/test_kernel/red.cl
            db.TryGetClKernel("kernel_red", out CLKernel redKernel);

            //Creating a MemoryBuffer with size of the image.
            //We are using the CLAPI instance of the main thread and specify that we`d like to read/write from the buffer
            MemoryBuffer imageBuffer = Clapi.CreateEmpty <byte>(Clapi.MainThread, imageSize, MemoryFlag.ReadWrite);

            //With plain OpenCL you would need to Set the Arguments/Buffers by their argument index/types/size/yada yads,
            //thanks to the CL abstraction for the engine, we can just specify the argument name how we do in OpenGL Shaders(But Faster).
            redKernel.SetBuffer("imageData", imageBuffer);

            //Set Arg has the capabilities to automatically cast the value that is passed to the right type,
            //however this is not really fast and can be avoided by specifying the correct type directly.
            redKernel.SetArg("strength", 0.5f); //We directly pass a float, no casting required

            //When We pass something as byte(uchar in cl), we need to cast it.
            //If we dont the Engine OpenCL Wrapper will automatically convert the integer into a byte, but it will apply rescaling
            //  This takes over automatic type conversion from float(opengl) to byte(System.Bitmap/opencl)
            //  Calculation when not passed: (4 / Int32.MaxSize) * byte.MaxValue.
            redKernel.SetArg("channelCount", (byte)4);

            //This Line runs the kernel.
            Clapi.Run(Clapi.MainThread, redKernel, imageSize);

            //After the kernel ran, we can read the buffer we have passed to the kernel and Convert it into a OpenGL Texture.
            Texture tex = TextureLoader.BytesToTexture(Clapi.ReadBuffer <byte>(Clapi.MainThread, imageBuffer, imageSize), 512, 512);



            GameObject box = new GameObject(-Vector3.UnitZ * 4, "Box");  //Creating a new Empty GameObject
            LitMeshRendererComponent lmr = new LitMeshRendererComponent( //Creating a Renderer Component
                DefaultFilepaths.DefaultLitShader,                       //The OpenGL Shader used(Unlit and Lit shaders are provided)
                Prefabs.Cube,                                            //The Mesh that is going to be used by the MeshRenderer
                tex,                                                     //Diffuse Texture to put on the mesh
                1);                                                      //Render Mask (UI = 1 << 30)

            box.AddComponent(lmr);                                       //Attaching the Renderer to the GameObject
            box.AddComponent(new RotatingComponent());                   //Adding a component that rotates the Object on the Y-Axis
            Add(box);                                                    //Adding the Object to the Scene.
        }