简体   繁体   中英

OpenGL using VBO with index

So I'm building a basic engine to render models and transformations, I did it with regular VBOs (without index), was working with no issues, but I decided to use VBO with index instead. Here are the important bits of my code:

Relevant global variables:

GLuint *modelBuffers;
GLuint *indexBuffers;

Relevant data structures:

struct databaseStruct
{
    int moveSize;
    int modelSize;
    int spinSize;
    int tracerSize;

    int currentMove;
    int currentSpin;
    int currentModel;
    int currentTracer;

    vector<moveNode> moveList;
    vector<modelNode> modelList;
    vector<spinNode> spinList;
    vector<tracerNode> tracerList;
    groupStruct scene;
} database;

struct modelNode
{
    int id;
    int numTri;
    string modelName;
    vector<GLfloat> model;
    vector<GLfloat> normals;
    vector<int> indices;
};

Model initialization at the beginning of the program:

void initModels(){
    glEnableClientState(GL_VERTEX_ARRAY);
    modelBuffers = new GLuint[database.modelSize];
    indexBuffers = new GLuint[database.modelSize];
    glGenBuffers(database.modelSize, modelBuffers);
    glGenBuffers(database.modelSize, indexBuffers);
    vector<modelNode> mList = database.modelList;
    for (int i = 0; i < database.modelSize; i++){
        glBindBuffer(GL_ARRAY_BUFFER, modelBuffers[mList[i].id]);
        glBufferData(GL_ARRAY_BUFFER, mList[i].model.size()*sizeof(GLfloat), &(mList[i].model[0]), GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffers[mList[i].id]);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, mList[i].indices.size() * sizeof(GLint), &(mList[i].indices[0]), GL_STATIC_DRAW);
    }
}

Draw call on render scene:

void drawModel(element* elem){
    if (elem != NULL){
        modelNode *it = &database.modelList[elem->nodeRef];
        glBindBuffer(GL_ARRAY_BUFFER, modelBuffers[it->id]);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffers[it->id]);
        glVertexPointer(3, GL_FLOAT, 0, 0);
        glDrawElements(GL_TRIANGLES, it->numTri*3, GL_INT, 0);
    }
}

The program structure works, as all I have changed since using simple VBOs was structure changes to host the indices and the OpenGL calls for handling them. If needed, I can provide the previous working versions.

you are overlapping the buffers, create separate buffers for the vertex info and indexes

void initModels(){
    glEnableClientState(GL_VERTEX_ARRAY);
    modelBuffers = new GLuint[database.modelSize*2];//twice as large
    glGenBuffers(database.modelSize*2, modelBuffers);//creating twice as many
    vector<modelNode> mList = database.modelList;
    for (int i = 0; i < database.modelSize; i++){
        glBindBuffer(GL_ARRAY_BUFFER, modelBuffers[mList[i].id*2]);
        glBufferData(GL_ARRAY_BUFFER, mList[i].model.size()*sizeof(GLfloat), &(mList[i].model[0]), GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelBuffers[mList[i].id*2+1]);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, mList[i].indices.size() * sizeof(int), &(mList[i].indices[0]), GL_STATIC_DRAW);
    }
}

void drawModel(element* elem){
    if (elem != NULL){
        modelNode *it = &database.modelList[elem->nodeRef];
        glBindBuffer(GL_ARRAY_BUFFER, modelBuffers[it->id*2]);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelBuffers[it->id*2+1]);
        glVertexPointer(3, GL_FLOAT, 0, 0);
        glDrawElements(GL_TRIANGLES, it->numTri*3, GL_INT, 0);
    }
}

you'll see I created twice as many buffers and each array buffer is the id*2 in modelBuffers and the index buffer is the id*2 + 1 in modelBuffers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM