Search code examples
cocoaopengltexturesscreensaver

Grayscaled texture rendered with color in OpenGL


I'm writing Cocoa screensaver, simple opengl scene, nothing special. I have a bunch of rgb gif's with patterns and all of them works great, except one.

What I see on the screensaver preview (rendering single quad with a texture on it):

Screensaver preview

Texture itself (scaled accordingly):

enter image description here

Some code:

Tex loading:

NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:[texImg TIFFRepresentation]];
if(bitmap) {
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)[texImg size].width, 
                 (GLsizei)[texImg size].height, 0, GL_RGB, GL_UNSIGNED_BYTE,
                 [bitmap bitmapData]) ;

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}

GL init:

glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);

Solution

  • Seems like it could be a pixel pack issue.

    Can you try setting this line before creating the texture?

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    You can read about what this does here:

    http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml

    and

    http://www.opengl.org/archives/resources/features/KilgardTechniques/oglpitfall/ (read section 8)

    But basically by default opengl expects rows of pixels to be a multiple of 4 bytes, which isn't always accurate when using 1/2/3 bytes per pixel.

    Recommend also setting it back to 4 (default) after the texture is created.