Question

I am having trouble when using GLEW + GLFW... The order I use is:

  1. Load GLFW and open the Window
  2. Init GLEW
  3. Compile shaders
  4. Create texture
  5. Create framebuffer

This is all the flow of my code:

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!" );
}

This flow runs perfectly, I mean, no crashes and no exceptions throws... but when I draw something to the framebuffer and read it back everything is black.

Details: I have another version with the exact steps above except that I don't use GLFW... that is: I create a windows by myself and create an OpenGL context and... everything works PERFECTLY! my shaders compile and run ok, my texture is created, the framebuffer, I render to it and get it back perfectly... But when I put the GLFW the render to texture stopped working... any idea?

Was it helpful?

Solution

You are not switching the context with glfwMakeContextCurrent(window);

Here is an example of a full initialization of GLFW and 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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top