Texture problems

Moderator
Posts: 608
Joined: 2002.04
Post: #1
I am having problems with my texture loading for OpenGL. It loads, but the alpha mask doesn't draw correctly and the colors get all mixed up. Below is my code.
Code:
unsigned long LoadTextureImage(Str255 path)
{
      FSSpec                    spec;
      Rect                    natbounds;
      GraphicsImportComponent gi;
      ComponentResult         cr;
      void                      *buffer;
      GWorldPtr               gw;
      unsigned long             texture;
    
      FSMakeFSSpec(0, 0, path, &spec);

      GetGraphicsImporterForFile(&spec, &gi);
  
      cr = GraphicsImportGetNaturalBounds(gi, &natbounds);
  
      buffer = malloc(4 * natbounds.bottom * natbounds.right);
      
      QTNewGWorldFromPtr(&gw, k32RGBAPixelFormat, &natbounds, NULL, NULL,
                           0, buffer, 4 * natbounds.right);
  
     cr = GraphicsImportSetGWorld(gi, gw, NULL);
      cr = GraphicsImportDraw(gi);
      CloseComponent(gi);

     glGenTextures(1, &texture);                        // Generate OpenGL texture IDs
     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
      glBindTexture(GL_TEXTURE_2D, texture);                // Bind Our Texture
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    // Linear Filtered
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    // Linear Filtered
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, natbounds.right, natbounds.bottom, 0,
          /*GL_BGRA_EXT*/GL_RGBA,   /*GL_UNSIGNED_INT_8_8_8_8_REV*//*0x8035*/GL_UNSIGNED_BYTE, buffer);
      
       return texture;
}
If I use GL_BGRA_EXT and GL_UNSIGNED_INT_8_8_8_8_REV, then it doesn't even show my texture.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
This looks like it was butchered from QTValuePak Smile

You should check that the QTVP sample works on your machine.

AFAIK, k32RGBAPixelFormat does not work on the Mac. It may work on Windows... You need to use k32ARGBPixelFormat on the Mac, at which point you do need GL_BGRA and GL_UNSIGNED_INT_8_8_8_8_REV. You're not checking the return from the QTNewGWorldFromPtr call, though, so it's hard to know if this is the problem.

Remember to free the buffer you've malloced in this function (unless you're using client storage).
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #3
Quote:Originally posted by OneSadCookie
This looks like it was butchered from QTValuePak Smile
Yep, it was. A classic copy-and-paste job Grin

Quote:You should check that the QTVP sample works on your machine.
It won't because the computer this is on runs Mac OS 9.

Quote:AFAIK, k32RGBAPixelFormat does not work on the Mac. It may work on Windows... You need to use k32ARGBPixelFormat on the Mac, at which point you do need GL_BGRA and GL_UNSIGNED_INT_8_8_8_8_REV. You're not checking the return from the QTNewGWorldFromPtr call, though, so it's hard to know if this is the problem.
I tried changing it to k32ARGBPixelFormat and to GL_BGRA and GL_UNSIGNED_INT_8_8_8_8_REV but it still doesn't work Sad I slightly motified the code to check what QTNewGWorldFromPtr returns and it is fine.

Perhaps something I should note: I don't actually use GL_BGRA and GL_UNSIGNED_INT_8_8_8_8_REV, I use the values they are defined as in gl.h and glext.h because I am too lazy to copy the headers from my Mac OS X computer. Should this really make a difference? Is there any reason a video card wouldn't support that stuff? I have use a Rage Pro.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Does your extensions string include GL_EXT_bgra and GL_APPLE_packed_pixel(s)?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #5
Quote:Originally posted by OneSadCookie
Does your extensions string include GL_EXT_bgra and GL_APPLE_packed_pixel(s)?
Yes.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
Perhaps I am binding the texture incorrectly.
Code:
glEnable(GL_BLEND);                                    // Enable Blending
    glDisable(GL_DEPTH_TEST);
    
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    
    glTranslatef(-1.0f, 0.0f, -3.0f);
    
    glRotatef(rotate, 1.0f, 0.0f, 0.0f);
    
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.0);

    glBindTexture(GL_TEXTURE_2D, testTexture);
    
    glBegin(GL_QUADS);
        //glColor3f(1.0f, 0.0f, 0.0f);
        glNormal3f(0, 0, 1);
        glTexCoord2f(0.0f, 0.0f); glVertex3d(0.0f,  0.0f, 0.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3d(1.0f, 0.0f, 0.0f);
        glTexCoord2f(1.0f, -1.0f); glVertex3d(1.0f, -1.0f, 0.0f);
        glTexCoord2f(0.0f, -1.0f); glVertex3d(0.0f, -1.0f, 0.0f);
        
        glNormal3f(0, 0, -1);
        glTexCoord2f(0.0f, 0.0f); glVertex3d(0.0f,  0.0f, 0.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3d(1.0f, 0.0f, 0.0f);
        glTexCoord2f(1.0f, -1.0f); glVertex3d(1.0f, -1.0f, 0.0f);
        glTexCoord2f(0.0f, -1.0f); glVertex3d(0.0f, -1.0f, 0.0f);
    glEnd();
    
    aglSwapBuffers(openGLContext);
Do I need to glEnable anything special?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
GL_TEXTURE_2D...

Does your image have power-of-two width and height? If not, you'll need either to call gluBuild2DMipmaps rather than glTexImage2D, or you'll need to resize your image, either in an image editor, or programmatically.
Quote this message in a reply
blb
Unregistered
 
Post: #8
Note also that using negative texture coordinates may not be what you expect, unless you do want repeating or have enabled clamping.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #9
I have checked all of that stuff Sad The texture draws, just with really odd colors. The texture is a PNG with an alpha channel, if it matters... I am beginning to think the problem is my version of the OpenGL SDK. It is version 1.0 Rasp
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
Are any GL errors being reported?
Quote this message in a reply
henryj
Unregistered
 
Post: #11
How about you post a small section of the image. It's usually pretty easy to tell by looking at it.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #12
Picture of the image or the image as it gets drawn in the program?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
Both.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #14
[Image: rendered.png] in the program. Outside the program: [Image: test.png]
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #15
Those links don't work (for, I think, obvious reasons Wink)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES Texture Problems jhbau1000 1 5,337 Jul 12, 2010 05:57 AM
Last Post: Kezhaya
  Texture mapping problems thingythekid 1 2,276 Aug 26, 2005 03:56 AM
Last Post: OneSadCookie
  Texture Problems Sta7ic 5 3,180 May 28, 2003 01:51 PM
Last Post: OneSadCookie