Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing a game for iOS and I'm quite new to OpenGL.

I'd like to ask some help please, hoping that it could be useful to other people too.

At the moment I'm managing buttons simply by drawing textured 'quads' and detecting the user's tapping. I understand that in OpenGL objects are drawn in a sequential order, but I'd like to manage the visibility of the buttons independently from the order in which my buttons (and more in general objects) are drawn.

My question is: is there a not too complex way to accomplish that (without changing the alpha component, but assigning a certain 'depth' to objects for example)?

Here's my class that handles this (hope it's not too much code, but it's for the sake of clarity):

#import "Sprite.h"
#import <QuartzCore/QuartzCore.h>
#import <GLKit/GLKit.h>

typedef struct {
    CGPoint geometryVertex;
    CGPoint textureVertex;
} TexturedVertex;

typedef struct {
    TexturedVertex bl;
    TexturedVertex br;
    TexturedVertex tl;
    TexturedVertex tr;

} TexturedQuad;

@interface Sprite()

@property (strong, nonatomic) GLKBaseEffect     *effect;
@property (assign, nonatomic) TexturedQuad      quad;
@property (strong, nonatomic) GLKTextureInfo    *textureInfo;


@implementation Sprite

@synthesize effect          = _effect;
@synthesize quad            = _quad;
@synthesize textureInfo     = _textureInfo;
@synthesize position        = _position;

@synthesize contentSize     = _size;
@synthesize moveVelocity    = _moveVelocity;

- (void)setupGeometry
    self.contentSize = CGSizeMake(self.textureInfo.width, self.textureInfo.height);

    TexturedQuad newQuad; = CGPointMake(0.0f, 0.0f); = CGPointMake(self.textureInfo.width, 0.0f); = CGPointMake(0.0f, self.textureInfo.height); = CGPointMake(self.textureInfo.width, self.textureInfo.height); = CGPointMake(0.0f, 0.0f); = CGPointMake(1.0f, 0.0f); = CGPointMake(0.0f, 1.0f); = CGPointMake(1.0f, 1.0f);

    self.quad = newQuad;

#pragma mark - Constructors

- (id)initWithLoader:(GLKTextureInfo * (^)(NSDictionary *))textureLoader andEffect:(GLKBaseEffect *)effect
    self = [super init];
    if (self)
        self.effect = effect;
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil];
        self.textureInfo = textureLoader(options);

        [self setupGeometry];
    return self;

- (id)initWithFile:(NSString *)fileName effect:(GLKBaseEffect *)effect
    return [self initWithLoader:^GLKTextureInfo *(NSDictionary *options) {
        NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];

        NSError *error;
        GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];

        return textureInfo;
    } andEffect:effect];

- (id)initWithCGImage:(CGImageRef)image effect:(GLKBaseEffect *)effect
    return [self initWithLoader:^GLKTextureInfo *(NSDictionary *options) {
        NSError *error;
        GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:image options:options error:&error];

        return textureInfo;
    } andEffect:effect];

#pragma mark - Screen functions

- (GLKMatrix4)modelMatrix
    GLKMatrix4 modelMatrix = GLKMatrix4Identity;
    modelMatrix = GLKMatrix4Translate(modelMatrix, self.position.x, self.position.y, self.Zindex);

    modelMatrix = GLKMatrix4Scale(modelMatrix, self.scaleFactor, self.scaleFactor, 1.0f);

    return modelMatrix;

- (void)render
{ =;
    self.effect.texture2d0.enabled = YES;
    self.effect.transform.modelviewMatrix = self.modelMatrix;
    [self.effect prepareToDraw];


    long offset = (long)&_quad;
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

- (void)update:(float)seconds
    GLKVector2 curMove = GLKVector2MultiplyScalar(self.moveVelocity, seconds);
    self.position = GLKVector2Add(self.position, curMove);


Thank you very much in advance.

share|improve this question

To draw things with transparency in OpenGL you will have to sort them by depth so you can render them in back-to-front order.

I would suggest that your render function create a list of all the textured quads to draw along with a depth value. Then sort the list. Then draw it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.