这是一个神秘的问题。
// There are in global space
VertexBufferGLES2 vb(true);
IndexBufferGLES2 ib(true);
...
// Initialization
vb.Create(sizeof(tris), tris, USAGE_STATIC_);
ib.Create(sizeof(indices), indices, USAGE_STATIC);
...
glBindBuffer(GL_ARRAY_BUFFER, vb.GetVertexBufferGLES2());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.GetIndexBufferGLES2());
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (GLvoid *)0);
在这种情况下,一切都很完美,但代码如下,没有什么是绘画。
// There are in global space
VertexBufferGLES2 *vb = NULL;
IndexBufferGLES2 *ib = NULL;
...
// Initialization
vb = new VertexBufferGLES2(true);
ib = new IndexBufferGLES2(true);
vb->Create(sizeof(tris), tris, USAGE_STATIC_);
ib->Create(sizeof(indices), indices, USAGE_STATIC);
...
glBindBuffer(GL_ARRAY_BUFFER, vb->GetVertexBufferGLES2());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib->GetIndexBufferGLES2());
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (GLvoid *)0);
...
delete vb;
delete ib;
所有其他代码都相同。不同于这两段代码。 我已经花了三天的时间进行修复,但没有找到任何东西,因为这段代码非常明显和逻辑相同,必须是...
问题解决了:在功能"创建"我在使用成员变量(枚举)之前从"创建"功能参数。在第一种情况下,编译器将此变量初始化为0,这对于代码是正确的(因为这是第一个枚举值),并且所有这些都正常工作。在第二种情况下,编译器没有初始化变量,因为类是在堆上创建的,而且变量是垃圾。
我的错误在于此代码:
int VertexBufferGLES2::Create(int size, void *data, USAGE_ usage)
{
if (size == 0) return -1;
Delete();
m_size = size;
glGenBuffers(1, &m_buffer);
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
// The function GetGLES2Usage() is mapping my m_usage enum to
// OpenGL ES 2 enum
glBufferData(GL_ARRAY_BUFFER, size, data, GetGLES2Usage());
// but the value to m_usage i assign here. Moving up (to above
// glBufferData line) this code line solves the problem
m_usage = usage;
if (m_useCache) CacheData(size, data);
return 0;
}
答案 0 :(得分:1)
行
VertexBufferGLES2 vb = NULL;
IndexBufferGLES2 ib = NULL;
看起来很可疑,也许你应该试试
VertexBufferGLES2* vb = NULL;
IndexBufferGLES2* ib = NULL;