Blending Problem !

Started by Jehan, October 04, 2020, 18:33:58

Previous topic - Next topic

Jehan

Hello !
I have a problem with the blending under lwjgl / opengl ...
I am using a beige background texture like this:


and I want to blend over a red texture like this:


When I do this blending on Gimp I get this:


But when I do this blending on opengl with GL11.glBlendFunc (GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
I get this:


There is indeed transparency, but it is too much opaque.
When I check the RGB value of the resulting pixels under GIMP, I got: resultColor = (srcAlpha * srcColor + (1 - srcAlpha) * dstColor). it works !
But with opengl, the equation is not respected, the transparency is much more opaque than it should be.

My fragment shader is quite standard, I only sample the texture:
vec4 textureColour = texture (modelTexture, pass_textureCoordinates);
out_Color = textureColour;


I don't understand where the problem is coming from, would anyone know?

Thankyou for answers !

KaiHH

You are probably missing:
glEnable(GL_BLEND);

Jehan

Hi thankyou for answer,

no, I have enabled gl Blend for sure:

GL11.glEnable (GL11.GL_BLEND);
GL11.glBlendFunc (GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glDepthMask (false);

// drawing

GL11.glDepthMask (true);
GL11.glDisable (GL11.GL_BLEND);

like that

transparency works, but what appears to be transparent is much more opaque than expected, but still transparent...

KaiHH

Then the problem is in how you load the texture.
Do you have a minimal, complete and verifiable example program that reproduces the problem?

Jehan

I have several texture load functions, I have tried all of them and get the same result, I think they are quite similar to each other.

Here is one:

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public int loadTextureEssai(String fileName)
	{
		Texture texture = null;
		try {
			texture = TextureLoader.getTexture("PNG", Class.class.getResourceAsStream("/res/" + fileName + ".png"));
			GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int textureID = texture.getTextureID();
		textures.add(textureID);
		return textureID;
	}



Jehan

here is another one:

import de.matthiasmann.twl.utils.PNGDecoder;
	import de.matthiasmann.twl.utils.PNGDecoder.Format;
	
	
	public int loadTexture1(String fileName)
	{
		int width = 0;
		int height = 0;
		ByteBuffer buffer = null;
		try {
			InputStream in = Class.class.getResourceAsStream("/res/" + fileName + ".png");
			PNGDecoder decoder = new PNGDecoder(in);
			width = decoder.getWidth();
			height = decoder.getHeight();
			buffer = ByteBuffer.allocateDirect(4 * width * height);
			decoder.decode(buffer, width * 4, Format.BGRA);
			buffer.flip();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Tried to load texture " + fileName + ", didn't work");
			System.exit(-1);
		}
		TextureData currentTextureData = new TextureData(buffer, width, height);
		
		int id = loadTexture2(currentTextureData);
		
		textures.add(id);
		
		return id;
	}
	
	protected static int loadTexture2(TextureData data)
	{
		int texID = GL11.glGenTextures();
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
		GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
		GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, data.getWidth(), data.getHeight(), 0, GL12.GL_BGRA,
				GL11.GL_UNSIGNED_BYTE, data.getBuffer());
		
		GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
		GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS, 0.0f);
		if (GLContext.getCapabilities().GL_EXT_texture_filter_anisotropic)
		{
			float amount = Math.min(4f, GL11.glGetFloat(EXTTextureFilterAnisotropic.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT));
			GL11.glTexParameterf(GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, amount);
		}
		else
		{
			System.out.println("Not supported");
		}
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		return texID;
	}

Jehan

ok, i did a minimal program, but it works!

Now I have to compare the two programs, see where my mistake was!

Thanks for the coaching, KaiHH!

Jehan

Ok, I found my mistake, I had displayed the red spot on itself several times without wanting to.

I apologize for wasting your time, thank you !!