OpenGl平面绘图

时间:2017-11-13 15:44:17

标签: c++ opengl graphics glsl shader

我正在尝试制作地平面。我的点和索引由函数makeplanevertsmakeplaneindices正确生成,但不显示地平面。谁能看到错误?

void makePlaneVerts(int dimensions){

    num = (dimensions)*(dimensions);
    number = num * 3;
    PlaneVerts = new GLfloat[number];
    int counter = 0;
    for (int i = 0; i < dimensions; i++)
    {
        for (int j = 0; j < dimensions; j++)
        {
            PlaneVerts[counter++] = (float)j; cout << counter << j<<endl;
            PlaneVerts[counter++] = (float)0; cout << counter << 0<<endl;
            PlaneVerts[counter++] = (float)i; cout << counter << i<<endl;
        }
    }
    return ;
}

GLushort *Planeindices;
int NumIndices;

void makePlaneIndices(int dimensions) {

    NumIndices = (dimensions - 1) * (dimensions - 1) * 2 * 3;
    Planeindices = new GLushort[NumIndices];
    int count = 0;

    for (int row = 0; row < dimensions - 1; row++)
    {
        for (int col = 0; col < dimensions - 1; col++)
        {
            Planeindices[count++] = dimensions * row + col;
            cout << count << ":" << dimensions * row + col;
            Planeindices[count++] = dimensions * row + col + dimensions;
            cout << count << ":" << dimensions * row + col + dimensions;
            Planeindices[count++] = dimensions * row + col + dimensions + 1;
            cout << count << ":" << dimensions * row + col + dimensions + 1 << endl;
            Planeindices[count++] = dimensions * row + col;
            cout << count << ":" << dimensions * row + col;
            Planeindices[count++] = dimensions * row + col + dimensions + 1;
            cout << count << ":" << dimensions * row + col + dimensions + 1;
            Planeindices[count++] = dimensions * row + col + 1;
            cout << count << ":" << dimensions * row + col + 1 << endl;
        }
    }
    return; 
}

初始化平面开口vbo一个顶点一个索引

void initPlane() {

    GLuint myBufferID;
    glGenBuffers(1, &myBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, myBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(PlaneVerts), PlaneVerts, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    GLuint indexBufferID;
    glGenBuffers(1, &indexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Planeindices), Planeindices, GL_STATIC_DRAW);
}

通过匹配索引与顶点显示播放

void displayPlane() {
   glDrawElements(GL_TRIANGLES, number/3, GL_UNSIGNED_SHORT, 0);
}

1 个答案:

答案 0 :(得分:0)

您甚至没有变量名称。定义变量时,需要为其命名:

PlaneVerts myVariableName;

如果您不熟悉这个概念,那么您可能以前没有做过多编程,退一步看看基本的C ++教程。