• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Katherine1

OpenGL Texture Loading with FreeImage

2 posts in this topic

I have been working on a project in OpenGL, and up until this point, I have been using SDL for the main structure of my program. This worked fine for the most part, as I could use SDL's image loading library to take care of texture loading. Unfortunately, I've reached a point where I want to be able to use my texture class in other OpenGL programs, most notably a Qt OpenGL app for a map editor. Unfortunately, as long as my texture loader is dependent on SDL, I can't really use the library with Qt.

I decided instead to use a separate image loading library, FreeImage. This [b]should[/b] be easy, but for whatever reason, the texture is not being loaded. I have no clue what I am doing wrong. The path to the png I am trying to load is unchanged from when I was using SDL, so I know I'm pointing to the correct location.

I am saving the file information to instance variables, however, the printf at the bottom of loadAtlas always reports a width and height of 0 for the image. I am not sure what is going wrong.

trimmed header file
[code]

class TextureAtlas {
protected:

/**
* Texture information
*/
GLuint texture;

unsigned int width;
unsigned int height;
GLenum texture_format;
GLubyte* data;

public:

TextureAtlas();
virtual ~TextureAtlas();

/**
* loadAtlas()
* Loads a Texture as a Texture Atlas
* Parameters:
* loc - The location of the file to load.
* tileWidth - The width of the tiles in the given atlus.
* tileHeight - The height of the tiles in the given atlus.
*/
void loadAtlas(std::string loc, unsigned char tileWidth, unsigned char tileHeight);
};
[/code]

Relevent Code
[code]void TextureAtlas::loadAtlas(std::string loc, unsigned char width, unsigned char height) {

const char *filename = loc.c_str();
FREE_IMAGE_FORMAT format;
FIBITMAP *dib(0);
FREE_IMAGE_COLOR_TYPE type;

format = FreeImage_GetFileType(filename);
dib = FreeImage_Load(format, filename);
data = FreeImage_GetBits(dib);
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
type = FreeImage_GetColorType(dib);

switch ( type )
{
case FIC_RGB:
texture_format = GL_RGB;
break;
case FIC_RGBALPHA:
texture_format = GL_RGBA;
break;
default:
break;
}

//Generate the texture
glGenTextures( 1, &texture );

//Store the width and height of the tiles contained within the texture.
tileWidth = width;
tileHeight = height;
//Save the texture location for later use.
textureLocation = loc;

printf("Image information:\nFilename: %s\nWidth: %d\nHeight %d\n",loc.c_str(), width, height);

}[/code]
0

Share this post


Link to post
Share on other sites
To debug this problem, these are the steps I'd take:

1. Make sure you're initialising the library correctly if necessary.

2. Make sure FreeImage isn't giving you any errors (give it an output message callback using FreeImage_SetOutputMessage).

3. Assuming there aren't any useful errors, separate logic for opening/reading the file and parsing the file. Use a FILE* or whatever you're comfortable with to open and read the file yourself, verify that the contents of it are what you're expecting, then tell FreeImage to parse it from memory. This way you will be absolutely sure that the path's correct and the file exists. So the first few lines of your loadAtlas functions would look something like this:
[code]
FILE * file = fopen(filename, "rb");
// ... verify file exists, contents are correct, check format if necessary, etc.
FIMEMORY stream = FreeImage_OpenMemory(file_data, file_size);
dib = FreeImage_LoadFromMemory(format, stream);
// ...
[/code]
The above's untested but you should get the idea; open the file itself separately and use FreeImage's load from memory functions so that you can be sure that FreeImage isn't/SDL wasn't doing something strange or unexpected under the hood with your file path.

4. If FreeImage is still failing to parse the file correctly and doesn't give you any useful errors after you've gotten the above working, then try using different formats for your images.

5. Use stb_image instead. Edited by teutonicus
0

Share this post


Link to post
Share on other sites
int loadImageToSkinPool(char* image_path)
{
if(!image_path) return 0;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP* dib(0);
BYTE* bits(0);
unsigned int width(0), height(0);
GLuint glHandle;
fif = FreeImage_GetFileType(image_path,0);
if(fif == FIF_UNKNOWN)
fif = FreeImage_GetFIFFromFilename(image_path);
if(fif == FIF_UNKNOWN)
{
return false;
}
if(FreeImage_FIFSupportsReading(fif))
dib = FreeImage_Load(fif,image_path);
if(!dib)
{
return false;
}
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
if(FREE_IMAGE_COLOR_TYPE::FIC_RGBALPHA != type)
{
FIBITMAP* ndib = FreeImage_ConvertTo32Bits(dib);
dib = ndib;
}
bits = FreeImage_GetBits(dib);
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
if((bits == 0) || (width == 0) || (height == 0))
return false;
SkinInfo skin;
glGenTextures(1,&skin.mHandle);
glBindTexture(GL_TEXTURE_2D,skin.mHandle);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_BGRA,GL_UNSIGNED_BYTE,bits);
skin.mWidth = width;
skin.mHeight = height;
skin.mName = image_path;
mSkinPool.push_back(skin);
FreeImage_Unload(dib);
return 1;
}

Codes above not so safe but can load images to build texture.. check it your self Edited by FireInDark
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0