c ++ OpenGL:Mesh仅出现在窗口的下半部分

时间:2016-09-29 19:42:36

标签: c++ opengl

我刚刚开始学习OpenGL,我认为我的索引数组公式存在问题。

我正在尝试使用IBO渲染方形地形。当我使用glDrawElements进行绘制时,结果将仅显示在屏幕的下半部分,所有内容都紧密地包裹在矩形形状中,而当我使用glDrawArrays时,它会完美地呈现为方形和居中啮合。

我从灰度加载顶点高度值,这是我加载顶点和创建索引的方法:

对于顶点:从右到左,从下到上

int numVertices = image.width() * image.height() * 3;
float rowResize = image.width() / 2;
float colResize = image.height() / 2;

GLfloat* vertexData;
vertexData = new GLfloat[numVertices];
int counter = 0;

for (float j = 0; j < col; j++){
    for (float i = 0; i < row; i++){
        vertexData[counter++] = (i - rowResize) / rowResize;
        vertexData[counter++] = (j - colResize) / colResize;
        vertexData[counter++] = image.getColor(i,j) / 255.0f;
    }
}

对于索引:尝试遵循{0,1,2,1,3,2 ...}的顺序

2     3
-------
|\    |
| \   |
|  \  |
|   \ |
|    \|
-------
0     1

int numIndices = (row - 1) * (col - 1) * 2 * 3;
unsigned short* indexData = new unsigned short[numIndices];

counter = 0;


for (short y = 0; y < col - 1; y++){
    for (short x = 0; x < row - 1; x++){
        // lower triangle
        short L_first = y*row + x;
        short L_second = L_first + 1;
        short L_third = L_first + row;

        //upper triangle
        short U_first = L_first + 1;
        short U_second = U_first + row;
        short U_third = U_second - 1;

        indexData[counter++] = L_first;
        indexData[counter++] = L_second;
        indexData[counter++] = L_third;

        indexData[counter++] = U_first;
        indexData[counter++] = U_second;
        indexData[counter++] = U_third;
    }
}

我初始化了VAO,VBO和IBO,然后生成,绑定,链接每个缓冲区对象的数据,然后取消绑定所有。

在游戏循环中我有:

glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, 0);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);    

glDrawArrays(GL_POINTS, 0, numVertices);
//glDrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);

glfwSwapBuffers(window);

由于从顶点绘制工作和从索引绘制没有,我的索引生成可能有什么问题?

感谢您的帮助!

(奇怪的是:我只是尝试了另一个灰度图像,它适用于从顶点GL_POINTS和索引GL_TRIANGLE_STRIP绘制... ... welp)

图片

Using glDrawArrays

Using glDrawElements

0 个答案:

没有答案