3

Here is what I want to do : I want to have a modular cube that when touching an other one will disable the two faces between eachothers. Like so :

I want to make the grey faces disapear.

Looks rather simple in theory right ?

Except that this is how we are supposed to make a a shape in modern OpenGL. First we declare the vertices, then the indices to tell which is connected to which.

FloatBuffer vertices = BufferUtils.createFloatBuffer(8 * 6);
vertices.put(new float[]{
        // front                // color
        -0.5f, -0.5f,  0.5f,    0.75f, 0.75f, 0.75f,
        0.5f, -0.5f,  0.5f,     0.75f, 0.75f, 0.75f,
        0.5f,  0.5f,  0.5f,     0.35f, 0.75f, 0.9f,
        -0.5f,  0.5f,  0.5f,    0.35f, 0.75f, 0.9f,
        // back
        -0.5f, -0.5f, -0.5f,    0.75f, 0.75f, 0.75f,
        0.5f, -0.5f, -0.5f,     0.75f, 0.75f, 0.75f,
        0.5f,  0.5f, -0.5f,     0.35f, 0.75f, 0.9f,
        -0.5f,  0.5f, -0.5f,    0.35f, 0.75f, 0.9f,
});
vertices.flip();

IntBuffer indices = BufferUtils.createIntBuffer(12 * 3);
indices.put(new int[]{
        // front
        0, 1, 2,
        2, 3, 0,
        // top
        3, 2, 6,
        6, 7, 3,
        // back
        7, 6, 5,
        5, 4, 7,
        // bottom
        4, 5, 1,
        1, 0, 4,
        // left
        4, 0, 3,
        3, 7, 4,
        // right
        1, 5, 6,
        6, 2, 1,
});
indices.flip();

This makes for a lightweight cube. But I don't see how I can disable the faces I want with this model.

Does anyone has a solution that doesn't imply me re-doing my cube ?

Thank you

Gaktan
  • 458
  • 3
  • 9
  • 1
    This might help you out http://stackoverflow.com/questions/6319655/how-to-do-face-removal-in-a-unit-cube-world-a-la-minecraft – sn710 Jan 24 '15 at 13:15
  • Thank you. The answer say that I have to individually change the block vertex data. But I don't want each cuve to have their own data. All the cubes share the same vertex data. That's why it gets complicated. – Gaktan Jan 24 '15 at 13:30
  • 2
    Instead of changing the vertex data you could only update the index buffer. You could even precompute the different cases and switch to the correct buffer. – Anton D Jan 24 '15 at 13:53
  • This sounds like a good idea. Do you know if I could combine index buffers ? – Gaktan Jan 24 '15 at 14:30
  • I have no experience with LWJGL and IntBuffers, sorry. – Anton D Jan 24 '15 at 19:08
  • LWJGL is just a OpenGL API for OpenGL, it works exactly the same. And an Intbuffer is used by LWJGL as an array. – Gaktan Jan 24 '15 at 19:58

1 Answers1

0

I have managed to solve my problem, thanks to the hint by Anton D.

I was able to "merge" multiple buffers like so :

i_n = BufferUtils.createIntBuffer(2 * 3);
i_n.put(new int[]{
        // front
        1, 0, 2,
        3, 2, 0,
});
i_n.flip();

i_s = BufferUtils.createIntBuffer(2 * 3);
i_s.put(new int[]{
        // back
        6, 7, 5,
        4, 5, 7,
});
i_s.flip();

i_e = BufferUtils.createIntBuffer(2 * 3);
i_e.put(new int[]{
        // right
        5, 1, 6,
        2, 6, 1,
});
i_e.flip();

i_w = BufferUtils.createIntBuffer(2 * 3);
i_w.put(new int[]{
        // left
        0, 4, 3,
        7, 3, 4,
});
i_w.flip();

One for every face.

Then when it comes to render them (I precalculated which to draw), this is how I render them :

if(orientation.contains("n")){
    GL11.glDrawElements(GL11.GL_TRIANGLES, i_n);
}
if(orientation.contains("s")){
    GL11.glDrawElements(GL11.GL_TRIANGLES, i_s);
}
if(orientation.contains("e")){
    GL11.glDrawElements(GL11.GL_TRIANGLES, i_e);
}
if(orientation.contains("w")){
    GL11.glDrawElements(GL11.GL_TRIANGLES, i_w);
}

Not the proper solution, I'm afraid. But it's working very well

Gaktan
  • 458
  • 3
  • 9