0

请问有人知道这个代码颜色不匹配有什么问题吗?iam 使用 ppm 加载程序加载图像,但在游戏中加载图像时颜色与图像不匹配。

当我传递一个白色图像时,它显示为黑色,当我传递黑色时,它显示为白色,当我传递 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);
 }
4

1 回答 1

0

看起来您没有正确读取标题,并且您正在读取从实际开始的像素数据偏移量。

首先,我会fgets在阅读尺寸后删除。然后开始使用地面实况测试图像检查您希望读取的像素值和获得的像素值。

于 2013-12-28T17:59:49.797 回答