EasyWay Java Game Engine
December 31, 2009, 08:36:53 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: bugs in TileManager and alpha mask render  (Read 235 times)
0 Members and 1 Guest are viewing this topic.
tom_ex
Newbie
*
Posts: 3


View Profile
« on: March 26, 2009, 05:13:53 PM »

Hi!

I've found and fixed two bugs.

The first one was in TileManager class. I was trying to used it as a background (non fixed), but when the camera was moved on x < 0 || y < 0, the tilemanager made a rapid move. The reason was the position calculation. It was just rounding the offset to cell size multiply.

original code in tilemanager render method:
Code:
int startx = Math.max(0, (int) StaticRef.getCamera().x / tileWidth);
int starty = Math.max(0, (int) StaticRef.getCamera().y / tileHeight);
float coordstartx = Math.max(-StaticRef.getCamera().x % tileWidth, x
- StaticRef.getCamera().x);
float coordstarty = Math.max(-StaticRef.getCamera().y % tileHeight, y
- StaticRef.getCamera().y);
fixed code
Code:
int startx = (int) Math.max(0, (int) (-x + StaticRef.getCamera().x) / tileWidth);
int starty = Math.max(0, (int) (-y + StaticRef.getCamera().y) / tileHeight);
float coordstartx = Math.max((x-StaticRef.getCamera().x) % tileWidth, x
- StaticRef.getCamera().x);
float coordstarty = Math.max((y-StaticRef.getCamera().y) % tileHeight, y
- StaticRef.getCamera().y);

The second bug was in sprite render method when using an alpha mask. Everything was ok when the sprite texture was black. When the texture was colorful every pixel with color other than black wasn't masked, just kind a semi transparent. When using my fix the mask texture has to have an alpha channel which is applied to the sprite texture.

original code in sprite render method:
Code:
if (alphaMask != null && colorAlpha == 1) {
                    GL11.glColor4f(1, 1, 1, 1);
                    GL11.glBlendFunc(GL11.GL_DST_COLOR, GL11.GL_ZERO);
                    alphaMask.bind();
                    drawPlain(translatex - cx, translatey - cy, alphaMask);
                    GL11.glColor4f(colorRed, colorGreen, colorBlue, colorAlpha);
                    GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
                    image.bind();
                    drawPlain(translatex - cx, translatey - cy, image);
}
fixed code
Code:
if (alphaMask != null && colorAlpha == 1) {
                GL11.glColor4f(1, 1, 1, 1);
                GL11.glEnable(GL11.GL_BLEND);
                GL11.glBlendFunc(GL11.GL_ZERO, GL11.GL_ONE_MINUS_SRC_ALPHA); // aplying alpha from mask
                alphaMask.bind();
                drawPlain(translatex - cx, translatey - cy, alphaMask);
                GL11.glColor4f(colorRed, colorGreen, colorBlue, colorAlpha);
                GL11.glBlendFunc(GL11.GL_ONE_MINUS_DST_ALPHA, GL11.GL_DST_ALPHA); // aplying colors from texture
                image.bind();
                drawPlain(translatex - cx, translatey - cy, image);
                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // restoring default function
}

Edit: The Sprite's render fix wasn't a good idea. It fixes one, breaks other. This is an improved fix using a texture combiner. GL11 and GL13 are imported static.

Modified part of the render method:
Code:
if (alphaMask != null && colorAlpha == 1) {
// alpha texture
                    glActiveTexture(GL_TEXTURE0);
                    glEnable(GL_TEXTURE_2D);
                    glColor4f(1, 1, 1, 1f);
                    glBindTexture(GL_TEXTURE_2D, alphaMask.getID());
// sprite texture
                    glActiveTexture(GL_TEXTURE1);
                    glEnable(GL_TEXTURE_2D);
                    glColor4f(colorRed, colorGreen, colorBlue, colorAlpha);
                    glBindTexture(GL_TEXTURE_2D, image.getID());
// textures combining
                    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
                            GL_COMBINE);
                    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
                            GL_MODULATE);
                    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE0);
                    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE1);
                    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
                        GL_SRC_ALPHA);
// drawing
                    drawPlain(translatex - cx, translatey - cy, image, true);
                    glDisable(GL_TEXTURE_2D);
                    glActiveTexture(GL_TEXTURE0);
                    glDisable(GL_TEXTURE_2D);
                    glEnable(GL_TEXTURE_2D);
}

Modified part of the drawPlain method
Code:
protected void drawPlain(float x, float y, ITexture image, boolean multiT) {
        ITexture mask;
        if (multiT && (mask = image.getAlphaMask()) != null) {
            glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE0, mask.getXStart(), mask.getYStart());
            glMultiTexCoord2f(GL_TEXTURE1, image.getXStart(), image.getYStart());
            glVertex2f(x, y);
            glMultiTexCoord2f(GL_TEXTURE0, mask.getXEnd(), mask.getYStart());
            glMultiTexCoord2f(GL_TEXTURE1, image.getXEnd(), image.getYStart());
            glVertex2f(x + width, y);
            glMultiTexCoord2f(GL_TEXTURE0, mask.getXEnd(), mask.getYEnd());
            glMultiTexCoord2f(GL_TEXTURE1, image.getXEnd(), image.getYEnd());
            glVertex2f(x + width, y + height);
            glMultiTexCoord2f(GL_TEXTURE0, mask.getXStart(), mask.getYEnd());
            glMultiTexCoord2f(GL_TEXTURE1, image.getXStart(), image.getYEnd());
            glVertex2f(x, y + height);
            glEnd();
        } else (...)
« Last Edit: March 31, 2009, 06:25:19 PM by tom_ex » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!