Skip to content

Instantly share code, notes, and snippets.

@Nihlus
Created July 6, 2016 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nihlus/e45544693a8a7d506ce4ff711f43abea to your computer and use it in GitHub Desktop.
Save Nihlus/e45544693a8a7d506ce4ff711f43abea to your computer and use it in GitHub Desktop.
private int GenerateImageVertices()
{
// Generate vertex positions
uint halfWidth = Image.GetResolution().X / 2;
uint halfHeight = Image.GetResolution().Y / 2;
/*
Vector2 upLeft = new Vector2(-halfWidth, halfHeight);
Vector2 upRight = new Vector2(halfWidth, halfHeight);
Vector2 downLeft = new Vector2(-halfWidth, -halfHeight);
Vector2 downRight = new Vector2(halfWidth, -halfHeight);
*/
Vector2 upLeft = new Vector2(-1, 1);
Vector2 upRight = new Vector2(1, 1);
Vector2 downLeft = new Vector2(-1, -1);
Vector2 downRight = new Vector2(1, -1);
this.vertexPositions.Clear();
this.vertexPositions.Add(upLeft);
this.vertexPositions.Add(upRight);
this.vertexPositions.Add(downLeft);
this.vertexPositions.Add(downRight);
// Buffer the generated vertices in the GPU
int bufferID;
GL.GenBuffers(1, out bufferID);
float[] vertices = GetVerticesAsArray();
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferID);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
// Generate vertex indices
List<ushort> vertexIndices = new List<ushort> {1, 0, 2, 2, 3, 1};
GL.GenBuffers(1, out this.VertexIndexBufferID);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.VertexIndexBufferID);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(vertexIndices.Count * sizeof(ushort)), vertexIndices.ToArray(), BufferUsageHint.StaticDraw);
return bufferID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment