2011-10-14 62 views
1

我有一個具有紋理的立方體,當我將數據數組更改爲VBO(glGen等)時,我的立方體呈現灰色。但如果我使用glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, myBuf);之類的東西,那麼一切正常。什麼問題?請幫幫我。爲什麼紋理不適用?

- (void)render:(CADisplayLink*)displayLink { 
     glClearColor(0.0f, 1.0f,0.5f, 1.0f); 


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glEnable(GL_DEPTH_TEST); 

     CC3GLMatrix *projection = [CC3GLMatrix matrix]; 
     float h = 4.0f * self.frame.size.height/self.frame.size.width; 
     [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:2 andFar:4]; 
     glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix); 
     CC3GLMatrix *modelView = [CC3GLMatrix matrix]; 
     [modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -2)]; 
     _currentRotation += displayLink.duration * 90; 
     [modelView rotateBy:CC3VectorMake(_currentRotation, _currentRotation, -1)]; 
     glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix); 

     // 1 
     glViewport(0, 0, self.frame.size.width, self.frame.size.height); 
     // 2 

     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
     glActiveTexture(GL_TEXTURE0); 

     glBindTexture(GL_TEXTURE_2D, texture[0].texID); 
     glUniform1i(uniformTexture, 0); 
     glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, 0); 
      glEnableVertexAttribArray(_positionSlot); 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndexes); 
     glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, 0, 0, texCoord); 
     glEnableVertexAttribArray(ATTRIB_TEXCOORD); 

     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (void*)0); 

     //glBindTexture(GL_TEXTURE_2D, 0); 
     glBindBuffer(GL_ARRAY_BUFFER,0); 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); 
     [_context presentRenderbuffer:GL_RENDERBUFFER]; 
    } 

回答

0

您需要使用客戶端的狀態對你的頂點緩衝申請的詳情,請參閱glEnableClientState和glClientActiveTexture手冊條目。

試着用這種方法改變你的代碼。

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
    glEnableClientState(GL_VERTEX_ARRAY); // NEW! we need to enable client state. 
    glActiveClientTexture(GL_TEXTURE0); // CHANGED! We need to use textures in the client state. 

參見How to call glDrawElements with static TexCoords and Dynamic Vertices

您正在使用什麼版本的OpenGL?我希望OpenGL ES 2.0

+1

錯誤:使用未聲明的標識符'GL_VERTEX_ARRAY'[3] – SevenDays

相關問題