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

时间:2012-09-11 14:54:29

标签: iphone objective-c ios ipad opengl-es

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

1 个答案:

答案 0 :(得分:10)

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

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

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

glUniform1i(uniforms[UNIFORM_UV], 1);

现在您已经丢失了一些包含在setupAVCapture中的非常重要的设置。所以把它放在viewDidLoad:

中的上述代码之后
//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); 

最后,您需要稍微更改片段着色器。

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

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];
}

无论如何,应该这样做。试验RippleModel& amp;看看你能做出什么样的蠢事。