# Why my images always change color when I use glDrawPixels?

I am doing a exercise for the college, where I need load a file 'ppm' but any picture does not load with correct colors. I tried change parameter of glDrawPixels, glBlendFunc, but nothing change for the correct colors. And some changes makes window do not show the picture. I don't know I need to do.

int CALLBACK WinMain(
__in  HINSTANCE hInstance,
__in  HINSTANCE hPrevInstance,
__in  LPSTR lpCmdLine,
__in  int nCmdShow )
{
if (!glfwInit()) return -1;

window = glfwCreateWindow(WIDTH, HEIGHT, "ParallaxGame", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WIDTH, HEIGHT, 0.0);
glMatrixMode(GL_MODELVIEW);

while (!glfwWindowShouldClose(window) && glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
functionTemp();

glfwSwapBuffers(window);
glfwPollEvents();
}

glfwTerminate();
return 0;
}

void functionTemp() {
vector<Image*> chars;

for (int y = base->getHeight() - 256; y >= 0; y -= 256) {
for (int x = 0; x < base->getWidth(); x += 256) {
Image *c = new Image(256, 256, 4); // width, height and channel
base->cut(c, x, y);
chars.push_back(c);
}
}

for (int i = 0; i < chars.capacity(); i++)
{
glDrawPixels(256, 256, GL_BGRA_EXT, GL_UNSIGNED_BYTE, chars[i]->getPixels());
}

}


Images:

Your images have a different color channel order than you are specifying to OpenGL. In the call to glDrawPixels(), you're passing GL_BGRA_EXT as the format. You should try GL_RGBA instead. Or at least, that's what I would try first. Swapping the channels in a photo editing app, it almost seems like blue is fine, but red and green are swapped. However, that would be extremely odd. I know of no file formats that accept something like GBR or GRB as pixel orders.
• my code just can receive a ppm file, I not created a structure to use anothers extensions. And I used GL_RGBA but it's not worked too. I'll try again tomorrow and when I found the answer I'll put here. Thanks Oct 3 '16 at 2:01