cant get GL_TEXTURE_RECTANGLE_EXT to work

nuero
Unregistered
 
Post: #1
I downloaded NSGLImage from the mac site, but it will only load po2 textures. If I attempt to load a NPO2 image, it displays the image in a distorted unrecognizable way.

I am using a powerBook G4 with a GeForce4 MX. I also downloaded glview and it lists GL_TEXTURE_RECTANGLE_EXT as one of the enxtensions I have.

I need to be able to load npo2 textures. I have tried several other techniques all without success. Any help would be greatly appreciated.

here is a portion of the code

Code:
-(void)loadTextureFromFile:(NSString*)filePath;
{
    // First, deallocate any image reps we might have.
    if(bmpRep)
    {
        [bmpRep release];
        // We also need to delete our texture ID
        glDeleteTextures(1, &txID);
    }
    if(!filePath)
        return;
    // Get raw bitmap data from the NSImage
    bmpRep = [[NSBitmapImageRep imageRepWithContentsOfFile:filePath] retain];
    // Set the image height & width for convenience
    imgWidth = [bmpRep size].width;
    imgHeight = [bmpRep size].height;
    [self genImageInfo:[filePath lastPathComponent]];
    // Set our scaling factors
    scaleX = imgWidth / scaleFactor;
    scaleY = imgHeight / scaleFactor;
    NSLog(@"Image: %d x %d", imgWidth, imgHeight);
    [self getTextureInfo];
    // Run through our initialization routine
    [self initGL];
    
}


-(void)initGL
{
    GLuint err = 0;
    // Enable texturing
    glEnable(GL_TEXTURE_RECTANGLE_EXT);
    // Enable client storage
    glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, 1);
    glPixelStorei(GL_UNPACK_ROW_LENGTH, imgWidth);
    // Generate a texture ID
    glGenTextures(1, &txID);
    // Bind to our newly created texture ID
    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, txID);
    // Specify the texture
    glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, bpp, imgWidth, imgHeight, 0, GL_RGB, datatype, [bmpRep bitmapData]);
    err = glGetError();
    if(err)
    {
        NSLog(@"WARNING: OpenGL Error 0x%x while loading texture with glTexImage2D().", err);
        [self discardData];
        ready = NO;
        return;
    }
    // Rebuild our geometry to match the texture
    [self genQuad];
    ready = YES;
}

-(void)drawTexture
{
    // Bail if our image rep is invalid
    if(!bmpRep)
        return;

    // Bail out if we have no image data
    if(![bmpRep bitmapData])
        return;

    // Shut off depth and face culling
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    // Enable texturing
    glEnable(GL_TEXTURE_RECTANGLE_EXT);
    // Make sure we've got the right texture
    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, txID);
    
    // Set color so we get full display
    glColor4f(1.0, 1.0, 1.0, 1.0);
    
    // Draw it!
    glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0);                                // Lower Left
        glVertex3f(glQuad[0].x, glQuad[0].y, glQuad[0].z);    // Upper Right

        glTexCoord2f(imgWidth, 0.0);                        // Lower Right
        glVertex3f(glQuad[1].x, glQuad[1].y, glQuad[1].z);    // Upper Left

        glTexCoord2f(imgWidth, imgHeight);                    // Upper Right
        glVertex3f(glQuad[2].x, glQuad[2].y, glQuad[2].z);    // Lower Left

        glTexCoord2f(0.0, imgHeight);                        // Upper Left
        glVertex3f(glQuad[3].x, glQuad[3].y, glQuad[3].z);    // Lower Right
    glEnd();

    // Gotta turn this off here, or we die
    glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, 0);
    // Turn all the other stuff back on
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glEnable(GL_CULL_FACE);
    // Reset the blending function to its previous state
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
that all looks right, maybe you need to set
Code:
glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
as well?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
Having those shouldn't make any difference. I think the problem is the data of the bitmap is compressed. For Tiger, you will need to use the colorAt or pixelAt methods to fill in your own uncompressed buffer, but in Panther you can just use TIFFRepresentation.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
If you dont provide mipmaps you do actually need to set the min/max filter or it will revert to a mip map which doesn't exist and you will see nothing.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
nuero
Unregistered
 
Post: #5
i inserted the glTexParameterf function calls into the initGL function, but that did not change anything.

I am using Tiger. When you say I need to fill in my own uncompressed buffer, do you mean I need to replace [bmpRep bitmapData] with a pointer to the uncompressed data? And I can create that uncompressed buffer byt having a loop iterate through the compressed data with the pixelAt method?

The thing that confuses me is that I am able to load po2 images. Why would having uncompressed data work for npo2 images, but not be needed for po2 images?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
nuero Wrote:I am using Tiger. When you say I need to fill in my own uncompressed buffer, do you mean I need to replace [bmpRep bitmapData] with a pointer to the uncompressed data? And I can create that uncompressed buffer byt having a loop iterate through the compressed data with the pixelAt method?

The thing that confuses me is that I am able to load po2 images. Why would having uncompressed data work for npo2 images, but not be needed for po2 images?
If you recompile your old projects, do they still work? I know that there have been some changes since Panther, so that may be one of them. I will admit that I'm not a huge expert on cocoa, but from my experience of OpenGL, what works for one should work for the other (excluding mipmaps) unless something else changes.

Unknown: I've been lazy before and not specified the MIN and MAX filter. It doesn't matter. It simply looks at the first mipmap, because that's the only one that's available. In fact, IIRC mipmaps aren't even available for rectangular textures. I commented out my filter designations for my water demo with rectangular textures, and it didn't change anything.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
unknown Wrote:If you dont provide mipmaps you do actually need to set the min/max filter or it will revert to a mip map which doesn't exist and you will see nothing.

Not for GL_TEXTURE_RECTANGLE_* targets, because the default parameters are different. Check the spec.
Quote this message in a reply
nuero
Unregistered
 
Post: #8
I don't have any old code to recompile and check. I only just started.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #9
Then it is likely the same problem with Tiger that you can't just use TIFFRepresentation. (I guess that means getting the data directly is automatically the TIFF data) In that case, you will need to go into a loop and get every pixel with pixelAt.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
Tiger doesn't just randomly compress NSBitmapImageRep's data without telling you. You *must* always check that you're getting the data in the expected format, though -- this behavior could change whenever Apple feels like it. If you look at the Apple sample code you'll see that they make various checks for things like whether the data is planar, whether it has 3 or 4 components per pixel, whether each component is one byte, how much padding there is at the end of each row, etc. Your code doesn't make any of those checks, so I have no faith that you're getting data in the format you're assuming you are.

Bottom line is, NSBitmapImageRep is *not* a good way to load image data for OpenGL texturing (or any other application where you care about the format of the returned data). Either ImageIO/Quartz or QuickTime will let you load the image into a buffer you've allocated, in a format you've specified. That will avoid all data format issues, and be future-proof.

QuickTime: http://onesadcookie.com/svn/repos/QTValuePak
ImageIO: http://gamewiki.evolpenguin.com/index.ph...re_Loading
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Gl_texture_rectangle_ext nabobnick 8 5,762 Aug 10, 2004 11:17 AM
Last Post: lightbringer