Sign in to follow this  

Need a little help w/ 1st texturing attempt

This topic is 3298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble with texturing. I'm dumping vertex, normal, and UV data from Blender and then drawing it all with the code below. The object looks ok without texturing (it's just a white hemisphere) but when I turn on the texturing it's just black. I think the UV coordinates are reasonable, they're all between 0 and 1. There isn't any black regions of my image so I'd expect to see something other than black even if I've mapped the texture poorly. I'm using a 3rd party texture loader from a magazine article. It's intended to do exactly what I'm using it for - get textures loaded from an image file on the iphone OS. I have NO idea whether it's working or not but my first suspicion is I'm missing an OGL call or have some kind of order messed up. The code below is pretty much everything relevant except I've truncated the data after the 2nd triangle. I can post the whole thing if need be. It's supposed to be a section of a sphere. At startup:
    glGenTextures(1, &groundTex);

    GLTextureHelper helper(1024, 1024);
    // Load the body texture
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, groundTex);
    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    helper.LoadTexture("ground.png");   
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 


Drawing:
void mdl_ground_draw() {
    const GLfloat vertices[] = {
        -0.191342, -0.038060, 0.980785,
        0.000000, 0.000000, 1.000000,
        -0.195090, -0.000000, 0.980785,
        -0.180240, -0.074658, 0.980785,
        0.000000, 0.000000, 1.000000,
        -0.191342, -0.038060, 0.980785,
        ...
    };

    const GLfloat normals[] = {
        -0.191321, -0.038057, 0.980773,
        0.000000, -0.062960, 0.997986,
        -0.195013, -0.019196, 0.980590,
        -0.180212, -0.074648, 0.980773,
        0.000000, -0.062960, 0.997986,
        -0.191321, -0.038057, 0.980773,
        ...
    };

    const GLfloat tex_uv[] = {
        0.938580, 0.725228,
        1.000000, 0.593457,
        0.986817, 0.725275,
        0.892447, 0.719881,
        1.000000, 0.593457,
        0.938580, 0.725228,
        ...
    };


    glShadeModel(GL_SMOOTH);

    glEnable(GL_TEXTURE_2D);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glNormalPointer(GL_FLOAT, 0, normals);
    glTexCoordPointer(2, GL_FLOAT, 0, tex_uv);
  
    glBindTexture(GL_TEXTURE_2D, groundTex);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glDrawArrays(GL_TRIANGLES, 0, 48);
    glDisable(GL_TEXTURE_2D);

}

Share this post


Link to post
Share on other sites
I can't see where the texture is copied into GL_TEXTURE_2D after loading, usually you would use glTexImage2D() to copy the pixels from your loaded image data into the currently bound GL_TEXTURE_2D.

Does this GLTextureHelper business do that for you?

Share this post


Link to post
Share on other sites
Yeah, the LoadTexture method eventually does this:


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _nWidth, _nHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, _pData);

Share this post


Link to post
Share on other sites
I don't have any lighting set up and lighting is disabled.

It looks like my texture isn't getting loaded, I happened to look at the stdout from the debugger.

<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component colorspace; kCGImageAlphaLast; 2048 bytes/row.
<Error>: CGContextDrawImage: invalid context



I don't know a lot about image formats or what this framework can support. I'm feeding this with a PNG. I switched to using a TGA format of the same image and it doesn't complain... and now the object is just white.

One thing I have noticed is commenting out the texture loading doesn't make any difference. How can I prove whether or not the texture is getting loaded?

Share this post


Link to post
Share on other sites
Quote:
Original post by zhyla
I don't have any lighting set up and lighting is disabled.

It looks like my texture isn't getting loaded, I happened to look at the stdout from the debugger.

*** Source Snippet Removed ***

I don't know a lot about image formats or what this framework can support. I'm feeding this with a PNG. I switched to using a TGA format of the same image and it doesn't complain... and now the object is just white.
Can you post the contents of your helper.LoadTexture function? It looks as if you are trying to load a PNG with an unsupported alpha channel.

Share this post


Link to post
Share on other sites
The loader source is below. Good news, I figured out that even with the TGA file it was failing, I just wasn't checking the return code properly and it wasn't reporting an error properly in the PNG case. I was able to get a TIFF format to load. The colors are inverted and it's scaled funny, but it's oriented correctly so that's something.

So PNG can obviously do alpha, and I've seen examples of using this library to load PNG. Is there something wrong with my image? I'm using gimp to create the image.

bool GLTextureHelper::LoadTexture(const char *szTextureFile)
{
CGImageRef Image;
CGContextRef Context;

// Creates a Core Graphics image from an image file
NSString *fileName = [[NSString alloc] initWithCString:(szTextureFile)];
Image = [UIImage imageNamed:fileName].CGImage;
[fileName release];
if(Image) {
size_t bytesPerRow = CGImageGetBytesPerRow(Image);
size_t bitsPerComponent = CGImageGetBitsPerComponent(Image);
_nWidth = CGImageGetWidth(Image);
_nHeight = CGImageGetHeight(Image);

// Uses the bitmatp creation function provided by the Core Graphics framework.
Context = CGBitmapContextCreate(_pData, _nWidth, _nHeight, bitsPerComponent, bytesPerRow,
CGImageGetColorSpace(Image),
CGImageGetBitmapInfo(Image));

if(!Context) // zhyla added
return false;

// After you create the context, you can draw the sprite image to the context.
CGContextDrawImage(Context, CGRectMake(0.0, 0.0, float(_nWidth), float(_nHeight)), Image);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(Context);

// Use OpenGL ES to generate a name for the texture.
// glGenTextures(1, &spriteTexture);
// Bind the texture name.
// glBindTexture(GL_TEXTURE_2D, spriteTexture);
// Speidfy a 2D texture image, provideing the a pointer to the image data in memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _nWidth, _nHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, _pData);
}
else
return false;

return true;
}

Share this post


Link to post
Share on other sites

This topic is 3298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this