예제 #1
0
        public static void Run()
        {
            // Initialize scene object
            Scene scene = new Scene();

            // Initialize Node class object
            Node cubeNode = new Node("box");

            //ExStart:ConvertBoxMeshtoTriangleMeshCustomMemoryLayout
            // get mesh of the Box
            Mesh box = (new Box()).ToMesh();
            //create a customized vertex layout
            VertexDeclaration vd       = new VertexDeclaration();
            VertexField       position = vd.AddField(VertexFieldDataType.FVector4, VertexFieldSemantic.Position);

            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Normal);
            // get a triangle mesh
            TriMesh triMesh = TriMesh.FromMesh(box);

            //ExEnd:ConvertBoxMeshtoTriangleMeshCustomMemoryLayout

            // Point node to the Mesh geometry
            cubeNode.Entity = box;

            // Add Node to a scene
            scene.RootNode.ChildNodes.Add(cubeNode);

            // The path to the documents directory.
            string MyDir = RunExamples.GetDataDir() + RunExamples.GetOutputFilePath("BoxToTriangleMeshCustomMemoryLayoutScene.fbx");

            // Save 3D scene in the supported file formats
            scene.Save(MyDir, FileFormat.FBX7400ASCII);

            Console.WriteLine("\n Converted a Box mesh to triangle mesh with custom memory layout of the vertex successfully.\nFile saved at " + MyDir);
        }
예제 #2
0
        public override void Initialize(Renderer renderer)
        {
            base.Initialize(renderer);
            //Prepare the render state used by the grid/axis
            var rs = new RenderState();

            rs.DepthTest = true;
            rs.DepthMask = true;

            //Define the memory layout of the vertex declaration
            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.VertexColor);
            //compile shader from SPIR-V source code and specify
            var src = new SPIRVSource()
            {
                VertexShader   = Shaders.GridVertex,
                FragmentShader = Shaders.GridFragment
            };

            shader = renderer.RenderFactory.CreateShaderProgram(src);
            //bake the graphic render pipeline
            pipeline = renderer.RenderFactory.CreatePipeline(shader, rs, vd, DrawOperation.Lines);

            ms = new MemoryStream(16 * 4);
            w  = new BinaryWriter(ms);
        }
        public static void Run()
        {
            // Initialize scene object
            Scene scene = new Scene();

            // Initialize Node class object
            Node cubeNode = new Node("box");

            // ExStart:ConvertBoxMeshtoTriangleMeshCustomMemoryLayout
            // Get mesh of the Box
            Mesh box = (new Box()).ToMesh();
            // Create a customized vertex layout
            VertexDeclaration vd = new VertexDeclaration();
            VertexField position = vd.AddField(VertexFieldDataType.FVector4, VertexFieldSemantic.Position);
            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Normal);
            // Get a triangle mesh
            TriMesh triMesh = TriMesh.FromMesh(box);
            // ExEnd:ConvertBoxMeshtoTriangleMeshCustomMemoryLayout

            // Point node to the Mesh geometry
            cubeNode.Entity = box;

            // Add Node to a scene
            scene.RootNode.ChildNodes.Add(cubeNode);

            // The path to the documents directory.
            string MyDir = RunExamples.GetDataDir() + RunExamples.GetOutputFilePath("BoxToTriangleMeshCustomMemoryLayoutScene.fbx");

            // Save 3D scene in the supported file formats
            scene.Save(MyDir, FileFormat.FBX7400ASCII);

            Console.WriteLine("\n Converted a Box mesh to triangle mesh with custom memory layout of the vertex successfully.\nFile saved at " + MyDir);
        }
예제 #4
0
        public Grid(Renderer renderer, ShaderProgram shader)
        {
            //render state for grid
            RenderState           = renderer.RenderFactory.CreateRenderState();
            RenderState.DepthTest = true;
            RenderState.DepthMask = true;
            this.Shader           = shader;
            //define the format of the control point to render the line
            VertexDeclaration vd = new VertexDeclaration();

            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            //and create a vertex buffer for storing this kind of data
            this.VertexBuffer = renderer.RenderFactory.CreateVertexBuffer(vd);
            // draw the primitive as lines
            this.DrawOperation = DrawOperation.Lines;
            this.RenderGroup   = RenderQueueGroupId.Geometries;

            List <FVector3> lines = new List <FVector3>();

            for (int i = -10; i <= 10; i++)
            {
                //draw - line
                lines.Add(new FVector3(i, 0, -10));
                lines.Add(new FVector3(i, 0, 10));


                //draw | line
                lines.Add(new FVector3(-10, 0, i));
                lines.Add(new FVector3(10, 0, i));
            }
            //put it to vertex buffer
            VertexBuffer.LoadData(lines.ToArray());
        }
예제 #5
0
        public Grid(Renderer renderer, ShaderProgram shader)
        {
            // Render state for grid
            RenderState = renderer.RenderFactory.CreateRenderState();
            RenderState.DepthTest = true;
            RenderState.DepthMask = true;
            this.Shader = shader;
            // Define the format of the control point to render the line
            VertexDeclaration vd = new VertexDeclaration();
            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            // and create a vertex buffer for storing this kind of data
            this.VertexBuffer = renderer.RenderFactory.CreateVertexBuffer(vd);
            // Draw the primitive as lines
            this.DrawOperation = DrawOperation.Lines;
            this.RenderGroup = RenderQueueGroupId.Geometries;

            List<FVector3> lines = new List<FVector3>();
            for (int i = -10; i <= 10; i++)
            {
                // Draw - line
                lines.Add(new FVector3(i, 0, -10));
                lines.Add(new FVector3(i,0, 10));


                // Draw | line
                lines.Add(new FVector3(-10, 0, i));
                lines.Add(new FVector3(10, 0, i));
            }
            // Put it to vertex buffer
            VertexBuffer.LoadData(lines.ToArray());
        }
예제 #6
0
        private void InitRenderer()
        {
            //create a default camera, because it's required during the viewport's creation.
            camera = new Camera();
            Scene.RootNode.CreateChildNode("camera", camera);
            //create the renderer and render window from window's native handle
            renderer = Renderer.CreateRenderer();
            //Right now we only support native window handle from Microsoft Windows
            //we'll support more platform on user's demand.
            window = renderer.RenderFactory.CreateRenderWindow(new RenderParameters(), Handle);
            //create 4 viewports, the viewport's area is meanless here because we'll change it to the right area in the SetViewports later
            viewports = new[]
            {
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1)),
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1)),
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1)),
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1))
            };
            SetViewports(1);


            //initialize shader for grid

            GLSLSource src = new GLSLSource();

            src.VertexShader   = @"#version 330 core
layout (location = 0) in vec3 position;
uniform mat4 matWorldViewProj;
void main()
{
    gl_Position = matWorldViewProj * vec4(position, 1.0f);
}";
            src.FragmentShader = @"#version 330 core
out vec4 color;
void main()
{
    color = vec4(1, 1, 1, 1);
}";
            //define the input format used by GLSL vertex shader
            //the format is
            // struct ControlPoint {
            //    FVector3 Position;
            //}
            VertexDeclaration fd = new VertexDeclaration();

            fd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            //compile shader from GLSL source code and specify the vertex input format
            gridShader = renderer.RenderFactory.CreateShaderProgram(src, fd);
            //connect GLSL uniform to renderer's internal variable
            gridShader.Variables = new ShaderVariable[]
            {
                new ShaderVariable("matWorldViewProj", VariableSemantic.MatrixWorldViewProj)
            };

            SceneUpdated("");
        }
        private static ShaderSet CreateDepthShader(Renderer renderer)
        {
            GLSLSource src = new GLSLSource();

            src.VertexShader   = @"#version 330 core
            layout (location = 0) in vec3 position;
            uniform mat4 matWorldViewProj;
            out float depth;
            void main()
            {
                gl_Position = matWorldViewProj * vec4(position, 1.0f);
                float zfar = 200.0;
                float znear = 0.5;
                //visualize the depth by linearize it so we don't get a blank screen
                depth = (2.0 * znear) / (zfar + znear - gl_Position.z /gl_Position.w  * (zfar - znear));
            }";
            src.FragmentShader = @"#version 330 core
            in float depth;
            out vec4 color;
            void main()
            {
                color = vec4(depth, depth, depth, 1);
            }";
            //we only need the position to render the depth map
            VertexDeclaration fd = new VertexDeclaration();

            fd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            //compile shader from GLSL source code and specify the vertex input format
            var shader = renderer.RenderFactory.CreateShaderProgram(src, fd);

            //connect GLSL uniform to renderer's internal variable
            shader.Variables = new ShaderVariable[]
            {
                new ShaderVariable("matWorldViewProj", VariableSemantic.MatrixWorldViewProj)
            };
            //create a shader set
            ShaderSet ret = new ShaderSet();

            //we only use the fallback, and left other shaders unassigned, so all materials will be rendered by this shader
            ret.Fallback = shader;
            return(ret);
        }
예제 #8
0
        /// <summary>
        /// Initialize the renderer
        /// </summary>
        /// <param name="renderer"></param>
        public override void Initialize(Renderer renderer)
        {
            base.Initialize(renderer);
            //create the shader program from the precompiled SPIR-V byte code
            var src = new SPIRVSource()
            {
                VertexShader   = Shaders.NormalsVertex,
                FragmentShader = Shaders.NormalsFragment
            };

            shader = renderer.RenderFactory.CreateShaderProgram(src);
            //default render state
            var rs = new RenderState();
            //Define the memory layout of the vertex
            var vd = new VertexDeclaration();

            vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            //create the pipeline and vertex buffer
            pipeline = renderer.RenderFactory.CreatePipeline(shader, rs, vd, DrawOperation.Lines);
            buffer   = renderer.RenderFactory.CreateVertexBuffer(vd);
        }
예제 #9
0
        //ExStart:RenderView
        private void InitRenderer()
        {
            // Create a default camera, because it's required during the viewport's creation.
            camera = new Camera();
            Scene.RootNode.CreateChildNode("camera", camera);
            // Create the renderer and render window from window's native handle
            renderer = Renderer.CreateRenderer();
            // Right now we only support native window handle from Microsoft Windows
            // We'll support more platform on user's demand.
            window = renderer.RenderFactory.CreateRenderWindow(new RenderParameters(), Handle);
            // Create 4 viewports, the viewport's area is meanless here because we'll change it to the right area in the SetViewports later
            viewports = new[]
            {
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1)),
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1)),
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1)),
                window.CreateViewport(camera, Color.Gray, RelativeRectangle.FromScale(0, 0, 1, 1))
            };
            SetViewports(1);


            //initialize shader for grid

            GLSLSource src = new GLSLSource();
            src.VertexShader = @"#version 330 core
            layout (location = 0) in vec3 position;
            uniform mat4 matWorldViewProj;
            void main()
            {
                gl_Position = matWorldViewProj * vec4(position, 1.0f);
            }";
                        src.FragmentShader = @"#version 330 core
            out vec4 color;
            void main()
            {
                color = vec4(1, 1, 1, 1);
            }";
            // Define the input format used by GLSL vertex shader the format is struct ControlPoint { FVector3 Position;}
            VertexDeclaration fd = new VertexDeclaration();
            fd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
            // Compile shader from GLSL source code and specify the vertex input format
            gridShader = renderer.RenderFactory.CreateShaderProgram(src, fd);
            // Connect GLSL uniform to renderer's internal variable
            gridShader.Variables = new ShaderVariable[]
            {
                new ShaderVariable("matWorldViewProj", VariableSemantic.MatrixWorldViewProj)
            };

            SceneUpdated("");
        }