Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / shape_info.cpp
blob71ac3f7dc434861ef6a4e6d06dc791fb868cc6ae
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "std3d.h"
23 #include "nel/3d/shape_info.h"
24 #include "nel/3d/mesh.h"
25 #include "nel/3d/mesh_geom.h"
26 #include "nel/3d/mesh_mrm.h"
27 #include "nel/3d/mesh_multi_lod.h"
29 #include "nel/misc/path.h"
32 using namespace NLMISC;
34 #ifdef DEBUG_NEW
35 #define new DEBUG_NEW
36 #endif
38 namespace NL3D
41 // ***************************************************************************
42 void CShapeInfo::swap(CShapeInfo &other)
44 Tris.swap(other.Tris);
45 std::swap(LocalBBox, (other.LocalBBox));
49 // ***************************************************************************
50 void CShapeInfo::build(const IShape &shape)
52 LocalBBox.setMinMax(CVector::Null, CVector::Null);
53 // Cast to CMesh
54 const CMesh *mesh=dynamic_cast<const CMesh*>(&shape);
56 // Cast to CMeshMultiLod
57 const CMeshMultiLod *meshMulti=dynamic_cast<const CMeshMultiLod*>(&shape);
59 // Cast to CMeshMultiLod
60 const CMeshMRM *meshMRM=dynamic_cast<const CMeshMRM*>(&shape);
62 // It is a mesh ?
63 if (mesh)
65 // Add its triangles
66 build(*mesh, mesh->getMeshGeom());
68 // It is a CMeshMultiLod ?
69 else if (meshMulti)
71 // Get the first geommesh
72 const IMeshGeom *meshGeom=&meshMulti->getMeshGeom (0);
74 // Dynamic cast
75 const CMeshGeom *geomMesh=dynamic_cast<const CMeshGeom*>(meshGeom);
76 if (geomMesh)
78 build(*meshMulti, *geomMesh);
81 // Dynamic cast
82 const CMeshMRMGeom *mrmGeomMesh=dynamic_cast<const CMeshMRMGeom*>(meshGeom);
83 if (mrmGeomMesh)
85 build(*meshMulti, *mrmGeomMesh);
88 // It is a CMeshMultiLod ?
89 else if (meshMRM)
91 // Get the first lod mesh geom
92 build(*meshMRM, meshMRM->getMeshGeom ());
94 // compute bbox
95 if (Tris.empty())
97 LocalBBox.setMinMax(CVector::Null, CVector::Null);
99 else
101 LocalBBox.setMinMax(Tris[0].V0, Tris[0].V0);
102 LocalBBox.extend(Tris[0].V1);
103 LocalBBox.extend(Tris[0].V2);
104 for(uint k = 1; k < Tris.size(); ++k)
106 LocalBBox.extend(Tris[k].V0);
107 LocalBBox.extend(Tris[k].V1);
108 LocalBBox.extend(Tris[k].V2);
113 // ***************************************************************************
114 void CShapeInfo::build(const CMeshBase &meshBase, const CMeshGeom &meshGeom)
116 // Get the vertex buffer
117 const CVertexBuffer &vb=meshGeom.getVertexBuffer();
118 CVertexBufferRead vba;
119 vb.lock (vba);
121 // For each matrix block
122 uint numBlock=meshGeom.getNbMatrixBlock();
123 for (uint block=0; block<numBlock; block++)
125 // For each render pass
126 uint numRenderPass=meshGeom.getNbRdrPass(block);
127 for (uint pass=0; pass<numRenderPass; pass++)
129 // Get the material
130 const CMaterial &material = meshBase.getMaterial (meshGeom.getRdrPassMaterial ( block, pass));
132 if (material.getBlend()) continue; // don't test against transparent materials
133 //if (material.getAlphaTest()) continue; // don't test against transparent materials
137 // Get the primitive block
138 const CIndexBuffer &primitive=meshGeom.getRdrPassPrimitiveBlock ( block, pass);
139 // Copy triangles
140 CIndexBufferRead iba;
141 primitive.lock (iba);
142 if (iba.getFormat() == CIndexBuffer::Indices32)
144 const uint32* triIndex= (const uint32*) iba.getPtr ();
145 uint numTri=primitive.getNumIndexes ()/3;
146 uint tri;
147 for (tri=0; tri<numTri; tri++)
149 // Vertex
150 CVector v0= *vba.getVertexCoordPointer (triIndex[tri*3]);
151 CVector v1= *vba.getVertexCoordPointer (triIndex[tri*3+1]);
152 CVector v2= *vba.getVertexCoordPointer (triIndex[tri*3+2]);
153 Tris.push_back(NLMISC::CTriangle (v0, v1, v2));
156 else
158 const uint16* triIndex= (const uint16*) iba.getPtr ();
159 uint numTri=primitive.getNumIndexes ()/3;
160 uint tri;
161 for (tri=0; tri<numTri; tri++)
163 // Vertex
164 CVector v0= *vba.getVertexCoordPointer (triIndex[tri*3]);
165 CVector v1= *vba.getVertexCoordPointer (triIndex[tri*3+1]);
166 CVector v2= *vba.getVertexCoordPointer (triIndex[tri*3+2]);
167 Tris.push_back(NLMISC::CTriangle (v0, v1, v2));
175 // ***************************************************************************
176 void CShapeInfo::build(const CMeshBase &meshBase, const CMeshMRMGeom &meshGeom)
178 // Get the vertex buffer
179 const CVertexBuffer &vb=meshGeom.getVertexBuffer();
180 CVertexBufferRead vba;
181 vb.lock (vba);
183 // For each render pass
184 uint numRenderPass=meshGeom.getNbRdrPass(0);
185 for (uint pass=0; pass<numRenderPass; pass++)
187 // Get the material
188 const CMaterial &material = meshBase.getMaterial (meshGeom.getRdrPassMaterial ( 0, pass));
190 if (material.getBlend()) continue; // don't test against transparent materials
191 //if (material.getAlphaTest()) continue; // don't test against transparent materials
194 // Get the primitive block
195 const CIndexBuffer &primitive=meshGeom.getRdrPassPrimitiveBlock ( 0, pass);
197 // Copy triangles
198 CIndexBufferRead iba;
199 primitive.lock (iba);
200 if (iba.getFormat() == CIndexBuffer::Indices32)
202 const uint32* triIndex= (const uint32 *) iba.getPtr ();
203 uint numTri=primitive.getNumIndexes ()/3;
204 uint tri;
205 for (tri=0; tri<numTri; tri++)
207 // Vertex
208 CVector v0 = *vba.getVertexCoordPointer (triIndex[tri*3]);
209 CVector v1 = *vba.getVertexCoordPointer (triIndex[tri*3+1]);
210 CVector v2 = *vba.getVertexCoordPointer (triIndex[tri*3+2]);
211 Tris.push_back (NLMISC::CTriangle (v0, v1, v2));
214 else
216 const uint16* triIndex= (const uint16 *) iba.getPtr ();
217 uint numTri=primitive.getNumIndexes ()/3;
218 uint tri;
219 for (tri=0; tri<numTri; tri++)
221 // Vertex
222 CVector v0 = *vba.getVertexCoordPointer (triIndex[tri*3]);
223 CVector v1 = *vba.getVertexCoordPointer (triIndex[tri*3+1]);
224 CVector v2 = *vba.getVertexCoordPointer (triIndex[tri*3+2]);
225 Tris.push_back (NLMISC::CTriangle (v0, v1, v2));
232 // ***************************************************************************
233 std::string standardizeShapeName(const std::string &name)
235 std::string result = NLMISC::toLowerAscii(name);
236 if (CFile::getExtension(result).empty())
238 result += ".shape";
240 return result;
243 } // NL3D