Exemplo n.º 1
0
        public void RenderCurrent(int instanceCount)
        {
            IBufferRange indexBufferRange     = Effective.Mesh.IndexBufferRange(Effective.MeshMode);
            ProgramGL1   requested_ProgramGL1 = (ProgramGL1)Requested.Program;
            bool         usePerInstance       = requested_ProgramGL1.FixedFunctionProgram.UsePerInstance;

            Timers.DrawCalls.Begin();
            for (int i = 0; i < instanceCount; ++i)
            {
                SetFrame(i);

                if (usePerInstance == true)
                {
                    Timers.ProgramSwitch.Begin();
                    Effective.Program.Use(i);
                    Timers.ProgramSwitch.End();
                }

                GL.DrawElements(
                    indexBufferRange.BeginMode,
                    (int)indexBufferRange.Count,
                    indexBufferRange.DrawElementsTypeGL,
                    (IntPtr)(indexBufferRange.OffsetBytes)
                    );
            }
            Timers.DrawCalls.End();
        }
Exemplo n.º 2
0
        public QuadRenderer(IRenderer renderer)
        {
            mesh = new Mesh();

            var vertexFormat = new VertexFormat();

            position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            texcoord = vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2));
            color    = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4));
            // Some gfx cards fail to show last vertex right if texcoord is set to have 3 components

            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.StaticDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw);

            mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange();

            indexBufferRange = mesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                indexBuffer,
                BeginMode.Triangles
                );

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);
            indexWriter  = new IndexBufferWriter(indexBufferRange);
        }
Exemplo n.º 3
0
        public NinePatch(NinePatchStyle style)
        {
            this.style = style;

            //mesh = new Mesh.Mesh(BufferUsageHint.DynamicDraw);
            mesh = new RenderStack.Mesh.Mesh();

            VertexFormat vertexFormat = new VertexFormat();

            vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2));

            // \todo Allocate vertex buffers form from UI BufferPool and use double buffered Buffers
            // \todo Share one index buffer among all UI components that have the same index buffer
            //Buffer vertexBuffer = BufferPool.Instance.GetVertexBuffer(vertexFormat, BufferUsageHint.DynamicDraw);
            //Buffer indexBuffer = BufferPool.Instance.GetIndexBuffer(DrawElementsType.UnsignedShort, BufferUsageHint.StaticDraw);
            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedShort, BufferUsageHint.StaticDraw);

            mesh.VertexBufferRange = vertexBuffer.CreateVertexBufferRange();
            IBufferRange indexBufferRange = mesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                indexBuffer,
                BeginMode.Triangles
                );
            var writer = new IndexBufferWriter(indexBufferRange);

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);

            //  12 13 14 15
            //
            //   8  9 10 11
            //
            //   4  5  6  7
            //
            //   0  1  2  3

            writer.BeginEdit();

            writer.Quad(4, 5, 1, 0); writer.CurrentIndex += 6;
            writer.Quad(5, 6, 2, 1); writer.CurrentIndex += 6;
            writer.Quad(6, 7, 3, 2); writer.CurrentIndex += 6;

            writer.Quad(8, 9, 5, 4); writer.CurrentIndex   += 6;
            writer.Quad(9, 10, 6, 5); writer.CurrentIndex  += 6;
            writer.Quad(10, 11, 7, 6); writer.CurrentIndex += 6;

            writer.Quad(12, 13, 9, 8); writer.CurrentIndex   += 6;
            writer.Quad(13, 14, 10, 9); writer.CurrentIndex  += 6;
            writer.Quad(14, 15, 11, 10); writer.CurrentIndex += 6;

            writer.EndEdit();

            // \bug
            //indexBuffer.UpdateAll();
        }
Exemplo n.º 4
0
 public void ExportTo(IBufferRange targetBufferRange)
 {
     if (bufferRange.Match(targetBufferRange) == false)
     {
         throw new InvalidOperationException("Target IBufferRange does not match with BufferWriter IBufferRange");
     }
     if (data != null)
     {
         bufferRange.Touch(CurrentIndex, data);
     }
 }
Exemplo n.º 5
0
        public UniformBufferRL(IUniformBlock uniformBlock)
        {
            data = new UniformBufferData(uniformBlock);

            IBuffer buffer = BufferPool.Instance.GetUniformBufferRL(OpenTK.Graphics.OpenGL.BufferUsageHint.DynamicDraw);

            bufferRange      = buffer.CreateUniformBufferRange(uniformBlock);
            bufferRange.Name = uniformBlock.Name;

            Sync();
        }
Exemplo n.º 6
0
        public void Place(
            float x0,
            float y0,
            float z0,
            float width,
            float height
            )
        {
            //xOffset = x0;
            size = new Vector2(width, height);

            IBufferRange vertexBufferRange = mesh.VertexBufferRange;

            var position = vertexBufferRange.VertexFormat.FindAttribute(VertexUsage.Position, 0);
            var texCoord = vertexBufferRange.VertexFormat.FindAttribute(VertexUsage.TexCoord, 0);

            vertexWriter.BeginEdit();

            float[] b = new float[4];
            b[0] = 0.0f;
            b[1] = style.Border;
            b[2] = 1.0f - style.Border;
            b[3] = 1.0f;

            float[] x = new float[4];
            x[0] = x0;
            x[1] = x0 + style.Border * style.Texture.Size.Width;
            x[2] = x0 + width - style.Border * style.Texture.Size.Width;
            x[3] = x0 + width;

            float[] y = new float[4];
            y[0] = y0;
            y[1] = y0 + style.Border * style.Texture.Size.Height;
            y[2] = y0 + height - style.Border * style.Texture.Size.Height;
            y[3] = y0 + height;

            for (int yi = 0; yi < 4; ++yi)
            {
                for (int xi = 0; xi < 4; ++xi)
                {
                    vertexWriter.Set(position, x[xi], y[yi], z0);
                    vertexWriter.Set(texCoord, b[xi], b[3 - yi]);
                    ++vertexWriter.CurrentIndex;
                }
            }

            vertexWriter.EndEdit();

            //  \todo optimize
            //  \bug
            //vertexBufferRange.Buffer.UpdateAll();
        }
Exemplo n.º 7
0
 public State(State old)
 {
     Camera           = old.Camera;
     Viewport         = old.Viewport;
     Mesh             = old.Mesh;
     Material         = old.Material;
     MeshMode         = old.MeshMode;
     Program          = old.Program;
     VertexBuffer     = old.VertexBuffer;
     IndexBuffer      = old.IndexBuffer;
     IndexBufferRange = old.IndexBufferRange;
     VertexStream     = old.VertexStream;
 }
Exemplo n.º 8
0
        public UniformBufferGL(IUniformBlock uniformBlock)
        {
            //  Required always
            data = new UniformBufferData(uniformBlock);

            //  Required for GL3 and OpenRL only
            if (Configuration.canUseUniformBufferObject && (Configuration.useGl1 == false))
            {
                IBuffer buffer = BufferPool.Instance.GetUniformBufferGL(BufferUsageHint.DynamicDraw);
                bufferRange      = buffer.CreateUniformBufferRange(uniformBlock);
                bufferRange.Name = uniformBlock.Name;
                Sync();
            }
        }
Exemplo n.º 9
0
        public void RenderCurrent()
        {
            UpdateMaterialProgramVertexStream(0);

            IBufferRange indexBufferRange = Effective.Mesh.IndexBufferRange(Effective.MeshMode);

            Timers.DrawCalls.Begin();
            GL.DrawElements(
                indexBufferRange.BeginMode,
                (int)indexBufferRange.Count,
                indexBufferRange.DrawElementsTypeGL,
                (IntPtr)(indexBufferRange.OffsetBytes)
                );
            Timers.DrawCalls.End();
        }
Exemplo n.º 10
0
        public void RenderCurrent(int instanceCount)
        {
            IBufferRange indexBufferRange = Effective.Mesh.IndexBufferRange(Effective.MeshMode);

            using (var t = new TimerScope(timers.DrawCalls))
            {
                GL.DrawElementsInstancedBaseVertex(
                    indexBufferRange.BeginMode,
                    (int)indexBufferRange.Count,
                    indexBufferRange.DrawElementsTypeGL,
                    (IntPtr)(indexBufferRange.OffsetBytes),
                    instanceCount,
                    Effective.Mesh.VertexBufferRange.BaseVertex
                    );
            }
        }
Exemplo n.º 11
0
        public void RenderCurrent()
        {
            using (var t = new TimerScope(timers.MaterialSwitch))
            {
                if (Effective.Material != Requested.Material)
                {
                    Requested.Material.Use();
                    Effective.Material = Requested.Material;
                }
            }

            using (var t = new TimerScope(timers.ProgramSwitch))
            {
                if (Effective.Program != Requested.Program)
                {
                    Requested.Program.Use(0);
                    Effective.Program = Requested.Program;
                }
            }

            using (var t = new TimerScope(timers.AttributeSetup))
            {
                BindAttributesAndCheckForUpdates();
            }

            IBufferRange indexBufferRange = Effective.Mesh.IndexBufferRange(Effective.MeshMode);

            using (var t = new TimerScope(timers.DrawCalls))
            {
                /*
                 * RL.DrawElementsBaseVertex(
                 *  indexBufferRange.BeginMode,
                 *  (int)indexBufferRange.Count,
                 *  indexBufferRange.DrawElementsType,
                 *  (IntPtr)(indexBufferRange.OffsetBytes),
                 *  Effective.Mesh.VertexBufferRange.BaseVertex
                 * );*/
                RL.DrawElements(
                    (Caustic.OpenRL.BeginMode)indexBufferRange.BeginMode,
                    (int)indexBufferRange.Count,
                    (Caustic.OpenRL.DrawElementsType)indexBufferRange.DrawElementsTypeGL,
                    (int)indexBufferRange.OffsetBytes
                    );
            }
        }
Exemplo n.º 12
0
        public bool Match(IBufferRange other)
        {
            //  This ensures that vertex format / index type match
            if (BufferTargetGL != other.BufferTargetGL)
            {
                return(false);
            }
            switch (BufferTargetGL)
            {
            //  Vertex Buffer
            case OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer:
            {
                if (VertexFormat != other.VertexFormat)
                {
                    return(false);
                }
                break;
            }

            //  Index Buffer
            case OpenTK.Graphics.OpenGL.BufferTarget.ElementArrayBuffer:
            {
                if (DrawElementsTypeGL != other.DrawElementsTypeGL)
                {
                    return(false);
                }
                break;
            }

            //  UniformBuffer
            case OpenTK.Graphics.OpenGL.BufferTarget.UniformBuffer:
            {
                if (UniformBlock != null)
                {
                    if (UniformBlock != other.UniformBlock)
                    {
                        return(false);
                    }
                }
                break;
            }
            }
            return(true);
        }
Exemplo n.º 13
0
        private void UseMap(Map map)
        {
            this.map = map;
            position = map.Position;
            texcoord = map.Texcoord;
            color    = map.Color;

            mesh = new Mesh();
            mesh.VertexBufferRange = vertexBufferRange = map.VertexBuffer.CreateVertexBufferRange();

            indexBufferRange = mesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                map.IndexBuffer,
                BeginMode.Triangles
                );

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);
            indexWriter  = new IndexBufferWriter(indexBufferRange);
        }
Exemplo n.º 14
0
        public void RenderCurrent(int instanceCount)
        {
            IBufferRange indexBufferRange = Effective.Mesh.IndexBufferRange(Effective.MeshMode);

            using (var t = new TimerScope(timers.DrawCalls))
            {
                /*GL.DrawElementsInstancedBaseVertex(
                 *  indexBufferRange.BeginMode,
                 *  (int)indexBufferRange.Count,
                 *  indexBufferRange.Buffer.DrawElementsType,
                 *  (IntPtr)(indexBufferRange.OffsetBytes),
                 *  instanceCount,
                 *  Effective.Mesh.VertexBufferRange.BaseVertex
                 * );*/
                RL.DrawElements(
                    (Caustic.OpenRL.BeginMode)indexBufferRange.BeginMode,
                    (int)indexBufferRange.Count,
                    (Caustic.OpenRL.DrawElementsType)indexBufferRange.DrawElementsTypeGL,
                    (int)indexBufferRange.OffsetBytes
                    );
            }
        }
Exemplo n.º 15
0
        protected override void InitializeService()
        {
            if (
                (RenderStack.Graphics.Configuration.canUseGeometryShaders) &&
                (RenderStack.Graphics.Configuration.glslVersion >= 330)
                )
            {
                material = new Material("", renderer.Programs["WideLine"], renderer.MaterialUB);
            }
            else
            {
                material = new Material("", renderer.Programs["ColorFill"], renderer.MaterialUB);
            }

            LineWidth = 1.0f;

            mesh = new Mesh();

            var vertexFormat = new VertexFormat();

            position  = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            edgeColor = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 1, 4));

            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.DynamicDraw);

            mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange();
            indexBufferRange       = mesh.FindOrCreateIndexBufferRange(
                MeshMode.EdgeLines,
                indexBuffer,
                BeginMode.Lines
                );

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);
            indexWriter  = new IndexBufferWriter(indexBufferRange);
        }
Exemplo n.º 16
0
 public VertexBufferWriter(IBufferRange bufferRange)
 {
     BufferWriter = new BufferWriter(bufferRange);
 }
Exemplo n.º 17
0
        public void Sync(IBufferRange bufferRange)
        {
            bool dirty = false;

            foreach (var uniformValue in parameterArray)
            {
                if (
                    (uniformValue == null) ||
                    (uniformValue.Index == -1) ||
                    (uniformValue.Dirty == false)
                    )
                {
                    continue;
                }
                dirty = true;
                var uniform = uniformBlock.Uniforms[uniformValue.Index];

                /*System.Diagnostics.Debug.WriteLine(
                 *  "Sync " + uniform.Type + " " + uniform.Name + " at offset " + uniform.Offset +
                 *  " index " + uniformValue.Index
                 * );*/
                switch (uniform.Type)
                {
                case ActiveUniformType.Float:       bufferRange.Floats(uniform.Offset, (Floats)uniformValue); break;

                case ActiveUniformType.FloatVec2:   bufferRange.Floats(uniform.Offset, (Floats)uniformValue); break;

                case ActiveUniformType.FloatVec3:   bufferRange.Floats(uniform.Offset, (Floats)uniformValue); break;

                case ActiveUniformType.FloatVec4:   bufferRange.Floats(uniform.Offset, (Floats)uniformValue); break;

                case ActiveUniformType.FloatMat4:   bufferRange.Floats(uniform.Offset, (Floats)uniformValue); break;

                case ActiveUniformType.Int:         bufferRange.Ints(uniform.Offset, (Ints)uniformValue); break;

                case ActiveUniformType.UnsignedInt: bufferRange.UInts(uniform.Offset, (UInts)uniformValue); break;

                default: break;
                }
            }

            if (dirty == false)
            {
                return;
            }

            if (bufferRange is BufferRangeGL)
            {
                bufferRange.UpdateGL();
            }
#if false
            else if (bufferRange is BufferRangeRL)
            {
                bufferRange.UpdateRL();
            }
#endif
            else
            {
                throw new System.ArgumentException();
            }
        }
Exemplo n.º 18
0
 public bool Match(IBufferRange other)
 {
     return(bufferRangeGL.Match(other));
 }
Exemplo n.º 19
0
 public IndexBufferWriter(IBufferRange bufferRange)
 {
     BufferWriter = new BufferWriter(bufferRange);
 }
Exemplo n.º 20
0
        private int modelCount;                         //  How many models in scene

        protected override void OnLoad(System.EventArgs e)
        {
            //  Check GL features
            RenderStack.Graphics.Configuration.Initialize();
            if (RenderStack.Graphics.Configuration.glslVersion < 150)
            {
                throw new System.PlatformNotSupportedException("You need at least GL 3.2");
            }

            AttributeMappings.Global.Add(0, "_position", VertexUsage.Position, 0, 3);
            AttributeMappings.Global.Add(1, "_normal", VertexUsage.Normal, 0, 3);

            //  Build uniform blocks
            {
                System.Text.StringBuilder uniforms = new System.Text.StringBuilder();
                CameraUniformBlock.NearFar             = "near_far";
                CameraUniformBlock.FovXFovYAspect      = "fovx_fovy_aspect";
                CameraUniformBlock.ViewToClip          = "view_to_clip_matrix";
                CameraUniformBlock.ClipToView          = "view_to_clip_matrix";
                CameraUniformBlock.WorldToClip         = "world_to_clip_matrix";
                CameraUniformBlock.ClipToWorld         = "clip_to_world_matrix";
                CameraUniformBlock.WorldToView         = "world_to_view_matrix";
                CameraUniformBlock.ViewToWorld         = "view_to_world_matrix";
                CameraUniformBlock.Viewport            = "viewport";
                CameraUniformBlock.ViewPositionInWorld = "view_position_in_world";
                CameraUniformBlock.Initialize("camera");
                uniforms.Append(CameraUniformBlock.UniformBlockGL.SourceGL);

                ModelsUB = new UniformBlockGL("models");
                ModelsUB.AddMat4("model_to_world_matrix", maxInstanceCount);
                ModelsUB.Seal();
                uniforms.Append(ModelsUB.SourceGL);

                GlobalUB = new UniformBlockGL("global");
                GlobalUB.AddVec4("add_color");
                GlobalUB.AddFloat("alpha");
                GlobalUB.Seal();
                uniforms.Append(GlobalUB.SourceGL);

                MaterialUB = new UniformBlockGL("material");
                MaterialUB.AddVec4("surface_diffuse_reflectance_color");
                MaterialUB.AddVec4("surface_specular_reflectance_color");
                MaterialUB.AddFloat("surface_roughness");
                MaterialUB.Seal();
                uniforms.Append(MaterialUB.SourceGL);

                LightsUB = new UniformBlockGL("lights");
                LightsUB.AddVec4("exposure");
                LightsUB.AddVec4("color", lightCount);
                LightsUB.AddVec4("ambient_light_color");
                LightsUB.AddVec4("direction", lightCount);
                LightsUB.AddMat4("world_to_shadow_matrix", lightCount);
                LightsUB.Seal();
                uniforms.Append(LightsUB.SourceGL);

                ShaderGL3.Replace("UNIFORMS;", uniforms.ToString());
            }

            //  Models
            List <Vector2> positions = UniformPoissonDiskSampler.SampleCircle(Vector2.Zero, poissonRadius, poissonMinDistance);

            modelCount = positions.Count;
            System.Diagnostics.Debug.WriteLine("Model count: " + modelCount);
            modelFrame = new Frame[modelCount];
            for (int i = 0; i < modelCount; ++i)
            {
                modelFrame[i] = new Frame();
                modelFrame[i].LocalToParent.Set(
                    Matrix4.CreateTranslation(positions[i].X, positions[i].Y, -40.0f)
                    );
            }

            //  Setup geometry and mesh
            Geometry geometry = new Sphere(radius, 20, 8);

            //Matrix4 translate = Matrix4.CreateTranslation(0.0f, 0.0f, -4.0f);
            //geometry.Transform(translate);
            mesh = new GeometryMesh(geometry, NormalStyle.PointNormals).GetMesh;
            //  \todo Get rid of this
            //mesh.VertexBufferRange.Buffer.VertexFormat.Seal(AttributeMappings.Global);

            //  This is where we collect models transformations
            models = UniformBufferFactory.Create(ModelsUB);

            global = UniformBufferFactory.Create(GlobalUB);
            global.Floats("alpha").Set(1.0f);
            global.Floats("add_color").Set(0.0f, 0.0f, 0.4f);
            global.Sync();

            //  Setup lights
            lights = UniformBufferFactory.Create(LightsUB);
            lights.Floats("exposure").Set(1.0f);
            lights.Floats("ambient_light_color").Set(1.0f, 1.0f, 1.0f);
            for (int i = 0; i < lightCount; ++i)
            {
                lights.Floats("direction").SetI(i, 0.0f, 1.0f, 0.0f);
                lights.Floats("color").SetI(i, 1.0f, 1.0f, 1.0f, 1.0f / (float)lightCount);
            }
            lights.Sync();

            global.Use();
            models.Use();
            lights.Use();

            //  Setup viewport and camera
            viewport = new Viewport(base.Width, base.Height);
            camera   = new Camera();
            camera.Projection.Near = 0.1f;
            camera.Projection.Far  = 300.0f;
            camera.Frame.LocalToParent.Set(
                Matrix4.CreateLookAt(
                    new Vector3(0.0f, 0.0f, 4.0f),      //  camera location
                    new Vector3(0.0f, 0.0f, 0.0f),      //  aim point
                    new Vector3(0.0f, 1.0f, 0.0f)       //  up vector
                    )
                );

            camera.Projection.FovYRadians    = RenderStack.Math.Conversions.DegreesToRadians(60.0f);
            camera.Projection.ProjectionType = ProjectionType.PerspectiveVertical;
            camera.UniformBufferGL.Use();

            //  Setup shader program
            program = (IProgram) new ProgramGL3(vs, fs);

            //  Setup material
            for (int i = 0; i < Materials.Length; ++i)
            {
                var   material = UniformBufferFactory.Create(MaterialUB);
                float h = (float)(i) / (float)(Materials.Length - 1);
                float r, g, b;
                Conversions.HSVtoRGB(360.0f * h, 1.0f, 1.0f, out r, out g, out b);
                float diffuse   = 0.2f;
                float specular  = 0.8f;
                float roughness = 0.1f;
                material.Floats("surface_diffuse_reflectance_color").Set(diffuse * r, diffuse * g, diffuse * b);
                material.Floats("surface_specular_reflectance_color").Set(specular * r, specular * r, specular * r);
                material.Floats("surface_roughness").Set(roughness);
                material.Sync();
                Materials[i] = material;
            }

            //  Update scene. There is no animation in this scene
            //  so we only need to do it once.
            camera.Frame.UpdateHierarchical(serial);
            foreach (var frame in modelFrame)
            {
                frame.UpdateHierarchical(serial);
            }

            //  Setup initial GL state
            {
                //  Since we only use one program, we don't need to touch this after.
                camera.UpdateFrame();
                camera.UpdateViewport(viewport);
                camera.UniformBufferGL.Sync();

                //  Bind program
                program.Use(0);

                GL.Enable(EnableCap.DepthTest);
                GL.Enable(EnableCap.CullFace);

                float gammaHalf = (float)Math.Pow(0.5, 1.0 / 2.2);
                GL.ClearColor(gammaHalf, gammaHalf, gammaHalf, 1.0f);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                SwapBuffers();

                Visible = true;

                //  Since we only use one mesh, we don't need to change this after
                SetupAttributeBindings();

                //  The mesh has not yet been uploaded to GL, do it now
                IBufferRange vertexBufferRange = mesh.VertexBufferRange;
                IBufferRange indexBufferRange  = mesh.IndexBufferRange(MeshMode.PolygonFill);
                if (vertexBufferRange.NeedsUploadGL)
                {
                    vertexBufferRange.UpdateGL();
                }
                if (indexBufferRange.NeedsUploadGL)
                {
                    indexBufferRange.UpdateGL();
                }
            }

            Resize += new EventHandler <EventArgs>(Application_Resize);
            Unload += new EventHandler <EventArgs>(Application_Unload);
        }
Exemplo n.º 21
0
 public BufferWriter(IBufferRange bufferRange)
 {
     this.bufferRange = bufferRange;
 }
Exemplo n.º 22
0
 private bool Match(IBufferRange targetBufferRange)
 {
     return(bufferRange.Match(targetBufferRange));
 }
Exemplo n.º 23
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            ++serial;
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            float z = 20.0f * (float)(System.Math.Sin(2.0f * Now));

            camera.Frame.LocalToParent.Set(
                Matrix4.CreateLookAt(
                    new Vector3(0.0f, 0.0f, z),           //  camera location
                    new Vector3(0.0f, 0.0f, -100.0f),     //  aim point
                    new Vector3(0.0f, 1.0f, 0.0f)         //  up vector
                    )
                );
            camera.Frame.UpdateHierarchical(serial);
            camera.UpdateFrame();
            camera.UpdateViewport(viewport);
            camera.UniformBufferGL.Sync();

            int          baseVertex       = mesh.VertexBufferRange.BaseVertex;
            IBufferRange indexBufferRange = mesh.IndexBufferRange(MeshMode.PolygonFill);

            if (RenderStack.Graphics.Configuration.canUseInstancing)
            {
                //  Instancing code path
                int i = 0;  //  index to models uniform block
                int j = 0;  //  index to material (changed for each draw call)
                for (int mi = 0; mi < modelCount; ++mi)
                {
                    models.Floats("model_to_world_matrix").SetI(
                        i, modelFrame[mi].LocalToWorld.Matrix
                        );
                    i++;
                    if (
                        (i == maxInstanceCount) ||
                        (mi == modelCount - 1)
                        )
                    {
                        models.Sync();
                        Materials[j].Use();
                        GL.DrawElementsInstanced(
                            indexBufferRange.BeginMode,
                            (int)(indexBufferRange.Count),
                            indexBufferRange.DrawElementsTypeGL,
                            (IntPtr)0,
                            i
                            );
                        i = 0;
                        ++j;
                        if (j == Materials.Length)
                        {
                            j = 0;
                        }
                    }
                }
            }
            else
            {
                int j = 0;  //  index to material (changed for each draw call)
                for (int i = 0; i < modelCount; ++i)
                {
                    Materials[j].Use();
                    models.Floats("model_to_world_matrix").SetI(
                        0, modelFrame[i].LocalToWorld.Matrix
                        );
                    models.Sync();
                    GL.DrawElements(
                        indexBufferRange.BeginMode,
                        (int)(indexBufferRange.Count),
                        indexBufferRange.DrawElementsTypeGL,
                        (IntPtr)0
                        );
                    ++j;
                    if (j == Materials.Length)
                    {
                        j = 0;
                    }
                }
            }

            SwapBuffers();
        }
Exemplo n.º 24
0
        public Primitive(Model model, IProgram program)
        {
            Model   = model;
            Program = program;

            RL.GenPrimitives(1, out PrimitiveHandle);
            RL.BindPrimitive(PrimitiveTarget.Primitive, PrimitiveHandle);

            RL.PrimitiveParameterString(PrimitiveTarget.Primitive, PrimitiveStringParameter.PrimitiveName, model.Name);

            model.Frame.UpdateHierarchicalNoCache();
            float[] matrix = model.Frame.LocalToWorld.Matrix.RLMatrix();
            RL.PrimitiveParameterMatrix(
                PrimitiveTarget.Primitive,
                PrimitiveParameter_m.PrimitiveTransformMatrix,
                matrix
                );

            RL.FrontFace(FrontFace);
            Program.Use(0);

            //RL.GetPrimitiveParameteri(PrimitiveTarget.Primitive, PrimitiveParameter_i.PrimitiveAnimationHint,
            IsVisible        = true;
            IsOccluder       = false;
            IsStatic         = true;
            DynamicVertices  = false;
            DynamicTransform = false;

            Mesh = model.Batch.Mesh;
            this.vertexBufferRange = Mesh.VertexBufferRange;
            VertexStream           = vertexBufferRange.VertexStreamRL(Program);
            this.indexBufferRange  = Mesh.IndexBufferRange(MeshMode.PolygonFill);
            indexBufferRange.Name  = Model.Name + " OpenRL IBO";
            vertexBufferRange.Name = Model.Name + " OpenRL VBO";

            /*System.Console.WriteLine(
             *  "-----------------------------------------------------------------------------\n" +
             *  "Model " + Model.Name + " primitive: " + PrimitiveHandle
             * );*/
            vertexBufferRange.BufferRL.UseRL();
            if (vertexBufferRange.NeedsUploadRL)
            {
                vertexBufferRange.UpdateRL();
            }
            indexBufferRange.BufferRL.UseRL();
            if (indexBufferRange.NeedsUploadRL)
            {
                indexBufferRange.UpdateRL();
            }
            VertexStream.SetupAttributePointers();

            /*System.Diagnostics.Debug.WriteLine(
             *  "OpenRL " + Model.Name + " primitive: " + PrimitiveHandle +
             *  " VBO: " + Mesh.VertexBufferRange.BufferRL.BufferObject.ToString("X") +
             *  " IBO: " + indexBufferRange.BufferRL.BufferObject.ToString("X") +
             *  " offset: " + indexBufferRange.OffsetBytes +
             *  " count: " + indexBufferRange.Count
             * );*/
            RL.DrawElements(
                (Caustic.OpenRL.BeginMode)indexBufferRange.BeginMode,
                (int)indexBufferRange.Count,
                (Caustic.OpenRL.DrawElementsType)indexBufferRange.DrawElementsTypeGL,
                (int)indexBufferRange.OffsetBytes
                );
        }