I am trying to draw a 2d game map and the objects on the map in a single pass. Here is my OpenGL initialization code

// Turn off unnecessary operations

// activate pointer to vertex & texture array

My drawing code is being called by a NSTimer every 1/60 s. Here is the drawing code of my world object:

- (void) draw:(NSRect)rect withTimedDelta:(double)d {
  GLint *t;
  glBindTexture(GL_TEXTURE_2D, [_textureManager textureByName:@"blocks"]);

  for (int x=0; x<[_map getWidth] ; x++) {
    for (int y=0; y<[_map getHeight] ; y++) {
      GLint v[] = {
        16*x ,16*y,
        16*x ,16*y+16

      t=[_textureManager getBlockWithNumber:[_map getBlockAtX:x andY:y]];

      glVertexPointer(2, GL_INT, 0, v);
      glTexCoordPointer(2, GL_INT, 0, t);
      glDrawArrays(GL_QUADS, 0, 4);


(_textureManager is a Singelton only loading a texture once!) The object drawing codes is identical (except the nested loops) in terms of OpenGL calls:

- (void) drawWithTimedDelta:(double)d {
  GLint *t;
  GLint v[] = {
    16*xpos ,16*ypos,
    16*xpos ,16*ypos+16
  glBindTexture(GL_TEXTURE_2D, [_textureManager textureByName:_textureName]);

  t=[_textureManager getBlockWithNumber:12];

  glVertexPointer(2, GL_INT, 0, v);
  glTexCoordPointer(2, GL_INT, 0, t);
  glDrawArrays(GL_QUADS, 0, 4);

As soon as my central drawing routine calls the two drawing methods the second call overlays the first one. i would expect the call to world.draw to draw the map and "stamp" the objects upon it. Debugging shows me, that the first call is performed correctly (world is being drawn), but the following call to all objects ONLY draws the objects, the rest of the scene is getting black.

I think i need to blend the drawn textures, but i cant seem to figure out how.

Any help is appreciated. Thanks

PS: Here is the github link to the project. It may not be in sync of my post here, but for some more in-depth analysis it may help.

| improve this question | | | | |
  • \$\begingroup\$ you say that second call draws only objects and every other tile becomes black. where your object drawing code gets it's ypos & xpos from? do you draw object tiles only at specific positions where you have your objects ? if you want your objects to stamp on the map then you probably need alpha channel for your object tiles and enable blending. please give more info. Thanks \$\endgroup\$ – alariq Sep 21 '12 at 19:55
  • \$\begingroup\$ Hi alariq - thanks for your comment. Actually, the post is still valid. I haven't been able to figure out how to blend or use alpha channels yet :( The x/y posistion com from object internal variables. Do you have a suggestion on where to start with alpha/blending? thanks \$\endgroup\$ – DocJones Sep 22 '12 at 15:52
  • \$\begingroup\$ Screenshot or two might help. \$\endgroup\$ – Jari Komppa Sep 27 '12 at 8:38
  • \$\begingroup\$ Ok, i will try to file them. \$\endgroup\$ – DocJones Sep 27 '12 at 8:43

Looking at the art assets in your github I think I know what the problem is.

What you need is alpha blending. Save your sprites with an alpha channel (or convert the background to zero alpha when loading the textures).

Set up blend function like this:


That tells OpenGL to blend (mix) the pixel data that's already on the screen with the pixel data from the texture based on the texture's alpha value.

| improve this answer | | | | |
  • \$\begingroup\$ That sounds very interessting. Thank you for looking into that! I will try that asap and revert back with your bounty :) \$\endgroup\$ – DocJones Sep 27 '12 at 11:33
  • \$\begingroup\$ A little sidequestion: Do you know any good program to check the assets and add ALPHA channel to them? I got them from a friend which is unavailable at the moment. \$\endgroup\$ – DocJones Sep 28 '12 at 7:13
  • \$\begingroup\$ I use photoshop myself, but I can understand if that's not an option. That's why I mentioned that you may want to change the texture data on the fly during loading of the texture. \$\endgroup\$ – Jari Komppa Sep 28 '12 at 8:42
  • \$\begingroup\$ Hmja - i have some code to support adding alpha in my texture manager class. it hasn't been tested so far, i will have a look. thanks anyway :) \$\endgroup\$ – DocJones Sep 28 '12 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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