Failure when uploading image data with glTexImage2D

I'm developing an OpenGL application and everything works fine under Linux (both x86_32 and x86_64) but I've hit the wall during porting app to Windows. My application uses the very basic OpenGL 1.0, great glfw 2.7.6 and libpng 1.5.7. Before porting entire program, I tried writing the simplest code possible which would test whether those libraries work properly under Windows and everything seemed to work just fine until I had started using textures!

Uusing textures with glTexImage2D(..) my program gets Access Violation with the following error:

First-chance exception at 0x69E8F858 (atioglxx.dll) in sidescroll.exe: 0xC0000005: Access violation reading location 0x007C1000.
Unhandled exception at 0x69E8F858 (atioglxx.dll) in sidescroll.exe: 0xC0000005: Access violation reading location 0x007C1000.

I've done some research and found out that it's probably the GPU driver bug. Sadly, I have a Toshiba L650-1NU notebook with AMD Radeon HD5650 for which none drivers are provided but obsolete vendor-distributed. Author of the given post suggest using glBindBuffer but since I use OpenGL 1.0 I don't have access to this method.

Do you have any ideas of how to bypass this issue without using newer OpenGL? Nevertheless, if this is The One Solution can I be provided with a tutorial or code snippet on how to use OpenGL 2.1 with glfw?

Here's the piece of my code, which is causing the error:

img = img_loadPNG(BACKGROUND);
if (img) {
    printf("%p %d %d %d %d", img->p_ubaData, img->uiWidth, img->uiHeight, img->usiBpp, img->iGlFormat);

    glGenTextures(1, &textures[0]);
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->uiWidth, img->uiHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->p_ubaData); //SEGFAULT HERE ON WINDOWS!
    //img_free(img); //it may cause errors on windows
} else printf("Error: loading texture '%s' failed!\n", BACKGROUND);


The error you're experiencing is because the buffer you pass to glTexImage2D is shorter than what glTexImage2D tries to read from what it deduces it from the parameters. That is crashes under Windows but not Linux is, because under Linux memory allocations tend to be a bit larger than what you request, while under Windows you get very tight constraints.

Most likely the PNG reads as a RGB. You however tell glTexImage2D that you pass over GL_RGBA, which will of course access out of bounds.

I see that the img structure you receive has a element iGlFormat. I bet that this will be exactly the format to pass to glTexImage2D. So try this:

    GL_TEXTURE_2D, 0,
    img->iGlFormat, // This is not ideal, OpenGL will chose whatever suits
    img->uiWidth, img->uiHeight, 0,
    img->iGlFormat, GL_UNSIGNED_BYTE,
    img->p_ubaData )

Need Your Help

How to get a win32 handle of an open file in python?

python winapi

I'm sure this is documented somewhere but i can't find it...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.