OpenGL 生成三角形网格
OpenGL generate triangle mesh
我是 OpenGl 的初学者,我正尝试在 OpenGL 中绘制三角形网格,例如 this,我的问题是它没有绘制,我不明白为什么。此外,如果我打印顶点数组,则所有顶点的 x 和 y 坐标保持不变。 x 和 z 坐标都应位于 +1 和 -1 之间。当 m_meshResolution = 1
时,坐标似乎是正确的,但在其他情况下则不然。我认为有一种更简单的方法可以尝试执行此操作,因此欢迎所有建议。
int size = m_meshResolution * m_meshResolution * 2 * 3 * 3;
float* positions = new float[size]();
double triangleWidth = 2 / m_meshResolution;
float startX = -1.0f;
float startZ = 1.0f;
float x1 = 0;
float x2 = 0;
float z1 = 0;
float z2 = 0;
int index = 0;
//For each row
for (int r = 0; r < m_meshResolution; r++) {
//For each column
for (int c = 0; c < m_meshResolution; c++) {
x1 = startX + c*divider;
x2 = startX + (c+1)*divider;
z1 = startZ - r*divider;
z2 = startZ -(r+1)*divider;
//Generate one triangle
positions[index++] = x1;
positions[index++] = 0;
positions[index++] = z1;
positions[index++] = x2;
positions[index++] = 0;
positions[index++] = z2;
positions[index++] = x2;
positions[index++] = 0;
positions[index++] = z1;
//Generate other triangle
positions[index++] = x1;
positions[index++] = 0;
positions[index++] = z1;
positions[index++] = x1;
positions[index++] = 0;
positions[index++] = z2;
positions[index++] = x2;
positions[index++] = 0;
positions[index++] = z2;
}
//Set buffers
glGenBuffers(1, &m_positionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/);
glEnableVertexAttribArray(0);
//Draw
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, m_meshResolution*m_meshResolution*2*3);
positions
是一个指针,而sizeof(positions)
returns4或8个字节,这取决于体系结构,但是glBufferData
的第二个参数告诉我们
size
Specifies the size in bytes of the buffer object's new data store.
你应该使用 sizeof(float) * size
作为第二个参数。
我是 OpenGl 的初学者,我正尝试在 OpenGL 中绘制三角形网格,例如 this,我的问题是它没有绘制,我不明白为什么。此外,如果我打印顶点数组,则所有顶点的 x 和 y 坐标保持不变。 x 和 z 坐标都应位于 +1 和 -1 之间。当 m_meshResolution = 1
时,坐标似乎是正确的,但在其他情况下则不然。我认为有一种更简单的方法可以尝试执行此操作,因此欢迎所有建议。
int size = m_meshResolution * m_meshResolution * 2 * 3 * 3;
float* positions = new float[size]();
double triangleWidth = 2 / m_meshResolution;
float startX = -1.0f;
float startZ = 1.0f;
float x1 = 0;
float x2 = 0;
float z1 = 0;
float z2 = 0;
int index = 0;
//For each row
for (int r = 0; r < m_meshResolution; r++) {
//For each column
for (int c = 0; c < m_meshResolution; c++) {
x1 = startX + c*divider;
x2 = startX + (c+1)*divider;
z1 = startZ - r*divider;
z2 = startZ -(r+1)*divider;
//Generate one triangle
positions[index++] = x1;
positions[index++] = 0;
positions[index++] = z1;
positions[index++] = x2;
positions[index++] = 0;
positions[index++] = z2;
positions[index++] = x2;
positions[index++] = 0;
positions[index++] = z1;
//Generate other triangle
positions[index++] = x1;
positions[index++] = 0;
positions[index++] = z1;
positions[index++] = x1;
positions[index++] = 0;
positions[index++] = z2;
positions[index++] = x2;
positions[index++] = 0;
positions[index++] = z2;
}
//Set buffers
glGenBuffers(1, &m_positionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/);
glEnableVertexAttribArray(0);
//Draw
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, m_meshResolution*m_meshResolution*2*3);
positions
是一个指针,而sizeof(positions)
returns4或8个字节,这取决于体系结构,但是glBufferData
的第二个参数告诉我们
size Specifies the size in bytes of the buffer object's new data store.
你应该使用 sizeof(float) * size
作为第二个参数。