Пример #1
0
        public static void Run()
        {
            using (var app = /*new VulkanApplication() */ new OpenGlApplication())
            {
                var win = app.CreateSimpleRenderWindow(samples: 8);

                var cone                = IndexedGeometryPrimitives.Cone.solidCone(V3d.OOO, V3d.OOI, 1.0, 0.2, 48, C4b.Red).ToSg();                            // build object from indexgeometry primitives
                var cube                = SgPrimitives.Sg.box(AValModule.constant(C4b.Blue), AValModule.constant(Box3d.FromCenterAndSize(V3d.Zero, V3d.III))); // or directly using scene graph
                var initialViewTrafo    = CameraView.LookAt(V3d.III * 3.0, V3d.OOO, V3d.OOI);
                var controlledViewTrafo = Aardvark.Application.DefaultCameraController.control(win.Mouse, win.Keyboard,
                                                                                               win.Time, initialViewTrafo);
                var frustum = win.Sizes.Map(size => FrustumModule.perspective(60.0, 0.1, 10.0, size.X / (float)size.Y));

                var whiteShader = Aardvark.Rendering.Effects.SimpleLighting.Effect;
                var trafo       = Effects.Trafo.Effect;

                var currentAngle = 0.0;
                var angle        = win.Time.Map(t =>
                {
                    return(currentAngle += 0.001);
                });
                var rotatingTrafo = angle.Map(a => Trafo3d.RotationZ(a));

                var sg =
                    new[] {
                    cone.Trafo(AValModule.constant(Trafo3d.Translation(1.0, 1.0, 0.0))),
                    cube.Trafo(rotatingTrafo)
                }
                .ToSg()
                .WithEffects(new[] { trafo, whiteShader })
                .ViewTrafo(controlledViewTrafo.Map(c => c.ViewTrafo))
                .ProjTrafo(frustum.Map(f => f.ProjTrafo()));

                win.RenderTask =
                    Aardvark.Rendering.RenderTask.ofArray(
                        new[] {
                    app.Runtime.CompileClear(win.FramebufferSignature, AValModule.constant(C4f.Gray10)),
                    app.Runtime.CompileRender(win.FramebufferSignature, sg)
                }
                        );

                win.Run();
            }
        }
Пример #2
0
        public static ISg WithVertexAttribute(this ISg sg, string semantic, Array data)
        {
            var bufferView = new BufferView(AValModule.constant((IBuffer) new ArrayBuffer(data)), data.GetType().GetElementType());

            return(new Sg.VertexAttributeApplicator(Symbol.Create(semantic), bufferView, AValModule.constant(sg)));
        }
Пример #3
0
        public static void Run()
        {
            using (var app = /*new VulkanApplication() */ new OpenGlApplication())
            {
                var win = app.CreateSimpleRenderWindow(samples: 8);

                // create CPU side array
                var indices = new int[] { 0, 1, 2, 0, 2, 3 };
                // wrap it into cpu buffer. ArrayBuffer is a CPU buffer (which will be uploaded on demand),
                // In contrast, BackendBuffer would be a buffer prepared for a specific backend.
                // both implement the IBuffer interface.
                var indexBuffer = (IBuffer) new ArrayBuffer(indices);

                // same applies for vertex data. Here we do not explicitly create an ArrayBuffer since
                // we use convinience functions which internally create the ArrayBuffer for us
                var vertices = new V3f[] {
                    new V3f(-1, -1, 0), new V3f(1, -1, 0), new V3f(1, 1, 0), new V3f(-1, 1, 0)
                };
                var colors = new C4b[] {
                    C4b.Green, C4b.Red, C4b.Blue, C4b.White
                };

                // In this low level API, we manually construct a drawCallInfo which essentially map
                // to the arguments of glDrawElements etc.
                var drawCallInfo = new DrawCallInfo()
                {
                    FaceVertexCount = 6,
                    InstanceCount   = 1, // DrawCallInfo is a struct and is initialized with zeros. make sure to set instanceCount to 1
                    FirstIndex      = 0,
                };

                // next we create a scene graph node which describes a simple scene which, when rendered
                // uses the supplied drawCallInfo to render geometry of type TriangleList (in constrast to points, linestrip etc)
                var drawNode = new Sg.RenderNode(drawCallInfo, IndexedGeometryMode.TriangleList);
                // the main principle is to use scene graph nodes as small building blocks to build together the
                // complete scene description - a bit like lego ;)
                // the same applies for applying geometry data. just like any other attribute (e.g. model trafos),
                // vertex data can be inherited along the edges in the scene graph. thus the scene graph would look like this
                //             VertexIndexApplicator   (applies index buffer to sub graph)
                //                 ^
                //                 |
                //              drawNode               (performs draw call using attributes inherited along scene graph edges)
                var sceneWithIndexBuffer =
                    new Sg.VertexIndexApplicator(
                        new BufferView(AValModule.constant(indexBuffer), typeof(int)),
                        drawNode
                        );

                // of course constructing scene graph nodes manually is tedious. therefore we use
                // convinience extension functions which can be chaned together, each
                // wrapping a node around the previously constructed scene graph
                var scene =
                    sceneWithIndexBuffer
                    .WithVertexAttribute("Positions", vertices)
                    // there are a lot such extension functions defined to conviniently work with scene graphs
                    .VertexAttribute(DefaultSemantic.Colors, colors)
                    // next, we apply the shaders (this way, the shader becomes the root node -> all children now use
                    // this so called effect (a pipeline shader which combines all shader stages into one object)
                    .WithEffects(new[] { Aardvark.Rendering.Effects.VertexColor.Effect });

                // next we use the aardvark scene graph compiler to construct a so called render task,
                // an optimized representation of the scene graph.
                var renderTask = app.Runtime.CompileRender(win.FramebufferSignature, scene);

                // next, we assign the rendertask to our render window.
                win.RenderTask = renderTask;

                win.Run();
            }
        }