I'm currently using OpenGL2 for one of my projects and I want to render a PointCloud. I can also already display the points at the correct positions but I have a problem with the colors. This is my Code:
public void draw(DrawContext drawContext){
gl.glEnable(GL2.GL_DEPTH_BUFFER_BIT);
int stride = 0;
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO.get(0));
gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);
gl.glColorPointer(3, GL2.GL_FLOAT, stride,vertBuf.limit());
gl.glPointSize(4);
gl.glDrawArrays(GL_POINTS, 0, vertBuf.limit());
gl.glPointSize(1);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
gl.glDisable(GL2.GL_DEPTH_BUFFER_BIT);
}
The position and color values are both in one VBO(the one that I bind). But I don't know how the call to DrawArrays has to look like so it actually uses the color values. Currently, when the points are rendered they are just grey as if I hadn't assigned a color to them. I know this would be easy with shaders but I can not use them. I have to do it this way.
The positions are saved in a FloatBuffer called vertBuf
and the colors in a FloatBuffer called colorBuffer
. The colorValues are also normalized between 0 and 1 and are in RGB if that matters.
I bind the position and color values to the VBO like this:
gl.glBufferData(GL_ARRAY_BUFFER,(vertBuf.limit()*4)+(colorBuffer.limit()*4),null, GL_STATIC_DRAW)
gl.glBufferSubData(GL_ARRAY_BUFFER,0,vertBuf.limit(),vertBuf)
gl.glBufferSubData(GL_ARRAY_BUFFER,vertBuf.limit(),colorBuffer.limit(),colorBuffer)
The last argument of
glColorPointer
is the buffer offset in bytes. So the offsetvertBuf.limit() * 4
and notvertBuf.limit()
:gl.glColorPointer(3, GL2.GL_FLOAT, stride, vertBuf.limit());
The offset and size argument of
glBufferSubData
also specifies the buffer offset and buffer size in bytes:gl.glBufferSubData(GL_ARRAY_BUFFER,0,vertBuf.limit(),vertBuf)
gl.glBufferSubData(GL_ARRAY_BUFFER,vertBuf.limit(),colorBuffer.limit(),colorBuffer)