2012-10-26 92 views
3

我试图使用VBOs来渲染一个正常的2d纹理正方形到FBO上。即时模式功能完美地工作,但不是这个VBO。 GL_TEXTURE_2D已经为代码启用。它有什么问题?一个简单的顶点缓冲区对象(C++),不渲染

unsigned int VBOid = 0; 
unsigned int Iid = 0; 

float *geometry; 
unsigned int *indices; 

int num_geometry = 1; 
int num_vertices = 4; 
int num_indices = num_geometry*num_vertices; 
geometry = new float[num_geometry*num_vertices*4]; 
indices = new unsigned int[num_indices]; 

indices[0] = 0; 
indices[1] = 1; 
indices[2] = 2; 
indices[3] = 3; 

/* Fill geometry: 0, 1, = vertex_xy 
*    2, 3 = tex_coord_uv 
*/ 
geometry[0] = 0.0f; 
geometry[1] = 0.0f; 
geometry[2] = 0.0f; 
geometry[3] = 0.0f; 

geometry[4] = 50.0f; 
geometry[5] = 0.0f; 
geometry[6] = 1.0f; 
geometry[7] = 0.0f; 

geometry[8] = 50.0f; 
geometry[9] = 50.0f; 
geometry[10] = 1.0f; 
geometry[11] = 1.0f; 

geometry[12] = 0.0f; 
geometry[13] = 50.0f; 
geometry[14] = 0.0f; 
geometry[15] = 1.0f; 

glGenBuffers(1, &VBOid); 
glBindBuffer(GL_ARRAY_BUFFER, VBOid); 
glBufferData(GL_ARRAY_BUFFER, sizeof(geometry), geometry, GL_STATIC_DRAW); 

glGenBuffers(1, &Iid); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Iid); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 

//GL_TEXTURE_2D is already enabled here 
//Buffers are already bound from above 

glBindTexture(GL_TEXTURE_2D, 2); //I used 2 just to test to see if it is rendering a texture correctly. Yes, 2 does exist in my program thats why i arbitrarily used it 
//glClientActiveTexture(GL_TEXTURE0); I dont know what this is for and where to put it 

glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
//glActiveTexture(GL_TEXTURE0); same here I dont know what this is for or where to put it 
glVertexPointer(2, GL_FLOAT, sizeof(GLfloat)*4, 0); 
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat)*4, (float*)(sizeof(GLfloat)*2)); 

glDrawElements(GL_QUADS, num_indices, GL_UNSIGNED_INT, indices); 

glDisableClientState(GL_VERTEX_ARRAY); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
+0

作为'glBufferData'中缓冲区大小的'sizeof(geometry)'是我能看到的第一个明显的错误。这将始终返回指针'(4)'的大小。另外,为什么你将一些z值设置为0,一些设置为1?你应该真的让你的调试器运行,并逐行验证你发送给GL ... – Grimmy

+0

以及那些纹理UV坐标。对于每个xy,语法是2 UV – sgtHale

+0

@ user1775989您是否真的阅读过他的评论的第一部分(这正是您的问题,如我的答案中所示)或者仅仅是他不相关的第二部分? –

回答

3

的问题是您的通话glBufferData里面的sizeof(geometry)使用(与同为indices)。这些变量实际上只是指针,不管它们是否指向动态分配的数组(编译器不知道)。所以你总会得到一个指针的大小(4或8个字节,取决于平台)。

用替换num_geometry*num_vertices*4*sizeof(float)sizeof(indices)num_indices*sizeof(unsigned int)。唉,其实你不需要这里的任何指标都和可以只画了整个事情用一个简单的

glDrawArrays(GL_QUADS, 0, 4); 

永远知道的实际(编译时大小)阵列和单纯之间的差异指针指向动态分配的数组,其结果是sizeof运算符是这一差异的一个(并且在稍后的某个时间点使用delete[]释放后者的存储器的要求是另一个但不是较不重要的差别)。

相关问题