Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to glBlendFuncSeparate() to fix alpha blending issue with FBO #2717

Closed
wants to merge 1 commit into from

Conversation

SAOMDVN
Copy link
Contributor

@SAOMDVN SAOMDVN commented Sep 24, 2022

When drawing to FBO of type RGBA, if using glBlendFunc the RGB component is blended normally, but the alpha component is overwritten instead of blended, this resulting in incorrect result when the FBO texture is drawn back to the main buffer or exported to file. Changing to glBlendFuncSeparate will fix this

@RobLoach
Copy link
Contributor

Makes sense to me. Is there an example you have where this is observable?

@SAOMDVN
Copy link
Contributor Author

SAOMDVN commented Sep 30, 2022

Consider these two transparent textures:
R
B
This is how it normally look like when drawn on an opaque white background:

int main() {
	InitWindow(500, 500, "Test");
	Texture2D red = LoadTexture("R.png");
	Texture2D blue = LoadTexture("B.png");
	while(!WindowShouldClose()) {
		BeginDrawing();
			ClearBackground(WHITE);
			DrawTexture(red, 0, 0, WHITE);
			DrawTexture(blue, 0, 0, WHITE);
		EndDrawing();
	}
	return 0;
}

F1

When I try to do the same thing but inside an FBO, here's what happened:

int main() {
	InitWindow(500, 500, "Test");
	Texture2D red = LoadTexture("R.png");
	Texture2D blue = LoadTexture("B.png");
	RenderTexture2D target = LoadRenderTexture(500, 500);
	while(!WindowShouldClose()) {
		BeginTextureMode(target);
			ClearBackground(WHITE);
			DrawTexture(red, 0, 0, WHITE);
			DrawTexture(blue, 0, 0, WHITE);
		EndTextureMode();
		BeginDrawing();
			ClearBackground(GREEN);
			DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
		EndDrawing();
	}
	return 0;
}

F2
Even though the ClearBackground(WHITE) call should make the render texture opaque, it is somehow transparent and the GREEN leaked through

@raysan5
Copy link
Owner

raysan5 commented Oct 2, 2022

@SAOMDVN Thanks for the improvement! It seems this change breaks some examples, they need to be reviewed.

EDIT: Affected example: shapes/shapes_top_down_lights.c

@SAOMDVN
Copy link
Contributor Author

SAOMDVN commented Oct 2, 2022

Hi! How should I proceed with this issue? Do I need to change the examples to use the new rlSetBlendFactors or should I create a separate function rlSetBlendFactorsSeparate and keep the old one the same to avoid breaking changes?

@raysan5
Copy link
Owner

raysan5 commented Oct 2, 2022

Hi! How should I proceed with this issue? Do I need to change the examples to use the new rlSetBlendFactors or should I create a separate function rlSetBlendFactorsSeparate and keep the old one the same to avoid breaking changes?

@SAOMDVN This is a good question, let me think about it...

@raysan5
Copy link
Owner

raysan5 commented Oct 5, 2022

@SAOMDVN I decided to merge PR #2741 instead of this one for the reasons stated in the comments. Thank you very much to you both for working on this improvement! :)

@raysan5 raysan5 closed this Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants