Can't get simple OpenGL texture working using SDL2 and FreeImage3

| | August 10, 2015

I created a simple OpenGL program that display a quad with texture, but it doesn’t seem to be working as it only displays a white quad. What could possibly be wrong? I checked everything I could think of that could cause the issue. The bitmap is also 256×256. Any clues?

Here’s the source code: http://pastebin.com/5nVbMPVp

I’m also on Linux if that helps. Thanks!

2 Responses to “Can't get simple OpenGL texture working using SDL2 and FreeImage3”

  1. Let’s debug it one step at a time.

    Set the parameters for the texture object

    Right now you’re not setting these and it may be why you’re not getting anything on the screen. Try this:

    GLuint texId;
    glGenTextures( 1, &texId );
    glBindTexture( GL_TEXTURE_2D, texId );
    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);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth,
        imageHeight, 0, GL_RGB,
        GL_UNSIGNED_BYTE, imageBits );
    

    Can you fill the texture with red?

    Let’s try to fill the texture with red instead of loading it from a file:

    imageWidth = 128;
    imageHeight = 128;
    
    GLuint texId;
    glGenTextures( 1, &texId );
    glBindTexture( GL_TEXTURE_2D, texId );
    
    GLubyte* image_data_temp = new GLubyte[imageWidth * imageHeight * 3];
    GLubyte* dst_image = image_data_temp;
    for (unsigned int i = 0; i < imageWidth * imageHeight; i++)
    {
        // this will fill the texture with red (0xFF0000)
        // ... unless I've flipped the byte order, in which case it will be blue
        dst_image[0] = 0xFF;
        dst_image[1] = 0x00;
        dst_image[2] = 0x00;
        dst_image += 3;
    }
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth,
                imageHeight, 0, GL_RGB,
                GL_UNSIGNED_BYTE, image_data_temp );
    
    // if you have a "new", you must have a "delete" too!
    delete [] image_data_temp; 
    

    Is the screen red now? Then you know it has to do with your image not loading right.

    If the screen is black then let’s continue to the next thing to try:

    Can you render anything at all?

    Instead of rendering a textured quad, try rendering a line instead:

    glBindTexture( GL_TEXTURE_2D, 0 ); // unbind the texture
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glColor3f(1.f, 0.f, 0.f);
    glBegin( GL_LINES );
        // top-left to bottom-right
        glVertex2f( 0.0f, 0.0f ); glVertex2f( imageWidth, imageHeight );
        // top-right to bottom-left
        glVertex2f( imageWidth, 0.0f ); glVertex2f( 0.0f, imageHeight );
    glEnd();
    

    Do you see a red x? If so, then you know your matrices are correct. If not…

    Try manipulating the modelview with the keyboard

    I’m just gonna hardcode everything here.

    float camera_rotation[3] = { 0.f, 0.f, 0.f };
    float camera_position[3] = { 0.f, 0.f, 0.f };
    
    while ( SDL_PollEvent(&event) )
    {
        switch ( event.type )
        {
            case SDL_KEYDOWN:
            {
                switch ( event.key.keysym.sym )
                {
                    // move left and right
                    case 'A':
                        camera_position[0] -= 3.f;
                        break;
                    case 'D':
                        camera_position[0] += 3.f;
                        break;
                    // move forwards and backwards
                    case 'W':
                        camera_position[2] += 3.f;
                        break;
                    case 'S':
                        camera_position[2] -= 3.f;
                        break;  
                    // move up and down
                    case 'Q':
                        camera_position[1] -= 3.f;
                        break;
                    case 'E':
                        camera_position[1] += 3.f;
                        break;
    
                    // look left and right
                    case SDLK_LEFT:
                        camera_rotation[0] -= 3.f;
                        break;
                    case SDLK_RIGHT:
                        camera_rotation[0] += 3.f;
                        break;
                    // look up and down
                    case SDLK_UP:
                        camera_rotation[2] += 3.f;
                        break;
                    case SDLK_DOWN:
                        camera_rotation[2] -= 3.f;
                        break;
    
                }
    
                break;
            }
            case SDL_QUIT:
            {
                running = false;
                break;
            }
        }
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotate3f(camera_rotation[0], camera_rotation[1], camera_rotation[2]);
        glTranslate3f(camera_position[0], camera_position[1], camera_position[2]);
    
        // render stuff here
    }
    

    That didn’t work either

    Well then I’m stumped too.

  2. A few things you could try to help you (and us) find the problem:

    • glGetError
    • attach the actual image you are trying to load
    • attach the program output – what does fprintf( stdout, "%u %u %pn", imageWidth, imageHeight, imageBits ) print out?

Leave a Reply