簡體   English   中英

OpenGL - 具有多個紋理的蒙版

[英]OpenGL - mask with multiple textures

我已經根據以下概念在 OpenGL 中實現了遮罩:

  • 面具由黑色和白色組成。
  • 前景紋理應該只在蒙版的白色部分可見。
  • 背景紋理應該只在遮罩的黑色部分可見。

我可以通過使用 glBlendFunc() 使白色部分或黑色部分按預期工作,但不能同時使用兩者,因為前景層不僅混合到蒙版上,還混合到背景層上。

有沒有人知道如何以最佳方式實現這一目標? 我一直在網上搜索並閱讀有關片段着色器的內容。 這是要走的路嗎?

這應該有效:

glEnable(GL_BLEND);
// Use a simple blendfunc for drawing the background
glBlendFunc(GL_ONE, GL_ZERO);
// Draw entire background without masking
drawQuad(backgroundTexture);
// Next, we want a blendfunc that doesn't change the color of any pixels,
// but rather replaces the framebuffer alpha values with values based
// on the whiteness of the mask. In other words, if a pixel is white in the mask,
// then the corresponding framebuffer pixel's alpha will be set to 1.
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
// Now "draw" the mask (again, this doesn't produce a visible result, it just
// changes the alpha values in the framebuffer)
drawQuad(maskTexture);
// Finally, we want a blendfunc that makes the foreground visible only in
// areas with high alpha.
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
drawQuad(foregroundTexture);

這相當棘手,所以如果有任何不清楚的地方,請告訴我。

在創建 GL 上下文時不要忘記請求一個 alpha 緩沖區 否則有可能在沒有 alpha 緩沖區的情況下獲得上下文。

編輯:在這里,我做了一個插圖。插圖

編輯:自從寫了這個答案,我了解到有更好的方法來做到這一點:

  • 如果您僅限於 OpenGL 的固定功能管道,請使用紋理環境
  • 如果您可以使用着色器,請使用片段着色器。

此答案中描述的方式有效,並且在性能上並不比這 2 個更好的選項差,但不太優雅且不太靈活。

Stefan Monov 的回答很好! 但是對於那些仍然有問題無法讓他的答案起作用的人:

  1. 您需要檢查 GLES20.glGetIntegerv(GLES20.GL_ALPHA_BITS, ib) - 您需要非零結果。
  2. 如果你得到 0 - 轉到 EGLConfig 並確保你傳遞了 alpha 位

    EGL14.EGL_RED_SIZE, 8, EGL14.EGL_GREEN_SIZE, 8, EGL14.EGL_BLUE_SIZE, 8, EGL14.EGL_ALPHA_SIZE, 8, <- i havn't this and spent a much of time EGL14.EGL_DEPTH_SIZE, 16,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM