1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
19 #include "nel/3d/vegetable_shape.h"
20 #include "nel/misc/path.h"
21 #include "nel/misc/file.h"
25 using namespace NLMISC
;
35 // ***************************************************************************
36 CVegetableShape::CVegetableShape()
40 PreComputeLighting
= false;
42 BestSidedPreComputeLighting
= false;
45 // ***************************************************************************
46 void CVegetableShape::build(CVegetableShapeBuild
&vbuild
)
48 // Must have TexCoord0.
49 nlassert( vbuild
.VB
.getVertexFormat() & CVertexBuffer::TexCoord0Flag
);
55 if(vbuild
.Lighted
&& ( vbuild
.VB
.getVertexFormat() & CVertexBuffer::NormalFlag
) )
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
;
73 BendCenterMode
= vbuild
.BendCenterMode
;
77 format
= CVertexBuffer::PositionFlag
| CVertexBuffer::TexCoord0Flag
| CVertexBuffer::TexCoord1Flag
;
80 format
|= CVertexBuffer::NormalFlag
;
82 VB
.setVertexFormat(format
);
89 TriangleIndices
.resize(vbuild
.PB
.getNumIndexes());
90 CIndexBufferRead ibaRead
;
91 vbuild
.PB
.lock (ibaRead
);
92 const uint32
*srcTri
= (const uint32
*) ibaRead
.getPtr();
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
++);
104 uint32 nbVerts
= vbuild
.VB
.getNumVertices();
105 VB
.setNumVertices(nbVerts
);
107 CVertexBufferRead vba
;
108 vbuild
.VB
.lock (vba
);
109 CVertexBufferReadWrite vbaOut
;
112 // if no vertex color,
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
;
125 // if no positive value, bend will always be 0.
130 // For all vertices, fill
131 for(i
=0;i
<nbVerts
;i
++)
134 const CVector
*srcPos
= vba
.getVertexCoordPointer(i
);
135 CVector
*dstPos
= vbaOut
.getVertexCoordPointer(i
);
141 const CVector
*srcNormal
= vba
.getNormalCoordPointer(i
);
142 CVector
*dstNormal
= vbaOut
.getNormalCoordPointer(i
);
143 *dstNormal
= *srcNormal
;
147 const CUV
*srcUV
= vba
.getTexCoordPointer(i
, 0);
148 CUV
*dstUV
= vbaOut
.getTexCoordPointer(i
, 0);
152 // copy to texture stage 1.
153 CUV
*dstUVBend
= vbaOut
.getTexCoordPointer(i
, 1);
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
;
163 float w
= srcPos
->z
/ maxZ
;
165 // Copy and scale by MaxBendWeight
166 dstUVBend
->U
= w
* vbuild
.MaxBendWeight
;
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);
190 // ***************************************************************************
191 void CVegetableShape::serial(NLMISC::IStream
&f
)
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"));
204 f
.serial(DoubleSided
);
205 f
.serial(PreComputeLighting
);
206 f
.serial(AlphaBlend
);
207 f
.serialEnum(BendCenterMode
);
209 f
.serialCont(TriangleIndices
);
212 f
.serial(BestSidedPreComputeLighting
);
213 else if(f
.isReading())
214 BestSidedPreComputeLighting
= false;
219 // prepare for instanciation
220 InstanceVertices
.resize(VB
.getNumVertices());