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.
本文介绍如何使用OpenGL将场景渲染到纹理,并实现径向模糊效果。通过设置视口大小、绘制对象、绑定纹理及复制像素,完成纹理渲染。径向模糊则通过混合多个渲染纹理,调整alpha和纹理拉伸实现。
2325

被折叠的 条评论
为什么被折叠?



