在尝试执行 OpenGL SuperBible 第 5 版中的示例时,我遇到了很多问题。来自第 9 章/hdr_bloom。
问题是由链接 OpenEXR 库引起的,所以我手动构建它们并用作者的库替换它们。
现在,我可以设法运行该程序,但是当我尝试加载用作纹理的 HDR 图像时出现未处理的异常错误。
这是用于加载 HDR 纹理的代码,如果我将其全部注释掉,程序运行没有问题,但我的对象上没有纹理。
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;
}
它是这样调用的:
LoadOpenEXRImage("window.exr", windowTexture, texWidth, texHeight);
请注意我的标记,它显示了未处理异常发生的确切位置。
如果我尝试运行它,我会收到此错误:
hdr_bloom.exe 中 0x77938E19 (ntdll.dll) 处未处理的异常:0xC0000005:访问冲突写入位置 0x00000014。
我的调试器指向这段代码:
virtual void __CLR_OR_THIS_CALL _Lock()
{ // lock file instead of stream buffer
if (_Myfile)
_CSTD _lock_file(_Myfile); // here
}
这是其中的一部分fstream
我的声明如下所示:
#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)
我不知道这是否重要,但是当我第一次尝试运行它时,我的 zlib.lib 出现 SAFESEH 错误,所以我在 Linker->Advanced 中关闭了 SAFESEH。
作者提供的项目是在 VisualStudio2008 中创建的,我使用的是较新版本,并在打开时对其进行了转换。
另外,我使用的是 Windows 7 64 位和 Microsoft Visual Studio 2013 Ultimate。
如果需要,请告诉我,我会发布更详细的信息,我尽量保持简短。