1
\$\begingroup\$

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);
    glLoadIdentity();
    gluOrtho2D(0.0, WIDTH, HEIGHT, 0.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    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;
    base->load("sully.ppm");

    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:

enter image description here

Someone can help me please?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Looks like your pixels are not BGRA. \$\endgroup\$
    – tkausl
    Oct 2 '16 at 3:17
1
\$\begingroup\$

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.

Is it possible your input image is CMYK? It almost seems like the channel order is reversed, and the pixel values are reversed. It would certainly explain the white vs. black background.

\$\endgroup\$
3
  • \$\begingroup\$ 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 \$\endgroup\$ Oct 3 '16 at 2:01
  • \$\begingroup\$ Hi @user1118321 I solved my problem! Was my fall, I inverted the pixels when I readed ppm file. Thanks for help me and you know how I can put as "problem solved" here? Tks \$\endgroup\$ Oct 5 '16 at 12:52
  • \$\begingroup\$ Glad you fixed it! If you click the check box just below the score, that marks this answer as the one that helped you solve it. \$\endgroup\$ Oct 5 '16 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .