PDA

View Full Version : Could not bind attribute



vanangamudi
11-29-2012, 08:06 AM
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_Programming/Modern_OpenGL_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



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_verti ces),
triangle_vertices, GL_STATIC_DRAW);


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;
}

// Enable alpha
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

return 1;
}



and the vertex shader code is:


attribute vec2 coord2d;
void main(void) {
gl_Position = vec4(coord2d, 0.0, 1.0);
}

tonyo_au
11-30-2012, 04:46 PM
You seem to be missing a call to

glEnableVertexAttribArray(attribute_coord2d);

vanangamudi
11-30-2012, 10:10 PM
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.

tonyo_au
12-02-2012, 03:06 AM
I do glVertexAttribPointer and glEnableVertexAttribArray after the bind. The bind say what object to apply these states to.