# 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  

vegetable_shape.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 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/vegetable_shape.h"
00029 #include "nel/misc/path.h"
00030 #include "nel/misc/file.h"
00031 
00032 
00033 using namespace std;
00034 using namespace NLMISC;
00035 
00036 
00037 namespace NL3D 
00038 {
00039 
00040 
00041 // ***************************************************************************
00042 CVegetableShape::CVegetableShape()
00043 {
00044         Lighted= false;
00045         DoubleSided= false;
00046         PreComputeLighting= false;
00047         AlphaBlend= false;
00048         BestSidedPreComputeLighting= false;
00049 }
00050 
00051 // ***************************************************************************
00052 void            CVegetableShape::build(CVegetableShapeBuild &vbuild)
00053 {
00054         // Must have TexCoord0.
00055         nlassert( vbuild.VB.getVertexFormat() & CVertexBuffer::TexCoord0Flag );
00056 
00057         // Header
00058         //---------
00059 
00060         // Lighted ?
00061         if(vbuild.Lighted && ( vbuild.VB.getVertexFormat() & CVertexBuffer::NormalFlag) )
00062                 Lighted= true;
00063         else
00064                 Lighted= false;
00065 
00066         // DoubleSided
00067         DoubleSided= vbuild.DoubleSided;
00068 
00069         // PreComputeLighting.
00070         PreComputeLighting= Lighted && vbuild.PreComputeLighting;
00071 
00072         // AlphaBlend: valid only for 2Sided and Unlit (or similar PreComputeLighting) mode
00073         AlphaBlend= vbuild.AlphaBlend && DoubleSided && (!Lighted || PreComputeLighting);
00074 
00075         // BestSidedPreComputeLighting
00076         BestSidedPreComputeLighting= PreComputeLighting && vbuild.BestSidedPreComputeLighting;
00077 
00078         // BendCenterMode
00079         BendCenterMode= vbuild.BendCenterMode;
00080 
00081         // Format of the VB.
00082         uint32  format;
00083         format= CVertexBuffer::PositionFlag | CVertexBuffer::TexCoord0Flag | CVertexBuffer::TexCoord1Flag;
00084         // lighted?
00085         if(Lighted)
00086                 format|= CVertexBuffer::NormalFlag;
00087         // set VB.
00088         VB.setVertexFormat(format);
00089 
00090 
00091         // Fill triangles.
00092         //---------
00093         uint    i;
00094         // resisz
00095         TriangleIndices.resize(vbuild.PB.getNumTri() * 3);
00096         uint32  *srcTri= vbuild.PB.getTriPointer();
00097         // fill
00098         for(i=0; i<vbuild.PB.getNumTri(); i++)
00099         {
00100                 TriangleIndices[i*3+0]= *(srcTri++);
00101                 TriangleIndices[i*3+1]= *(srcTri++);
00102                 TriangleIndices[i*3+2]= *(srcTri++);
00103         }
00104 
00105 
00106         // Fill vertices.
00107         //---------
00108         // resize
00109         uint32          nbVerts= vbuild.VB.getNumVertices();
00110         VB.setNumVertices(nbVerts);
00111         // if no vertex color, 
00112         float   maxZ= 0;
00113         bool    bendFromColor= true;
00114         if(! (vbuild.VB.getVertexFormat() & CVertexBuffer::PrimaryColorFlag) )
00115         {
00116                 // must compute bendWeight from z.
00117                 bendFromColor= false;
00118                 // get the maximum Z.
00119                 for(i=0;i<nbVerts;i++)
00120                 {
00121                         float   z= ((CVector*)vbuild.VB.getVertexCoordPointer(i))->z;
00122                         maxZ= max(z, maxZ);
00123                 }
00124                 // if no positive value, bend will always be 0.
00125                 if(maxZ==0)
00126                         maxZ= 1;
00127         }
00128 
00129         // For all vertices, fill
00130         for(i=0;i<nbVerts;i++)
00131         {
00132                 // Position.
00133                 CVector         *srcPos= (CVector*)vbuild.VB.getVertexCoordPointer(i);
00134                 CVector         *dstPos= (CVector*)VB.getVertexCoordPointer(i);
00135                 *dstPos= *srcPos;
00136 
00137                 // Normal
00138                 if(Lighted)
00139                 {
00140                         CVector         *srcNormal= (CVector*)vbuild.VB.getNormalCoordPointer(i);
00141                         CVector         *dstNormal= (CVector*)VB.getNormalCoordPointer(i);
00142                         *dstNormal= *srcNormal;
00143                 }
00144 
00145                 // Texture.
00146                 CUV             *srcUV= (CUV*)vbuild.VB.getTexCoordPointer(i, 0);
00147                 CUV             *dstUV= (CUV*)VB.getTexCoordPointer(i, 0);
00148                 *dstUV= *srcUV;
00149 
00150                 // Bend.
00151                 // copy to texture stage 1.
00152                 CUV             *dstUVBend= (CUV*)VB.getTexCoordPointer(i, 1);
00153                 if(bendFromColor)
00154                 {
00155                         CRGBA   *srcColor= (CRGBA*)vbuild.VB.getColorPointer(i);
00156                         // Copy and scale by MaxBendWeight
00157                         dstUVBend->U= (srcColor->R / 255.f) * vbuild.MaxBendWeight;
00158                 }
00159                 else
00160                 {
00161                         float   w= srcPos->z / maxZ;
00162                         w= max(w, 0.f);
00163                         // Copy and scale by MaxBendWeight
00164                         dstUVBend->U= w * vbuild.MaxBendWeight;
00165                 }
00166         }
00167 
00168 
00169         // Misc.
00170         //---------
00171         // prepare for instanciation
00172         InstanceVertices.resize(VB.getNumVertices());
00173 
00174 }
00175 
00176 // ***************************************************************************
00177 void            CVegetableShape::loadShape(const std::string &shape)
00178 {
00179         string  path= CPath::lookup(shape);
00180         // read this file
00181         CIFile  f(path);
00182         serial(f);
00183 }
00184 
00185 // ***************************************************************************
00186 void            CVegetableShape::serial(NLMISC::IStream &f)
00187 {
00188         /*
00189         Version 1: 
00190                 - BestSidedPreComputeLighting
00191         */
00192         sint    ver= f.serialVersion(1);
00193         f.serialCheck((uint32)'_LEN');
00194         f.serialCheck((uint32)'GEV_');
00195         f.serialCheck((uint32)'BATE');
00196         f.serialCheck((uint32)'__EL');
00197 
00198         f.serial(Lighted);
00199         f.serial(DoubleSided);
00200         f.serial(PreComputeLighting);
00201         f.serial(AlphaBlend);
00202         f.serialEnum(BendCenterMode);
00203         f.serial(VB);
00204         f.serialCont(TriangleIndices);
00205 
00206         if(ver>=1)
00207                 f.serial(BestSidedPreComputeLighting);
00208         else if(f.isReading())
00209                 BestSidedPreComputeLighting= false;
00210 
00211         // if reading
00212         if(f.isReading())
00213         {
00214                 // prepare for instanciation
00215                 InstanceVertices.resize(VB.getNumVertices());
00216         }
00217 }
00218 
00219 
00220 
00221 } // NL3D