0

I am working on a voxel engine in OpenGL and want a way to send my chunks to my shader

I know there are some really efficient methods and data structures out there to send voxels such as Sparse Voxel Octrees but I want to start simple, with only one chunk. In c++ chunk is defined as:

struct Chunk {
    alignas(4) int voxelNum;
    alignas(4) float voxelSize;
    alignas(4) float chunkSize;
    alignas(16) glm::vec4 position;
    bool grid[16 * 16];
};

The SSBO is like this in glsl:

struct Chunk {
    int voxelNum;
    float voxelSize;
    float chunkSize;
    vec4 position;
    bool grid[16 * 16];
};

layout (std140)  uniform ChunkData {
    Chunk chunk;
};

And the data is sent like this:

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_UNIFORM_BUFFER, buffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(Chunk), &chunk, GL_STATIC_DRAW);

I tested the code and this works great for all the variables up to the grid[] array. There, only the first element is correct.

How should I tackle this? More specifically, how can I send a single struct like this from my c++ code to the shader?

2
  • make bool an int. I assume bool is 8 bit on your CPU side, while it's 32 on the GPU side. Commented Oct 28, 2024 at 7:03
  • 1
    This question is similar to: std140 and bool alignment. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 28, 2024 at 7:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.