OpenGL ES 1.x 根据主内存中的位图混合两个纹理

发布于 2024-10-14 20:00:32 字数 237 浏览 4 评论 0原文

我有两个纹理(1024x1024,完全 MipMapped,均为 PVRTC-4bitsPerPixel 压缩)。纹理应用于 3D 网格。

在 CPU 上,我正在更新 512x512 布尔数组。

现在我想相对于这个布尔数组将纹理 2 混合到纹理 1 上(true 表示纹理 1 的相应 4 个像素可见,false 表示其他纹理在此位置可见)

我的硬件有两个可用的纹理单元。

我怎样才能做到这一点?

I have two textures (1024x1024, fully MipMapped, both PVRTC-4bitsPerPixel compressed). The Textures are applied on a 3D mesh.

On the CPU I'm updating a 512x512 boolean array.

Now I want to blend texture 2 over texture 1 with respect to this boolean array (true means the respective 4 pixels of texture1 are visible, false means the other texture is visible at this location)

My hardware has two texture units available.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2024-10-21 20:00:32

您可以为此使用特定的着色器。如果您有 tex1、tex2 和 texbool:

# set your textures
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, tex1)
glActiveTexture(GL_TEXTURE0+1)
glBindTexture(GL_TEXTURE_2D, tex2)
glActiveTexture(GL_TEXTURE0+2)
glBindTexture(GL_TEXTURE_2D, texbool)

# when you're starting your shader (id is 1 for eg)
glUseProgram(1)
glUniformi(glGetUniformLocation(1, "tex1"), 0)
glUniformi(glGetUniformLocation(1, "tex2"), 1)
glUniformi(glGetUniformLocation(1, "texbool"), 2)

# and now, draw your model as you wish

片段着色器可能类似于:

// i assume that you'll pass tex coords too. Since the tex1 and tex2
// look the same size, i'll assume that for now.
varying vec2 tex1_coords;

// get the binded texture
uniform sample2D tex1;
uniform sample2D tex2;
uniform sample2D texbool;

// draw !
void main(void) {
  vec4 c1 = texture2D(tex1, tex1_coords);
  vec4 c2 = texture2D(tex2, tex1_coords);
  vec4 cbool = texture2D(texbool, tex1_coords / 2.);
  // this will mix c1 to c2, according to cbool.
  // if cbool is 0, c1 will be used, otherwise, it will be c2
  glFragColor = mix(c1, c2, cbool);
}

我没有测试您的示例,但我将如何开始找到解决方案:)

You can use a specific shader for that. If you have tex1, tex2, and texbool:

# set your textures
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, tex1)
glActiveTexture(GL_TEXTURE0+1)
glBindTexture(GL_TEXTURE_2D, tex2)
glActiveTexture(GL_TEXTURE0+2)
glBindTexture(GL_TEXTURE_2D, texbool)

# when you're starting your shader (id is 1 for eg)
glUseProgram(1)
glUniformi(glGetUniformLocation(1, "tex1"), 0)
glUniformi(glGetUniformLocation(1, "tex2"), 1)
glUniformi(glGetUniformLocation(1, "texbool"), 2)

# and now, draw your model as you wish

The fragment shader can be something like:

// i assume that you'll pass tex coords too. Since the tex1 and tex2
// look the same size, i'll assume that for now.
varying vec2 tex1_coords;

// get the binded texture
uniform sample2D tex1;
uniform sample2D tex2;
uniform sample2D texbool;

// draw !
void main(void) {
  vec4 c1 = texture2D(tex1, tex1_coords);
  vec4 c2 = texture2D(tex2, tex1_coords);
  vec4 cbool = texture2D(texbool, tex1_coords / 2.);
  // this will mix c1 to c2, according to cbool.
  // if cbool is 0, c1 will be used, otherwise, it will be c2
  glFragColor = mix(c1, c2, cbool);
}

I didn't test for your example, but that how i'll start to found a solution :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文