我正试图根据这个问题的答案来构建一个镶嵌矩形。
How to build data for a tessellated rectangle
我的最终结果只绘制了半个矩形,并且几何体看起来是断开的。
这就是它的样子。
这是我的代码,我试图移植到 VAO 和VBO。
生成数据。
std::vector<float> verticesRect;
std::vector<unsigned int> indicesRect;
nSegments = 16; mSegments = 16;
void Rectangle2::Generate()
{
const int n8 = nSegments * 8; // size of VBO gfx data
const int sz0 = mSegments * n8; // size of VBO gfx data
const int sz1 = (mSegments - 1) * (nSegments - 1) * 6;// size of indices
verticesRect.clear();
indicesRect.clear();
int a,i, j, k, b;
GLfloat x, y, z, dx, dy, l;
glm::vec3 u, v, nor;
dx = 2.0 * (width / float(nSegments - 1));
dy = 2.0 * (height / float(mSegments - 1));
for (a = 0,y = -height, j = 0; j < mSegments; j++, y += dy)
for (x = -width, i = 0; i < nSegments; i++, x += dx)
{
z = 20.0 * sin((x * x) + (y * y));
verticesRect.push_back(x); a++;
verticesRect.push_back(y); a++;
verticesRect.push_back(z); a++;
// Normal ( will be recomputed later)
verticesRect.push_back(0.0); a++;
verticesRect.push_back(0.0); a++;
verticesRect.push_back(1.0); a++;
// TexCoord
verticesRect.push_back((x + width) / (width + width)); a++;
verticesRect.push_back((y + height) / (height + height)); a++;
}
// triangulation indices
for(a = 0, j = 1; j < mSegments; j++ )
for (i = 1; i < nSegments; i++)
{
b = ((nSegments * j) + i) * 8;
// First triangle per quad
indicesRect.push_back(b - 8); a++;
indicesRect.push_back(b - 8 - n8); a++;
indicesRect.push_back(b); a++;
// Second triangle per quad
indicesRect.push_back(b - 8 - n8); a++;
indicesRect.push_back(b - n8); a++;
indicesRect.push_back(b); a++;
// recompute inner normals
for (k = 0; k < 3; k++) {
u[k] = verticesRect[indicesRect[a - 6] + k] - verticesRect[indicesRect[a - 4] + k];
v[k] = verticesRect[indicesRect[a - 5] + k] - verticesRect[indicesRect[a - 4] + k];
}
glm::vec3 cross1 = crossProduct(u, v);
cross1 = glm::normalize(cross1);
for (k = 0; k < 3; k++) {
u[k] = verticesRect[indicesRect[a - 3] + k] - verticesRect[indicesRect[a - 1] + k];
v[k] = verticesRect[indicesRect[a - 2] + k] - verticesRect[indicesRect[a - 1] + k];
}
glm::vec3 cross2 = crossProduct(u, v);
cross2 = glm::normalize(cross2);
for (k = 0; k < 3; k++) {
verticesRect[indicesRect[a - 1] + 3 + k] = 0.5 * (cross1[k] + cross2[k]);
}
}
}
创建 VAO 和VBO
void Rectangle2::init()
{
Generate();
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);
glGenBuffers(1, &m_EBO);
glBindVertexArray(m_VAO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, verticesRect.size() * sizeof(float), &verticesRect[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indicesRect.size(), &indicesRect[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
isInited = true;
}
绘制对象。
glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, indicesRect.size() - 1, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
1条答案
按热度按时间7gcisfzg1#
问题出在索引上...您移植了使用直接索引的my old code,但您使用的是使用逻辑索引的VBO...因此,补救方法是在Generate函数末尾将所有索引除以8(步长大小)(或重新索引整个genere以使用逻辑索引,但这需要更多的编码...)以下是完整的C++/VCL工作代码,以供交叉引用:
和预览:
使用固定函数和nVidia Default attribute locations(太懒了,没有为此制作着色器)...