Boodog
Boodog

Reputation: 176

Adding Blocks to a Chunk - Java OpenGL

In my voxel engine, right now I am trying to add blocks to the chunk, and it's half-working. It's in Java and OpenGL. Anyways, when I am placing the block I am doing this:

public void placeBlock(int x, int y, int z, Block block)
{
    // c = chunk
    c.addToChunk(x, y, z, block);
}

After that, in my chunk class I have this method:

 public void addToChunk(int x, int y, int z, Block block)
{
    glNewList(test, GL_COMPILE);
    glBegin(GL_QUADS);
    Shape.createCube(x, y, z, Block.getBlockById(block.getId()).getColor(), Block.getBlockById(block.getId()).getTexCoords(), 1);
    blocks[x][y][z] = block.getId();
    glEnd();
    glEndList();
}

(I am rendering it by calling the list) Anyways, it adds the block to the chunk, but when I place a new block it removes the old block! I have no idea why it's doing this, but if anyone can help me that would be great!

Upvotes: 0

Views: 222

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54642

Ok, so the question is: How do I add something to a display list?

I'll answer this in two parts. First, I normally try to resist telling people that they shouldn't be doing what they want to do. But in this case I can't help it: Do not use display lists! They were officially obsoleted in 2009, and IMHO were really outdated for about 10 years before that. Unless you have to maintain legacy code that can't easily be changed, you should learn about modern ways of drawing. Look up keywords like VBO (vertex buffer object) and VAO (vertex array object).

Now, let's assume that you're really attached to display lists, and want to use them anyway. You can't directly modify display lists in OpenGL. When you call glNewList(), the previous content is wiped out. You have a few options for your situation:

  1. When a block is added to a chunk, rebuild the whole display list for the chunk. So you basically do what you have now, but iterate over all the visible blocks in the chunk between glNewList() and glEndList() after you added the new block to your list.
  2. Keep each block in a separate display list. You can execute multiple display lists with a single glCallLists() call. glCallLists() takes an array of display list indices, so you would want to maintain an array that contains the display list index for each block.
  3. Use a hierarchical display list. With this approach, you have a display list for each block, but you would also have an overall display list for the entire chunk, which in turn contains a glCallList() call for each block. If there is an upper bound for the number of blocks per chunk, you can build this overall display list once, with a glCallList() call for every possible block, and keep the per-block lists empty until the corresponding block becomes visible. Then, to add a block, you populate the per-block list with the call sequence you have now. To render the chunk, you call glCallList() with the overall display list.

Upvotes: 2

Related Questions