MoNo
MoNo

Reputation: 407

Can RGB values be negative?

I'm trying to draw a picture using glVertex. And here is my code:

struct Pixel{
    GLbyte R, G, B;
};

 GLbyte * originalData = NULL;

 . . . 

 originalData = (GLbyte *)malloc(IMAGE_SIZE);
 fread(originalData, IMAGE_SIZE, 1, file);
  for (int n = 0; n < 256 * 256; n++){
        pixels[n].R = data[n * 3 + 0];
        pixels[n].G = data[n * 3 + 1];
        pixels[n].B = data[n * 3 + 2];

        if (pixels[n].R < (GLbyte)0) std::cerr << "??" << std::endl;
    }

And the Display Function:

glBegin(GL_POINTS);
unsigned int i = 0;
for (unsigned row = 0; row < iWidth; row++){
    for (unsigned col = 0; col < iHeight; col++){
        glColor3b(pixels[i].R, pixels[i].G, pixels[i].B);
        glVertex3f(row,col,0.0f);
        i++;
    }
}
glEnd();

When I'm using glDrawPixels(256, 256, GL_RGB, GL_UNSIGNED_BYTE, originalData); Everything is OK, but Colors get mixed up when I'm using my method.

Can RGB values be negative? when I use

glColor3b(abs(pixels[i].R), abs(pixels[i].G), abs(pixels[i].B));

my output looks better(but again some colors get mixed up).

NOTE1: I'm trying to rasterize a .raw file that I created with Photoshop

NOTE2: I know my method is dummy, but I'm experimenting things

Upvotes: 4

Views: 3713

Answers (1)

derhass
derhass

Reputation: 45352

You are using glColor3b which interprets the arguments as signed bytes. So any color value >= 128 will be interpreted as negative - and clamped to 0 later in the pipeline (assuming reasonable defaults).

Since you want to use the full range 0-255, just use glColor3ub and use the type GLubyte which is for unsigned bytes.

Upvotes: 5

Related Questions