0

我在使用 GLEW + GLFW 时遇到问题...我使用的顺序是:

  1. 加载 GLFW 并打开窗口
  2. 初始化 GLEW
  3. 编译着色器
  4. 创建纹理
  5. 创建帧缓冲区

这是我的代码的所有流程:

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渲染到纹理停止工作......任何想法?

4

1 回答 1

1

您没有使用 glfwMakeContextCurrent(window); 切换上下文

这是 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);
}
于 2016-05-15T15:38:36.713 回答