Jump to content
  • Advertisement
Sign in to follow this  
Mimulous

OpenGL Bitmap Loader

This topic is 3325 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

Hey, I'm really new to texture mapping in OpenGL and am having a lot of problems with it. I have managed to master the art of actually physically mapping the image to the geometry but I can't seem to load the image itself. I have a 256x256 bitmap which I want to load into the program, preferably as GLuint. Can someone help me find a really simple bitmap loader please? I have searched the internet and can't seem to find anything that is well explained and which works. I've tried loading it as AUX_RGBImageRec which seems to work but when I map it as a texture the Dev C++ debugger tells me I have "An Access Violation (Segmentation Fault) raised in your program" Any assistance would be greatly appreciated and I appologise if it seems a stupid question. Mimulous

Share this post


Link to post
Share on other sites
Advertisement
Try stb_image.c (search on google for it).
It is just one file that will allow to load without any external dependencies pictures from BMP, JPG, PNG, TGA and some few more formats.
Loading images with it is very simple:

int width, height, n;
unsigned char* data = stbi_load(image_filename, &with, &height, &n, 4); // request RGBA
if (data != NULL)
{
// use image in data
// ...
// for example, to load as OpenGL texture:
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

stbi_image_free(data);
}




[Edited by - bubu LV on August 14, 2009 8:45:06 AM]

Share this post


Link to post
Share on other sites
Thankyou. That makes a lot more sense and is much easier to understand. Thankyou

I am still having some problems though, I have implemented this but I don't ever seem to see a texture, I just see a white square. I have included my code to look through, it appears I haven't grasped texture mapping as well as I had thought.

void display(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);

int width, height, n;
width = 256;
height = 256;
n = 4;
unsigned char* data = stbi_load("Dogportrait1.bmp",&width, &height, &n, 4);
if (data != NULL)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);

glEnd();
stbi_image_free(data);
}

glDisable(GL_TEXTURE_2D);
}

Share this post


Link to post
Share on other sites
Are you reloading the texture every game loop??
That is really slow you should do it only once at the start of your program (or level, just not every frame).

Next you need to look into glBindTexture (I think it is called this way) and glGenTextures (again not 100% because I haven't used OpenGl for some time.) I can dig op some code from my wrapper but I think its better for you if you look at the NeHe tutorials first. It think it is the first tutorial from the second page.

Call glBindTexture before you are going to use it to render (so before glBegin(...)) and glGenTextures followed by a glBindTexture just before you are going to add the pixel data.

Hope this helps

edit: Made NeHe a clicky:P

Share this post


Link to post
Share on other sites
Thankyou very much.
I have been through those tutorials before but as I've been reaching the point of sheer desparation I have been removing lines of code. (Silly thing to do but when a problem bugs you for days you end up doing things like that).

I now have it working, I added in the lines I was missing combined with the stb_image.c and it works beautifully. Thankyou everyone for your help, you have taught me a lot and I really appreciate it.

Mimulous

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!