PDA

View Full Version : Error with VBO



foollove
04-20-2003, 03:58 PM
Hi,are there any friends who can check the following code for me.
I use the memory allocated in the same way to render with Vertex Array, there are no errors. BUT if i use it with Vertex Buffer Object as follows, it corrups. Who can find it for me. Thanks a lot.


NonEndMeshStripIndex[NonEndTotalStripNum][22] is the index matrix£»
NonEndTotalMemorySink[NonEndTotalVertexNum *8] holds the vertex position,normal,texture corrdinates.
NonEndTotalVertexNum is the total vertex number£»

#define BUFFER_OFFSET(i) ((char*)NULL +(i)*sizeof(float))

void CAniTree::AllocateMemory()
{
//////Allocate the memory for the vertex array********************************************* *****
int i;
try
{
/////////------------------------------------------------------------
NonEndMeshStripIndex = new unsigned int*[NonEndTotalStripNum];
for(i=0;i<NonEndTotalStripNum;i++)
NonEndMeshStripIndex[i] = new unsigned int[22];

}
catch(...)
{
AfxMessageBox("Error to allocate memory during AllocateMemory!");
throw;
}

//////////////NonEnd parts********************************************* **************************
int AGPMemorySize = NonEndTotalVertexNum*8;
NonEndTotalMemorySink = new float[AGPMemorySize];
if(NonEndTotalMemorySink == NULL)
{
CString tmp;
tmp.Format("Error to allocate memory in AllocateMemory! Need %d M AGP memory",AGPMemorySize/1024/1024);
AfxMessageBox(tmp);
exit(0);
}

return;

}


void CAniTree::InitVertexArray()
{
////////Non-branches****************************************** ************************************************** *************
//////Index
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, IndexBufferNonEnd);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, NonEndTotalStripNum*22*sizeof(GLuint), NonEndMeshStripIndex, GL_STATIC_DRAW_ARB);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, VertexBufferNonEnd);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, NonEndTotalVertexNum * 8 * sizeof(GLfloat), NonEndTotalMemorySink, GL_STATIC_DRAW_ARB);

return;
}

void CAniTree: :DrawTreeWithArray()
{
int i,j;

//////Non-end branches****************************************** **********************
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, IndexBufferNonEnd);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VertexBufferNonEnd);
glVertexPointer(3, GL_FLOAT, 8 * sizeof(GLfloat), BUFFER_OFFSET(0) );
glNormalPointer(GL_FLOAT,8 * sizeof(GLfloat), BUFFER_OFFSET(3));

glDrawElements(GL_TRIANGLE_STRIP, NonEndTotalStripNum*22, GL_UNSIGNED_INT, 0);
}

[This message has been edited by foollove (edited 04-20-2003).]