Why is one color affecting whats already draw

The basic problem is, im drawing some tiles ( with no color ), then draw the cursor using an outline and fill color, then the last outline color used fills the tiles.

I don’t understand why the tiles are being affected after there drawn. Tryed all the different blendFunc modes, nothing has worked.
Thanks for any help clearing up this annoying problem! :slight_smile:

Basic init code for ortho mode. all there is.

    gl.matrixMode(GL.PROJECTION);
        gl.loadIdentity();
        gl.ortho(0.0f, Display.getWidth(), Display.getHeight(), 0.0f, -100.0f, 100.0f);
        gl.matrixMode(GL.MODELVIEW);
        gl.loadIdentity();

        gl.shadeModel(GL.SMOOTH); 
        gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); 
        gl.enable(GL.POLYGON_SMOOTH);
        gl.enable(GL.BLEND);

Main render method (simplifyed)


        gl.enable(GL.TEXTURE_2D);
        gl.blendFunc(GL.ONE, GL.ONE);
        gl.bindTexture(GL.TEXTURE_2D, 1);
        Tools.fillQuad2i(gl, 100, 100, 200, 200);
                
        cursor.render(gl);

The cursor render method

   /***********  Draw the Mouse Cursor  ****************/
        gl.color3f(0, 1, 0);
        gl.translatef(mouseX, mouseY, 0);
        Tools.fillPolygon(gl, poly);
        gl.color3f(1, 0, 0);
        Tools.drawPolygon(gl, poly);

        /***********  Draw Selection Rectangle  ****************/
        gl.translatef(-mouseX, -mouseY, 0);
        gl.blendFunc(GL.SRC_ALPHA, GL.DST_ALPHA);
        gl.disable(GL.TEXTURE_2D);
        gl.color4f(1, 1, 0, 0.2f);
        Tools.fillQuad2i(gl, dragX, dragY, dragX + dragWidth, dragY + dragHeight);
        gl.color3f(1, 0, 0);
        Tools.drawQuad2i(gl, dragX, dragY, dragX + dragWidth, dragY + dragHeight);     

Both rendering ops have the wrong blending mode.
Tiles should be GL.ONE, GL.ZERO (ie., disable blending) as there’s nothing in the background you need to blend with (I assume).

Sprites should be drawn with GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA.

DST_ALPHA refers to the alpha value already stored in the destination framebuffer, and you very likely won’t have one of those, and if you did, you’d only be doing some super clever special effects with it.

Cas :slight_smile:

Hey thanks for the reply, but the blending modes did not change the effect. :-/

Its strange because i had the same thing working in perspective mode. I was drawing the tiles and sprites in perspective, then switching into ortho mode for the cursor. And it worked. Same rendering code, now in all ortho mode, and it doesn’t work.

Its for the josrts project. Ive just refactored and cleand up the interfaces. If you have some time, id really appreciate if you could check out the design and code. :slight_smile:

We are pretty close to having something here. I can create a level in the editor, and see it run in gl. Only problem is damn color blending! :smiley:

http://www.froggy.com.au/harleyrana/java/JOSRTS_0025.zip


       gl.bindTexture(GL.TEXTURE_2D, 1);

is likely to cause a crash or at the very least something unexpected; you’d best fix it (in 2 places!)


        gl.blendFunc(GL.ONE, GL.ONE_MINUS_SRC_ALPHA);

is still the wrong blending mode for sprites :slight_smile: It’s GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA

(Your sprites do have alpha don’t they? Coz I didn’t see any TGAs in that zip, only JPEGs, and they don’t have an alpha channel. Otherwise we’d all be using them :slight_smile: )

Cas :slight_smile:

Hey i know i had the wrong blend modes in places, but in the 2 things that are being rendered it was as you said.

ive tryed to simplify it down to see the problem. so everything is in one render method.

this just draws one colored quad overlapping and area of another and works find.



    public void render()
    {
        gl.clear(GL.COLOR_BUFFER_BIT);
        
            gl.disable(GL.TEXTURE_2D);
        gl.blendFunc(GL.ONE, GL.ZERO);        
        gl.color3f(0, 1, 0);
        Tools.fillQuad2i(gl, 100, 100, 200, 200);
  
        gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
        gl.color4f(1, 0, 0, 0.1f);
        gl.disable(GL.TEXTURE_2D);
        Tools.fillQuad2i(gl, 150, 150, 400, 400);
        
        gl.swapBuffers();
    }



Then if i change it to draw a textured quad, then a colored quad, it doesn’t work. The textured quad is fully colored, when it should only be partly colored in the overlapping area.

    public void render()
    {
        gl.clear(GL.COLOR_BUFFER_BIT);
        
            gl.enable(GL.TEXTURE_2D);
        gl.blendFunc(GL.ONE, GL.ZERO);        
        gl.bindTexture(GL.TEXTURE_2D,tileTextures[0]);
        Tools.fillQuad2i(gl, 100, 100, 200, 200);
  
        gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
        gl.color4f(1, 0, 0, 0.1f);
        gl.disable(GL.TEXTURE_2D);
        Tools.fillQuad2i(gl, 150, 150, 400, 400);
        
        gl.swapBuffers();
    }

The init code is the same as before, super simple. I just don’t see where the problem is. Im using the correct blend modes.
Managed to keep my sanity, but it won’t stay for long. :stuck_out_tongue:

Thanks for any suggestions! :slight_smile:

What does Tools.fillQuad look like? The only random suggestion i’d make is try putting a gl.bindTexture(GL.TEXTURE_2D, 0) in before gl.disable(GL.TEXTURE_2D) and see if that makes a difference (shouldn’t though…).

Two things:

  • You haven’t reset the colour after setting it to transparent red;
  • It may be that you’re using a TEXENV_MODE of MODULATE in which case the entire textured quad will subsequently be modulated with transparent red.

Cas :slight_smile:

Hey thanks for your help!! :slight_smile:

Just needed gl.texEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.DECAL);
before drawing the texture. Simple :slight_smile:

Also how do you reset the color?
its not gl.color4f(0, 0, 0, 0)?

Thanks princec! :smiley: