3
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\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, 2012 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, 2012 at 15:52
  • \$\begingroup\$ Screenshot or two might help. \$\endgroup\$ Sep 27, 2012 at 8:38
  • \$\begingroup\$ Ok, i will try to file them. \$\endgroup\$
    – DocJones
    Sep 27, 2012 at 8:43

1 Answer 1

5
+25
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\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, 2012 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, 2012 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\$ Sep 28, 2012 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, 2012 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .