PDA

View Full Version : Filling a Buffer with structure (Java)



Exalia
05-28-2015, 08:10 AM
Hi,

I'm attempting to fill a buffer of fixed size with a structure :



struct Vector2f
{
float x;
float y;
};

struct Node
{
Vector2f id;
float f;
float h;
float g;
};


I'm wondering what the best approach to this would be.

I'm using java where a conventional C structure isn't available so have created a class with a similar structure;



public class Node
{
public float[] pos;
public float h;
public float g;
public float f;
}


and would like to fill my buffer with the data:



map = new Node[8][8];


For example.

I've looked into glBufferData(); that takes the parameters:

glBufferData(GL_SHADER_STORAGE_BUFFER, size, ptr,
GL_STATIC_DRAW);

However Java does not support ptrs and must be passed a buffer instead.
Would this be a Java side issue of converting a class to a buffer? In that case I guess I may have to ask this question somewhere else?
I'm quite lost here so any suggestions would be greatly appreciated.

Thanks in advance

Exalia
05-28-2015, 08:23 AM
After doing a bit more research and using some different keywords I've come across a stackoverflow answer that describes converting my entire class into a single float array and then converting that array to a bytebuffer.

To get my class values of:


public class Node
{
public float[] pos;
public float h;
public float g;
public float f;
}

Into a Buffer Layout of:



layout(std140, binding = 0) buffer destBuffer
{
Vector2f id;
float f;
float h;
float g;
}



I would need to convert my class into a floatArray:



float bufferData[] = {
1.0f, 1.0f,
0.1f,
0.25f,
0.45f};


Then convert this float array into a floatbuffer and use this as a parameter for my glBufferData:


glBufferData(..., FloatBuffer.wrap(bufferData));

I'll attempt this while waiting for a reply and see if this solves my issue.

Source : http://stackoverflow.com/questions/26316179/copying-float-arrays-and-float-values-to-a-bytebuffer-java

Cornix
05-28-2015, 10:13 AM
What java library are you using for the OpenGL bindings?
If it is LWJGL then your approach will not work as you need to set the buffers byte order and you also have to rewind it. If you are new to LWJGL I would recommend reading its documentation and many tutorials.

Exalia
05-28-2015, 10:47 AM
I'm not entirely sure what this means, I've seen a lot of information about JOGL and LWJGL online but I don't believe I've been in contact with any of these.

I'm using Android Studio and I'm using OpenGLES3.1.

I followed Google's documentation to get OpenGL working in my Android Application.

http://developer.android.com/guide/topics/graphics/opengl.html

I'll keep trying to find which library I'm using.

Exalia
05-28-2015, 11:35 AM
I'm not sure if I can find a good answer to this or if I'm just not looking in the right place.

But maybe I'm asking these questions in the wrong place entirely as this is an open gl forum and not specifically an open gl es forum?

Please let me know if you think there would be a place better fit for these questions.

Alfonse Reinheart
05-28-2015, 12:49 PM
The answer would be the same for desktop GL as for ES. The problem is (at least from my perspective) Java.

OpenGL, of either form, was designed for C. And thus, OpenGL's layout rules for buffer-backed interface blocks (https://www.opengl.org/wiki/Interface_Block_%28GLSL%29#Buffer_backed) are built around what you can do with C. You can design C structs to match OpenGL's standardized layout rules, such that you just do a memcpy from your array of structs into a buffer object.

That's not really possible in Java. Which means that every Java implementation of OpenGL has to find some way of getting arbitrary data into a buffer object. Thus, the only people who could help you are those who are Java programmers, OpenGL ES programmers, using the Android OpenGL ES implementation, and who are using modern OpenGL techniques like UBOs and SSBO.

Outside of an Android forum, Stack Overflow is probably your best bet, considering that your problem rests at the intersection of so many distinct issues.

Do note that it is very important to follow the layout rules (PDF) (https://www.opengl.org/registry/doc/glspec45.core.pdf#page=159). And if you're using this `Vector2f` struct in GLSL (instead of `vec2`), then that can impact how the object is laid out in memory. So your array of floats approach can work, but you need to make sure that you're padding it correctly.

Exalia
05-28-2015, 09:03 PM
Thanks so much, I've learned so much just from your response and can see many avenues to explore and learn further.

Cheers :)