OpenGl 平面绘图

OpenGl Plane Drawing

本文关键字:绘图 平面 OpenGl      更新时间:2023-10-16

我正在尝试制作一个接地层。我的点和索引由函数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);
}

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

PlaneVerts myVariableName;

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