Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / 3d / vegetable_shape.cpp
blob3e4c2b7f0073cfe89c4b0b235cf5e3c33565b456
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/vegetable_shape.h"
20 #include "nel/misc/path.h"
21 #include "nel/misc/file.h"
24 using namespace std;
25 using namespace NLMISC;
27 #ifdef DEBUG_NEW
28 #define new DEBUG_NEW
29 #endif
31 namespace NL3D
35 // ***************************************************************************
36 CVegetableShape::CVegetableShape()
38 Lighted= false;
39 DoubleSided= false;
40 PreComputeLighting= false;
41 AlphaBlend= false;
42 BestSidedPreComputeLighting= false;
45 // ***************************************************************************
46 void CVegetableShape::build(CVegetableShapeBuild &vbuild)
48 // Must have TexCoord0.
49 nlassert( vbuild.VB.getVertexFormat() & CVertexBuffer::TexCoord0Flag );
51 // Header
52 //---------
54 // Lighted ?
55 if(vbuild.Lighted && ( vbuild.VB.getVertexFormat() & CVertexBuffer::NormalFlag) )
56 Lighted= true;
57 else
58 Lighted= false;
60 // DoubleSided
61 DoubleSided= vbuild.DoubleSided;
63 // PreComputeLighting.
64 PreComputeLighting= Lighted && vbuild.PreComputeLighting;
66 // AlphaBlend: valid only for 2Sided and Unlit (or similar PreComputeLighting) mode
67 AlphaBlend= vbuild.AlphaBlend && DoubleSided && (!Lighted || PreComputeLighting);
69 // BestSidedPreComputeLighting
70 BestSidedPreComputeLighting= PreComputeLighting && vbuild.BestSidedPreComputeLighting;
72 // BendCenterMode
73 BendCenterMode= vbuild.BendCenterMode;
75 // Format of the VB.
76 uint32 format;
77 format= CVertexBuffer::PositionFlag | CVertexBuffer::TexCoord0Flag | CVertexBuffer::TexCoord1Flag;
78 // lighted?
79 if(Lighted)
80 format|= CVertexBuffer::NormalFlag;
81 // set VB.
82 VB.setVertexFormat(format);
85 // Fill triangles.
86 //---------
87 uint i;
88 // resisz
89 TriangleIndices.resize(vbuild.PB.getNumIndexes());
90 CIndexBufferRead ibaRead;
91 vbuild.PB.lock (ibaRead);
92 const uint32 *srcTri= (const uint32 *) ibaRead.getPtr();
93 // fill
94 for(i=0; i<vbuild.PB.getNumIndexes()/3; i++)
96 TriangleIndices[i*3+0]= *(srcTri++);
97 TriangleIndices[i*3+1]= *(srcTri++);
98 TriangleIndices[i*3+2]= *(srcTri++);
101 // Fill vertices.
102 //---------
103 // resize
104 uint32 nbVerts= vbuild.VB.getNumVertices();
105 VB.setNumVertices(nbVerts);
107 CVertexBufferRead vba;
108 vbuild.VB.lock (vba);
109 CVertexBufferReadWrite vbaOut;
110 VB.lock (vbaOut);
112 // if no vertex color,
113 float maxZ= 0;
114 bool bendFromColor= true;
115 if(! (vbuild.VB.getVertexFormat() & CVertexBuffer::PrimaryColorFlag) )
117 // must compute bendWeight from z.
118 bendFromColor= false;
119 // get the maximum Z.
120 for(i=0;i<nbVerts;i++)
122 float z= (vba.getVertexCoordPointer(i))->z;
123 maxZ= max(z, maxZ);
125 // if no positive value, bend will always be 0.
126 if(maxZ==0)
127 maxZ= 1;
130 // For all vertices, fill
131 for(i=0;i<nbVerts;i++)
133 // Position.
134 const CVector *srcPos= vba.getVertexCoordPointer(i);
135 CVector *dstPos= vbaOut.getVertexCoordPointer(i);
136 *dstPos= *srcPos;
138 // Normal
139 if(Lighted)
141 const CVector *srcNormal= vba.getNormalCoordPointer(i);
142 CVector *dstNormal= vbaOut.getNormalCoordPointer(i);
143 *dstNormal= *srcNormal;
146 // Texture.
147 const CUV *srcUV= vba.getTexCoordPointer(i, 0);
148 CUV *dstUV= vbaOut.getTexCoordPointer(i, 0);
149 *dstUV= *srcUV;
151 // Bend.
152 // copy to texture stage 1.
153 CUV *dstUVBend= vbaOut.getTexCoordPointer(i, 1);
154 if(bendFromColor)
156 // todo hulud d3d vertex color RGBA / BGRA
157 const CRGBA *srcColor= (const CRGBA*)vba.getColorPointer(i);
158 // Copy and scale by MaxBendWeight
159 dstUVBend->U= (srcColor->R / 255.f) * vbuild.MaxBendWeight;
161 else
163 float w= srcPos->z / maxZ;
164 w= max(w, 0.f);
165 // Copy and scale by MaxBendWeight
166 dstUVBend->U= w * vbuild.MaxBendWeight;
171 // Misc.
172 //---------
173 // prepare for instanciation
174 InstanceVertices.resize(VB.getNumVertices());
178 // ***************************************************************************
179 bool CVegetableShape::loadShape(const std::string &shape)
181 string path= CPath::lookup(shape, false);
182 if( path.empty() )
183 return false;
184 // read this file
185 CIFile f(path);
186 serial(f);
187 return true;
190 // ***************************************************************************
191 void CVegetableShape::serial(NLMISC::IStream &f)
194 Version 1:
195 - BestSidedPreComputeLighting
197 sint ver= f.serialVersion(1);
198 f.serialCheck(NELID("_LEN"));
199 f.serialCheck(NELID("GEV_"));
200 f.serialCheck(NELID("BATE"));
201 f.serialCheck(NELID("__EL"));
203 f.serial(Lighted);
204 f.serial(DoubleSided);
205 f.serial(PreComputeLighting);
206 f.serial(AlphaBlend);
207 f.serialEnum(BendCenterMode);
208 f.serial(VB);
209 f.serialCont(TriangleIndices);
211 if(ver>=1)
212 f.serial(BestSidedPreComputeLighting);
213 else if(f.isReading())
214 BestSidedPreComputeLighting= false;
216 // if reading
217 if(f.isReading())
219 // prepare for instanciation
220 InstanceVertices.resize(VB.getNumVertices());
226 } // NL3D