Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl VBO没有';不要使用UV坐标_Opengl_Opengl Es 2.0 - Fatal编程技术网

Opengl VBO没有';不要使用UV坐标

Opengl VBO没有';不要使用UV坐标,opengl,opengl-es-2.0,Opengl,Opengl Es 2.0,我的渲染方法当前如下所示: void Renderer::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); checkGlError("glClear"); EntityCamera* camera = (EntityCamera*) resourceManager_->getResource(GHOST_CAMERA); mat4 proj; Matrix::proje

我的渲染方法当前如下所示:

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");
    }
}
void呈现器::render(){
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
勾选错误(“glClear”);
EntityCamera*摄像头=(EntityCamera*)资源管理器->获取资源(GHOST\u摄像头);
mat4项目;
矩阵::projection3D(项目,45.0f,
(浮动)nscreen-width\uu/nscreen-height\uu、GHOST\u-NEAR\u-DISTANCE、GHOST\u-FAR\u-DISTANCE);
mat4视图;
矩阵::乘法(项目,摄像机->获取矩阵(),视图);
相机->提取平面(视图);
对于(vector::const_迭代器it=renderArray_uu.begin();it!=renderArray_uu.end();
it++){
Node*Node=*it;
如果(!node->isRenderable()){
继续;
}
if(node->hasBV()&&node->getBV()->isInFrustum(摄影机,节点)==BoundingVolume::OUTSIDE){
LOGI(“节点%s在:O之外”,节点->getName().c_str());
继续;
}
EntityModel*实体=
静态\u转换(resourceManager->getResource(
(*it)->getEntity());
如果(实体==0 | |实体->获取顶点()==0 | |实体->获取顶点()->大小()==0){
LOGI(“空实体%s.”,节点->getName().c_str());
继续;
}
Resource*Resource=resourceManager->getResource(节点->getShader());
着色器*着色器=静态施法(资源);
Resource*resource2=resourceManager->getResource(实体->getTexture());
Image*Image=static_cast(资源2);
mat4 res;
矩阵::乘法(视图,节点->getMatrix(),res);
//选择要使用的着色器程序。
glUseProgram(着色器->getId());
检查错误(“glUseProgram”);
int matrix=glGetUniformLocation(着色器->getId(),“uWVP”);
int-texture=glGetUniformLocation(着色器->getId(),“纹理0”);
检查错误(“glGetUniformLocation”);
int textureCoords=glGetAttribLocation(着色器->getId(),“attrTexCoords”);
int顶点=glGetAttribLocation(着色器->getId(),“attrPos”);
检查错误(“GLGetAttriblLocation”);
//指定WVP矩阵。
glUniformMatrix4fv(基质,1,假,res);
勾选错误(“glUniformMatrix4fv”);
//加载顶点位置。
如果(!entity->isCompiled()){
//LOGI(“实体%s,未编译。”,实体->getName().c_str());
继续;
}
GlenableVertexAttributeArray(顶点);
检查错误(“GlenableVertexAttributeArray”);
//glVertexAttribute指针(顶点,3,GL_浮点,GL_假,0,
//&(*实体->获取顶点())[0]);
//LOGI(“%s vbo id:%d”,节点->getName().c_str(),实体->getVBO());
glBindBuffer(GL_数组_BUFFER,entity->getVBO());
检查错误(“glBindBuffer”);
glVertexAttribute指针(顶点,3,GL_浮点,GL_假,0,0);
checkleror(“glvertexattributepointer”);
//加载UV坐标。
GlenableVertexAttributeArray(TextureOrds);
检查错误(“GlenableVertexAttributeArray”);
glVertexAttributePointer(纹理字,2,GL\u浮点,GL\u假,0,
&(*entity->getTextureCoords())[0]);
checkleror(“glvertexattributepointer”);
//绑定纹理。
玻璃纹理(GL_纹理0);
勾选错误(“glActiveTexture”);
glBindTexture(GL_TEXTURE_2D,image->getId());
勾选错误(“glBindTexture”);
glUniform1i(纹理,0);
检查错误(“glUniform1i”);
如果(实体->索引()){
向量*索引=实体->获取索引();
对于(无符号整数i=0;isize();i++){
如果(实体->hasBoundingVolumes()){
BoundingVolume*volume=(*entity->getBoundingVolumes())[i];
如果(体积->isInFrustum(摄影机,节点)=边界体积::外部){
继续;
}
}
向量*ind=(*指数)[i];
glpaurements(GL_三角形,ind->size(),GL_UNSIGNED_SHORT,&(*ind)[0]);
支票错误(“票据要素”);
}
}
否则{
glDrawArray(GL_三角形,0,实体->获取顶点()->大小()/3);
检查错误(“glDrawArrays”);
}
glBindBuffer(GL_数组_BUFFER,0);
检查错误(“glBindBuffer”);
}
}

我最近刚刚尝试使用VBO,在我直接发送顶点数据之前,一切正常,纹理映射正确。现在我用VBO更改了顶点数组,即使它可以工作,也没有应用任何纹理,我只能看到黑色对象


  • 我的纹理可能有什么问题
  • 为什么我要更改glVertexAttribute指针(顶点,3,GL_浮点,GL_假,0,0);带glBindBuffer的行顺序(GL_数组_BUFFER,entity->getVBO());我有毁容的东西吗?这是我使用的呼叫顺序吗

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

    也就是说,我认为您的问题在于,在发送UV坐标之前,您没有解除VBO的绑定。您的代码应该是:

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

    我假设您的
    getTextureCoords()
    在VBO中不返回偏移量。

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

    也就是说,我认为您的问题在于,在发送UV坐标之前,您没有解除VBO的绑定。您的代码应该是:

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

    我假设您的
    getTextureCoords()
    在VBO中没有返回偏移量。

    我还没有仔细阅读您的代码,但是操作的一般顺序是1。绑定对象2。设置对象参数3。将数据加载到对象中。步骤2和步骤3可以互换