I’ve been using OpenGL and C++ to make a voxel engine, and for some reason, my code works, but only shows 2 blocks on the y axis, even though I specified in the main function to make the world 8x8x8 blocks.
chunk.h
#ifndef CHUNK_CLASS_H
#define CHUNK_CLASS_H
#include <glad/glad.h>
#include <model.h>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <cmath>
class chunk
{
public:
model* chunk_mesh;
int*** blocks;
chunk(int size_x, int size_y, int size_z);
~chunk();
bool get_block_status(int*** block_data, int x_pos, int y_pos, int z_pos, int x_size, int y_size, int z_size);
void render(camera player_cam);
void destroy();
int*** create_blocks(int x_size, int y_size, int z_size);
};
#endif
chunk.cpp
#include <chunk.h>
chunk::chunk(int size_x, int size_y, int size_z)
{
std::srand(static_cast<unsigned int>(std::time(nullptr)));
std::vector<GLfloat> vertex_data;
blocks = create_blocks(size_x, size_y, size_z);
for (int y = 0; y < size_y - 1; y++)
{
for (int x = 0; x < size_x; x++)
{
for (int z = 0; z < size_z; z++)
{
int block_id = blocks[x][y][z];
if (block_id == 0)
{
// front face
GLfloat front_face_data[] =
{
1.0 + x, 1.0 + y, 1.0 + z, 0.7, 0.7, 0.7, 1.0, 1.0,
0.0 + x, 0.0 + y, 1.0 + z, 0.7, 0.7, 0.7, 0.0, 0.0,
1.0 + x, 0.0 + y, 1.0 + z, 0.7, 0.7, 0.7, 1.0, 0.0,
1.0 + x, 1.0 + y, 1.0 + z, 0.7, 0.7, 0.7, 1.0, 1.0,
0.0 + x, 1.0 + y, 1.0 + z, 0.7, 0.7, 0.7, 0.0, 1.0,
0.0 + x, 0.0 + y, 1.0 + z, 0.7, 0.7, 0.7, 0.0, 0.0,
};
for (float attr : front_face_data)
{
vertex_data.push_back(attr);
}
// back face
GLfloat back_face_data[] =
{
1.0 + x, 0.0 + y, 0.0 + z, 0.4, 0.4, 0.4, 1.0, 0.0,
0.0 + x, 0.0 + y, 0.0 + z, 0.4, 0.4, 0.4, 0.0, 0.0,
1.0 + x, 1.0 + y, 0.0 + z, 0.4, 0.4, 0.4, 1.0, 1.0,
0.0 + x, 0.0 + y, 0.0 + z, 0.4, 0.4, 0.4, 0.0, 0.0,
0.0 + x, 1.0 + y, 0.0 + z, 0.4, 0.4, 0.4, 0.0, 1.0,
1.0 + x, 1.0 + y, 0.0 + z, 0.4, 0.4, 0.4, 1.0, 1.0,
};
for (float attr : back_face_data)
{
vertex_data.push_back(attr);
}
// left face
GLfloat left_face_data[] =
{
0.0 + x, 1.0 + y, 1.0 + z, 0.5, 0.5, 0.5, 1.0, 1.0,
0.0 + x, 0.0 + y, 0.0 + z, 0.5, 0.5, 0.5, 0.0, 0.0,
0.0 + x, 0.0 + y, 1.0 + z, 0.5, 0.5, 0.5, 1.0, 0.0,
0.0 + x, 1.0 + y, 1.0 + z, 0.5, 0.5, 0.5, 1.0, 1.0,
0.0 + x, 1.0 + y, 0.0 + z, 0.5, 0.5, 0.5, 0.0, 1.0,
0.0 + x, 0.0 + y, 0.0 + z, 0.5, 0.5, 0.5, 0.0, 0.0,
};
for (float attr : left_face_data)
{
vertex_data.push_back(attr);
}
// right face
GLfloat right_face_data[] =
{
1.0 + x, 0.0 + y, 1.0 + z, 0.3, 0.3, 0.3, 1.0, 0.0,
1.0 + x, 0.0 + y, 0.0 + z, 0.3, 0.3, 0.3, 0.0, 0.0,
1.0 + x, 1.0 + y, 1.0 + z, 0.3, 0.3, 0.3, 1.0, 1.0,
1.0 + x, 0.0 + y, 0.0 + z, 0.3, 0.3, 0.3, 0.0, 0.0,
1.0 + x, 1.0 + y, 0.0 + z, 0.3, 0.3, 0.3, 0.0, 1.0,
1.0 + x, 1.0 + y, 1.0 + z, 0.3, 0.3, 0.3, 1.0, 1.0,
};
for (float attr : right_face_data)
{
vertex_data.push_back(attr);
}
// top face
if (!get_block_status(blocks, x, y + 1, z, size_x, size_y, size_z))
{
GLfloat top_face_data[] =
{
1.0 + x, 1.0 + y, 1.0 + z, 1.0, 1.0, 1.0, 1.0, 1.0,
0.0 + x, 1.0 + y, 0.0 + z, 1.0, 1.0, 1.0, 0.0, 0.0,
0.0 + x, 1.0 + y, 1.0 + z, 1.0, 1.0, 1.0, 1.0, 0.0,
1.0 + x, 1.0 + y, 1.0 + z, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0 + x, 1.0 + y, 0.0 + z, 1.0, 1.0, 1.0, 0.0, 1.0,
0.0 + x, 1.0 + y, 0.0 + z, 1.0, 1.0, 1.0, 0.0, 0.0,
};
for (float attr : top_face_data)
{
vertex_data.push_back(attr);
}
}
// bottom face
if (!get_block_status(blocks, x, y - 1, z, size_x, size_y, size_z))
{
GLfloat bottom_face_data[] =
{
0.0 + x, 0.0 + y, 1.0 + z, 0.15, 0.15, 0.15, 1.0, 0.0,
0.0 + x, 0.0 + y, 0.0 + z, 0.15, 0.15, 0.15, 0.0, 0.0,
1.0 + x, 0.0 + y, 1.0 + z, 0.15, 0.15, 0.15, 1.0, 1.0,
0.0 + x, 0.0 + y, 0.0 + z, 0.15, 0.15, 0.15, 0.0, 0.0,
1.0 + x, 0.0 + y, 0.0 + z, 0.15, 0.15, 0.15, 0.0, 1.0,
1.0 + x, 0.0 + y, 1.0 + z, 0.15, 0.15, 0.15, 1.0, 1.0,
};
for (float attr : bottom_face_data)
{
vertex_data.push_back(attr);
}
}
std::cout << vertex_data.size() << 'n';
}
}
}
}
chunk_mesh = new model(vertex_data.data(), vertex_data.size());
}
chunk::~chunk()
{
delete chunk_mesh;
delete blocks;
}
int*** chunk::create_blocks(int x_size, int y_size, int z_size)
{
int*** blocks = new int** [x_size];
for (int x = 0; x <= x_size; x++)
{
blocks[x] = new int* [y_size];
for (int y = 0; y <= y_size; y++)
{
blocks[x][y] = new int[z_size];
for (int z = 0; z <= z_size; z++)
{
int random_value = std::round(static_cast<double>(std::rand()) / RAND_MAX);
std::cout << random_value;
blocks[x][y][z] = random_value;
}
}
}
return blocks;
}
bool chunk::get_block_status(int*** block_data, int x_pos, int y_pos, int z_pos, int x_size, int y_size, int z_size) {
int block_id = block_data[x_pos][y_pos][z_pos];
if (block_id)
{
if (block_id == 0) {
return false;
}
else {
return true;
}
}
return true;
}
void chunk::render(camera player_cam)
{
chunk_mesh->render(player_cam);
}
void chunk::destroy()
{
chunk_mesh->destroy();
}
main.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb/stb_image.h>
#include <texture.h>
#include <chunk.h>
#include <camera.h>
#include <window.h>
#include <shader.h>
#include <model.h>
#include <iostream>
#include <vector>
const int width = 2400;
const int height = 1300;
bool wire_frame = false;
void check_input(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
if (wire_frame)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
wire_frame = false;
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
wire_frame = true;
}
}
int main()
{
glfwInit();
window window(width, height, "Voxel Engine");
gladLoadGL();
glViewport(0, 0, width, height);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
// world
chunk chunk1(8, 8, 8);
// camera
camera player_cam(window.glfw_window, width, height);
double prev_time = glfwGetTime();
double delta_time = 0;
float fps = 0;
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
while (!glfwWindowShouldClose(window.glfw_window))
{
check_input(window.glfw_window);
glfwPollEvents();
glClearColor(0.08, 0.12, 0.24, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
double crnt_time = glfwGetTime();
delta_time = crnt_time - prev_time;
fps = (float)1.0 / delta_time;
//std::cout << fps << 'n';
prev_time = crnt_time;
player_cam.update();
chunk1.render(player_cam);
window.swap_buffers();
}
window.destroy();
chunk1.destroy();
glfwTerminate();
return 0;
}
I tried to see if it was possibly a memory leak, or the positions were wrong, but I honestly cant find anything that could possibly be wrong.