Hello Guest

Difference of glBufferData between C++ and Java?

  • 1 Replies
  • 325 Views
Difference of glBufferData between C++ and Java?
« on: August 20, 2019, 04:09:11 »
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.

Re: Difference of glBufferData between C++ and Java?
« Reply #1 on: August 20, 2019, 13:01:23 »
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

http://www.java-gaming.org/topics/introduction-to-vertex-arrays-and-vertex-buffer-objects-opengl/24272/view.html