NeHe OpenGL Lesson36 – Radial Blur(径向模糊)
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:
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了