I'm trying to use an index buffer but have no idea how
I really am stuck in a wall as to what I'm supposed to even try so. none of the tutorials really show anything similar to my code which is mostly why I'm having problems. I might have to rewrite cube vertex array creator. (on a different note maybe I shouldn't have just shoved opengl into some ready code) right now I've only enabled the top face as to see what's going on.
/// <summary>
/// Draws the specified Cube Onsccreen.
/// </summary>
public void Draw()
{
GL.UseProgram(VSID);
Vector3 ObjectPosition = new Vector3((new Vector(1, 1) * (Position - Offset)))
{
Z = Layer * 0.1f
};
Matrix4 Scale = Matrix4.CreateScale(Width + highlight, Height + highlight, Height + highlight);
Matrix4 Translation = Matrix4.CreateTranslation(ObjectPosition);
Matrix4 ViewPoint = Matrix4.CreateOrthographic(Game.window.Width , Game.window.Height, -1, 1000);
ViewPoint = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI-Zoom,Game.window.Width/Game.window.Height,0.0001f,1000);
Matrix4 Rotation = Matrix4.CreateRotationZ((float)Angle);
Matrix4 CameraRotX = Matrix4.CreateRotationX((float)Math.PI / 180 * 40f);
Matrix4 CameraRotZ = Matrix4.CreateRotationZ(Game.CameraAngle);
Matrix4 Combined = Scale * Rotation * Translation * ViewPoint * CameraRotZ * CameraRotX;
GL.ProgramUniformMatrix4(VSID, GL.GetUniformLocation(VSID, "QuadMatrix"), false, ref Combined);
GL.ProgramUniform4(VSID, GL.GetUniformLocation(VSID, "ColorIn"), Color);
GL.ProgramUniform1(VSID, GL.GetUniformLocation(VSID, "SS"), 0);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.IndexArray);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, Texture);
GL.BindVertexArray(QID);
GL.DrawArrays(PrimitiveType.Triangles, 0, indices.Length);
}
public static void CreateVisuals()
{
int VS = Shaders.Load("Shaders.vs", ShaderType.VertexShader);
int FS = Shaders.Load("Shaders.fs", ShaderType.FragmentShader);
VSID = Visuals.Create(VS, FS);
}
public static void CreateCube()
{
QID = GL.GenVertexArray();
GL.BindVertexArray(QID);
int VID =GL.GenBuffer();
float[] Verticles =
{
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f
};
GL.BindBuffer(BufferTarget.ArrayBuffer, VID);
GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * Verticles.Length, Verticles,BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.EnableVertexAttribArray(0);
int UVID = GL.GenBuffer();
float[] UVs =
{
0, 1,
1, 1,
0, 0,
0, 0,
1, 1,
1, 0
};
GL.BindBuffer(BufferTarget.ArrayBuffer, UVID);
GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * UVs.Length, UVs, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 0, 0);
GL.EnableVertexAttribArray(1);
indices = new uint[]
{
0, 1, 2,
2, 1, 3 /*,
0, 1, 5,
0, 1, 4,
1, 3, 5,
3, 5, 7,
2, 3, 7,
2, 6, 7,
0, 2, 6,
0, 4, 6,
4, 5, 6,
5, 6, 7 //*/
};
IBO = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(uint) * indices.Length), indices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(IBO,1,VertexAttribPointerType.UnsignedInt,false,0,0);
GL.EnableVertexAttribArray(2);
}
instead of drawing a simple quad it the 2 triangles get drawn in the wrong order. it's actually just using the coordinates in space instead of the indexes for those coordinates currently looks like this: