iDevGames Forums
glColor4f not working after glDrawArrays - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: glColor4f not working after glDrawArrays (/thread-853.html)

glColor4f not working after glDrawArrays - Technoman - Aug 14, 2009 09:27 PM

Howdy. I'm developing a game for the iPhone 3G (v3.0) and am having trouble getting all my openGL ducks in a row. As the subject says, glColor4f doesn't do anything after calling glDrawArrays. Instead, the color drawn is that of the last pixel of the last Texture2D image (also glDrawArrays) which draws fine.

I'm sure I'm missing a state flag somewhere but I'm not familiar enough with openGL despite my research to see where my mistake is.

Here's the relevant code in snippits:
OpenGL init
    if (!glSetup){
        //Set up OpenGL projection matrix
        [glView setCurrentContext];
        //Set up OpenGL projection matrix
        CGRect rect = [[UIScreen mainScreen] bounds];
        glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
        //Initialize OpenGL states
                // Works
        glColor4f(0, 0, 0, 1); // Black for the background color
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        //Load the background texture and configure it
        printf("Loading background\n");
        Texture2D *Background = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"Background.png"]];
        glBindTexture(GL_TEXTURE_2D, [Background name]);
        printf("Display title\n");
                // For some reason, have to have this or else the screen won't update
        [Background drawInRect:[glView bounds]];        
        printf("Swap buffers\n");
        [glView swapBuffers];
        glSetup = YES;
Note: glView is MyEAGLView pointer.

This view is later used in a different thread.
// in the updateThread
// SNIP: removal of variable declarations not affecting openGL
[[(AppDelegate*)[[UIApplication sharedApplication] delegate] getView] setCurrentContext];
    Texture2D *background = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
    Texture2D *test = [[[Texture2D alloc] initWithImage:[UIImage imageNamed:@"Icon.png"]] retain];
    while (!quitGame){ // this is the main updateloop
        glColor4f(1, 0, 0, 1);
                // These commented out in an attempt to find this bug
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        // Blit the black background
        [background drawInRect:CGRectMake(0,0, 320,480)];
    // Draw objects
    for (int i=0; i<[DrawOrder count]; i++){
        // SNIP: Lots of irrelevant code removed here, no gl interactions
                        NSLog(@"BACKGROUNDCOLOR: %@",bgcolor);
                        // Draw rectangle filled with bgcolor
                        // figure out if bgcolor is textual or color code
                        // convert either to nsarray of nsnumber
                        // then draw rectangle with the color as specified
                        NSArray* arr;

                        if ([bgcolor characterAtIndex:0]=='#' || [self isInt:bgcolor]){
                            arr = [self sixDigitToCode:bgcolor];
                            arr = [self colorToCode:bgcolor];
                        CGRect rect = CGRectMake(x, y, width, height);
    (Function with bad glColor calls)    [[(AppDelegate*)[[UIApplication sharedApplication] delegate] getView] drawRect:rect red:red blue:blue green:green];
                // TEST IMAGES
                [test drawAtPoint:CGPointMake(200,50)];
                // END TEST
                //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
                // removed to find bug
        //glColor4f(0, 0, 0, 1); // set fill color to black
        [[(AppDelegate*)[[UIApplication sharedApplication] delegate] getView] swapBuffers];
        sleep(1);    // decrease FPS for now
    [test release];
The drawRect code called from MyEAGLView in the code above:
    // Color conversion, these come as 1 byte, need to make them float
    float red = (float)r / 255.0;
    float green = (float)g / 255.0;
    float blue = (float)b / 255.0;
     // Draw Polygon
     CGPoint poli[4] = {CGPointMake(rect.origin.x, rect.origin.y),
                    CGPointMake(rect.origin.x + rect.size.width, rect.origin.y),
                    CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height),
                    CGPointMake(rect.origin.x, rect.origin.y + rect.size.height)
    const int vertices = 4;
    glVertexPointer(2, GL_FLOAT, 0, poli);
    glColor4f(red, green, blue, 1.0f);
    glDrawArrays(GL_TRIANGLE_FAN, 0, vertices);
    glColor4f(red, green, blue, 1.0f);
    glDrawArrays(GL_LINE_LOOP, 0, vertices);
    printf("drawRect end\n");

glColor4f not working after glDrawArrays - AnotherJake - Aug 14, 2009 10:34 PM

It's hard to say exactly with the code you've posted, but are you just drawing an un-textured quad and it's not coloring correctly? Because I don't see that you've turned off texturing first. If so, then try:


right before your glDrawArrays call.

Welcome to iDG BTW Smile

glColor4f not working after glDrawArrays - Technoman - Aug 15, 2009 08:09 AM

That solved it. I also forgot to actually set variables I had declared so I was getting junk as my color codes. Blush

Thanks so much. That had been driving me crazy for at least 2 days. Smile