PDA

View Full Version : How to use a big GLfloat array to display a dense mesh model?



ddguan
07-26-2014, 11:59 PM
Dear All:
I want to display a 3d facial mesh(50496 vertices and 106466 faces). I use the code of OpenGL SuperBible just like this:

GLBatch PlyBatch;
void SetupRC()
{
....
GLfloat mVertex[106466*3*3];
GLfloat mColor[106466*3*3];
......
PlyBatch.Begin(GL_TRIANGLES, 106466);
PlyBatch.CopyVertexData3f(mVertex);
PlyBatch.CopyColorData4f(mColor);
PlyBatch.End();
...
}
and draw scene using this:
void RenderScene()
{
.....
PlyBatch.Draw();

}
but when allocating the array mVertex[106466*3*3]; there is a program error. When i reduce the array to mVertex[15000*3*3], it's all ok.
How to allocate a big GLfloat array?

Nikki_k
07-27-2014, 12:20 AM
If I understand the code correctly you allocate x*3 entries for your color array but then copy x*4 values, accessing undefined memory.



GLfloat mColor[106466*3*3]; <----- x*3

PlyBatch.CopyColorData4f(mColor); <--- here you use '4f'

Dan Bartlett
07-27-2014, 12:31 AM
Because you've declared mVertex & mColor as local variables, they will be assigned on the stack. IIRC the default maximum stack size is ~1MB so you will get a stack overflow as both variables are about 3.8MB. You could declare them in a way so that they are created on the heap instead (global variable/static/allocate at run-time using new/delete) or perhaps increase the size of the stack.

ddguan
07-27-2014, 05:51 PM
If I understand the code correctly you allocate x*3 entries for your color array but then copy x*4 values, accessing undefined memory.
Yes,it is an error.Thank you!

ddguan
07-27-2014, 07:20 PM
Because you've declared mVertex & mColor as local variables, they will be assigned on the stack. IIRC the default maximum stack size is ~1MB so you will get a stack overflow as both variables are about 3.8MB. You could declare them in a way so that they are created on the heap instead (global variable/static/allocate at run-time using new/delete) or perhaps increase the size of the stack.

Thank you! i will try it