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
Code:
if (!glSetup){
//Set up OpenGL projection matrix
printf("GLSetup\n");
[glView setCurrentContext];
//Set up OpenGL projection matrix
CGRect rect = [[UIScreen mainScreen] bounds];
glMatrixMode(GL_PROJECTION);
glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
//Initialize OpenGL states
// Works
glColor4f(0, 0, 0, 1); // Black for the background color
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//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]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
printf("Display title\n");
glDisable(GL_BLEND);
// For some reason, have to have this or else the screen won't update
[Background drawInRect:[glView bounds]];
glEnable(GL_BLEND);
printf("Swap buffers\n");
[glView swapBuffers];
glSetup = YES;
}
Note: glView is MyEAGLView pointer.
This view is later used in a different thread.
Code:
// 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);
//glDisable(GL_BLEND);
// 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];
}else{
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
//glEnable(GL_BLEND);
//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
}
}
printf("SwapBuffers\n");
// 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:
Code:
printf("drawRect\n");
// 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);
// THIS DOESN'T WORK
glColor4f(red, green, blue, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices);
// THIS DOESN'T WORK
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:
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
right before your glDrawArrays call.
Welcome to iDG BTW
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. 
Thanks so much. That had been driving me crazy for at least 2 days.
|