Exemplo n.º 1
0
        public void GenerateDataBuffer(SpriteVOrigin vOrigin, SpriteHOrigin hOrigin)
        {
            //If there is a texture loaded and clips to make vertex data from
            if( TextureID != 0 && mClips.Count > 0 ) {
                //Allocate vertex buffer data
                int totalSprites = mClips.Count;
                VertexData[] vertexData = new VertexData[ totalSprites * 4 ];
                mIndexBuffers = new int[ totalSprites ];

                //Allocate vertex data buffer name
                GL.GenBuffers( 1, out mVertexDataBuffer );
                //Allocate index buffers names
                GL.GenBuffers( totalSprites, mIndexBuffers );
                //Go through clips
                float tW = ImageWidth;
                float tH = ImageHeight;
                int[] spriteIndices = new int[ 4 ] { 0, 0, 0, 0 };

                //Origin variables
                float vTop = 0;
                float vBottom = 0;
                float vLeft = 0;
                float vRight = 0;

                for( int i = 0; i < totalSprites; ++i ) {
                    //Initialize indices
                    spriteIndices[ 0 ] = i * 4 + 0;
                    spriteIndices[ 1 ] = i * 4 + 1;
                    spriteIndices[ 2 ] = i * 4 + 2;
                    spriteIndices[ 3 ] = i * 4 + 3;

                    //Set origin (vertical)
                    switch (vOrigin) {
                        case SpriteVOrigin.Top: {
                            vTop = 0;
                            vBottom = mClips[ i ].Height;
                        }
                            break;
                        case SpriteVOrigin.Bottom: {
                            vTop = -mClips[ i ].Height;
                            vBottom = 0;
                        }
                            break;
                        default: {
                            vTop = (float)-mClips[ i ].Height / 2;
                            vBottom = (float)mClips[ i ].Height / 2;
                        }
                            break;
                    }
                    //Set origin (horizontal)
                    switch (hOrigin) {
                        case SpriteHOrigin.Left: {
                            vLeft = 0;
                            vRight = mClips[ i ].Width;
                        }
                            break;
                        case SpriteHOrigin.Right: {
                            vLeft = -mClips[ i ].Width;
                            vRight = 0;
                        }
                            break;
                        default: {
                            vLeft = (float)-mClips[ i ].Width / 2;
                            vRight = (float)mClips[ i ].Width / 2;
                        }
                            break;
                    }

                    //Top left
                    vertexData[spriteIndices[0]] = new VertexData(vLeft, vTop, ((float)mClips[i].Left) / tW, ((float)mClips[i].Top) / tH);
                    //Top right
                    vertexData[spriteIndices[1]] = new VertexData(vRight, vTop, ((float)mClips[i].Right) / tW, ((float)mClips[i].Top) / tH);
                    //Bottom right
                    vertexData[spriteIndices[2]] = new VertexData(vRight, vBottom, ((float)mClips[i].Right) / tW, ((float)mClips[i].Bottom) / tH);
                    //Bottom left
                    vertexData[spriteIndices[3]] = new VertexData(vLeft, vBottom, ((float)mClips[i].Left) / tW, ((float)mClips[i].Bottom) / tH);

                    //Bind sprite index buffer data
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIndexBuffers[ i ] );
                    GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(4 * sizeof(int)), spriteIndices, BufferUsageHint.StaticDraw);

                }
                //Bind vertex data
                GL.BindBuffer(BufferTarget.ArrayBuffer, mVertexDataBuffer );
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(totalSprites * 4 * VertexData.SizeInBytes), vertexData, BufferUsageHint.StaticDraw);

            } else {
                 if( TextureID == 0 ) {
                     throw new Exception( "No texture to render with!" );
                 }
                 if( mClips.Count <= 0 ) {
                     throw new Exception( "No clips to generate vertex data from!" );
                 }
            }
        }
Exemplo n.º 2
0
        public void GenerateDataBuffer(SpriteVOrigin vOrigin, SpriteHOrigin hOrigin)
        {
            //If there is a texture loaded and clips to make vertex data from
            if (TextureID != 0 && mClips.Count > 0)
            {
                //Allocate vertex buffer data
                int          totalSprites = mClips.Count;
                VertexData[] vertexData   = new VertexData[totalSprites * 4];
                mIndexBuffers = new int[totalSprites];

                //Allocate vertex data buffer name
                GL.GenBuffers(1, out mVertexDataBuffer);
                //Allocate index buffers names
                GL.GenBuffers(totalSprites, mIndexBuffers);
                //Go through clips
                float tW            = ImageWidth;
                float tH            = ImageHeight;
                int[] spriteIndices = new int[4] {
                    0, 0, 0, 0
                };

                //Origin variables
                float vTop    = 0;
                float vBottom = 0;
                float vLeft   = 0;
                float vRight  = 0;

                for (int i = 0; i < totalSprites; ++i)
                {
                    //Initialize indices
                    spriteIndices[0] = i * 4 + 0;
                    spriteIndices[1] = i * 4 + 1;
                    spriteIndices[2] = i * 4 + 2;
                    spriteIndices[3] = i * 4 + 3;

                    //Set origin (vertical)
                    switch (vOrigin)
                    {
                    case SpriteVOrigin.Top:
                    {
                        vTop    = 0;
                        vBottom = mClips[i].Height;
                    }
                    break;

                    case SpriteVOrigin.Bottom:
                    {
                        vTop    = -mClips[i].Height;
                        vBottom = 0;
                    }
                    break;

                    default:
                    {
                        vTop    = (float)-mClips[i].Height / 2;
                        vBottom = (float)mClips[i].Height / 2;
                    }
                    break;
                    }
                    //Set origin (horizontal)
                    switch (hOrigin)
                    {
                    case SpriteHOrigin.Left:
                    {
                        vLeft  = 0;
                        vRight = mClips[i].Width;
                    }
                    break;

                    case SpriteHOrigin.Right:
                    {
                        vLeft  = -mClips[i].Width;
                        vRight = 0;
                    }
                    break;

                    default:
                    {
                        vLeft  = (float)-mClips[i].Width / 2;
                        vRight = (float)mClips[i].Width / 2;
                    }
                    break;
                    }

                    //Top left
                    vertexData[spriteIndices[0]] = new VertexData(vLeft, vTop, mClips[i].Left / tW, mClips[i].Top / tH);
                    //Top right
                    vertexData[spriteIndices[1]] = new VertexData(vRight, vTop, mClips[i].Right / tW, mClips[i].Top / tH);
                    //Bottom right
                    vertexData[spriteIndices[2]] = new VertexData(vRight, vBottom, mClips[i].Right / tW, mClips[i].Bottom / tH);
                    //Bottom left
                    vertexData[spriteIndices[3]] = new VertexData(vLeft, vBottom, mClips[i].Left / tW, mClips[i].Bottom / tH);

                    //Bind sprite index buffer data
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIndexBuffers[i]);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(4 * sizeof(int)), spriteIndices, BufferUsageHint.StaticDraw);
                }
                //Bind vertex data
                GL.BindBuffer(BufferTarget.ArrayBuffer, mVertexDataBuffer);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(totalSprites * 4 * VertexData.SizeInBytes), vertexData, BufferUsageHint.StaticDraw);
            }
            else
            {
                if (TextureID == 0)
                {
                    throw new Exception("No texture to render with!");
                }
                if (mClips.Count <= 0)
                {
                    throw new Exception("No clips to generate vertex data from!");
                }
            }
        }