Reputation: 176
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
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:
glNewList()
and glEndList()
after you added the new block to your list.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.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