Beispiel #1
0
        internal static bool OpenGLVertexAttribNormalized(RVertexElement element)
        {
            // TODO: This may or may not be the right behavor.
            //
            // For instance the VertexElementFormat.Byte4 format is not supposed
            // to be normalized, but this line makes it so.
            //
            // The question is in MS XNA are types normalized based on usage or
            // normalized based to their format?
            //
            if (element.VertexElementUsage == RVertexElementUsage.Color)
            {
                return(true);
            }

            switch (element.VertexElementFormat)
            {
            case RVertexElementFormat.NormalizedShort2:
            case RVertexElementFormat.NormalizedShort4:
                return(true);

            default:
                return(false);
            }
        }
Beispiel #2
0
        static RVertexData2D()
        {
            RVertexElement[] elements = new RVertexElement[]
            {
                new RVertexElement(0, RVertexElementFormat.Vector2, RVertexElementUsage.Position),
                new RVertexElement(8, RVertexElementFormat.Vector2, RVertexElementUsage.TextureCoordinate),
                new RVertexElement(16, RVertexElementFormat.Vector4, RVertexElementUsage.Color)
            };
            RVertexDeclaration declaration = new RVertexDeclaration(elements);

            VertexDeclaration = declaration;
        }
Beispiel #3
0
        static RVertexData()
        {
            RVertexElement[] elements = new RVertexElement[]
            {
                new RVertexElement(0, RVertexElementFormat.Vector3, RVertexElementUsage.Position),
                new RVertexElement(sizeof(float) * (3 * 1), RVertexElementFormat.Vector3, RVertexElementUsage.Normal),
                new RVertexElement(sizeof(float) * (3 * 2), RVertexElementFormat.Vector3, RVertexElementUsage.Bitangent),
                new RVertexElement(sizeof(float) * (3 * 3), RVertexElementFormat.Vector3, RVertexElementUsage.Tangent),
                new RVertexElement(sizeof(float) * (3 * 4), RVertexElementFormat.Vector2, RVertexElementUsage.TextureCoordinate)
            };
            RVertexDeclaration declaration = new RVertexDeclaration(elements);

            VertexDeclaration = declaration;
        }
Beispiel #4
0
        private static int GetVertexStride(RVertexElement[] elements)
        {
            int max = 0;

            for (var i = 0; i < elements.Length; i++)
            {
                var start = elements[i].Offset + RVertexElement.GetSize(elements[i].VertexElementFormat);
                if (max < start)
                {
                    max = start;
                }
            }

            return(max);
        }
Beispiel #5
0
        internal void Apply(RShader shader, IntPtr offset)
        {
            RVertexDeclarationAttributeInfo attrInfo;
            int shaderHash = shader.GetHashCode();

            if (!shaderAttributeInfo.TryGetValue(shaderHash, out attrInfo))
            {
                // Get the vertex attribute info and cache it
                attrInfo = new RVertexDeclarationAttributeInfo(16);

                foreach (var ve in _elements)
                {
                    var attributeLocation = shader.GetAttribLocation(ve.VertexElementUsage);

                    if (attributeLocation >= 0)
                    {
                        attrInfo.Elements.Add(new RVertexDeclarationAttributeInfo.Element()
                        {
                            Offset                  = ve.Offset,
                            AttributeLocation       = attributeLocation,
                            NumberOfElements        = RVertexElement.OpenGLNumberOfElements(ve.VertexElementFormat),
                            VertexAttribPointerType = RVertexElement.OpenGLVertexAttribPointerType(ve.VertexElementFormat),
                            Normalized              = RVertexElement.OpenGLVertexAttribNormalized(ve),
                        });
                        attrInfo.EnabledAttributes[attributeLocation] = true;
                    }
                }

                shaderAttributeInfo.Add(shaderHash, attrInfo);
            }

            // Apply the vertex attribute info
            foreach (var element in attrInfo.Elements)
            {
                GL.EnableVertexAttribArray(element.AttributeLocation);
                REngine.CheckGLError();
                GL.VertexAttribPointer(element.AttributeLocation,
                                       element.NumberOfElements,
                                       element.VertexAttribPointerType,
                                       element.Normalized,
                                       this.VertexStride,
                                       (IntPtr)(offset.ToInt64() + element.Offset));
                REngine.CheckGLError();
            }
            //GraphicsDevice.SetVertexAttributeArray(attrInfo.EnabledAttributes);
        }