使用GLEW + GLFW,“渲染到纹理”始终是黑色的

时间:2013-03-02 22:11:38

标签: opengl glew glfw render-to-texture

使用GLEW + GLFW时遇到问题...... 我使用的订单是:

  1. 加载GLFW并打开窗口
  2. Init GLEW
  3. 编译着色器
  4. 创建纹理
  5. 创建framebuffer
  6. 这是我的代码的所有流程:

    if( !glfwInit() )
    {
        glfwTerminate();
        throw exception( "Failed to initialize GLFW" );
    }
    
    glfwOpenWindowHint( GLFW_FSAA_SAMPLES, 4 );
    glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
    glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 3 );
    glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    
    if( !glfwOpenWindow( m_width, m_height, 0, 0, 0, 0, 32, 0, GLFW_WINDOW ) )
    {
        throw exception( "Failed to open GLFW window." );
        glfwTerminate();
    }
    
    if ( glewInit() != GLEW_OK )
    {
        throw exception( "Failed to initialize GLEW" );
    }
    
    // shaders...
    compile some shaders.... glCreateShader(), glCompileShader(), glCreateProgram(), glAttachShader() glLinkProgram(), etc...
    
    // texture
    glGenTextures( 1, &m_texture );
    glBindTexture( GL_TEXTURE_2D, m_texture );
    
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    
    // frame buffer
    glGenFramebuffers( 1, &m_frameBuffer ); // IT CRASHES HERE! :-(
    glBindFramebuffer( GL_FRAMEBUFFER, m_frameBuffer );
    
    glBindTexture( GL_TEXTURE_2D, m_texture );
    
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    
    glFramebufferTexture( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texture, 0 );
    
    GLenum drawBuffers[ 1 ] = { GL_COLOR_ATTACHMENT0 };
    glDrawBuffers( 1, drawBuffers );
    
    if ( glCheckFramebufferStatus( GL_FRAMEBUFFER ) != GL_FRAMEBUFFER_COMPLETE )
    {
        throw exception( "Problems when creating OpenGL texture in GPU!" );
    }
    

    这个流程完美运行,我的意思是,没有崩溃,也没有异常抛出......但是当我向帧缓冲区绘制一些东西并将其读回来时,一切都是黑色的。

    详细信息:我有另一个版本,其中包含上面的确切步骤,但我不使用GLFW ...即:我自己创建一个窗口并创建一个OpenGL上下文并...一切正常完美!我的着色器编译并运行正常,我的纹理被创建,帧缓冲,我渲染到它并完美地恢复它...但是当我把 GLFW 渲染到纹理停止工作...任何想法?

1 个答案:

答案 0 :(得分:1)

您没有切换上下文     glfwMakeContextCurrent(窗口);

以下是GLFW和GLEW的完整初始化示例:

//Init GLFW
if(!glfwInit())
{
    std::cout << "ERROR"  << std::endl; 
    return(-1);
}

//Give hints
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
glfwWindowHint(GLFW_DECORATED, GL_TRUE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

#if defined(__APPLE__)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
int const DPI = 2; // For retina screens only
#else
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
int const DPI = 1;
# endif

// Create Wikndow
window = glfwCreateWindow(screenWidth, screenHeight, "DEMO", NULL, NULL );
if(window == 0)
{
    std::cout << "ERROR"  << std::endl;
    glfwTerminate();
    return(-1);
}

//Switch context to use on window
glfwMakeContextCurrent(window);

//Init glew
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
    std::cout << "ERROR" << std::endl;
    return(-1);
}