0

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);
}
frankie
  • 728
  • 1
  • 10
  • 28

1 Answers1

0

Khm... the solution was really simple. I just have missed to EnableVertexAttribArray for colors.

I insert

        GL.EnableVertexAttribArray(1);

Before

        GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[1]);

And everything gets worked.

frankie
  • 728
  • 1
  • 10
  • 28
  • 1
    Those two things are actually unrelated. The point of binding a buffer is to setup a vertex pointer, you can setup `glVertexAttribPointer (...)` whether or not the attribute is enabled. The only time enabling or disabling an attribute matters is actually before a draw call. VAOs take care of that for you, they remember which arrays are enabled and which are disabled and restore that state when you bind them. – Andon M. Coleman Jun 06 '14 at 21:23
  • Do I understand that right that `EnableVertexAttribArray` just make effect on draw calls? – frankie Jun 07 '14 at 09:37