I am trying to draw VAO
from separate VBO
. My goal is to get different colors for each vertex of my geometry. But with my code it still all red.
I think my error is this code fragment. Please, help me to find it. (I have skipped program and matrices set ups)
Set up
vao = new int[1];
buffers = new int[2];
GL.GenVertexArrays(1, vao);
GL.GenBuffers(2, buffers);
GL.BindVertexArray(vao[0]);
GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[0]);
unsafe
{
fixed (void* verts = quad_strip3)
{
var prt = new IntPtr(verts);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(quad_strip3.Length * sizeof(float)), prt,
BufferUsageHint.StaticDraw);
}
}
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, new IntPtr(0));
GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[1]);
var r = new Random();
var colors = new float[quad_strip3.Length];
for (int i = 0; i < colors.Length; i++)
{
colors[i] = (float)r.NextDouble();
}
unsafe
{
fixed (void* verts = colors)
{
var prt = new IntPtr(verts);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(colors.Length * sizeof(float)), prt,
BufferUsageHint.StaticDraw);
}
}
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, new IntPtr(0));
Draw code
GL.BindVertexArray(vao[0]);
GL.DrawArrays(PrimitiveType.QuadStrip, 0, 26);
Vertex shader
#version 150 core
in vec3 in_Position;
in vec3 in_color;
out vec3 pass_Color;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
void main(void) {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
pass_Color = in_color;
}
Fragment shader
#version 150 core
in vec3 pass_Color;
out vec4 out_Color;
void main(void) {
out_Color = vec4(pass_Color, 1.0);
}