Exemplo n.º 1
0
        /// <summary>
        /// Adds the specified vertex buffer.
        /// </summary>
        /// <param name="vertexBuffer">The vertex buffer.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Add(IVertexBuffer vertexBuffer)
        {
            if (vertexBuffer?.Layout == null)
            {
                throw new ArgumentNullException();
            }

            Bind();
            vertexBuffer.Bind();

            uint index = 0;

            foreach (var element in vertexBuffer.Layout.Elements)
            {
                Gl.EnableVertexAttribArray(index);
                Gl.VertexAttribPointer(
                    index,
                    element.GetCount(),
                    ToOpenGLDataType(element.Type),
                    element.Normalized,
                    vertexBuffer.Layout.Stride,
                    new IntPtr(element.Offset)
                    );

                index++;
            }

            _vertexBuffers.Add(vertexBuffer);
        }
Exemplo n.º 2
0
 public void DrawBatch <T>(IVertexBuffer <T> vertices,
                           int firstVertex, int numVertices, PrimitiveType type)
     where T : struct
 {
     vertices.Bind();
     Device.DrawPrimitives(type, firstVertex, numVertices);
     PerfHistory.Increment("batches", 1);
 }
Exemplo n.º 3
0
        public static void BeginDrawColor(IVertexBuffer vertexBuffer, Matrix4 transform, Vector4 color)
        {
            ColorShader.Use();
            ColorShader.ModelMatrix.Set(transform);
            ColorShader.Color.Set(color);

            vertexBuffer.Bind();
            vertexBuffer.BindAttribute(ColorShader.Position, 0);
        }
Exemplo n.º 4
0
        public static void BeginDrawWireframe2(IVertexBuffer vertexBuffer, Matrix4 transform, float thickness, Vector4 color)
        {
            WireframeShader2.Use();
            WireframeShader2.Color.Set(color);
            WireframeShader2.ModelMatrix.Set(transform);
            WireframeShader2.Size.Set(thickness);

            vertexBuffer.Bind();
            vertexBuffer.BindAttribute(WireframeShader2.Position, 0);
        }
Exemplo n.º 5
0
        public static void BeginDrawModel(IVertexBuffer vertexBuffer, Matrix4 transform, MaterialInfo material)
        {
            ModelShader.Use();
            ModelShader.ModelMatrix.Set(transform);
            ModelShader.Material.Set(material);

            vertexBuffer.Bind();
            vertexBuffer.BindAttribute(ModelShader.Position, 0);
            vertexBuffer.BindAttribute(ModelShader.Normal, 12);
            vertexBuffer.BindAttribute(ModelShader.TexCoord, 24);
        }
Exemplo n.º 6
0
        public static void BeginDrawWireframe(IVertexBuffer vertexBuffer, Matrix4 transform, float thickness, Vector4 color)
        {
            WireframeShader.Use();
            WireframeShader.Color.Set(color);
            WireframeShader.ModelMatrix.Set(transform);

            GL.PushAttrib(AttribMask.LineBit);
            GL.LineWidth(thickness);

            vertexBuffer.Bind();
            vertexBuffer.BindAttribute(WireframeShader.Position, 0);
            vertexBuffer.BindAttribute(WireframeShader.Normal, 12);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Sets up a new vertex array object if needing to be updated.
        /// </summary>
        private void UpdateArray()
        {
            if (m_dirty && m_program != null && m_vertexBuffer != null)
            {
                // destroy the old vertex array
                if (m_vertexArrayGenerated)
                {
                    GL.DeleteVertexArray(this);
                    m_vertexArrayGenerated = false;
                }

                // create a new vertex array
                m_handle = GL.GenVertexArray();
                m_vertexArrayGenerated = true;

                // bind the new array
                GL.BindVertexArray(this);

                // set the source vertex and index buffers
                m_vertexBuffer.Bind();
                if (m_indexBuffer != null)
                {
                    m_indexBuffer.Bind();
                }

                // set the vertex attributes
                m_program.SetVertexAttributes(m_vertexBuffer.VertexAttributes);

                // unbind all the buffers
                GL.BindVertexArray(0);
                m_vertexBuffer.Unbind();
                if (m_indexBuffer != null)
                {
                    m_indexBuffer.Unbind();
                }

                m_dirty = false;
            }
        }
Exemplo n.º 8
0
        public void AddVertexBuffer(IVertexBuffer vertexBuffer)
        {
            Debug.Assert(vertexBuffer.GetLayout().Elements.Length != 0, "Vertex buffer has no layout!");

            Gl.BindVertexArray(rendererID);
            vertexBuffer.Bind();

            uint index = 0;

            foreach (BufferElement element in vertexBuffer.GetLayout().Elements)
            {
                Gl.EnableVertexAttribArray(index);
                Gl.VertexAttribPointer(index,
                                       (int)element.GetComponentCount(),
                                       ShaderDataTypeToOpenGLBaseType(element.Type),
                                       element.Normalized,
                                       (int)vertexBuffer.GetLayout().Stride,
                                       (IntPtr)element.Offset);
                index++;
            }

            vertexBuffers.Add(vertexBuffer);
        }
Exemplo n.º 9
0
        public void Draw(Matrix4 model, Matrix4 view, Matrix4 projection)
        {
            if (geometry != null)
            {
                if (geometry.NeedsUpdate)
                {
                    geometry.Upload(vertexBuffer, indexBuffer);
                }

                if (DepthTestFlag)
                {
                    GL.Enable(EnableCap.DepthTest);
                }
                else
                {
                    GL.Disable(EnableCap.DepthTest);
                }
                if (CullFaceFlag)
                {
                    GL.Enable(EnableCap.CullFace);
                    GL.CullFace(CullFaceMode);
                }
                else
                {
                    GL.Disable(EnableCap.CullFace);
                }
                if (BlendingFlag)
                {
                    GL.Enable(EnableCap.Blend);
                    GL.BlendFuncSeparate(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha, BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
                    GL.BlendEquationSeparate(BlendEquationMode.FuncAdd, BlendEquationMode.FuncAdd);
                }
                else
                {
                    GL.Disable(EnableCap.Blend);
                }
                if (shader != null)
                {
                    shader.Use();

                    if (texture != null)
                    {
                        texture.Bind();
                        shader.SetSamplerUniform(0, 0);
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, 0);
                    }

                    // Set up uniforms:
                    Matrix4 mv   = model * view;
                    Matrix3 mvIT = new Matrix3(mv);
                    mvIT.Invert();
                    mvIT.Transpose();
                    shader.SetUniformMatrix3("mvIT", mvIT);
                    shader.SetUniformMatrix4("modelView", mv);
                    shader.SetUniformMatrix4("projection", projection);
                    shader.SetUniformMatrix4("model", model);
                    shader.SetUniformMatrix4("view", view);

                    foreach (var uniform in uniforms)
                    {
                        uniform.SetUniform(shader);
                    }
                }
                vertexBuffer.Bind(shader);

                if (indexBuffer != null)
                {
                    indexBuffer.Bind();
                    GL.DrawElements(geometry.PrimitiveType, indexBuffer.Size(), DrawElementsType.UnsignedInt, 0);
                }
                else
                {
                    GL.DrawArrays(geometry.PrimitiveType, 0, vertexBuffer.Size);
                }

                GL.BindVertexArray(0);
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            }
        }
Exemplo n.º 10
0
 public void DrawBatch <T>(IVertexBuffer <T> vertices, int firstVertex, int numVertices, PrimitiveType type) where T : struct
 {
     vertices.Bind();
     Device.DrawPrimitives(type, firstVertex, numVertices);
 }