0

any one please know what is the wrong with this code colors did not match ? iam using ppm loader to load an image but colors did not match the image when it loaded in the game .

when i pass a white image it is appear black , when i pass black it is appear white , when i pass 255 , 0 ,0 it is appear 0 ,255,255 , when i pass 128 , 128 , 192 it is appear 128 , 128 , 64

Fullscreen Image

#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);
 }
vallentin
  • 23,478
  • 6
  • 59
  • 81
  • 1
    From the code posted so far, it is not clear what is going on. How do you draw the textured surfaces? Also, your PPM loader is not followinf the spec. It might work for the files the GIMP writes, but ppm does allow generic whitespace characters, while you assume a specific layout of whitespaces and newlines. – derhass Dec 28 '13 at 16:49
  • 1
    Are you using a fragment shader, or using the fixed-function texturing facilities? From the image, it looks like you might be _modulating_ the texture (combining the color in the texture with the color of the polygon it's texturing). If you're using the fixed-function stuff, you might try calling `glTexEnv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)`, which should just paste the texture on the polygon, as compared to blending colors. (That's a guess; more code would help narrow the problem). – radical7 Dec 28 '13 at 17:41
  • @radical7 http://stackoverflow.com/questions/20819094/opengl-texturing-ppm-background please here is the code – Saif Suleiman Dec 28 '13 at 20:44

1 Answers1

0

It looks like you're not reading the header correctly and you are reading the pixel data offset from the actual start.

To begin with, I would remove the fgets after reading the dimensions. Then start checking what pixel values you expect to read and what you get, using a ground truth test image.

koan
  • 3,596
  • 2
  • 25
  • 35