Holmes
2004.09.02, 02:42 AM
I'm trying to find a way to optimize my sprite drawing in OpenGL.
Right now for each sprite I am binding a texture, and drawing a quad. It works, and it looks perfect, but I have a feeling I can make things much faster. The code is below:
-(void)drawFrame:(int)frame size:(NSSize)size repeatTexture:(BOOL)repeating {
GLTexture *image = [framesArray objectAtIndex: frame];
[image bind];
if (coordMode == GLSPRITE_CENTER)//move origin for differing coord modes
glTranslatef(-size.width / 2.0f,-size.height / 2.0f,0.0f);
//use the image size for texturing or pattern it?
NSSize texCoordSize = repeating ? size : [image size];
//is our image non-power-of-two? If not we'll need to adjust the texture coords
if (![image textureIsRectangle]){
NSPoint imageSize = [image size];
texCoordSize.width /= imageSize.width;
texCoordSize.height /= imageSize.height;
glDisable(GL_TEXTURE_RECTANGLE_EXT);
}
else
glEnable(GL_TEXTURE_RECTANGLE_EXT);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2f(0,0);
glTexCoord2f(texCoordSize.width,0.0);
glVertex2f(size.width,0.0);
glTexCoord2f(texCoordSize.width,texCoordSize.heigh t);
glVertex2f(size.width,size.height);
glTexCoord2f(0.0,texCoordSize.height);
glVertex2f(0.0f,size.height);
glEnd();
}
Since it is not allowed to bind textures (or for that matter perform translations) between glBegin( ... ) and glEnd, I'm wasting 50% (according to OpenGL profiler) of my time calling glBegin(). This is pretty unacceptable. The next contender for GL time is CGLFlushDrawable with 23.28%, and glVertex2f with 3.69%.
I tried using glDrawPixels(), but presumedly since this uses a pointer to the texture data in RAM and not VRAM actually turned out much slower than using a quad.
Does anyone have any suggestions of how to get this faster? Keep in mind I don't want to get in a nit picky discussion about which function is 1% faster or slower than whatever function, I'm talking about major optimizations only.
P.S. Depth sorting is not an issue as I'm doing it manually and not using the depth buffer.
Right now for each sprite I am binding a texture, and drawing a quad. It works, and it looks perfect, but I have a feeling I can make things much faster. The code is below:
-(void)drawFrame:(int)frame size:(NSSize)size repeatTexture:(BOOL)repeating {
GLTexture *image = [framesArray objectAtIndex: frame];
[image bind];
if (coordMode == GLSPRITE_CENTER)//move origin for differing coord modes
glTranslatef(-size.width / 2.0f,-size.height / 2.0f,0.0f);
//use the image size for texturing or pattern it?
NSSize texCoordSize = repeating ? size : [image size];
//is our image non-power-of-two? If not we'll need to adjust the texture coords
if (![image textureIsRectangle]){
NSPoint imageSize = [image size];
texCoordSize.width /= imageSize.width;
texCoordSize.height /= imageSize.height;
glDisable(GL_TEXTURE_RECTANGLE_EXT);
}
else
glEnable(GL_TEXTURE_RECTANGLE_EXT);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2f(0,0);
glTexCoord2f(texCoordSize.width,0.0);
glVertex2f(size.width,0.0);
glTexCoord2f(texCoordSize.width,texCoordSize.heigh t);
glVertex2f(size.width,size.height);
glTexCoord2f(0.0,texCoordSize.height);
glVertex2f(0.0f,size.height);
glEnd();
}
Since it is not allowed to bind textures (or for that matter perform translations) between glBegin( ... ) and glEnd, I'm wasting 50% (according to OpenGL profiler) of my time calling glBegin(). This is pretty unacceptable. The next contender for GL time is CGLFlushDrawable with 23.28%, and glVertex2f with 3.69%.
I tried using glDrawPixels(), but presumedly since this uses a pointer to the texture data in RAM and not VRAM actually turned out much slower than using a quad.
Does anyone have any suggestions of how to get this faster? Keep in mind I don't want to get in a nit picky discussion about which function is 1% faster or slower than whatever function, I'm talking about major optimizations only.
P.S. Depth sorting is not an issue as I'm doing it manually and not using the depth buffer.