NeHe OpenGL Lesson36 – Radial Blur(径向模糊)

lesson36-300x224 This sample shows us how to implement a radial blur effect with OpenGL render into a texture technology.
To render objects into a texture:
1) set the view port size with the texture size;
2) draw the objects normally;
3) call function glBindTexture to bind a texture object;
4) call function glCopyTexImage2D to copy pixels from current view port to the currently bind texture object. (This step should be done before swapping the back buffer between font buffer). The above process look like following:

void RenderToTexture()                        
{
    glClearColor(0.0f, 0.0f, 0.5f, 0.5);        
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    int w = texture_width;
    int h = texture_height;
 
    glViewport(0,0,w,h);                
 
    { draw the scene objects };
 
    glBindTexture(GL_TEXTURE_2D,BlurTexture);    
 
    // Copy Our ViewPort To The Blur Texture (From 0,0 To w,h... No Border)
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, w, h, 0);
 
    // Do the restore work settings here, some times we just set the settings before rendering.
    // Because we may not know the previous settings, or we roll back to the general settings.
    //glClearColor(0.0f, 0.0f, 0.5f, 0.5);        
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //
    //glViewport(0 , 0,640 ,480);                            
}

 

Make a Radial Blur Effect

To make a radial blur effect, what we need to do is blending a series of previous render texture in front of the 3d objects (Draw a full screen quad). Each blending will work with different alpha and texture stretching. Here texture stretching will ask you set the texture filtering mode to linear. And the ways you stretching texture will create different blur effects, if you stretch the texture from outside edges to the center, you will get a radial blur effect, just as the demo did. But if you stretch vertically or horizontally, you could get vertical blur or horizontal blur effect . The texture stretch by means of moving texture coordinates with command glTexCoord2f. The following is a diagram that used different texture stretching methods:texture_stretch_all

Something More

In addition to the above funny thing provided in this sample, there are still something else:
1) Give us a way to generate a spring model with the code;
2) I guess, there maybe a bug for allocating the buffer for the empty texture. For each pixel, we use 32 bits (4 bytes) to hold them is ok, no need to use 128 bits (16 bytes). Just as the following code described:

// Create Storage Space For Texture Data (128x128x4)
data = (unsigned int*)new GLuint[((128 * 128)* 4 * sizeof(unsigned int))];
ZeroMemory(data,((128 * 128)* 4 * sizeof(unsigned int)));
 
// Our PC frame buffer format is LDR, so 8 bits for each pixel color channel.
// So the code could be modified like this without any problem !!!
data = (unsigned int*)new GLuint[((128 * 128)* 4 * sizeof(unsigned char))];
ZeroMemory(data,((128 * 128)* 4 * sizeof(unsigned char)));

 

The full source code could be checked from here.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/23/2651825.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值