Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Misunderstanding of Location and VertexAttribPointer

  1. #1
    Newbie Newbie
    Join Date
    Jun 2017
    Posts
    2

    Misunderstanding of Location and VertexAttribPointer

    I am sure that I have misunderstood something important about vertex attributes, but i just can't see where I go wrong.

    My understanding is that the vertex attributes are loaded into buffers with glBufferData and then the individual attributes are set with VertexAttribPointer by the software and then read out with the layout(location=1), they are enabled with EnableVertexAttribArray, thus if they are all the same index then they will all be correct.

    Code c#:
     
        class SimpleQuad
        {
            uint points_vbo = 0;
            uint texcoords_vbo = 0;
            uint vao;
            uint indices;
     
            public SimpleQuad()
            {
                float[] points = {
                   -0.5f,  +0.5f,
                   +0.5f,  +0.5f,
                   +0.5f,  -0.5f,
                   -0.5f,  -0.5f
                };
     
                float [] texcoords = {
                   0.0f,  0.0f,
                   0.0f,  1.0f,
                   1.0f,  1.0f,
                   1.0f,  0.0f
                };
     
                ushort [] indexes = {
                    0,1,2,2,3,0
                };
     
                GL.GenBuffers(1, out indices);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, indices);
                GL.BufferData(BufferTarget.ElementArrayBuffer, 6 * sizeof(ushort),indexes, BufferUsageHint.StaticDraw);
     
                GL.GenBuffers(1, out points_vbo);
                GL.BindBuffer(BufferTarget.ArrayBuffer, points_vbo);
                GL.BufferData(BufferTarget.ArrayBuffer, 8 * sizeof(float), points, BufferUsageHint.StaticDraw);
     
                GL.GenBuffers(1, out texcoords_vbo);
                GL.BindBuffer(BufferTarget.ArrayBuffer, texcoords_vbo);
                GL.BufferData(BufferTarget.ArrayBuffer, 8 * sizeof(float), texcoords, BufferUsageHint.StaticDraw);
     
                GL.GenVertexArrays(1, out vao);
                GL.BindVertexArray(vao);
     
                GL.BindBuffer(BufferTarget.ArrayBuffer, points_vbo);
                GL.EnableVertexAttribArray(0);
                GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);
     
                GL.BindBuffer(BufferTarget.ArrayBuffer, texcoords_vbo);
                GL.EnableVertexAttribArray(1);
                GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 0, 0);
     
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, indices);
     
                GL.BindVertexArray(0);
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            }
     
            public void Draw()
            {
                GL.BindVertexArray(vao);
                GL.DrawElements(BeginMode.Triangles, 6, DrawElementsType.UnsignedShort, 0);
            }
        }

    Which i then draw with this shader.

    Code glsl:
    // Vertex Shader
    #version 440 core
     
    layout(location = 0) in vec2 in_position;
    layout(location = 1) in vec2 in_texcoord;
     
    out vec2 texcoord;
     
    void main(void)
    {
    	texcoord = in_texcoord;
    	gl_Position = vec4(in_position, 0.0, 1.0);
    }
     
    // Fragment Shader
    #version 440 core
     
    out vec4 color;
     
    void main(void)
    {
    	color = vec4(1.0f, 0.5f, 1.0f, 1.0f);
    }

    What I get from this code and shader is a black screen, but what I expect is a colored square (rect) draw over the clear color. There must be something that I have not understaood, because the code appears to be correct to me

  2. #2
    Newbie Newbie
    Join Date
    Jun 2017
    Posts
    2
    OK - so this morning back to work and i took another look at this - and there are no bugs in it. I was calling the wrong shader program and getting undefined results

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •