NeHe OpenGL Lesson44 - 3D Lens Flare With Occlusion Testing

lesson44_screenshot

This samples shows us how to render a 3D lens flare with OpenGL. A lens flare effect go with a light source. Before divide into the render part, there are several things that need to be addressed first.
1) How to make sure whether a light source in the view frustum or not;
2) Whether a light source is occluded by other objects or not;
3) How to build a view frustum given by the view matrix and projection matrix;
4) After all above problems solved, then try to solve the position of the 3D lens flare.

 

Point check in view frustum

There is no frustum structure in the OpenGL, what we could get are some matrixes: view matrix & projection matrix. The view frustum used here is just an volume en-closed by 6 clip planes. So the first question is how to build those 6 clip plane given by the matrix. matrix

Right clip plane : W – X;
Left clip plane : W + X;

Bottom clip Plane : W + Y;

Top clip plane : W – Y;

Far clip plane: W – Z;

Near clip plane: W + Z;

As you see, the function that to create clip planes is very easy. It is very easy to understand if you take the matrix as an identity matrix.

After you get those 6 clip planes, the next step is to go though all those planes and to check whether this point locate in front of all planes.

 

Occlusion Testing with gluProject command

After we make sure that the light source locate in the view frustum, we need to further make sure that the whether this position occluded by other objects or not.
With gluProject function, we could project a 3d position into 2d window position with a completely depth value as one in the depth buffer. We could check this depth value with depth buffer one given by the screen position. The following are the code:

glGetIntegerv (GL_VIEWPORT, viewport);                        //get actual viewport
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);            //get actual model view matrix
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);    //get actual projiection matrix

// this asks OGL to guess the 2d position of a 3d point inside the viewport
gluProject(p.x, p.y, p.z, mvmatrix, projmatrix, viewport, &winx, &winy, &winz);
flareZ = winz;

// we read back one pixel from th depth buffer (exactly where our flare should be drawn)
glReadPixels(winx, winy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &bufferZ);

// if the buffer Z is lower than our flare guessed Z then don't draw 
// this means there is something in front of our flare
if (bufferZ < flareZ)
    return true;
else
    return false;

 

Calculate the 3D Lens Flare effect

Remember the 3D lens flare effect goes with the light source position. What we need to do is find the light source position and offsets (based on the camera view direction) for those series of alpha blending glow textures.Glow_PosAs the diagram details, the bold and dark green line are the areas that used to draw those glow textures. For more details about how to set those offsets and steps, the source code could be found here.  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值