Jump to content
  • Advertisement
Sign in to follow this  
Demolit

Using FreeImage with GLUT

This topic is 3475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to map an image onto a quad, but it seems to turn out white. I'm quite confident that my image loading code is fine, because I've gotten an image to come out fine when I'm using SDL, so it's not so much a FreeImage question. And GL_TEXTURE_2D seems to be enabled, so I'm stumped. Is there something else I have to initialize when using GLUT? I've cut out the main bits of my code, hopefully someone sees something I've missed. Compiles just fine on VC.
#ifdef WIN32
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "FreeImage.lib")
#endif

#include <windows.h>
#include <iostream>
#include <cstdlib>
#include "FreeImage.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

static GLuint TextureID;

void reshape(GLsizei width, GLsizei height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(75.0, width / (float) height, 1, 1000);
    glMatrixMode(GL_MODELVIEW);
}

void init()
{
    glClearDepth(1.0f);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glBlendFunc(GL_ONE, GL_ONE);

    glEnable(GL_TEXTURE_2D);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0, 0, -10);

    glColor3f(1,1,1);
    glBindTexture(GL_TEXTURE_2D, TextureID);
    glBegin(GL_QUADS);
    glTexCoord2f(0,1);
    glVertex2f(-2.5,5);
    glTexCoord2f(0,0);
    glVertex2f(-2.5,-5);
    glTexCoord2f(1,0);
    glVertex2f(2.5,-5);
    glTexCoord2f(1,1);
    glVertex2f(2.5,5);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

void loadImage(const char* imageName)
{
    FREE_IMAGE_FORMAT formato =
        FreeImage_GetFileType(imageName,0);
	FIBITMAP* imagen = FreeImage_Load(formato, imageName);
	
    FIBITMAP* temp = imagen;

    imagen = FreeImage_ConvertTo32Bits(imagen);
    FreeImage_Unload(temp);
	
    int w = FreeImage_GetWidth(imagen);
    int h = FreeImage_GetHeight(imagen);
	
    GLubyte* textura = new GLubyte[4*w*h];
    char* pixels = (char*)FreeImage_GetBits(imagen);
    //FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR).
	
    for(int j= 0; j<w*h; j++){
        textura[j*4+0]= pixels[j*4+2];
        textura[j*4+1]= pixels[j*4+1];
        textura[j*4+2]= pixels[j*4+0];
        textura[j*4+3]= pixels[j*4+3];
    }

    glGenTextures(1, &TextureID);
    glBindTexture(GL_TEXTURE_2D, TextureID);
    if(FreeImage_GetBPP(imagen) == 24)
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, 
             GL_RGB,GL_UNSIGNED_BYTE,(GLvoid*)textura );
    else if(FreeImage_GetBPP(imagen) == 32)
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, 
             GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textura );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

int main(int argc, char** argv)
{
    init();

    loadImage("image.gif");

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowSize(SCREEN_WIDTH,SCREEN_HEIGHT);
    glutCreateWindow("A Window");
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);

    glutMainLoop();

    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
In the main function, try moving your call to init() and your call loadImage("image.gif") below your call to glutCreateWindow(). That should fix it as I just tested it on my tetris game, and while you call some extra stuff in your init function that I don't, if I move my init function to where you have it and try to load a texture there my textures don't load either.

[Edited by - Chadwell on January 15, 2009 11:29:50 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!