Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DITHER);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);

// activate pointer to vertex & texture array
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_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;
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, [_textureManager textureByName:@"blocks"]);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

  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,16*y,
        16*x+16,16*y+16,
        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,16*ypos,
    16*xpos+16,16*ypos+16,
    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.

share|improve this question
    
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 –  alariq Sep 21 '12 at 19:55
    
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 –  DocJones Sep 22 '12 at 15:52
    
Screenshot or two might help. –  Jari Komppa Sep 27 '12 at 8:38
    
Ok, i will try to file them. –  DocJones Sep 27 '12 at 8:43
add comment

1 Answer 1

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:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

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.

share|improve this answer
    
That sounds very interessting. Thank you for looking into that! I will try that asap and revert back with your bounty :) –  DocJones Sep 27 '12 at 11:33
    
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. –  DocJones Sep 28 '12 at 7:13
    
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. –  Jari Komppa Sep 28 '12 at 8:42
    
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 :) –  DocJones Sep 28 '12 at 8:50
add comment

Your Answer

 
discard

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.