iDevGames Forums
Texture blues - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Texture blues (/thread-9320.html)



Texture blues - Miglu - Sep 3, 2011 12:10 PM

I am trying to render sprites:
Code:
NSRect LoadTexture(CFStringRef name)
{
    CFURLRef texture_url = CFBundleCopyResourceURL(
                                                   CFBundleGetMainBundle(),
                                                   name,
                                                   CFSTR("png"),
                                                   NULL);
    
    CGImageSourceRef image_source = CGImageSourceCreateWithURL(
                                                               texture_url,
                                                               NULL);
    
    CGImageRef image = CGImageSourceCreateImageAtIndex(
                                                       image_source,
                                                       0,
                                                       NULL);
    
    unsigned width = CGImageGetWidth(image);
    unsigned height = CGImageGetHeight(image);
    
    NSRect rect;
    rect.size.width = width;
    rect.size.height = height;
    rect.origin.x = 0;
    rect.origin.y = 0;
    
    void *data = malloc(width * height * 4);
    
    CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef context = CGBitmapContextCreate(
                                                 data,
                                                 width,
                                                 height,
                                                 8,
                                                 width * 4,
                                                 color_space,
                                                 kCGImageAlphaPremultipliedFirst);
    
    CGContextDrawImage(
                       context,
                       CGRectMake(0, 0, width, height),
                       image);
    
    GLuint texture_id;
    glGenTextures(1, &texture_id);
    glBindTexture(GL_TEXTURE_2D, texture_id);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
    glTexImage2D(
                 GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 width,
                 height,
                 0,
                 GL_BGRA,
                 GL_UNSIGNED_INT_8_8_8_8_REV,
                 data);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_S,
                    GL_CLAMP_TO_BORDER);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_MAG_FILTER,
                    GL_LINEAR);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR);
    
    CFRelease(context);
    CFRelease(color_space);
    free(data);
    CFRelease(image);
    CFRelease(image_source);
    CFRelease(texture_url);
    return rect;
}

This is in the draw method:
Code:
    glBegin(GL_QUADS);
        glTexCoord2f( 0, spriteRect.size.height );                          
        glVertex2f( 0, 0 );
        glTexCoord2f( spriteRect.size.width, spriteRect.size.height );    
        glVertex2f( spriteRect.size.width, 0 );
        glTexCoord2f( spriteRect.size.width, 0 );    
        glVertex2f( spriteRect.size.width, spriteRect.size.height );
        glTexCoord2f( 0, 0 );          
        glVertex2f( 0, spriteRect.size.height );
    glEnd();
These are the problems:
1. The only hues in the rendered textures are blue and white.
2. The images are scaled so that their height is equal to the view's height.

How to solve these?


RE: Texture blues - OneSadCookie - Sep 3, 2011 01:05 PM

If you haven't changed your projection matrix, the view's coords are -1..1 on all axes.


RE: Texture blues - Miglu - Sep 4, 2011 12:58 AM

Alright, thanks.
Could someone help to solve these problems:
1. the textures are rendered in bluescale.
2. glEnable(GL_TEXTURE_2D) causes primitives to have some transparency.