PDA

View Full Version : Vertex Attributes



thatguy
08-22-2011, 04:54 AM
Hello, I started coding OpenGL 3.2 (forward compatible with 4.2) using the OpenTK C# binding.
For some reason, the following code doesn't display anything on the screen.

Main code: (class constructor)


Matrix4 Projection;
Matrix4 View;

int[] ShaderIDs;
int[] ProgramIDs;
uint[] VAOs;
uint[] VBOs;
ushort[] Indices;
Vector3[] Vertices;

public Game1()
: base(800, 600)
{
// Our uniform matrices
Projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, 0.01f, 10);
View = Matrix4.LookAt(new Vector3(0, 0, 100), Vector3.Zero, Vector3.UnitY);

InitVAOsVBOs();
InitShaderProgram();

// Get uniform projection and view matrices locations
int uProjIndex = GL.GetUniformLocation(ProgramIDs[0], "projection");
int uViewIndex = GL.GetUniformLocation(ProgramIDs[0], "view");

// Get inVertexPos location
int inVertexPosIndex = GL.GetAttribLocation(ProgramIDs[0], "inVertexPos");

// Start using our shader program
GL.UseProgram(ProgramIDs[0]);

// Set uniform values
GL.UniformMatrix4(uProjIndex, false, ref Projection);
GL.UniformMatrix4(uViewIndex, false, ref View);

// Set inVertexPos Array
GL.BindVertexArray(VAOs[0]);
GL.VertexAttribPointer(inVertexPosIndex, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
GL.EnableVertexAttribArray(inVertexPosIndex);
}

Buffer initialization code:


private void InitVAOsVBOs()
{
VAOs = new uint[1];
VBOs = new uint[2];
GL.GenVertexArrays(VAOs.Length, VAOs);
GL.GenBuffers(VBOs.Length, VBOs);

Vertices = new Vector3[8] {
new Vector3(-100, 100, -100), //0 Top, Left, Front
new Vector3(-100, 100, 100), //1 Top, Left, Back
new Vector3(-100, -100, -100), //2 Top, Right, Front
new Vector3(-100, -100, 100), //3 Top, Right, Back
new Vector3( 100, 100, 100), //4 Bottom, Left, Front
new Vector3( 100, 100, -100), //5 Bottom, Left, Back
new Vector3( 100, -100, -100), //6 Bottom, Right, Front
new Vector3( 100, -100, 100) //7 Bottom, Right, Back
};
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Vertices.Length * Vector3.SizeInBytes), Vertices, BufferUsageHint.StaticDraw);
GL.BindVertexArray(VAOs[0]);

Indices = new ushort[24] {
0, 1, 3, 2, // Top face
0, 1, 5, 4, // Left face
1, 3, 7, 5, // Back face
2, 3, 7, 6, // Right face
4, 5, 7, 6, // Bottom face
0, 2, 6, 4 // Front face
};
GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOs[0]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(Indices.Length * sizeof(ushort)), Indices, BufferUsageHint.StaticDraw);
}

Shader code:


private void InitShaderProgram()
{
ShaderIDs = new int[2];
ProgramIDs = new int[1];

string VS_Source =
@"#version 150

uniform mat4 projection;
uniform mat4 view;

in vec3 inVertexPos;

void main()
{
gl_Position = projection * view * vec4(inVertexPos, 1.0);
}";
ShaderIDs[0] = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(ShaderIDs[0], VS_Source);
GL.CompileShader(ShaderIDs[0]);
Console.WriteLine(GL.GetShaderInfoLog(ShaderIDs[0]));

string FS_Source =
@"#version 150

void main()
{
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}";
ShaderIDs[1] = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(ShaderIDs[1], FS_Source);
GL.CompileShader(ShaderIDs[1]);
Console.WriteLine(GL.GetShaderInfoLog(ShaderIDs[1]));

ProgramIDs[0] = GL.CreateProgram();
foreach (int id in ShaderIDs)
GL.AttachShader(ProgramIDs[0], id);
GL.LinkProgram(ProgramIDs[0]);
Console.WriteLine(GL.GetProgramInfoLog(ProgramIDs[0]));
}

Render code:


protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

GL.DrawElements(BeginMode.Quads, Indices.Length, DrawElementsType.UnsignedShort, 0);

this.SwapBuffers();
}