Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / 3d / motion_blur.cpp
blob410ac8610a88f8c3672e4029d6842520fde152a2
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "std3d.h"
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"
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D {
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) ;
42 _X = x ;
43 _Y = y ;
44 _W = width ;
45 _H = height ;
46 _Tex = new CTextureBlank ;
47 _Tex->resize(NLMISC::raiseToNextPowerOf2(width), NLMISC::raiseToNextPowerOf2(height)) ;
50 void CMotionBlur::releaseMotionBlur()
52 _Tex = NULL ;
53 _X = _Y = _W = _H = 0 ;
56 void CMotionBlur::performMotionBlur(IDriver *driver, float motionBlurAmount)
58 nlassert(_Tex) ; //start motion blur has not been called !!
59 nlassert(driver) ;
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;
77 vb.lock (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
88 if (!matSetup)
90 mbMat.setBlend(true) ;
91 mbMat.setBlendFunc(CMaterial::srcalpha, CMaterial::invsrcalpha) ;
92 mbMat.setZWrite(false) ;
93 mbMat.setZFunc(CMaterial::always) ;
94 // stage 0
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) ;
102 matSetup = 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) ;
131 } // NL3D