Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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
C++ 调用gldrawerelements中断某些GPU上的显示?_C++_Opengl_Glfw_Framebuffer - Fatal编程技术网

C++ 调用gldrawerelements中断某些GPU上的显示?

C++ 调用gldrawerelements中断某些GPU上的显示?,c++,opengl,glfw,framebuffer,C++,Opengl,Glfw,Framebuffer,我和一些同事(OpenGL 3.3+GLFW3和ImGui)正在开发一个3D PBR引擎,我们有一个特殊的错误:应用程序正常地进入它的绘图循环,并且没有给出任何OpenGL错误(如大量使用glCheckError()所示),但是屏幕是完全空白的。奇怪的是,这只发生在我同事的电脑上,它使用Nvidia GeForce GTX 1060作为图形卡(我的两个朋友有相同的电脑) 经过调查,这个错误是由在主循环之前调用一次的函数触发的。长话短说,它使用一对着色器将四边形渲染为通过帧缓冲区绑定的纹理。我把问

我和一些同事(OpenGL 3.3+GLFW3和ImGui)正在开发一个3D PBR引擎,我们有一个特殊的错误:应用程序正常地进入它的绘图循环,并且没有给出任何OpenGL错误(如大量使用
glCheckError()
所示),但是屏幕是完全空白的。奇怪的是,这只发生在我同事的电脑上,它使用Nvidia GeForce GTX 1060作为图形卡(我的两个朋友有相同的电脑)

经过调查,这个错误是由在主循环之前调用一次的函数触发的。长话短说,它使用一对着色器将四边形渲染为通过帧缓冲区绑定的纹理。我把问题缩小到
glpaurements
调用;在这些计算机上,除了注释掉这一行之外,不做任何更改都会修复显示(代价是纹理显示垃圾,而不是绘制调用的预期结果),而所有其他计算机即使在未注释这一行的情况下也能很好地运行应用程序

罪魁祸首的作用如下:

/**
 * Returns a texture identifier filled with a precomputed irradiance map calculated
 * from a provided, already set up-environment map
 */
GLuint precomputeIrradianceMap(GLuint envMapId)
{
    std::cerr << "Precomputing irradiance map ... " << std::endl;

    GLuint irradianceVAO, irradianceVBO, irradianceMap,
        irradianceVertShader, irradianceFragShader, irradianceProgram, captureFBO,
        captureRBO;
    GLint aPos_location, env_map;

    glGenVertexArrays(1, &irradianceVAO);
    glBindVertexArray(irradianceVAO);
    glGenBuffers(1, &irradianceVBO);
    glBindBuffer(GL_ARRAY_BUFFER, irradianceVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW);

    glGenFramebuffers(1, &captureFBO);
    glGenRenderbuffers(1, &captureRBO);

    glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
    glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 320, 320);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, captureRBO);

    checkGLerror();

    glGenTextures(1, &irradianceMap);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, irradianceMap);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 320, 320, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glBindTexture(GL_TEXTURE_2D, envMapId);

    checkGLerror();

    irradianceVertShader = createShaderFromSource(GL_VERTEX_SHADER, "shaders/irradiance_vert.glsl");
    irradianceFragShader = createShaderFromSource(GL_FRAGMENT_SHADER, "shaders/irradiance_frag.glsl");

    irradianceProgram = glCreateProgram();
    glAttachShader(irradianceProgram, irradianceVertShader);
    glAttachShader(irradianceProgram, irradianceFragShader);
    printShaderLog(irradianceVertShader);
    printShaderLog(irradianceFragShader);
    glLinkProgram(irradianceProgram);

    glUseProgram(irradianceProgram);

    checkGLerror();

    env_map = glGetUniformLocation(irradianceProgram, "environmentMap");
    aPos_location = glGetAttribLocation(irradianceProgram, "aPos");
    glEnableVertexAttribArray(aPos_location);
    checkGLerror();
    glVertexAttribPointer(aPos_location, 3, GL_FLOAT, GL_FALSE, 0, (void *) 0);

    glUniform1i(env_map, 0);

    checkGLerror();

    glViewport(0, 0, 320, 320);
    glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, irradianceMap, 0);

    checkGLerror();

    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    checkGLerror();

    // This is the culprit
    // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, quad_indices);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    checkGLerror();

    // glDisableVertexAttribArray(aPos_location);
    glDeleteShader(irradianceVertShader);
    glDeleteShader(irradianceFragShader);
    glDeleteProgram(irradianceProgram);
    glDeleteFramebuffers(1, &captureFBO);
    glDeleteRenderbuffers(1, &captureRBO);
    glDeleteBuffers(1, &irradianceVBO);
    glDeleteVertexArrays(1, &irradianceVAO);

    checkGLerror();

    std::cerr << "... done " << std::endl;

    return irradianceMap;
}
while (!glfwWindowShouldClose(window))
{
    glfwGetFramebufferSize(window, &display_w, &display_h);
    float newRatio = (float)display_w / display_h;
    if(ratio != newRatio)
    {
        ratio = newRatio;
        setAspectRatio(p, ratio);
        invP = p.inverse();
        glViewport(0, 0, display_w, display_h);
    }

    ImGui_ImplGlfwGL3_NewFrame();

    // Rendering + OpenGL rendering
    // Draw the skybox, then the model
    ImGui::Begin("Physical parameters", NULL, ImGuiWindowFlags_AlwaysAutoResize);
    ImGui::SliderFloat("Dielectric specularity", &ds, 0.f, 1.f, "%.3f");
    ImGui::SliderFloat("Light intensity", &l0, 0.f, 10.f, "%.2f");
    ImGui::Checkbox("Use irradiance map as skybox", &skyOrIrradiance);
    ImGui::Checkbox("Debug draw irradiance map", &debugDraw);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, skyOrIrradiance ? irradianceTexture : skybox_texture);
    ImGui::End();
    Matrix4f v = camera->getMatrix(), invV = v.inverse();

    // glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindVertexArray(vertex_array_objs[0]);
    glUseProgram(skybox_program);
    glUniformMatrix4fv(skybox_v_location, 1, GL_FALSE, (const GLfloat *) invV.data());
    glUniformMatrix4fv(skybox_p_location, 1, GL_FALSE, (const GLfloat *) invP.data());
    glDisable(GL_DEPTH_TEST);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, quad_indices);

    glBindVertexArray(vertex_array_objs[1]);
    glUseProgram(program);

    glUniformMatrix4fv(mv_location, 1, GL_FALSE, (const GLfloat *) v.data());
    glUniformMatrix4fv(p_location, 1, GL_FALSE, (const GLfloat *)p.data());
    glUniform1f(uDS_location, ds);
    glUniform1f(L0_location, l0);
    glActiveTexture(GL_TEXTURE0 + model.activeTexturesCount());
    glBindTexture(GL_TEXTURE_2D, irradianceTexture);
    glUniform1i(irradiance_location, model.activeTexturesCount());

    glEnable(GL_DEPTH_TEST);
    model.render();

    if(debugDraw)
    {
        displayTexture(irradianceTexture);
        displayTexture(skybox_texture, 0.f, -1.f);
    }

    camera->drawControls();
    // Draw basic interface
    basicInterface();

    ImGui::Render();
    ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
    glfwSwapBuffers(window);
    glfwPollEvents();
    camera->update(window);
}
void GltfModel::render() const
{
    for(unsigned int i = 0; i < _activeTextures.size(); i++)
    {
        if(_textureLocations[i] > -1)
        {
            int j = _activeTextures[i];
            glActiveTexture(GL_TEXTURE0 + i);
            glBindTexture(GL_TEXTURE_2D, _textureIds[j]);
            glUniform1i(_textureLocations[i], i);
        }
    }
    glBindBuffer(GL_ARRAY_BUFFER, _buffers[ARRAY_BUFFER]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffers[ELEMENT_ARRAY_BUFFER]);
    glDrawElements(_drawingMode, _indicesCount, _indicesType, NULL);
}
model.render()
如下所示:

/**
 * Returns a texture identifier filled with a precomputed irradiance map calculated
 * from a provided, already set up-environment map
 */
GLuint precomputeIrradianceMap(GLuint envMapId)
{
    std::cerr << "Precomputing irradiance map ... " << std::endl;

    GLuint irradianceVAO, irradianceVBO, irradianceMap,
        irradianceVertShader, irradianceFragShader, irradianceProgram, captureFBO,
        captureRBO;
    GLint aPos_location, env_map;

    glGenVertexArrays(1, &irradianceVAO);
    glBindVertexArray(irradianceVAO);
    glGenBuffers(1, &irradianceVBO);
    glBindBuffer(GL_ARRAY_BUFFER, irradianceVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW);

    glGenFramebuffers(1, &captureFBO);
    glGenRenderbuffers(1, &captureRBO);

    glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
    glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 320, 320);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, captureRBO);

    checkGLerror();

    glGenTextures(1, &irradianceMap);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, irradianceMap);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 320, 320, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glBindTexture(GL_TEXTURE_2D, envMapId);

    checkGLerror();

    irradianceVertShader = createShaderFromSource(GL_VERTEX_SHADER, "shaders/irradiance_vert.glsl");
    irradianceFragShader = createShaderFromSource(GL_FRAGMENT_SHADER, "shaders/irradiance_frag.glsl");

    irradianceProgram = glCreateProgram();
    glAttachShader(irradianceProgram, irradianceVertShader);
    glAttachShader(irradianceProgram, irradianceFragShader);
    printShaderLog(irradianceVertShader);
    printShaderLog(irradianceFragShader);
    glLinkProgram(irradianceProgram);

    glUseProgram(irradianceProgram);

    checkGLerror();

    env_map = glGetUniformLocation(irradianceProgram, "environmentMap");
    aPos_location = glGetAttribLocation(irradianceProgram, "aPos");
    glEnableVertexAttribArray(aPos_location);
    checkGLerror();
    glVertexAttribPointer(aPos_location, 3, GL_FLOAT, GL_FALSE, 0, (void *) 0);

    glUniform1i(env_map, 0);

    checkGLerror();

    glViewport(0, 0, 320, 320);
    glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, irradianceMap, 0);

    checkGLerror();

    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    checkGLerror();

    // This is the culprit
    // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, quad_indices);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    checkGLerror();

    // glDisableVertexAttribArray(aPos_location);
    glDeleteShader(irradianceVertShader);
    glDeleteShader(irradianceFragShader);
    glDeleteProgram(irradianceProgram);
    glDeleteFramebuffers(1, &captureFBO);
    glDeleteRenderbuffers(1, &captureRBO);
    glDeleteBuffers(1, &irradianceVBO);
    glDeleteVertexArrays(1, &irradianceVAO);

    checkGLerror();

    std::cerr << "... done " << std::endl;

    return irradianceMap;
}
while (!glfwWindowShouldClose(window))
{
    glfwGetFramebufferSize(window, &display_w, &display_h);
    float newRatio = (float)display_w / display_h;
    if(ratio != newRatio)
    {
        ratio = newRatio;
        setAspectRatio(p, ratio);
        invP = p.inverse();
        glViewport(0, 0, display_w, display_h);
    }

    ImGui_ImplGlfwGL3_NewFrame();

    // Rendering + OpenGL rendering
    // Draw the skybox, then the model
    ImGui::Begin("Physical parameters", NULL, ImGuiWindowFlags_AlwaysAutoResize);
    ImGui::SliderFloat("Dielectric specularity", &ds, 0.f, 1.f, "%.3f");
    ImGui::SliderFloat("Light intensity", &l0, 0.f, 10.f, "%.2f");
    ImGui::Checkbox("Use irradiance map as skybox", &skyOrIrradiance);
    ImGui::Checkbox("Debug draw irradiance map", &debugDraw);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, skyOrIrradiance ? irradianceTexture : skybox_texture);
    ImGui::End();
    Matrix4f v = camera->getMatrix(), invV = v.inverse();

    // glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindVertexArray(vertex_array_objs[0]);
    glUseProgram(skybox_program);
    glUniformMatrix4fv(skybox_v_location, 1, GL_FALSE, (const GLfloat *) invV.data());
    glUniformMatrix4fv(skybox_p_location, 1, GL_FALSE, (const GLfloat *) invP.data());
    glDisable(GL_DEPTH_TEST);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, quad_indices);

    glBindVertexArray(vertex_array_objs[1]);
    glUseProgram(program);

    glUniformMatrix4fv(mv_location, 1, GL_FALSE, (const GLfloat *) v.data());
    glUniformMatrix4fv(p_location, 1, GL_FALSE, (const GLfloat *)p.data());
    glUniform1f(uDS_location, ds);
    glUniform1f(L0_location, l0);
    glActiveTexture(GL_TEXTURE0 + model.activeTexturesCount());
    glBindTexture(GL_TEXTURE_2D, irradianceTexture);
    glUniform1i(irradiance_location, model.activeTexturesCount());

    glEnable(GL_DEPTH_TEST);
    model.render();

    if(debugDraw)
    {
        displayTexture(irradianceTexture);
        displayTexture(skybox_texture, 0.f, -1.f);
    }

    camera->drawControls();
    // Draw basic interface
    basicInterface();

    ImGui::Render();
    ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
    glfwSwapBuffers(window);
    glfwPollEvents();
    camera->update(window);
}
void GltfModel::render() const
{
    for(unsigned int i = 0; i < _activeTextures.size(); i++)
    {
        if(_textureLocations[i] > -1)
        {
            int j = _activeTextures[i];
            glActiveTexture(GL_TEXTURE0 + i);
            glBindTexture(GL_TEXTURE_2D, _textureIds[j]);
            glUniform1i(_textureLocations[i], i);
        }
    }
    glBindBuffer(GL_ARRAY_BUFFER, _buffers[ARRAY_BUFFER]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffers[ELEMENT_ARRAY_BUFFER]);
    glDrawElements(_drawingMode, _indicesCount, _indicesType, NULL);
}
void GltfModel::render()常量
{
对于(无符号int i=0;i<_activeTextures.size();i++)
{
if(_textureLocations[i]>-1)
{
int j=_活动纹理[i];
玻璃结构(GL_纹理0+i);
glBindTexture(GL_TEXTURE_2D,_textureIds[j]);
glUniform1i(_-textureLocations[i],i);
}
}
glBindBuffer(GL_数组_BUFFER,_buffers[ARRAY_BUFFER]);
glBindBuffer(GL_元素_数组_BUFFER,_buffers[ELEMENT_数组_BUFFER]);
GLDraweElements(_drawingMode,_IndicateSCount,_IndicateSType,NULL);
}
提前感谢您的时间


编辑:将
glClear(GL\u COLOR\u BUFFER\u BIT)
调用放在
glfwSwapBuffers(窗口)
之前,即使清除颜色已设置为浅蓝色,仍会显示一个白色屏幕,且罪犯行未注释。注释罪魁祸首行时确实会显示一个浅蓝色屏幕,因此我认为这是帧缓冲区问题,但我不能确定。

这不是完整的代码,对吗?例如,在哪里编译着色器?另外,
glGetError()
不会捕获所有错误,因为帧缓冲区使用了
glCheckFramebufferStatus
,正如我所说,我没有包括所有的设置代码,因为这是几十行创建、绑定和定位各种内容的代码,这不是很有趣。如果需要,我可以编辑以添加它。好主意,我将尝试用这种方式捕获帧缓冲区错误(如果有)。