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!"); } } }
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!" ); } } }
public virtual void GenerateDataBuffer(int tileWidth, int tileHeight) { //If there is a texture loaded and clips to make vertex data from int maxX = ImageWidth / tileWidth; int maxY = ImageHeight / tileHeight; if (TextureID != 0 && maxX > 0 && maxY > 0) { MaxX = maxX; MaxY = maxY; TileWidth = tileWidth; TileHeight = tileHeight; //Allocate vertex buffer data int totalSprites = MaxX * MaxY; VertexData[] vertexData = new VertexData[4 * totalSprites]; 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 = TileHeight; float vLeft = 0; float vRight = TileWidth; for (int y = 0; y < MaxY; y++) { for (int x = 0; x < MaxX; x++) { //Initialize indices spriteIndices[0] = 4 * (y * MaxX + x); spriteIndices[1] = 4 * (y * MaxX + x) + 1; spriteIndices[2] = 4 * (y * MaxX + x) + 2; spriteIndices[3] = 4 * (y * MaxX + x) + 3; float clipTop = y * TileHeight; float clipBottom = (y + 1) * TileHeight; float clipLeft = x * TileWidth; float clipRight = (x + 1) * TileWidth; //Top left vertexData[spriteIndices[0]] = new VertexData(vLeft, vTop, clipLeft / tW, clipTop / tH); //Top right vertexData[spriteIndices[1]] = new VertexData(vRight, vTop, clipRight / tW, clipTop / tH); //Bottom right vertexData[spriteIndices[2]] = new VertexData(vRight, vBottom, clipRight / tW, clipBottom / tH); //Bottom left vertexData[spriteIndices[3]] = new VertexData(vLeft, vBottom, clipLeft / tW, clipBottom / tH); //Bind sprite index buffer data GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIndexBuffers[y * MaxX + x]); 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 (!(maxX > 0 && maxY > 0)) { throw new Exception("No tile possible!"); } } }
public void Render(Rectangle?rect) { if (mTextureID != 0) { //Texture coordinates float texTop = 0; float texBottom = 1; float texLeft = 0; float texRight = 1; //Vertex coordinates float quadWidth = ImageWidth; float quadHeight = ImageHeight; //Handle clipping if (rect != null) { Rectangle clip = rect.Value; //Texture coordinates texLeft = (float)clip.Left / ImageWidth; texRight = (float)clip.Right / ImageWidth; texTop = (float)clip.Top / ImageHeight; texBottom = (float)clip.Bottom / ImageHeight; //Vertex coordinates quadWidth = (float)clip.Width; quadHeight = (float)clip.Height; } //Set vertex data VertexData[] vData = new VertexData[4] { new VertexData(0, 0, texLeft, texTop), new VertexData(quadWidth, 0, texRight, texTop), new VertexData(quadWidth, quadHeight, texRight, texBottom), new VertexData(0, quadHeight, texLeft, texBottom) }; //Set texture ID GL.BindTexture(TextureTarget.Texture2D, mTextureID); //Enable vertex and texture coordinate arrays mTextureProgram.EnableVertexPointer(); mTextureProgram.EnableTexCoordPointer(); //Bind vertex buffer GL.BindBuffer(BufferTarget.ArrayBuffer, mVBOID); //Update vertex buffer data GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (IntPtr)(4 * VertexData.SizeInBytes), vData); //Set texture coordinate data mTextureProgram.SetTexCoordPointer(VertexData.SizeInBytes, VertexData.TexCoordOffset); //Set vertex data mTextureProgram.SetVertexPointer(VertexData.SizeInBytes, VertexData.PositionOffset); //bind indexes again GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIBOID); //Draw quad using vertex data and index data GL.DrawElements(BeginMode.Quads, 4, DrawElementsType.UnsignedInt, 0); //Disable vertex and texture coordinate arrays mTextureProgram.DisableVertexPointer(); mTextureProgram.DisableTexCoordPointer(); } }
private void initVBO() { //If texture is loaded and VBO does not already exist if( mTextureID != 0 && mVBOID == 0 ) { //Vertex data VertexData[] vData = new VertexData[4]; //LVertexData2D vData[ 4 ]; //Set rendering indices int[] iData = new int[ 4 ] { 0, 1, 2, 3 }; //Create VBO GL.GenBuffers( 1, out mVBOID ); GL.BindBuffer(BufferTarget.ArrayBuffer, mVBOID ); //send vertex buffer data to GPU; data in this address space can be changed GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(4 * VertexData.SizeInBytes), vData, BufferUsageHint.DynamicDraw); //Create IBO GL.GenBuffers( 1, out mIBOID ); GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIBOID ); //send index buffer data to GPU; data in this address space can be changed GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(4 * sizeof(int)), iData, BufferUsageHint.DynamicDraw); //Unbind buffers GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } }
public void Render(Rectangle ? rect) { if( mTextureID != 0 ) { //Texture coordinates float texTop = 0; float texBottom = 1; float texLeft = 0; float texRight = 1; //Vertex coordinates float quadWidth = ImageWidth; float quadHeight = ImageHeight; //Handle clipping if( rect != null ) { Rectangle clip = rect.Value; //Texture coordinates texLeft = (float)clip.Left / ImageWidth; texRight = (float)clip.Right / ImageWidth; texTop = (float)clip.Top / ImageHeight; texBottom = (float)clip.Bottom / ImageHeight; //Vertex coordinates quadWidth = (float)clip.Width; quadHeight = (float)clip.Height; } //Set vertex data VertexData[] vData = new VertexData[4] { new VertexData(0,0,texLeft,texTop), new VertexData(quadWidth,0,texRight,texTop), new VertexData(quadWidth,quadHeight,texRight,texBottom), new VertexData(0,quadHeight,texLeft,texBottom) }; //Set texture ID GL.BindTexture(TextureTarget.Texture2D, mTextureID ); //Enable vertex and texture coordinate arrays mTextureProgram.EnableVertexPointer(); mTextureProgram.EnableTexCoordPointer(); //Bind vertex buffer GL.BindBuffer(BufferTarget.ArrayBuffer, mVBOID ); //Update vertex buffer data GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (IntPtr)(4 * VertexData.SizeInBytes), vData); //Set texture coordinate data mTextureProgram.SetTexCoordPointer(VertexData.SizeInBytes, VertexData.TexCoordOffset); //Set vertex data mTextureProgram.SetVertexPointer(VertexData.SizeInBytes, VertexData.PositionOffset); //bind indexes again GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIBOID ); //Draw quad using vertex data and index data GL.DrawElements(BeginMode.Quads, 4, DrawElementsType.UnsignedInt, 0 ); //Disable vertex and texture coordinate arrays mTextureProgram.DisableVertexPointer(); mTextureProgram.DisableTexCoordPointer(); } }
public virtual void GenerateDataBuffer(int tileWidth, int tileHeight) { //If there is a texture loaded and clips to make vertex data from int maxX = ImageWidth / tileWidth; int maxY = ImageHeight / tileHeight; if( TextureID != 0 && maxX > 0 && maxY > 0) { MaxX = maxX; MaxY = maxY; TileWidth = tileWidth; TileHeight = tileHeight; //Allocate vertex buffer data int totalSprites = MaxX * MaxY; VertexData[] vertexData = new VertexData[ 4*totalSprites ]; 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 = TileHeight; float vLeft = 0; float vRight = TileWidth; for (int y = 0; y < MaxY; y++) { for (int x = 0; x < MaxX; x++) { //Initialize indices spriteIndices[0] = 4 * (y * MaxX + x); spriteIndices[1] = 4 * (y * MaxX + x) + 1; spriteIndices[2] = 4 * (y * MaxX + x) + 2; spriteIndices[3] = 4 * (y * MaxX + x) + 3; float clipTop = y * TileHeight; float clipBottom = (y+1) * TileHeight; float clipLeft = x * TileWidth; float clipRight = (x+1) * TileWidth; //Top left vertexData[spriteIndices[0]] = new VertexData(vLeft, vTop, clipLeft / tW, clipTop / tH); //Top right vertexData[spriteIndices[1]] = new VertexData(vRight, vTop, clipRight / tW, clipTop / tH); //Bottom right vertexData[spriteIndices[2]] = new VertexData(vRight, vBottom, clipRight / tW, clipBottom / tH); //Bottom left vertexData[spriteIndices[3]] = new VertexData(vLeft, vBottom, clipLeft / tW, clipBottom / tH); //Bind sprite index buffer data GL.BindBuffer(BufferTarget.ElementArrayBuffer, mIndexBuffers[y*MaxX+x]); 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 (!(maxX > 0 && maxY > 0)) { throw new Exception( "No tile possible!" ); } } }