glColor4f not working after glDrawArrays

Posts: 2
Joined: 2009.08
Post: #1
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");
Quote this message in a reply
Posts: 3,574
Joined: 2003.06
Post: #2
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
Quote this message in a reply
Posts: 2
Joined: 2009.08
Post: #3
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL glDrawArrays not working dotbianry 12 11,596 Dec 21, 2012 09:21 AM
Last Post: Skorche
  Drawing using glDrawArrays agreendev 9 17,222 Jul 17, 2010 05:20 AM
Last Post: Bersaelor
  glColor4f and glBlendFunc for fading to white? dave05 3 5,267 Jun 24, 2005 05:31 PM
Last Post: Skorche
  glDrawArrays Muffinking 24 15,550 Oct 28, 2003 01:47 PM
Last Post: Mars_999
  glDrawElements vs. glDrawArrays - The numbers are in! inio 22 20,169 Jul 19, 2003 10:00 AM
Last Post: Josh