Difference of glBufferData between C++ and Java?

Started by j9263178, August 20, 2019, 04:09:11

Previous topic - Next topic


In order to make a 2D RPG game using LWGJL,I am learning OpenGL from the book :
ãâ,¬Å'OpenGL Programming Guide: The Official Guide to Learning OpenGLãâ,¬Â
This book use C++ code as Examples , here are some context from the book about  glBufferData :

Suppose you have an array containing some vertex data, another containing some color information,
and yet another containing texture coordinates or some other data.
Youââ,¬â,,¢d like to pack the data back to back into one big buffer object so that OpenGL can use it.
The arrays may or may not be contiguous in memory, so you canââ,¬â,,¢t use glBufferData() to upload all of it in one go.
Further, if you use glBufferData() to upload, say, the vertex data first,
then the buffer will be sized to exactly match the vertex data and there wonââ,¬â,,¢t be room for the color or texture coordinate information.
Thatââ,¬â,,¢s where glBufferSubData() comes in.

then they showed me an examples about making use of glBufferData and glBufferSubData, the following are the part that I think is important

glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(positions) + sizeof(colors),NULL,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0,sizeof(positions),  positions);
glBufferSubData(GL_ARRAY_BUFFER,sizeof(positions), sizeof(colors), colors);

and I notice the parameters are a little bit of different to that in Java,
please take look at part my code of VBO (which actually comes from a youtube tutorial) :

v_id=glGenBuffers();   //for vertices
glBindBuffer(GL_ARRAY_BUFFER, v_id);
glBufferData(GL_ARRAY_BUFFER, createBuffer(vertices), .GL_STATIC_DRAW);
t_id=glGenBuffers();   //for textures
glBindBuffer(GL15.GL_ARRAY_BUFFER, t_id);
glBufferData(GL15.GL_ARRAY_BUFFER, createBuffer(tex_coords), GL_STATIC_DRAW);       
glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

So in Java we didn't give the size and offset and just simply bind a new buffer object ,
it seems like the ãâ,¬Å'size problemãâ,¬Âdidn't appear here as the book mentioned ,
and I don't see the benefits of using glBufferSubData,
Are these two different situation  or  just different approaches to deal with drawing  vertices,texture,color...etc. ?
I am so confused.


You are seeing differences between the two because they are solutions to two different ways of storing data in vertex buffers, one stores two sets of data in one buffer (c++) and the other has multiple buffers for each vertex buffer type (java). There is no reason why you couldn't code up the c++ solution in java nor the Java solution in c++.

Here is a useful post on java-gaming.org which shows many ways to draw vertex data