【发布时间】:2013-12-28 16:19:27
【问题描述】:
请大家知道这段代码有什么问题 颜色不匹配? 我正在使用 ppm loader 加载图像,但在游戏中加载图像时颜色与图像不匹配。
当我传递一个白色图像时,它显示为黑色,当我传递黑色时,它显示为白色,当我传递 255 , 0 ,0 时,它显示为 0 ,255,255,当我传递 128 、128 、192 时,它显示为 128 , 128 , 64
#include <fstream>
#include <glut.h>
#include "Texture.h"
#include <iostream>
#pragma warning (disable : 4996)
Texture::Texture ()
{
}
void Texture::Prepare (int texN)
{
texName = texN;
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glBindTexture (GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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_RGB, image.width,
image.height, 0, GL_RGB, GL_UNSIGNED_BYTE,
image.pixels);
}
void Texture::ReadPPMImage (char* fn)
{
int tmpint;
char str[100];
FILE* inFile = fopen (fn,"rb");
if (inFile == NULL)
{
printf ("Can't open input file %s. Exiting.\n",fn);
exit (1);
}
fscanf (inFile,"P%d\n", &tmpint);
if (tmpint != 6)
{
printf ("Input file is not ppm. Exiting.\n");
exit (1);
}
// skip comments embedded in header
fgets (str,100,inFile);
while (str[0]=='#')
fgets(str,100,inFile);
// read image dimensions
sscanf (str,"%d %d",&image.width, &image.height);
fgets (str,100,inFile);
sscanf (str,"%d",&tmpint);
if (tmpint != 255)
printf("Warning: maxvalue is not 255 in ppm file\n");
image.numChannels = 3;
image.pixels = (unsigned char*) malloc (image.numChannels * image.width*image.height * sizeof (unsigned char));
if (image.pixels == NULL)
{
printf ("Can't allocate image of size %dx%d. Exiting\n", image.width, image.height);
exit (1);
}
else
printf("Reading image %s of size %dx%d\n", fn, image.width,image.height);
fread (image.pixels, sizeof (unsigned char), image.numChannels * image.width * image.height, inFile);
fclose (inFile);
}
【问题讨论】:
-
从目前发布的代码来看,还不清楚发生了什么。你如何绘制纹理表面?此外,您的 PPM 加载程序不符合规范。它可能适用于 GIMP 写入的文件,但 ppm 确实允许通用空格字符,而您假设空格和换行符的特定布局。
-
您是在使用片段着色器,还是使用固定功能的纹理工具?从图像上看,您可能正在调制纹理(将纹理中的颜色与其正在纹理化的多边形的颜色相结合)。如果您使用的是固定功能的东西,您可以尝试调用
glTexEnv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL),与混合颜色相比,它应该只是将纹理粘贴到多边形上。 (这是一个猜测;更多代码将有助于缩小问题范围)。 -
@radical7 stackoverflow.com/questions/20819094/…这里是代码
标签: opengl graphics background ppm