gpt4 book ai didi

iphone - 如何在 iPhone 上的 OpenGL ES 2.0 中混合具有不同坐标的两个纹理?

转载 作者:技术小花猫 更新时间:2023-10-29 11:19:32 24 4
gpt4 key购买 nike

当两个纹理覆盖相同的矩形时,我可以在片段着色器中用不同的混合模式混合两个纹理。但现在我的问题是,一个纹理是一个没有旋转的普通矩形,另一个纹理是另一个具有旋转/缩放和平移的矩形。如何以我想要的方式合并这些纹理? (图中)

我知道怎么做...

enter image description here

但不知道该怎么做...

enter image description here

为了在一个矩形(第一个图像)中混合纹理,我使用了以下代码..

objective-c 代码...

- (void) display {
[EAGLContext setCurrentContext:context];

glBindFramebuffer(GL_FRAMEBUFFER, targetFBO);

glUseProgram(program);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureTop);

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, textureBot);

glUniform1i(inputTextureTop, 2);
glUniform1i(inputTextureBot, 3);

glUniform1f(alphaTop, alpha);

glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glVertexAttribPointer(position, 2, GL_FLOAT, 0, 0, imageVertices);
glVertexAttribPointer(inputTextureCoordinate, 2, GL_FLOAT, 0, 0, textureCoordinates);

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}

顶点着色器...

 attribute vec4 position;
attribute vec4 inputTextureCoordinate;

varying vec2 textureCoordinate;

void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}

片段着色器...

varying highp vec2 textureCoordinate;

uniform sampler2D inputTextureTop;
uniform sampler2D inputTextureBot;

uniform highp float alphaTop;

void main()
{
lowp vec4 pixelTop = texture2D(inputTextureTop, textureCoordinate);
lowp vec4 pixelBot = texture2D(inputTextureBot, textureCoordinate);

gl_FragColor = someBlendOperation(pixelTop, pixelBot);
}

最佳答案

你必须将 2 个纹理坐标传递给着色器并修改着色器

添加到 ObjectiveC

glVertexAttribPointer(inputTextureCoordinate2, 2, GL_FLOAT, 0, 0, textureCoordinates2);

顶点着色器

attribute vec4 position;
attribute vec4 inputTextureCoordinate;
attribute vec4 inputTextureCoordinate2;

varying vec2 textureCoordinate;
varying vec2 textureCoordinate2;

void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
textureCoordinate2 = inputTextureCoordinate2.xy;
}

片段着色器

varying highp vec2 textureCoordinate;
varying highp vec2 textureCoordinate2;

uniform sampler2D inputTextureTop;
uniform sampler2D inputTextureBot;

uniform highp float alphaTop;

void main()
{
lowp vec4 pixelTop = texture2D(inputTextureTop, textureCoordinate);
lowp vec4 pixelBot = texture2D(inputTextureBot, textureCoordinate2);

gl_FragColor = someBlendOperation(pixelTop, pixelBot);
}

顺便说一句,inputTextureCoordinate 不一定是 vec4,但可以是 vec2

关于iphone - 如何在 iPhone 上的 OpenGL ES 2.0 中混合具有不同坐标的两个纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com