示例#1
0
            /// <summary>
            /// Set the vertex attribute.
            /// </summary>
            /// <param name="ctx">
            /// The <see cref="GraphicsContext"/> used for rendering.
            /// </param>
            /// <param name="shaderProgram">
            /// The <see cref="ShaderProgram"/> that is used for rendering.
            /// </param>
            /// <param name="attributeName">
            /// A <see cref="String"/> that specify the attribute which binds to this vertex array.
            /// </param>
            public void SetVertexAttribute(GraphicsContext ctx, ShaderProgram shaderProgram, string attributeName)
            {
                if (attributeName == null)
                {
                    throw new ArgumentNullException("attributeName");
                }

                ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName);

                switch (attributeBinding.Type)
                {
                case ShaderAttributeType.Float:
                    Gl.VertexAttrib1(attributeBinding.Location, (float)DefaultValue.x);
                    break;

                case ShaderAttributeType.Vec2:
                    Gl.VertexAttrib2(attributeBinding.Location, (float)DefaultValue.x, (float)DefaultValue.y);
                    break;

                case ShaderAttributeType.Vec3:
                    Gl.VertexAttrib3(attributeBinding.Location, (float)DefaultValue.x, (float)DefaultValue.y, (float)DefaultValue.z);
                    break;

                case ShaderAttributeType.Vec4:
                    Gl.VertexAttrib4(attributeBinding.Location, (float)DefaultValue.x, (float)DefaultValue.y, (float)DefaultValue.z, (float)DefaultValue.w);
                    break;

                default:
                    throw new NotImplementedException(String.Format("default value for attributes of type {0} not implemented", attributeBinding.Type));
                }
            }
示例#2
0
            /// <summary>
            /// Set the vertex attribute.
            /// </summary>
            /// <param name="ctx">
            /// The <see cref="GraphicsContext"/> used for rendering.
            /// </param>
            /// <param name="shaderProgram">
            /// The <see cref="ShaderProgram"/> that is used for rendering.
            /// </param>
            /// <param name="attributeName">
            /// A <see cref="String"/> that specify the attribute which binds to this vertex array.
            /// </param>
            public void SetVertexAttribute(GraphicsContext ctx, ShaderProgram shaderProgram, string attributeName)
            {
                if (attributeName == null)
                {
                    throw new ArgumentNullException("attributeName");
                }

                if (shaderProgram != null)
                {
                    ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName);

                    // Avoid rendundant buffer binding and relative vertex array setup
                    if (ctx.Extensions.VertexArrayObject_ARB && IsDirty == false)
                    {
                        // CheckVertexAttribute(ctx, attributeBinding.Location);
                        return;
                    }

                    // Enable/Disable shader attribute
                    if (ArrayBuffer != null)
                    {
                        EnableVertexAttribute(ctx, attributeBinding.Location, attributeBinding.Type);
                    }
                    else
                    {
                        DisableVertexAttribute(ctx, attributeBinding.Location);
                    }
                }
                else
                {
                    switch (attributeName)
                    {
                    case VertexArraySemantic.Position:
                        SetPositionAttribute(ctx);
                        break;

                    case VertexArraySemantic.Color:
                        SetColorAttribute(ctx);
                        break;

                    case VertexArraySemantic.Normal:
                        SetNormalAttribute(ctx);
                        break;

                    case VertexArraySemantic.TexCoord:
                        SetTexCoordAttribute(ctx);
                        break;

                    default:
                        throw new NotSupportedException(String.Format("attribute {0} not supported on fixed pipeline", attributeName));
                    }
                }


                // Next time do not set bindings and array state if GL_ARB_vertex_array_object is supported
                IsDirty = false;
            }
示例#3
0
        private void CheckVertexAttributes(GraphicsContext ctx, ShaderProgram shaderProgram)
        {
            // Vertex array state overall check
            foreach (string attributeName in shaderProgram.ActiveAttributes)
            {
                ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName);
                VertexArray shaderVertexArray = GetVertexArray(attributeName, shaderProgram) as VertexArray;
                if (shaderVertexArray == null)
                {
                    continue;
                }

                shaderVertexArray.CheckVertexAttribute(ctx, attributeBinding.Location);
            }
        }
示例#4
0
        /// <summary>
        /// Set the vertex arrays state for the shader program.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> on which the shader program is bound.
        /// </param>
        /// <param name="shaderProgram">
        /// A <see cref="ShaderProgram"/> on which the vertex arrays shall be bound.
        /// </param>
        private void SetVertexArrayState(GraphicsContext ctx, ShaderProgram shaderProgram)
        {
            CheckThisExistence(ctx);

            // Set vertex array state all at once...
            ctx.Bind(this, true);

            if (shaderProgram != null)
            {
                ICollection <string> activeAttributes = shaderProgram.ActiveAttributes;
                uint attributesSet = 0;

                // Set vertex array state
                foreach (string attributeName in activeAttributes)
                {
                    IVertexArray shaderVertexArray = GetVertexArray(attributeName, shaderProgram);
                    if (shaderVertexArray == null)
                    {
                        continue;
                    }

                    ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName);

                    IVertexArray currentVertexArray = _VertexArrayState[attributeBinding.Location];
                    //if (ReferenceEquals(shaderVertexArray, currentVertexArray) == false) {
                    shaderVertexArray.SetVertexAttribute(ctx, attributeBinding, attributeName);
                    _VertexArrayState[attributeBinding.Location] = shaderVertexArray;
                    //}

                    attributesSet++;
                }

                if (attributesSet == 0)
                {
                    throw new InvalidOperationException("no attribute is set");
                }
            }
            else
            {
                IVertexArray attributeArray;

                // No shader program: using fixed pipeline program. ATM enable all attributes having a semantic
                if ((attributeArray = GetVertexArray(VertexArraySemantic.Position)) == null)
                {
                    throw new InvalidOperationException("no position semantic array defined");
                }
                attributeArray.SetVertexAttribute(ctx, null, VertexArraySemantic.Position);

                // Optional attributes
                if ((attributeArray = GetVertexArray(VertexArraySemantic.Color)) != null)
                {
                    attributeArray.SetVertexAttribute(ctx, null, VertexArraySemantic.Color);
                }
                if ((attributeArray = GetVertexArray(VertexArraySemantic.Normal)) != null)
                {
                    attributeArray.SetVertexAttribute(ctx, null, VertexArraySemantic.Normal);
                }
                if ((attributeArray = GetVertexArray(VertexArraySemantic.TexCoord)) != null)
                {
                    attributeArray.SetVertexAttribute(ctx, null, VertexArraySemantic.TexCoord);
                }
            }
        }