为什么有些顶点没有画出来
Why are some vertices not drawing?
我试图用OpenGL在c++中制作一个体素地形生成器,代码正确地添加了元素,但有些没有显示。我检查了它们是否在屏幕边界之外,但事实并非如此。构造立方体的代码为。
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,
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,
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;
}
添加和分配数据集的代码是
void Recalibrated::Init() {
camera = PerspectiveCamera(75.0f, (float)GameApplication::GetWidth() / (float)GameApplication::GetHeight(), 0.01f, 2000.0f);
camera.Translate(0.0f, 0.0f, 16.0f * 8.0f * 2.0f);
shader = Shader("../Recalibrated/vertex.glsl", "../Recalibrated/frag.glsl");
std::vector<GLfloat> verts;
std::vector<unsigned int> inds;
std::vector<GLfloat> colors;
CubeData cube1 = createCube(10 * 8, 0, 0, 4);
appendFloatVector(&verts, cube1.verts);
appendFloatVector(&colors, cube1.colors);
appendUnsignedIntVector(&inds, cube1.inds);
CubeData cube2 = createCube(10 * 8, 0, 0, 4);
appendFloatVector(&verts, cube2.verts);
appendFloatVector(&colors, cube2.colors);
appendUnsignedIntVector(&inds, cube2.inds);
//int x, y, z;
//for (x = 0; x < 32; x++) {
//}
drawer = VAODrawer();
drawer.SetVerticesWithIndicesAndColors(verts, inds, colors);
drawer.Init();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
CubeData
只是一个包含三个向量的结构体。两个append*
函数只是插入到向量的末尾。
帮忙吗?
编辑:我忘记了VAODrawer类。这可能很重要,所以代码是。
#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.data(), 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()) + "n").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);
}
我知道问题是什么了。我认为指标被每个顶点的浮点数所抵消,而不是顶点的实际数量。将偏移量更改为添加8(实际顶点的数量)使所有立方体都显示。
相关文章:
- 如何循环打印顶点结构
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么顶点数组对象会导致错误?
- 为什么GLM不翻译顶点
- 为什么我的顶点缓冲区对象出现访问冲突错误?
- C++提升图 - 为什么我有一个额外的顶点
- 在 OpenGL 中,为什么除非我再次重新指定顶点属性,否则我的缓冲区对象不会绘制?
- 在glsl中使用实例化时,为什么纹理缓冲区比顶点输入快
- 为什么我的 openGL 顶点缓冲区对象不会绘制任何内容?
- 为什么 OpenGL 多边形会忽略两个顶点
- 为什么要为一个属性指定不同的顶点格式
- 为什么Boost图库在删除顶点时会使所有迭代器无效
- 为什么在调用 glDrawArray 之前不需要绑定顶点缓冲区对象?
- 图形、顶点、STL 列表:为什么列表是空的
- 从.dae加载顶点会使我的程序变慢,为什么
- 为什么此顶点缓冲区对象无法更新
- 图,为什么图顶点的度数和总是偶的
- 为什么Box2D/Farseer EdgeShape定义了4个顶点
- 为什么有些顶点没有画出来