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 4 of 4

Thread: Could not bind attribute

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    3

    Unhappy Could not bind attribute

    I'm now starting to learn Opengl tutorials from Wikibooks[http://en.wikibooks.org/wiki/OpenGL_Programming]. In the tutorial 02[http://en.wikibooks.org/wiki/OpenGL_...L_Tutorial_02]. They taught to use VBO's. I did and the code compiles succesfully. but when I execute the program. the attribute "coord2d" from vertex shader is not able to bind with the "attribute_coord2d". look at the bold faced code segment.

    here is the code
    Code :
     
    GLuint program;
    GLint attribute_coord2d;
    GLint  link_ok = GL_FALSE; 
    GLint vbo_triangle;
     
     
    int init_resources(void)
    {
     
      GLuint vs = create_shader("triangle_01.vs.glsl",GL_VERTEX_SHADER);
      GLuint fs = create_shader("triangle_01.fs.glsl",GL_FRAGMENT_SHADER);
     
      program = glCreateProgram();
      glAttachShader(program, vs);
      glAttachShader(program, fs);
      glLinkProgram(program);
      glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
      if (!link_ok) {
        fprintf(stderr, "glLinkProgram:");
        return 0;
      }
     
     
      GLfloat triangle_vertices[] = {
         0.0,  0.8,
        -0.8, -0.8,
         0.8, -0.8,
      };
     
     glVertexAttribPointer(
        attribute_coord2d, // attribute
        2,                 // number of elements per vertex, here (x,y)
        GL_FLOAT,          // the type of each element
        GL_FALSE,          // take our values as-is
        0,                 // no extra data between each position
        triangle_vertices  // pointer to the C array
      );
     
      glGenBuffers(1,&vbo_triangle);
      glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
      glBufferData(GL_ARRAY_BUFFER,sizeof(triangle_vertices),
    	       triangle_vertices, GL_STATIC_DRAW);
     
    [B]
      const char* attribute_name = "coord2d";
      attribute_coord2d = glGetAttribLocation(program, attribute_name);
      if (attribute_coord2d == -1) {
        fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
        return 0;
      }[/B]
     
    // Enable alpha
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
      return 1;
    }


    and the vertex shader code is:
    Code :
    attribute vec2 coord2d;
    void main(void) {
      gl_Position = vec4(coord2d, 0.0, 1.0);
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,101
    You seem to be missing a call to

    glEnableVertexAttribArray(attribute_coord2d);

  3. #3
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by tonyo_au View Post
    You seem to be missing a call to

    glEnableVertexAttribArray(attribute_coord2d);
    I added this like immediately below the
    glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);

    but it doesn't work either? where do I place it.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,101
    I do glVertexAttribPointer and glEnableVertexAttribArray after the bind. The bind say what object to apply these states to.

Posting Permissions

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