Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

jogl - OpenGL DrawArrays with Color

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)
question from:https://stackoverflow.com/questions/65832399/opengl-drawarrays-with-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The last argument of glColorPointer is the buffer offset in bytes. So the offset vertBuf.limit() * 4 and not vertBuf.limit():

gl.glColorPointer(3, GL2.GL_FLOAT, stride, vertBuf.limit());

gl.glColorPointer(3, GL2.GL_FLOAT, stride, vertBuf.limit()*4);

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)

gl.glBufferSubData(GL_ARRAY_BUFFER, 0, vertBuf.limit()*4, vertBuf);
gl.glBufferSubData(GL_ARRAY_BUFFER, vertBuf.limit()*4, colorBuffer.limit()*4, colorBuffer);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...