00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "std3d.h"
00027
00028 #include "3d/deform_2d.h"
00029
00030
00031
00032 #include <vector>
00033 #include "nel/misc/vector_2f.h"
00034 #include "nel/misc/smart_ptr.h"
00035 #include "3d/heat_haze.h"
00036 #include "3d/quad_effect.h"
00037 #include "3d/texture_blank.h"
00038 #include "3d/driver.h"
00039 #include "3d/dru.h"
00040 #include "3d/material.h"
00041
00042 namespace NL3D {
00043
00044
00045
00046 uint CDeform2d::_Width;
00047 uint CDeform2d::_Height;
00048 uint CDeform2d::_XGranularity;
00049 uint CDeform2d::_YGranularity;
00050 uint CDeform2d::_XQuad;
00051 uint CDeform2d::_YQuad;
00052 NLMISC::CSmartPtr<ITexture> CDeform2d::_Tex;
00053
00054
00055 void CDeform2d::setupBuffer(uint width, uint height, uint xGranularity, uint yGranularity
00056 , uint xQuad, uint yQuad)
00057 {
00058 _Width = width;
00059 _Height = height;
00060 _XGranularity = xGranularity;
00061 _YGranularity = yGranularity;
00062 _XQuad = xQuad;
00063 _YQuad = yQuad;
00064
00065 uint realWidth = NLMISC::raiseToNextPowerOf2(_Width);
00066 uint realHeight= NLMISC::raiseToNextPowerOf2(_Height);
00067
00068 _Tex = new CTextureBlank;
00069 _Tex->resize(realWidth, realHeight, CBitmap::RGBA);
00070 }
00071
00072
00073
00074
00076 static inline void computePerturbation(const float x, const float y, float &dx, float &dy)
00077 {
00078
00079
00080
00081 dx= 0.f;
00082 dy = 0.f;
00083 }
00084
00085 void CDeform2d::doDeform(const TPoint2DVect &surf, IDriver *drv, IPerturbUV *uvp)
00086 {
00087
00088 nlassert(uvp);
00089
00090 typedef CQuadEffect::TPoint2DVect TPoint2DVect;
00091 TPoint2DVect dest;
00092
00093 CQuadEffect::processPoly(surf, (float) _XGranularity, (float) _YGranularity, dest);
00094
00095 uint realWidth = NLMISC::raiseToNextPowerOf2(_Width);
00096 uint realHeight= NLMISC::raiseToNextPowerOf2(_Height);
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 static CMaterial mat;
00109 mat.setDoubleSided(true);
00110 mat.setLighting(false);
00111 mat.setZFunc(CMaterial::always);
00112
00113
00114
00115 static CVertexBuffer vb;
00116 vb.setVertexFormat(CVertexBuffer::PositionFlag | CVertexBuffer::TexCoord0Flag);
00117
00118
00119
00120 drv->setFrustum(0, (float) _Width, 0, (float) _Height, -1, 1, false);
00121 drv->setupViewMatrix(CMatrix::Identity);
00122 drv->setupModelMatrix(CMatrix::Identity);
00123
00124
00125 const float iDu = 1.f / _Width;
00126 const float iDv = 1.f / _Height;
00127 const float widthRatio = _Width / (float) realWidth;
00128 const float heightRatio = _Height / (float) realHeight;
00129
00130
00131 float u, u2, v;
00132 float du, dv;
00133
00134 TPoint2DVect::const_iterator it;
00135
00136
00137 for (it = dest.begin(); it != dest.end(); ++it)
00138 {
00139 drv->copyFrameBufferToTexture(_Tex, 0, (uint32) it->x,(uint32) it->y, (uint32) it->x, (uint32) it->y, _XGranularity, _YGranularity);
00140 }
00141
00142
00146 vb.setNumVertices(dest.size() << 2);
00147 mat.setTexture(0, _Tex);
00148 uint k = 0;
00149 for (it = dest.begin(); it != dest.end(); ++it, k += 4)
00150 {
00151
00152
00153
00154
00155 vb.setVertexCoord(k, NLMISC::CVector(it->x, 0, it->y));
00156 vb.setVertexCoord(k + 1, NLMISC::CVector(it->x + _XGranularity, 0, it->y));
00157 vb.setVertexCoord(k + 2, NLMISC::CVector(it->x + _XGranularity, 0, it->y + _YGranularity));
00158 vb.setVertexCoord(k + 3, NLMISC::CVector(it->x , 0, it->y + _YGranularity));
00159
00160
00161
00162 u = it->x * iDu;
00163 v = it->y * iDv;
00164 uvp->perturbUV(u, v, du, dv);
00165 vb.setTexCoord(k, 0, (u + du) * widthRatio, (v + dv) * heightRatio );
00166
00167 u2 = (it->x + _XGranularity) * iDu;
00168 uvp->perturbUV(u2, v, du, dv);
00169 vb.setTexCoord(k + 1, 0, (u2 + du) * widthRatio, (v + dv) * heightRatio );
00170
00171 v = (it->y + _YGranularity) * iDv;
00172 uvp->perturbUV(u2, v, du, dv);
00173 vb.setTexCoord(k + 2, 0, (u2 + du) * widthRatio, (v + dv) * heightRatio );
00174
00175 uvp->perturbUV(u, v, du, dv);
00176 vb.setTexCoord(k + 3, 0, (u + du) * widthRatio, (v + dv) * heightRatio );
00177 }
00178
00179 drv->activeVertexBuffer(vb);
00180 drv->renderQuads(mat, 0, dest.size());
00181 }
00182
00183 }