如何将.pgm纹理文件加载到openGL中
How to load a .pgm texture file into openGL
到目前为止,我有以下代码:
ifstream textureFile;
char* fileName="BasketBall.pgm";
textureFile.open(fileName, ios::in);
if (textureFile.fail()) {
displayMessage(ERROR_MESSAGE, "initTexture(): could not open file %s",fileName);
}
skipLine(textureFile); // skip the "P2" line in the Net.pgm file.
textureFile >> textureWidth; // read in the width
textureFile >> textureHeight; // read in the height
int maxGrayScaleValue;
textureFile >> maxGrayScaleValue; // read in the maximum gray value (255 in this case)
texture = new GLubyte[textureWidth*textureHeight];
int grayScaleValue;
for (int k = 0; k < textureHeight; k++) {
for(int l = 0; l < textureWidth; l++) {
textureFile >> grayScaleValue;
texture[k * textureWidth + l]=(GLubyte) grayScaleValue;
}
}
textureFile.close();
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
但当我在成功构建后尝试运行它时,它只是说我的项目已经"停止工作"。
这很可能是由于您正试图将灰度图像加载到RGBA容器中。
我可以想出两种方法来解决你的问题:
第一种是将texture
放大3倍,并将灰度值加载到3个连续的空间中。因此:
texture = new GLubyte[textureWidth*textureHeight*3];
和
for (int k = 0; k < textureHeight; k++) {
for(int l = 0; l < textureWidth; l+=3) {
textureFile >> grayScaleValue;
texture[k * textureWidth + l]=(GLubyte) grayScaleValue;
texture[k * textureWidth + l + 1]=(GLubyte) grayScaleValue;
texture[k * textureWidth + l + 2]=(GLubyte) grayScaleValue;
}
}
和
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight,
0, GL_RGB, GL_UNSIGNED_BYTE, texture);
第二个是尝试它只有一个单一的通道所以:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, textureWidth, textureHeight,
0, GL_RED, GL_UNSIGNED_BYTE, texture);
除此之外,我建议使用调试器和断点来遍历代码并找出崩溃的位置,但正如我上面提到的,这很可能是因为您正在将一个通道纹理传递到一个容器中,该容器需要4个通道。
相关文章:
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- GLFW DDS 加载函数的 OpenGL 链路错误
- 为什么我尝试在 OpenGL 中加载的 3ds 文件会出现此未经处理的异常?
- 在 OpenGL 中加载纹理C++
- 从文件问题加载纹理 Android NDK 和 OpenGL
- 如何使用 openGL 在屏幕上显示加载的纹理
- opengl SOIL 库仅加载未压缩的 dds 图像
- 使用 OpenGL 加载图像时0x69ABF340 STBI_Image引发异常
- OpenGL 中的动态加载着色器毫无意义吗?
- 使用 QOpenGLWidget 加载 OpenGL 函数指针
- stb_image在加载 opengl 纹理的图像时不会垂直翻转
- 在运行时加载OpenGL/DirectX API功能
- 如何保存和加载OpenGL
- 加载 OpenGL > 1.1 函数窗口
- Assimp 不加载 OpenGL 中 MD5 模型 Bob 节点中的骨骼名称
- 在 std::thread 中使用 SOIL 加载 OpenGL 纹理会引发"Integer Division by Zero"
- 使用 SDL 加载 OpenGL 纹理
- SDL 无法在 2.1 Mac OSX Yosemite 上加载 OpenGL 上下文
- 在运行时加载OpenGL扩展
- 使用SOIL加载OpenGl纹理时,纹理不会出现