2012-07-08 73 views
0

我的渲​​染方法目前看起来是这样的:VBO不使用UV坐标

void Renderer::render() { 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    checkGlError("glClear"); 

    EntityCamera* camera = (EntityCamera*) resourceManager_->getResource(GHOST_CAMERA); 

    mat4 proj; 
    Matrix::projection3D(proj, 45.0f, 
      (float) nScreenWidth_/nScreenHeight_, GHOST_NEAR_DISTANCE, GHOST_FAR_DISTANCE); 

    mat4 view; 
    Matrix::multiply(proj, camera_->getMatrix(), view); 
    camera->extractPlanes(view); 

    for (vector<Node*>::const_iterator it = renderArray_.begin(); it != renderArray_.end(); 
      it++) { 
     Node* node = *it; 
     if (!node->isRenderable()) { 
      continue; 
     } 
     if (node->hasBV() && node->getBV()->isInFrustum(camera, node) == BoundingVolume::OUTSIDE) { 
      LOGI("Node %s is outside :O", node->getName().c_str()); 
      continue; 
     } 
     EntityModel* entity = 
       static_cast<EntityModel*>(resourceManager_->getResource(
         (*it)->getEntity())); 
     if (entity == 0 || entity->getVertices() == 0 || entity->getVertices()->size() == 0) { 
      LOGI("Empty entity %s.", node->getName().c_str()); 
      continue; 
     } 
     Resource* resource = resourceManager_->getResource(node->getShader()); 
     Shader* shader = static_cast<Shader*>(resource); 
     Resource* resource2 = resourceManager_->getResource(entity->getTexture()); 
     Image* image = static_cast<Image*>(resource2); 
     mat4 res; 
     Matrix::multiply(view, node->getMatrix(), res); 

     // Select shader program to use. 
     glUseProgram(shader->getId()); 
     checkGlError("glUseProgram"); 

     int matrix = glGetUniformLocation(shader->getId(), "uWVP"); 
     int texture = glGetUniformLocation(shader->getId(), "texture_0"); 
     checkGlError("glGetUniformLocation"); 
     int textureCoords = glGetAttribLocation(shader->getId(), "attrTexCoords"); 
     int vertices = glGetAttribLocation(shader->getId(), "attrPos"); 
     checkGlError("glGetAttribLocation"); 

     // Specify WVP matrix. 
     glUniformMatrix4fv(matrix, 1, false, res); 
     checkGlError("glUniformMatrix4fv"); 

     // Load vertex positions. 
     if (!entity->isCompiled()) { 
      //LOGI("Entity %s, not compiled.", entity->getName().c_str()); 
      continue; 
     } 
     glEnableVertexAttribArray(vertices); 
     checkGlError("glEnableVertexAttribArray"); 
     //glVertexAttribPointer(vertices, 3, GL_FLOAT, GL_FALSE, 0, 
     //  &(*entity->getVertices())[0]); 
     //LOGI("%s vbo id: %d", node->getName().c_str(), entity->getVBO()); 
     glBindBuffer(GL_ARRAY_BUFFER, entity->getVBO()); 
     checkGlError("glBindBuffer"); 
     glVertexAttribPointer(vertices, 3, GL_FLOAT, GL_FALSE, 0, 0); 
     checkGlError("glVertexAttribPointer"); 
     // Load UV coordinates. 
     glEnableVertexAttribArray(textureCoords); 
     checkGlError("glEnableVertexAttribArray"); 
     glVertexAttribPointer(textureCoords, 2, GL_FLOAT, GL_FALSE, 0, 
       &(*entity->getTextureCoords())[0]); 
     checkGlError("glVertexAttribPointer"); 

     // Bind the texture. 
     glActiveTexture(GL_TEXTURE0); 
     checkGlError("glActiveTexture"); 
     glBindTexture(GL_TEXTURE_2D, image->getId()); 
     checkGlError("glBindTexture"); 
     glUniform1i(texture, 0); 
     checkGlError("glUniform1i"); 

     if (entity->hasIndices()) { 
      vector<vector<GLushort>*>* indices = entity->getIndices(); 
      for (unsigned int i = 0; i < indices->size(); i++) { 
       if (entity->hasBoundingVolumes()) { 
        BoundingVolume* volume = (*entity->getBoundingVolumes())[i]; 
        if (volume->isInFrustum(camera, node) == BoundingVolume::OUTSIDE) { 
         continue; 
        } 
       } 
       vector<GLushort>* ind = (*indices)[i]; 
       glDrawElements(GL_TRIANGLES, ind->size(), GL_UNSIGNED_SHORT, &(*ind)[0]); 
       checkGlError("glDrawElements"); 
      } 
     } 
     else { 
      glDrawArrays(GL_TRIANGLES, 0, entity->getVertices()->size()/3); 
      checkGlError("glDrawArrays"); 
     } 
     glBindBuffer(GL_ARRAY_BUFFER, 0); 
     checkGlError("glBindBuffer"); 
    } 
} 

我刚刚试过用VBO,我是直接发送顶点数据和一切运行良好之前,质地均正确映射。现在我用VBO改变了顶点数组,尽管它起作用,没有纹理被应用,我只能看到黑色的物体。


  1. 可能是什么毛病我的纹理?
  2. 为什么当我改变glVertexAttribPointer(顶点,3,GL_FLOAT,GL_FALSE,0,0); line命令与glBindBuffer(GL_ARRAY_BUFFER,entity-> getVBO());我得到毁容的物品?这是我正在使用的正确呼叫顺序吗?
+1

我没有穿过你的代码,但操作的一般操作是1.绑定对象2.设置对象参数3.将数据加载到对象中。步骤2和步骤3可以通过规范进行互换,但给定的顺序会触发更少的驱动程序错误。并非活动单位(如纹理单位或类似物)甚至在绑定之前就已经完成。因此,纹理首先设置活动单位,然后绑定。 – datenwolf 2012-07-08 09:04:04

回答

3

你从简单记忆中发送你的UV坐标,而你似乎从VBO发送你的顶点坐标。这可能不是那么高效,你应该在VBO中拥有两个数据集以利用VBO的优势。

这就是说,我认为你的问题是,你不要在发送你的UV坐标之前解除绑定你的VBO。您的代码应该是:

glBindBuffer(GL_ARRAY_BUFFER, 0); 
glVertexAttribPointer(textureCoords, 2, GL_FLOAT, GL_FALSE, 0, 
      &(*entity->getTextureCoords())[0]); 

,我想你的getTextureCoords()不回你的VBO偏移。

+0

这确实是解绑的情况,现在它起作用了。是的,我计划在稍后将纹理坐标添加到同一个VBO中,我只是想通过几个步骤来完成此操作,以便更好地理解其工作原理。谢谢。 – SMart 2012-07-08 10:39:30