1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "nel/misc/common.h"
20 #include "nel/3d/motion_blur.h"
21 #include "nel/3d/driver.h"
22 #include "nel/3d/texture.h"
23 #include "nel/3d/texture_blank.h"
24 #include "nel/3d/material.h"
33 CMotionBlur::CMotionBlur() : _Tex(NULL
), _X(0), _Y(0), _W(0), _H(0)
39 void CMotionBlur::startMotionBlur(uint x
, uint y
, uint width
, uint height
)
41 nlassert(width
> 0 && height
> 0) ;
46 _Tex
= new CTextureBlank
;
47 _Tex
->resize(NLMISC::raiseToNextPowerOf2(width
), NLMISC::raiseToNextPowerOf2(height
)) ;
50 void CMotionBlur::releaseMotionBlur()
53 _X
= _Y
= _W
= _H
= 0 ;
56 void CMotionBlur::performMotionBlur(IDriver
*driver
, float motionBlurAmount
)
58 nlassert(_Tex
) ; //start motion blur has not been called !!
60 nlassert(motionBlurAmount
>= 0.f
&& motionBlurAmount
<= 1.f
) ;
62 static CVertexBuffer vb
;
63 vb
.setVertexFormat(CVertexBuffer::PositionFlag
| CVertexBuffer::TexCoord0Flag
) ;
64 vb
.setNumVertices(4) ;
66 uint32 width
, height
;
67 driver
->getWindowSize(width
, height
) ;
69 float widthRatio
= _W
/ (float) NLMISC::raiseToNextPowerOf2 (_W
) ;
70 float heightRatio
= _H
/ (float) NLMISC::raiseToNextPowerOf2 (_H
) ;
72 driver
->setFrustum(0, (float) width
, 0, (float) height
, -1, 1, false) ;
74 static CMaterial mbMat
;
76 CVertexBufferReadWrite vba
;
78 for (uint sn
= 0 ; sn
< 2 ; ++sn
)
80 vba
.setTexCoord(0, sn
, CUV(0, 0)) ;
81 vba
.setTexCoord(1, sn
, CUV(widthRatio
, 0)) ;
82 vba
.setTexCoord(2, sn
, CUV(widthRatio
, heightRatio
)) ;
83 vba
.setTexCoord(3, sn
, CUV(0, heightRatio
)) ;
87 static bool matSetup
= false ; // set to true when mbMat has Been setup
90 mbMat
.setBlend(true) ;
91 mbMat
.setBlendFunc(CMaterial::srcalpha
, CMaterial::invsrcalpha
) ;
92 mbMat
.setZWrite(false) ;
93 mbMat
.setZFunc(CMaterial::always
) ;
95 mbMat
.setTexture(0, _Tex
) ;
96 mbMat
.texEnvOpRGB(0, CMaterial::Replace
);
98 mbMat
.texEnvArg0Alpha(0, CMaterial::Diffuse
, CMaterial::SrcAlpha
);
99 mbMat
.texEnvOpAlpha(0, CMaterial::Replace
);
101 mbMat
.setDoubleSided(true) ;
106 mbMat
.setColor(CRGBA(255, 255, 255, (uint8
) (255.f
* motionBlurAmount
) ) ) ;
109 vba
.setVertexCoord(0, CVector((float) _X
, 0, 0) ) ;
110 vba
.setVertexCoord(1, CVector((float) (_X
+ _W
), 0 ,0) ) ;
111 vba
.setVertexCoord(2, CVector((float) (_X
+ _W
), 0, (float) (_Y
+ _H
) ) );
112 vba
.setVertexCoord(3, CVector(0 , 0, (float) (_Y
+ _H
) ) ) ;
115 driver
->setupViewMatrix(CMatrix::Identity
) ;
116 driver
->setupModelMatrix(CMatrix::Identity
) ;
119 driver
->activeVertexBuffer(vb
) ;
120 driver
->renderRawQuads(mbMat
, 0, 1) ;
122 // blit back frame buffer to save this frame
126 // todo hulud : use the new render to texture interface
127 // driver->copyFrameBufferToTexture(_Tex, 0, 0, 0, _X, _Y, _W, _H) ;