使用此网格函数的立方体的正确顶点和索引是什么?
What are the correct vertices and indices for a cube using this mesh function?
我正在尝试使用顶点和索引创建一个立方体。我通过将其绘制到图表上找到了顶点,并且对索引进行了仔细的图案化和计算,以在立方体的每一侧形成两个三角形。但是,当我打开程序时,没有显示立方体,这是为什么?我的 vertices/indices 不正确吗?我是否在 createMesh
函数中输入了错误数量的 vertices/indices?还是我做错了?
具有索引和顶点的函数。
void createObjects() {
std::vector<GLuint> indices {
//Top
0, 1, 2,
2, 3, 1,
//Bottom
4, 5, 6,
6, 7, 5,
//Front
8, 9, 10,
10, 11, 9,
//Back
12, 13, 14,
14, 15, 13,
//Left
16, 17, 18,
18, 19, 17,
//Right
20, 21, 22,
22, 23, 21
/*0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2*/
};
std::vector<GLfloat> vertices {
//Top
-1, 1, -1, //0
1, 1, -1 //1
-1, 1, 1, //2
1, 1, 1, //3
//Bottom
-1, -1, -1, //4
1, -1, -1, //5
-1, -1, 1, //6
1, -1, 1 //7
//Front
-1, 1, 1, //8
1, 1, 1, //9
-1, -1, 1, //10
1, -1, 1, //11
//Back
-1, 1, -1, //12
1, 1, -1, //13
-1, -1, -1, //14
1, -1, -1, //15
//Left
-1, 1, 1, //16
-1, 1, -1, //17
-1, -1, 1, //18
-1, -1, -1, //19
//Right
1, 1, 1, //20
1, 1, -1, //21
1, -1, 1, //22
1, -1, -1 //23
/*-1, -1, 0,
0, -1, 1,
1, -1, 0,
0, 1, 0*/
};
for (std::size_t x = 0; x < 2; x++) {
meshVector.emplace_back(new Mesh());
meshVector[x]->createMesh(vertices, indices, 24, 12);
}
}
创建网格函数和绘制函数
void Mesh::createMesh(const std::vector<GLfloat>& vertices, const std::vector<GLuint>& indices, GLuint numOfVertices, GLuint numOfIndices) {
indexCount = numOfIndices;
//Binding
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
//Information
//VBO Information
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(*vertices.data()) * numOfVertices, vertices.data(), GL_STATIC_DRAW);
//IBO Information
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices.data()) * numOfIndices, indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
//Unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Mesh::renderMesh() {
//Binding
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
//Rendering
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
//Unbinding
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
删除了 GLuint numOfVertices/Indices
并将 sizeof(*vertices.data()) * numOfVertices
替换为 sizeof(GLuint) * indices.size()`。最后,用新坐标替换立方体顶点和索引
std::vector<GLuint> indices {
//Top
2, 6, 7,
2, 3, 7,
//Bottom
0, 4, 5,
0, 1, 5,
//Left
0, 2, 6,
0, 4, 6,
//Right
1, 3, 7,
1, 5, 7,
//Front
0, 2, 3,
0, 1, 3,
//Back
4, 6, 7,
4, 5, 7
};
std::vector<GLfloat> vertices {
-1, -1, 0.5, //0
1, -1, 0.5, //1
-1, 1, 0.5, //2
1, 1, 0.5, //3
-1, -1, -0.5, //4
1, -1, -0.5, //5
-1, 1, -0.5, //6
1, 1, -0.5 //7
};
就是这样。
我正在尝试使用顶点和索引创建一个立方体。我通过将其绘制到图表上找到了顶点,并且对索引进行了仔细的图案化和计算,以在立方体的每一侧形成两个三角形。但是,当我打开程序时,没有显示立方体,这是为什么?我的 vertices/indices 不正确吗?我是否在 createMesh
函数中输入了错误数量的 vertices/indices?还是我做错了?
具有索引和顶点的函数。
void createObjects() {
std::vector<GLuint> indices {
//Top
0, 1, 2,
2, 3, 1,
//Bottom
4, 5, 6,
6, 7, 5,
//Front
8, 9, 10,
10, 11, 9,
//Back
12, 13, 14,
14, 15, 13,
//Left
16, 17, 18,
18, 19, 17,
//Right
20, 21, 22,
22, 23, 21
/*0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2*/
};
std::vector<GLfloat> vertices {
//Top
-1, 1, -1, //0
1, 1, -1 //1
-1, 1, 1, //2
1, 1, 1, //3
//Bottom
-1, -1, -1, //4
1, -1, -1, //5
-1, -1, 1, //6
1, -1, 1 //7
//Front
-1, 1, 1, //8
1, 1, 1, //9
-1, -1, 1, //10
1, -1, 1, //11
//Back
-1, 1, -1, //12
1, 1, -1, //13
-1, -1, -1, //14
1, -1, -1, //15
//Left
-1, 1, 1, //16
-1, 1, -1, //17
-1, -1, 1, //18
-1, -1, -1, //19
//Right
1, 1, 1, //20
1, 1, -1, //21
1, -1, 1, //22
1, -1, -1 //23
/*-1, -1, 0,
0, -1, 1,
1, -1, 0,
0, 1, 0*/
};
for (std::size_t x = 0; x < 2; x++) {
meshVector.emplace_back(new Mesh());
meshVector[x]->createMesh(vertices, indices, 24, 12);
}
}
创建网格函数和绘制函数
void Mesh::createMesh(const std::vector<GLfloat>& vertices, const std::vector<GLuint>& indices, GLuint numOfVertices, GLuint numOfIndices) {
indexCount = numOfIndices;
//Binding
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
//Information
//VBO Information
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(*vertices.data()) * numOfVertices, vertices.data(), GL_STATIC_DRAW);
//IBO Information
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices.data()) * numOfIndices, indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
//Unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Mesh::renderMesh() {
//Binding
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
//Rendering
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
//Unbinding
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
删除了 GLuint numOfVertices/Indices
并将 sizeof(*vertices.data()) * numOfVertices
替换为 sizeof(GLuint) * indices.size()`。最后,用新坐标替换立方体顶点和索引
std::vector<GLuint> indices {
//Top
2, 6, 7,
2, 3, 7,
//Bottom
0, 4, 5,
0, 1, 5,
//Left
0, 2, 6,
0, 4, 6,
//Right
1, 3, 7,
1, 5, 7,
//Front
0, 2, 3,
0, 1, 3,
//Back
4, 6, 7,
4, 5, 7
};
std::vector<GLfloat> vertices {
-1, -1, 0.5, //0
1, -1, 0.5, //1
-1, 1, 0.5, //2
1, 1, 0.5, //3
-1, -1, -0.5, //4
1, -1, -0.5, //5
-1, 1, -0.5, //6
1, 1, -0.5 //7
};
就是这样。