如何用Image作为背景重写GLCameraRipple示例?

Var*_*pal 7 iphone opengl-es objective-c ipad ios

我试图用图像作为背景重写Apple Sample GLCameraRipple代码.实际上我正在尝试使用此代码创建水图像的涟漪效果,但代码适用于相机,而我只想使用简单的水图像作为背景而不是相机.任何方向或任何示例代码都将是一个很大的帮助.提前致谢.

jan*_*ins 10

相机涟漪效果的工作方式是它在三角形网格上变形纹理坐标 - 所以好消息是你想要的效果与视频纹理完全分开; 他们的纹理恰好是一个视频.要完成你想要的,你所要做的就是删除所有的摄像机视频代码,然后绑定你自己的纹理.

所以在viewDidLoad中,你要注释掉[self setupAVCapture],然后在SetupGL中注释掉两个纹理制服,因为你只使用一个(两行是glUniform1i(uniforms[UNIFORM_Y], 0);,glUniform1i(uniforms[UNIFORM_UV], 1);),然后创建并绑定你自己的纹理.

GLKit是完成这样的事情的最快方法.GLKit隐藏了很多来自你的OpenGL函数调用,但非常快速有效.

//create the GLKTextureInfo object
NSError *error;
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft];
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"] options:options error:&error];
if (error) NSLog(@"Ripple FRONT TEXTURE ERROR: %@", error);

//bind the texture to texture unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(texture.target, texture.name);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
Run Code Online (Sandbox Code Playgroud)

现在你已经失去了setupAVCapture中包含的一些非常重要的设置.所以将它放在viewDidLoad中的上述代码之后:

if (_ripple == nil ||
    texture.width != _textureWidth ||
    texture.height != _textureHeight)
{
    _textureWidth = texture.width;
    _textureHeight = texture.height;

    _ripple = [[RippleModel alloc] initWithScreenWidth:_screenWidth
                                          screenHeight:_screenHeight
                                            meshFactor:_meshFactor
                                           touchRadius:5
                                          textureWidth:_textureWidth
                                         textureHeight:_textureHeight];

    [self setupBuffers];
}
Run Code Online (Sandbox Code Playgroud)

最后,您必须稍微更改片段着色器.

打开Shader.fsh并将main函数更改为:

void main()
{
    gl_FragColor = texture2D(SamplerY, texCoordVarying);
}
Run Code Online (Sandbox Code Playgroud)

无论如何,应该这样做.尝试使用RippleModel非常有趣,看看你可以做出什么样的打击效果.

  • 很棒的答案jankins,这就像一个魅力!这个答案应该标记为已接受. (2认同)