Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / 3d / deform_2d.cpp
blobf36f6182074d678ca1f1b695151f6bbde2c06cf3
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/3d/deform_2d.h"
21 #include "nel/misc/vector_2f.h"
22 #include "nel/misc/smart_ptr.h"
23 #include "nel/3d/heat_haze.h"
24 #include "nel/3d/quad_effect.h"
25 #include "nel/3d/texture_blank.h"
26 #include "nel/3d/driver.h"
27 #include "nel/3d/dru.h"
28 #include "nel/3d/material.h"
30 #ifdef DEBUG_NEW
31 #define new DEBUG_NEW
32 #endif
34 namespace NL3D {
38 uint CDeform2d::_Width;
39 uint CDeform2d::_Height;
40 uint CDeform2d::_XGranularity;
41 uint CDeform2d::_YGranularity;
42 uint CDeform2d::_XQuad;
43 uint CDeform2d::_YQuad;
44 NLMISC::CSmartPtr<ITexture> CDeform2d::_Tex;
47 void CDeform2d::setupBuffer(uint width, uint height, uint xGranularity, uint yGranularity
48 , uint xQuad, uint yQuad)
50 _Width = width;
51 _Height = height;
52 _XGranularity = xGranularity;
53 _YGranularity = yGranularity;
54 _XQuad = xQuad;
55 _YQuad = yQuad;
57 uint realWidth = NLMISC::raiseToNextPowerOf2(_Width);
58 uint realHeight= NLMISC::raiseToNextPowerOf2(_Height);
60 _Tex = new CTextureBlank;
61 _Tex->resize(realWidth, realHeight, CBitmap::RGBA);
67 /// compute the UV perturbation at one position of the screen (with screen mapped to 0..1, 0..1)
68 static inline void computePerturbation(const float x, const float y, float &dx, float &dy)
70 // dummy func for now
71 /* dx = 0.05f * (float) (rand() * (1.F / RAND_MAX));
72 dy = 0.05f * (float) (rand() * (1.F / RAND_MAX));*/
73 dx= 0.f;
74 dy = 0.f;
77 void CDeform2d::doDeform(const TPoint2DVect &surf, IDriver *drv, IPerturbUV *uvp)
80 nlassert(uvp);
82 typedef CQuadEffect::TPoint2DVect TPoint2DVect;
83 TPoint2DVect dest;
85 CQuadEffect::processPoly(surf, (float) _XGranularity, (float) _YGranularity, dest);
87 uint realWidth = NLMISC::raiseToNextPowerOf2(_Width);
88 uint realHeight= NLMISC::raiseToNextPowerOf2(_Height);
91 // draw the poly contour
92 /*for (uint k = 0; k < dest.size(); ++k)
94 CDRU::drawLine(dest[k].x, dest[k].y, dest[(k + 1) % dest.size()].x, dest[(k + 1) % dest.size()].y, *drv, CRGBA::Red);
95 }*/
100 static CMaterial mat;
101 mat.setDoubleSided(true);
102 mat.setLighting(false);
103 mat.setZFunc(CMaterial::always);
104 /* mat.setColor(CRGBA::Red);
105 mat.texEnvOpRGB(0, CMaterial::Add); */
107 static CVertexBuffer vb;
108 vb.setName("CDeform2d");
109 vb.setVertexFormat(CVertexBuffer::PositionFlag | CVertexBuffer::TexCoord0Flag);
113 drv->setFrustum(0, (float) _Width, 0, (float) _Height, -1, 1, false);
114 drv->setupViewMatrix(CMatrix::Identity);
115 drv->setupModelMatrix(CMatrix::Identity);
118 const float iDu = 1.f / _Width;
119 const float iDv = 1.f / _Height;
120 const float widthRatio = _Width / (float) realWidth;
121 const float heightRatio = _Height / (float) realHeight;
124 float u, u2, v;
125 float du, dv;
127 TPoint2DVect::const_iterator it;
129 // get back datas from frame buffer
130 for (it = dest.begin(); it != dest.end(); ++it)
132 // todo hulud use the new render to texture interface
133 // drv->copyFrameBufferToTexture(_Tex, 0, (uint32) it->x,(uint32) it->y, (uint32) it->x, (uint32) it->y, _XGranularity, _YGranularity);
137 /** setup the whole vertex buffer
138 * we don't share vertices here, as we work with unaligned quads
140 vb.setNumVertices((uint32)dest.size() << 2);
141 mat.setTexture(0, _Tex);
143 CVertexBufferReadWrite vba;
144 vb.lock (vba);
146 uint k = 0; // current index in the vertex buffer
147 for (it = dest.begin(); it != dest.end(); ++it, k += 4)
150 // \todo optimize this by a direct access to the vertex buffer (if needed)
151 // blit data to frame buffer and apply deformations
153 vba.setVertexCoord(k, NLMISC::CVector(it->x, 0, it->y));
154 vba.setVertexCoord(k + 1, NLMISC::CVector(it->x + _XGranularity, 0, it->y));
155 vba.setVertexCoord(k + 2, NLMISC::CVector(it->x + _XGranularity, 0, it->y + _YGranularity));
156 vba.setVertexCoord(k + 3, NLMISC::CVector(it->x , 0, it->y + _YGranularity));
158 // perturbation of the uv coordinates
160 u = it->x * iDu;
161 v = it->y * iDv;
162 uvp->perturbUV(u, v, du, dv);
163 vba.setTexCoord(k, 0, (u + du) * widthRatio, (v + dv) * heightRatio );
165 u2 = (it->x + _XGranularity) * iDu;
166 uvp->perturbUV(u2, v, du, dv);
167 vba.setTexCoord(k + 1, 0, (u2 + du) * widthRatio, (v + dv) * heightRatio );
169 v = (it->y + _YGranularity) * iDv;
170 uvp->perturbUV(u2, v, du, dv);
171 vba.setTexCoord(k + 2, 0, (u2 + du) * widthRatio, (v + dv) * heightRatio );
173 uvp->perturbUV(u, v, du, dv);
174 vba.setTexCoord(k + 3, 0, (u + du) * widthRatio, (v + dv) * heightRatio );
178 drv->activeVertexBuffer(vb);
179 drv->renderRawQuads(mat, 0, (uint32)dest.size());
182 } // NL3D