[英]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. 因此,我正在构建一个用于渲染模型和转换的基本引擎,我使用常规VBO(无索引)完成了此工作,没有任何问题,但是我决定改为使用具有索引的VBO。 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. 程序的结构有效,因为自从使用简单的VBO以来,我所做的所有更改都是对结构的更改,以托管索引和用于处理索引的OpenGL调用。 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
你会看到我创建的两倍尽可能多的缓冲区,并且每个数组缓冲区是
id*2
在modelBuffers
和索引缓冲区是id*2 + 1
在modelBuffers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.