I’m attempting to make a voxel terrain generator in C++ with OpenGL, the code adds the elements correctly, but some aren’t displayed. I checked if they are out of the screen bounds, as well as out of the znear or zfar, but that is not the case. The code for constructing a cube is as follows.
CubeData createCube(float x, float y, float z, float size) {
float r = (float) (rand() % 255 + 1);
float g = (float)(rand() % 255 + 1);
float b = (float)(rand() % 255 + 1);
float a = 1.0f;
if (r > 1.0f)
r = 100.0f / r;
if (g > 1.0f)
g = 100.0f / g;
if (b > 1.0f)
b = 100.0f / b;
std::vector<GLfloat> _cube_verts = {
-size + x, -size + y, size + z,
size + x, -size + y, size + z,
size + x, size + y, size + z,
-size + x, size + y, size + z,
-size + x, -size + y, -size + z,
size + x, -size + y, -size + z,
size + x, size + y, -size + z,
-size + x, size + y, -size + z
};
std::vector<unsigned int> _indices = {
// front
last_verts_len, (last_verts_len + 1), (last_verts_len + 2),
(last_verts_len + 2), (last_verts_len + 3), last_verts_len,
// top
(last_verts_len + 3), (last_verts_len + 2), (last_verts_len + 6),
(last_verts_len + 6), (last_verts_len + 7), (last_verts_len + 3),
// back
(last_verts_len + 7), (last_verts_len + 6), (last_verts_len + 5),
(last_verts_len + 5), (last_verts_len + 4), (last_verts_len + 7),
// bottom
(last_verts_len + 4), (last_verts_len + 5), (last_verts_len + 1),
(last_verts_len + 1), last_verts_len, (last_verts_len + 4),
// left
(last_verts_len + 4), last_verts_len, (last_verts_len + 3),
(last_verts_len + 3), (last_verts_len + 7), (last_verts_len + 4),
// right
(last_verts_len + 1), (last_verts_len + 5), (last_verts_len + 6),
(last_verts_len + 6), (last_verts_len + 2), (last_verts_len + 1)
};
std::vector<GLfloat> _colors = {
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a
};
CubeData data;
data.verts = _cube_verts;
data.inds = _indices;
data.colors = _colors;
last_verts_len += _cube_verts.size();
cubeIndex++;
return data;
}
CubeData
is just a struct containing the three vectors. And the two append*
functions are just inserts to the end of the vector.
The VAODrawer class is as follows.
#include "VAODrawer.h"
void VAODrawer::SetVerticesWithIndicesAndColors(std::vector<GLfloat> verts, std::vector<GLuint> indices, std::vector<GLfloat> colors) {
this->_VERTS = std::vector<GLfloat>(verts);
this->_INDICES = std::vector<unsigned int>(indices);
this->_COLORS = std::vector<GLfloat>(colors);
}
void VAODrawer::Init() {
_MODEL_MATRIX = glm::mat4(1.0f);
if (_VAO_ID == -1 && _VBO_VERT_ID == -1 && _VBO_COL_ID == -1 && _VAO_ID == -1 && _VERTS.size() != 0 && _INDICES.size() != 0 && _COLORS.size() != 0)
{
glGenVertexArrays(1, &_VAO_ID);
glGenBuffers(1, &_VBO_VERT_ID);
glGenBuffers(1, &_VBO_COL_ID);
glGenBuffers(1, &_IND_ID);
glBindVertexArray(_VAO_ID);
glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
glBufferData(GL_ARRAY_BUFFER, _VERTS.size() * sizeof(GLfloat), &_VERTS[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
glBufferData(GL_ARRAY_BUFFER, _COLORS.size() * sizeof(GLfloat), _COLORS.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IND_ID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _INDICES.size() * sizeof(GLuint), &_INDICES[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
printf(std::string("Ind length" + std::to_string(_INDICES.size()) + "
").c_str());
}
}
const glm::mat4 VAODrawer::GetModelMatrix() {
return _MODEL_MATRIX;
}
void VAODrawer::Draw(const Camera& camera, Shader* shader) {
if (_VAO_ID != -1 && _VBO_VERT_ID != -1 && _VBO_COL_ID != -1) {
glBindVertexArray(_VAO_ID);
glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IND_ID);
shader->UniformMatrix4("MVPMatrix", (camera.GetProjectionMatrix() * camera.GetViewMatrix() * GetModelMatrix()));
glDrawElements(GL_TRIANGLES, _INDICES.size(), GL_UNSIGNED_INT, (void*)0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
else {
this->Init();
}
}
void VAODrawer::Rotate(float x, float y, float z) {
_MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, x, glm::vec3(1, 0, 0));
_MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, y, glm::vec3(0, 1, 0));
_MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, z, glm::vec3(0, 0, 1));
}
void VAODrawer::Dispose() {
glDeleteBuffers(1, &_VBO_VERT_ID);
glDeleteBuffers(1, &_VBO_COL_ID);
glDeleteBuffers(1, &_IND_ID);
glDeleteVertexArrays(1, &_VAO_ID);
}