Hello Guest

Problem with VBOs and colours

  • 2 Replies
  • 2108 Views
Problem with VBOs and colours
« on: April 23, 2016, 17:52:23 »
I'm trying to render some shapes on screen. I have a VBO and a CBO (for colours, I'm not using textures), and the shapes aren't being coloured. They just show up on screen black.

Here is my code:

Code: [Select]
public class Mesh {

private static final int VERTEX_SIZE = 3;

private int vbo;
private int ibo;
private int cbo;

private int size;

public Mesh() {
vbo = glGenBuffers();
ibo = glGenBuffers();
cbo = glGenBuffers();
}

public Mesh addVertices(float sizeMultiplier, Vector3f[] vertex, int[] indices, Colour[] colours) {
size = indices.length;

FloatBuffer vbuf = BufferUtils.createFloatBuffer(vertex.length * VERTEX_SIZE);

for (Vector3f v : vertex) {
vbuf.put(v.x * sizeMultiplier);
vbuf.put(v.y * sizeMultiplier);
vbuf.put(v.z * sizeMultiplier);
}

vbuf.flip();

IntBuffer ibuf = BufferUtils.createIntBuffer(size * VERTEX_SIZE);
ibuf.put(indices);
ibuf.flip();

FloatBuffer cbuf = BufferUtils.createFloatBuffer(size * 4);

for (int i = 0; i < vertex.length; i++) {
Colour c = colours[0]; // Just repeats the same colour for debug purposes
cbuf.put(c.r);
cbuf.put(c.g);
cbuf.put(c.b);
cbuf.put(c.a);
}

glBindBuffer(GL_ARRAY_BUFFER, vbo); Main.checkGLError();
glBufferData(GL_ARRAY_BUFFER, vbuf, GL_STATIC_DRAW); Main.checkGLError();

glBindBuffer(GL_ARRAY_BUFFER, cbo); Main.checkGLError();
glBufferData(GL_ARRAY_BUFFER, cbuf, GL_STATIC_DRAW); Main.checkGLError();

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); Main.checkGLError();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ibuf, GL_STATIC_DRAW); Main.checkGLError();
return this;
}

public void render() {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, VERTEX_SIZE, GL_FLOAT, false, VERTEX_SIZE * 4, 0);

glBindBuffer(GL_ARRAY_BUFFER, cbo);
glVertexAttribPointer(1, 4, GL_FLOAT, false, 16, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}

}

And my shaders:

Vertex Shader:

Code: [Select]
#version 330

layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;

out vec4 fragColor;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main() {

fragColor = color;

gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
}

Fragment Shader:

Code: [Select]
#version 330

in vec4 fragColor;

out vec4 finalColor;

void main() {
finalColor = fragColor;
}

Please help! Thanks.

*

Kai

Re: Problem with VBOs and colours
« Reply #1 on: April 23, 2016, 20:11:10 »
1. Why do you size your element buffer with "size * VERTEX_SIZE" ? It should be just "size".

2. You do not flip() the cbuf.

Re: Problem with VBOs and colours
« Reply #2 on: April 23, 2016, 22:48:56 »
Ok, thanks a lot.