Skip to content

feat: add alpha channel blending support#7

Open
tslmy wants to merge 6 commits into
jserv:mainfrom
tslmy:alpha
Open

feat: add alpha channel blending support#7
tslmy wants to merge 6 commits into
jserv:mainfrom
tslmy:alpha

Conversation

@tslmy

@tslmy tslmy commented May 20, 2026

Copy link
Copy Markdown
  • Add a field to ZBufferPoint (8.16 fixed-point, same as r,g,b)
  • Propagate vertex alpha from glColor4f through the vertex pipeline
  • Add GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA blend factors to TGL_BLEND_FUNC and TGL_BLEND_FUNC_RGB macros
  • Use TGL_ALPHA_MUL helper with (factor + factor>>7) correction to map [0,255] -> [0,256], ensuring alpha=255 is identity and alpha=0 produces exactly zero (no darkening artifacts)
  • Preserve texture alpha through RGB_MIX_FUNC in 32-bit mode
  • Add INTERP_A support to ztriangle.h for per-vertex alpha interpolation
  • Update smooth-shaded blend triangles to interpolate and use alpha
  • Update flat-shaded blend triangles to pack vertex alpha into pixel
  • Update line/point blend paths to pass alpha

Added tests, for both textured and untextured models, to ensure that alpha blending works correctly.

Also added a compilable demo, examples/raw/alpha.c, to illustrate how transparency may look like. It renders a scene like the image below:

alpha_demo

closes #6

Disclaimer: Coding agent (GitHub Copilot + Claude Sonnet 4.6) is heavily used in this PR.

tslmy added 4 commits May 19, 2026 19:06
- Add 'a' field to ZBufferPoint (8.16 fixed-point, same as r,g,b)
- Propagate vertex alpha from glColor4f through the vertex pipeline
- Add GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA blend factors to
  TGL_BLEND_FUNC and TGL_BLEND_FUNC_RGB macros
- Use TGL_ALPHA_MUL helper with (factor + factor>>7) correction to
  map [0,255] -> [0,256], ensuring alpha=255 is identity and alpha=0
  produces exactly zero (no darkening artifacts)
- Preserve texture alpha through RGB_MIX_FUNC in 32-bit mode
- Add INTERP_A support to ztriangle.h for per-vertex alpha interpolation
- Update smooth-shaded blend triangles to interpolate and use alpha
- Update flat-shaded blend triangles to pack vertex alpha into pixel
- Update line/point blend paths to pass alpha
- Add gl_convertRGBA_to_8A8R8G8B() that preserves the alpha channel
  when converting from RGBA byte data to internal 0xAARRGGBB format
- Add gl_resizeImageNoInterpolate4() for resizing 4-channel textures
- Update glopTexImage2D to accept GL_RGBA with components=4
- Texture alpha is preserved through TGL_BLEND_FUNC via the pixel's
  high byte, enabling proper alpha-blended textured geometry
Add 7 tests covering alpha channel rendering correctness:

Untextured:
- alpha_zero_transparent: alpha=0 produces fully transparent output
- alpha_full_opaque: alpha=1 produces fully opaque output
- alpha_half_blend: alpha=0.5 produces correct 50/50 blend
- alpha_smooth_interp: GL_SMOOTH interpolates alpha per-vertex

Textured (GL_RGBA):
- alpha_texture_transparent: RGBA texture with alpha=0 is invisible
- alpha_texture_opaque: RGBA texture with alpha=255 is fully visible
- alpha_texture_half_blend: RGBA texture with alpha=128 blends correctly

All tests use direct framebuffer readback and verify pixel values
with a small tolerance for fixed-point rounding.
@jserv

jserv commented May 20, 2026

Copy link
Copy Markdown
Owner

Please provide sample programs to illustrate the use of newly-introduced APIs.

@tslmy
tslmy marked this pull request as ready for review May 20, 2026 14:46
@tslmy

tslmy commented May 20, 2026

Copy link
Copy Markdown
Author

@jserv good idea. Added a examples/raw/alpha.c file that renders a PNG. Also attached the PNG to a new README as well as the description of this PR.

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.

Feature request: alpha channel emulation

2 participants