Filling a Buffer with structure (Java)

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

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 : opengl - Copying float arrays and float values to a bytebuffer - Java - Stack Overflow

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.

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.

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.

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 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). 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.

Thanks so much, I’ve learned so much just from your response and can see many avenues to explore and learn further.

Cheers :slight_smile: