Ok I'm not sure what is the problem but I got a fix for it.
The problem seems to be in all the temporary buffers in RegenerateGrid. Maybe they get free, deleted or something. I'm not really sure but I tried to create the whole function step by step and it started crashing from the begin. I'm very confused as this only happens in Release build. At first I guess it was something wrong with me but I saw Virion has the same problem.
Anyway here is what I did:
1) Removed the m_vertexBuffer and m_indexBuffer buffers;
2) Removed Buffer.append...
3) Changed the section that checks if the arrays are created to a check if vertices are <= 65535
4) Replaced:
Code: Select all
m_vertexBuffer[vertIndex++] = video::S3DVertex(start, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f));
m_vertexBuffer[vertIndex++] = video::S3DVertex(end, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f));
m_indexBuffer[indexIndex] = indexIndex++;
m_indexBuffer[indexIndex] = indexIndex++;
with
Code: Select all
Buffer.Vertices.push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Vertices.push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Indices.push_back(indexIndex++);
Buffer.Indices.push_back(indexIndex++);
Irr::core::array happens to be a very usefull class - I use it all the time
So this fix the problem and it all works fine now.
So here is the whole function:
Code: Select all
void CGridSceneNode::RegenerateGrid()
{
//Clean up memory
Buffer.Indices.clear();
Buffer.Vertices.clear();
u32 m_numVertices = ((m_size / m_spacing) + 1) * 2 * 2;
if (m_accentlineoffset) m_numVertices += ((m_size / (m_spacing * m_accentlineoffset)) + 1) * 2 * 2;
if ( m_numVertices > 65535) {
//Too many vertices on 16 bit for for 16bit indices of SMeshBuffer
//Returning with a blank buffer to avoid segfaulting the entire application
return;
}
//Set our left corner
core::vector3df leftMost = core::vector3df(0,0,0);
leftMost.X -= m_size/2;
leftMost.Z -= m_size/2;
//Set our right corner
core::vector3df rightMost = core::vector3df(0,0,0);
rightMost.X += m_size/2;
rightMost.Z += m_size/2;
u32 indexIndex = 0;
//X-axis lines
for(u32 x = 0; x <= m_size; x+= m_spacing)
{
core::vector3df start = leftMost;
start.X += x ;
core::vector3df end = rightMost;
end.X = start.X;
Buffer.Vertices.push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Vertices.push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Indices.push_back(indexIndex++);
Buffer.Indices.push_back(indexIndex++);
}
//Z-axis lines
for(u32 z = 0; z <= m_size; z+= m_spacing)
{
core::vector3df start = leftMost;
start.Z += z ;
core::vector3df end = rightMost;
end.Z = start.Z;
Buffer.Vertices.push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Vertices.push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Indices.push_back(indexIndex++);
Buffer.Indices.push_back(indexIndex++);
}
//Accent lines are only drawn if the offset is greater than 0
if(m_accentlineoffset > 0)
{
//X-axis
for(u32 x = 0; x <= m_size; x+= m_spacing*m_accentlineoffset)
{
core::vector3df start = leftMost;
start.X += x ;
core::vector3df end = rightMost;
end.X = start.X;
Buffer.Vertices.push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Vertices.push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Indices.push_back(indexIndex++);
Buffer.Indices.push_back(indexIndex++);
}
//Z-axis
for(u32 z = 0; z <= m_size; z+= m_spacing*m_accentlineoffset)
{
core::vector3df start = leftMost;
start.Z += z ;
core::vector3df end = rightMost;
end.Z = start.Z;
Buffer.Vertices.push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Vertices.push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
Buffer.Indices.push_back(indexIndex++);
Buffer.Indices.push_back(indexIndex++);
}
}
// Create our box, it is the size of the grid exactly, plus 1 in the Y axis
Buffer.BoundingBox = core::aabbox3df(-(f32)m_size/2,-0.5f,-(f32)m_size/2,(f32)m_size/2,0.5f,(f32)m_size/2);
}
[/b]