# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

deform_2d.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
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         // dummy func for now
00079 /*      dx = 0.05f * (float) (rand() * (1.F  / RAND_MAX));
00080         dy = 0.05f * (float) (rand() * (1.F  / RAND_MAX));*/
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         // draw the poly contour
00100         /*for (uint k = 0; k < dest.size(); ++k)
00101         {
00102                 CDRU::drawLine(dest[k].x, dest[k].y, dest[(k + 1) % dest.size()].x, dest[(k + 1) % dest.size()].y, *drv, CRGBA::Red);
00103         }*/
00104 
00105 
00106         
00107         
00108         static CMaterial mat;
00109         mat.setDoubleSided(true);
00110         mat.setLighting(false);
00111         mat.setZFunc(CMaterial::always);
00112 /*      mat.setColor(CRGBA::Red);
00113         mat.texEnvOpRGB(0, CMaterial::Add); */
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         // get back datas from frame buffer
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; // current index in the vertex buffer
00149         for (it = dest.begin(); it != dest.end(); ++it, k += 4)
00150         {
00151 
00152                 // \todo optimize this by a direct access to the vertex buffer (if needed)
00153                 // blit data to frame buffer and apply deformations
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                 // perturbation of the uv coordinates
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 } // NL3D