首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBO不使用UV坐标

VBO不使用UV坐标
EN

Stack Overflow用户
提问于 2012-07-08 08:51:42
回答 1查看 1.2K关注 0票数 0

我的呈现方法当前如下所示:

代码语言:javascript
复制
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);与glBindBuffer的行顺序(GL_ARRAY_BUFFER,entity->getVBO());我得到了变形的对象?这是我正在使用的通话顺序吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-08 10:06:42

您正在发送您的UV坐标从普通内存,而您似乎发送您的顶点坐标从一个VBO。这可能不是那么有效,您应该在VBO中同时拥有两个数据集,以获得VBO优势。

话虽如此,我认为您的问题是,您没有解除您的VBO之前发送您的UV坐标。您的代码应该是:

代码语言:javascript
复制
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribPointer(textureCoords, 2, GL_FLOAT, GL_FALSE, 0,
            &(*entity->getTextureCoords())[0]);

正如我所设想的那样,您的getTextureCoords()不会在VBO中返回偏移量。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11381867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档