Ok I recently came back to some OpenGL development, but my experience dates back to 1999 and I mostly used immediate mode stuff (glBegin, glEnd, etc).
So I started reading into OpenGL 3.2 + GLSL 1.5 and my assumption is that everthing now is done with VBOs and Vertex/Geometry/Fragment shaders … am I right?
I succesfully initiated a “3.2.9704 Forward-Compatible Context” on Linux (Ubuntu 9.10) with a HD 4850.
So far I got shaders loaded and executed allright, and I can draw a simple quad using a VBO and glDrawElements() with an indices array.
Is this how all drawing is supposed to happen now?
Now I met a problem in this early stage:
I am using a second VBO with vertex color values (3 floats each) and the vertex shader inputs the color and transfers it to the fragment shader.
The first 2 vertices are drawn with the correct color, but then color values don’t match the supplied array.
What am I doing wrong here?
(vertex, fragment shader, relevant C++ code)
#version 150
in vec4 vertex;
in vec3 col;
out vec3 v_col;
void main(void) {
v_col = col;
gl_Position = vertex;
}
#version 150
in vec3 v_col;
out vec4 fragColor;
void main(void) {
fragColor = vec4(v_col, 1.0);
}
void createBuffers(GLuint program) {
// generate vertex data
GLfloat data[] = {
-1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
1.0, 0.0, 0.0,
0.0, -1.0, 0.0
};
GLfloat color[] = {
1.0, 0.0, 0.0,
0.0, 1.0, 0,0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0
};
// VertexArrayObject is needed - ToDo: lookup what a VAO is
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// vertex data
glGenBuffers(1, &buffer_data);
glBindBuffer(GL_ARRAY_BUFFER, buffer_data);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
// map input attribute: vertex
GLuint attr_vertex = glGetAttribLocation(program, "vertex");
glVertexAttribPointer(attr_vertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr_vertex);
// color data
glGenBuffers(1, & buffer_color);
glBindBuffer(GL_ARRAY_BUFFER, buffer_color);
glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
// map input attribute: col
GLuint attr_color = glGetAttribLocation(program, "col");
glVertexAttribPointer(attr_color, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr_color);
}
void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, buffer_data);
glBindBuffer(GL_ARRAY_BUFFER, buffer_color);
GLuint indices[] = {0, 1, 2, 2, 3, 0};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices);
glutSwapBuffers();
glFlush();
}