Drawing Blocks....

Ok guys… so at the moment, im basically teaching myself opengl… by making a voxel engine thingy… anyway… i have this block class that “works” but its LAGGY as crap… so… heres the code


import static org.lwjgl.opengl.GL11.*;

public class Block
{
	private static final double size = 1;

	public static void renderBlockAt(int x, int y, int z)
	{
		glTranslatef(x, y, z);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glBegin(GL_QUADS);

		glVertex3d(size, size, size);
		glVertex3d(-size, size, size);
		glVertex3d(-size, -size, size);
		glVertex3d(size, -size, size);

		glVertex3d(size, size, -size);
		glVertex3d(-size, size, -size);
		glVertex3d(-size, -size, -size);
		glVertex3d(size, -size, -size);

		glVertex3d(size, size, size);
		glVertex3d(size, -size, size);
		glVertex3d(size, -size, -size);
		glVertex3d(size, size, -size);

		glVertex3d(-size, size, size);
		glVertex3d(-size, -size, size);
		glVertex3d(-size, -size, -size);
		glVertex3d(-size, size, -size);

		glVertex3d(size, size, size);
		glVertex3d(-size, size, size);
		glVertex3d(-size, size, -size);
		glVertex3d(size, size, -size);

		glVertex3d(size, -size, size);
		glVertex3d(-size, -size, size);
		glVertex3d(-size, -size, -size);
		glVertex3d(size, -size, -size);

		glEnd();
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		glBegin(GL_QUADS);
		glColor3d(1, 0, 0);
		glVertex3d(size, size, -size);
		glVertex3d(-size, size, -size);
		glVertex3d(-size, size, size);
		glVertex3d(size, size, size);
		glColor3d(0, 1, 0);
		glVertex3d(size, -size, size);
		glVertex3d(-size, -size, size);
		glVertex3d(-size, -size, -size);
		glVertex3d(size, -size, -size);
		glColor3d(0, 0, 1);
		glVertex3d(size, size, size);
		glVertex3d(-size, size, size);
		glVertex3d(-size, -size, size);
		glVertex3d(size, -size, size);
		glColor3d(1, 1, 0);
		glVertex3d(size, -size, -size);
		glVertex3d(-size, -size, -size);
		glVertex3d(-size, size, -size);
		glVertex3d(size, size, -size);
		glColor3d(1, 0, 1);
		glVertex3d(-size, size, size);
		glVertex3d(-size, size, -size);
		glVertex3d(-size, -size, -size);
		glVertex3d(-size, -size, size);
		glColor3d(1, 1, 1);
		glVertex3d(size, size, -size);
		glVertex3d(size, size, size);
		glVertex3d(size, -size, size);
		glVertex3d(size, -size, -size);
		glEnd();
		glTranslatef(-x, -y, -z);

	}
}

Now, I’m wondering what the non laggy way to do this would be. If anyone can help me out, please reply! Thanks!

You have to enlarge your knowledge: Find out about “Vertex Buffer Objects” and then put a whole chunk of blocks into one of those. But only a chunk. You’ll see the problems of not having VBO Chunks for blocks later too.

You could even start with minimizing the “glBegin()” and “glEnd()” calls. But then you face the “glTranslate” problem.

And instead of VBO’s you could first start with VAO’s or VA’s: Vertex Array Objects / Vertex Arrays.

ok thanks for fast reply!

Mind the edit :slight_smile:

so… not a vbo per block? … or… 1 vbo for all blocks since i just use the static thing?

edit: also, is this how minecraft does it?

NOT a VBO per block. That is the big problem you have and why it slows down: You draw each block seperately.

You have to draw a chunk at once.

As you (probably) know, minecraft has 16x16x128 chunks.
These chunks are further divided into 16x16x16 chunks, which are all in one Display List. Well, a Display list is an old feature and it has been deprecated now, so I guess they have a good reason for that.

You should now have 16x16x16 chunks, where all the block’s vertices are stored in one VBO. You should also “cull” (~delete) the faces, which aren’t visible, because other blocks are connected to them. You should only render the faces of the blocks, which are connected to the air.

… or some other transparent/semi-transparent block like glass.

you know of an example? im trying to do it based off of tutorials but … eh…

edit: was missing a line


package com.sci.bluebear.src;

import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;

public class Chunk
{
	public static final int CHUNK_WIDTH = 16;
	public static final int CHUNK_HEIGHT = 16;
	public static final int CHUNK_LENGTH = 16;
	private Block[][][] blocks;
	private int vboVertexHandle;
	private int vboColorHandle;
	private int startX, startY, startZ;

	public Chunk(int x, int y, int z)
	{
		startX = x;
		startY = y;
		startZ = z;
		
		blocks = new Block[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_LENGTH];

		for(int xx = 0; xx < CHUNK_WIDTH; xx++)
		{
			for(int yy = 0; yy < CHUNK_HEIGHT; yy++)
			{
				for(int zz = 0; zz < CHUNK_WIDTH; zz++)
				{
					blocks[xx][yy][zz] = Block.stone;
				}
			}
		}

		vboColorHandle = GL15.glGenBuffers();
		vboVertexHandle = GL15.glGenBuffers();
		this.rebuildMesh(startX, startY, startZ);
	}

	private void rebuildMesh(int startX2, int startY2, int startZ2)
	{
		vboColorHandle = GL15.glGenBuffers();
		vboVertexHandle = GL15.glGenBuffers();
		FloatBuffer vertexPositionData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_LENGTH * CHUNK_HEIGHT) * 6 * 12);
		FloatBuffer vertexColorData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_LENGTH * CHUNK_HEIGHT) * 6 * 12);
		for(int xx = 0; xx < CHUNK_WIDTH; xx++)
		{
			for(int yy = 0; yy < CHUNK_WIDTH; yy++)
			{
				for(int zz = 0; zz < CHUNK_WIDTH; zz++)
				{
					if(blocks[xx][yy][zz] != null)
					{
						vertexPositionData.put(makeCube((float) startX + xx, (float) startY + yy, (float) startZ + zz));
						vertexColorData.put(makeCubeVertexCol(getCubeColor(blocks[xx][yy][zz])));
					}
				}
			}
		}
		vertexColorData.flip();
		vertexPositionData.flip();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexPositionData, GL15.GL_STATIC_DRAW);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexColorData, GL15.GL_STATIC_DRAW);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
	}

	private float[] makeCubeVertexCol(float[] cubeColorData)
	{
		float[] cubeColors = new float[cubeColorData.length * 4 * 6];
		for(int i = 0; i < cubeColors.length; i++)
		{
			cubeColors[i] = cubeColorData[i % cubeColorData.length];
		}
		return cubeColors;
	}

	private float[] getCubeColor(Block block)
	{
		return new float[]
		{ 1, 0.5f, 1 };
	}

	private float[] makeCube(float x, float y, float z)
	{
		int offset = Block.SIZE / 2;
		return new float[]
		{ x + offset, y + offset, z, x - offset, y + offset, z, x - offset, y + offset, z - Block.SIZE, x + offset, y + offset, z - Block.SIZE, x + offset, y - offset, z - Block.SIZE, x - offset, y - offset, z - Block.SIZE, x - offset, y - offset, z, x + offset, y - offset, z, x + offset, y + offset, z - Block.SIZE, x - offset, y + offset, z - Block.SIZE, x - offset, y - offset, z - Block.SIZE, x + offset, y - offset, z - Block.SIZE, x + offset, y - offset, z, x - offset, y - offset, z, x - offset, y + offset, z, x + offset, y + offset, z, x - offset, y + offset, z - Block.SIZE, x - offset, y + offset, z, x - offset, y - offset, z, x - offset, y - offset, z - Block.SIZE, x + offset, y + offset, z, x + offset, y + offset, z - Block.SIZE, x + offset, y - offset, z - Block.SIZE, x + offset, y - offset, z };
	}

	private float[] getNormalVector()
	{
		return new float[]
		{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, };
	}

	public void tick()
	{

	}

	public void render()
	{
		GL11.glPushMatrix();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
		GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
		GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);
		GL11.glDrawArrays(GL11.GL_QUADS, 0, CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH * 24);
		GL11.glPopMatrix();
	}
}

its not rendering anything… but at least theres no crash…

you need to have [icode]glEnableClientState()[/icode] <- Link

Then you have to enable the GL_VERTEX_ARRAY and the GL_COLOR_ARRAY, afaik.