OpenGL Voxel Engine chunks won't render

Hey OGL forums! My names verideth, and I’ve got a question

I’ve recently been writing in OpenGL modern, and I’m now stumped. I wrote up a basic chunk class, but for some odd reason it won’t render. Here is my code:

void Chunk::createMesh()
{
	int i = 0;

	for (int x = 0; x < CHUNK_SIZE; x++) 
	{
		for (int y = 0; y < CHUNK_HEIGHT; y++)
		{
			for (int z = 0; z < CHUNK_SIZE; z++)
			{
				uint8_t currentBlock = this->blockCur[x][y][z];

				// View from negative x
				this->vertex[i++] = byte4(x, y, z, currentBlock);
				this->vertex[i++] = byte4(x, y, z + 1, currentBlock);
				this->vertex[i++] = byte4(x, y + 1, z, currentBlock);
				this->vertex[i++] = byte4(x, y + 1, z, currentBlock);
				this->vertex[i++] = byte4(x, y, z + 1, currentBlock);
				this->vertex[i++] = byte4(x, y + 1, z + 1, currentBlock);

				// View from positive x
				this->vertex[i++] = byte4(x + 1, y, z, currentBlock);
				this->vertex[i++] = byte4(x + 1, y + 1, z, currentBlock);
				this->vertex[i++] = byte4(x + 1, y, z + 1, currentBlock);
				this->vertex[i++] = byte4(x + 1, y + 1, z, currentBlock);
				this->vertex[i++] = byte4(x + 1, y + 1, z + 1, currentBlock);
				this->vertex[i++] = byte4(x + 1, y, z + 1, currentBlock);
			}
		}
	}

	std::cout << i << std::endl;
	this->elements = i;
	
	glGenBuffers(1, &this->vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, this->elements * sizeof(*this->vertex), this->vertex, GL_STATIC_DRAW);

	this->shader.loadShader("cube.vs", "cube.fs");
	this->attribCoord3dChunk = glGetAttribLocation(this->shader.program, "coord3d");

	if (this->attribCoord3dChunk < 0)
	{
		std::cout << "attribCoord3dChunk was negative!" << std::endl;
	}

	glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
	glVertexAttribPointer(
		this->attribCoord3dChunk, // attribute
		3,                 // number of elements per vertex, here (R,G,B)
		GL_FLOAT,          // the type of each element
		GL_FALSE,          // take our values as-is
		0,                 // no extra data between each position
		(GLvoid*)0                  // offset of first element
	);

	this->loaded = true;
}

void Chunk::render()
{
	if (this->loaded)
	{
		if (!this->elements)
		{
			std::cout << "Ello" << std::endl;
			return;
		}

		glEnableVertexAttribArray(this->attribCoord3dChunk);
		glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
		glDrawArrays(GL_POINTS, 0, this->elements);
		glDisableVertexAttribArray(this->attribCoord3dChunk);
	}
}

cube.vs:

#version 330

attribute vec3 coord3d;
attribute vec3 v_color;
uniform mat4 mvp;
varying vec3 f_color;

void main(void) {
  gl_Position = mvp * vec4(coord3d, 1.0);
  f_color = v_color;
}

cube.fs:

#version 330

varying vec3 f_color;

void main(void) {
  gl_FragColor = vec4(f_color.r, f_color.g, f_color.b, 1.0);
}

I’m calling createMesh inside of my init function, and render inside of my game loop.

Thanks for reading!! All helps appreciated :slight_smile:

[–]Fish_Biter 2 points 2 years ago
I don’t think you can do much better than a single vbo. There are some out-there strategies like bindless gl which reduce draw cost, but come with their own difficulties.
One piece of advice I’d offer based on your “decrease fps incredibly” statement is to always profile and not make assumptions. I doubt some redundant shims would impact fps: on any modern graphics card draw calls are normally the overhead.

Ethan Stark