OpenEXR + OpenGL unhandled exception

1

I've had quite a lot problems when trying to execute an example from OpenGL SuperBible 5th ed. from Chapter09/hdr_bloom.

Problems were caused from linking OpenEXR libs so I've build them manualy and replaced them with libs from authors.

Right now, I can manage to run the program but I get unhandled exception error when I try to load HDR image that Is used as a texture.

This is the piece of the code that is used to load HDR texture, if I comment it all out, program runs without problems but there's just no texture on my object.

bool LoadOpenEXRImage(char *fileName, GLint textureName, GLuint &texWidth, GLuint &texHeight)
{
    // The OpenEXR uses exception handling to report errors or failures
    // Do all work in a try block to catch any thrown exceptions.
    try
    {
        Imf::Array2D<Imf::Rgba> pixels;
        Imf::RgbaInputFile file(fileName); // UNHANDLED EXCEPTION
        Imath::Box2i dw = file.dataWindow();

        texWidth = dw.max.x - dw.min.x + 1;
        texHeight = dw.max.y - dw.min.y + 1;

        pixels.resizeErase(texHeight, texWidth);

        file.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y * texWidth, 1, texWidth);
        file.readPixels(dw.min.y, dw.max.y);

        GLfloat* texels = (GLfloat*)malloc(texWidth * texHeight * 3 * sizeof(GLfloat));
        GLfloat* pTex = texels;

        // Copy OpenEXR into local buffer for loading into a texture
        for (unsigned int v = 0; v < texHeight; v++)
        {
            for (unsigned int u = 0; u < texWidth; u++)
            {
                Imf::Rgba texel = pixels[texHeight - v - 1][u];
                pTex[0] = texel.r;
                pTex[1] = texel.g;
                pTex[2] = texel.b;

                pTex += 3;
            }
        }

        // Bind texture, load image, set tex state
        glBindTexture(GL_TEXTURE_2D, textureName);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, texWidth, texHeight, 0, GL_RGB, GL_FLOAT, texels);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        free(texels);
    }
    catch (Iex::BaseExc & e)
    {
        std::cerr << e.what() << std::endl;
        //
        // Handle exception.
        //
    }

    return true;
}

It is called like this:

LoadOpenEXRImage("window.exr", windowTexture, texWidth, texHeight);

Please notice my mark, which shows where exactly the unhandled exception happens.

If i try to run it, I get this error:

Unhandled exception at 0x77938E19 (ntdll.dll) in hdr_bloom.exe: 0xC0000005: Access violation writing location 0x00000014.

My debugger points to this piece of code:

virtual void __CLR_OR_THIS_CALL _Lock()
    {   // lock file instead of stream buffer
    if (_Myfile)
        _CSTD _lock_file(_Myfile); // here
    }

Which is part of the fstream

My declarations look like this:

#include <ImfRgbaFile.h>            // OpenEXR headers
#include <ImfArray.h>

#ifdef _WIN32
#pragma comment (lib, "half.lib") 
#pragma comment (lib, "Iex.lib")
#pragma comment (lib, "IlmImf.lib")
#pragma comment (lib, "IlmThread.lib")
#pragma comment (lib, "Imath.lib")
#pragma comment (lib, "zlib.lib")
#endif

#pragma warning( disable : 4244)

I've no idea if this matters but when I tried to run it for the first time I got SAFESEH errors about my zlib.lib so I turned SAFESEH off in Linker->Advanced.

And the project provided by the authors was created in VisualStudio2008 where I'm using newer version and I've converted it while opening.

Also, I'm using Windows 7 64-bit and Microsoft Visual Studio 2013 Ultimate.

If it's needed, let me know and I will post more detailed information, I've tried to keep it as short as possible.

c++
opengl
exception
openexr
asked on Stack Overflow Jan 1, 2015 by Patryk Krawczyk • edited Jan 1, 2015 by Patryk Krawczyk

1 Answer

1

I've finally found the issue although I'm not sure how it is happening.

To solve this I had to create a brand new project and just copy everything from the original project so my prediction is that somewhere during conversion of original project, some project properties changed and this caused some errors.

I found out that it's possible that such converted project may not how permission to write or read files in some directories and that's why I got unhandled exception from fstream.

So for future people with similar problems, instead of converting your project, create a brand new one and just copy what you need, in my case, I just had to copy Library and Include directories : ).

answered on Stack Overflow Jan 1, 2015 by Patryk Krawczyk

User contributions licensed under CC BY-SA 3.0