Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / 3d / ig_surface_light_build.cpp
blob70d66c3883cd3d85b39a2665836c2e5a36f32346
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/ig_surface_light_build.h"
20 #include "nel/3d/scene_group.h"
23 using namespace NLMISC;
24 using namespace std;
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D
34 // ***************************************************************************
35 void CIGSurfaceLightBuild::buildSunDebugMesh(CMesh::CMeshBuild &meshBuild,
36 CMeshBase::CMeshBaseBuild &meshBaseBuild, const CVector &deltaPos)
38 contReset(meshBuild);
39 contReset(meshBaseBuild);
40 meshBaseBuild.Materials.resize(1);
41 meshBaseBuild.Materials[0].initUnlit();
42 meshBaseBuild.Materials[0].setBlend(true);
43 meshBaseBuild.Materials[0].setBlendFunc(CMaterial::srcalpha, CMaterial::invsrcalpha);
45 meshBuild.VertexFlags= CVertexBuffer::PositionFlag | CVertexBuffer::PrimaryColorFlag;
47 // For all grids.
48 ItRetrieverGridMap it;
49 for(it= RetrieverGridMap.begin(); it!= RetrieverGridMap.end(); it++)
51 for(uint iSurf= 0; iSurf<it->second.Grids.size(); iSurf++)
53 CSurface &surface= it->second.Grids[iSurf];
55 // Resize vector.
56 uint wVert= surface.Width;
57 uint hVert= surface.Height;
58 uint vId0= (uint)meshBuild.Vertices.size();
59 // Allocate vertices / colors
60 meshBuild.Vertices.resize(vId0 + wVert*hVert);
61 vector<CRGBA> colors;
62 colors.resize(wVert*hVert);
64 // Build vertices pos and colors.
65 uint x, y;
66 for(y=0;y<hVert; y++)
68 for(x=0;x<wVert; x++)
70 uint vId= y*wVert + x;
71 // Copy Pos.
72 meshBuild.Vertices[vId0 + vId]= surface.Cells[vId].CenterPos + deltaPos;
73 // Copy Color.
74 uint8 col= surface.Cells[vId].SunContribution;
75 colors[vId].set(col, col, col, 128);
76 // Force Blue color, to simulate ambiant.
77 colors[vId].B= 128 + colors[vId].B/2;
78 // OutSurface => green is 128.
79 if(!surface.Cells[vId].InSurface)
80 colors[vId].G= 128;
85 // Build faces
86 addDebugMeshFaces(meshBuild, surface, vId0, colors);
94 // ***************************************************************************
95 void CIGSurfaceLightBuild::buildPLDebugMesh(CMesh::CMeshBuild &meshBuild, CMeshBase::CMeshBaseBuild &meshBaseBuild, const CVector &deltaPos, const CInstanceGroup &igOut)
97 contReset(meshBuild);
98 contReset(meshBaseBuild);
99 meshBaseBuild.Materials.resize(1);
100 meshBaseBuild.Materials[0].initUnlit();
102 meshBuild.VertexFlags= CVertexBuffer::PositionFlag | CVertexBuffer::PrimaryColorFlag;
104 // Get the number of lights in Ig.
105 uint numLight= (uint)igOut.getPointLightList().size();
106 numLight= raiseToNextPowerOf2(numLight);
107 uint idMultiplier= 256/ numLight;
109 // For all grids.
110 ItRetrieverGridMap it;
111 for(it= RetrieverGridMap.begin(); it!= RetrieverGridMap.end(); it++)
113 // get the final surface
114 CIGSurfaceLight::TRetrieverGridMap::const_iterator itIg=
115 igOut.getIGSurfaceLight().getRetrieverGridMap().find(it->first);
117 // If not found, abort
118 if( itIg== igOut.getIGSurfaceLight().getRetrieverGridMap().end() )
120 nlwarning("buildPLDebugMesh fails to find retriever '%d' in igOut", it->first);
121 continue;
123 else if( it->second.Grids.size()!=itIg->second.Grids.size() )
125 nlwarning("buildPLDebugMesh find retriever '%d' in igOut, but with bad size: excepting: %d, get: %d",
126 it->first, it->second.Grids.size(), itIg->second.Grids.size() );
127 continue;
130 // For all surface of the retriever.
131 for(uint iSurf= 0; iSurf<it->second.Grids.size(); iSurf++)
133 CSurface &surface= it->second.Grids[iSurf];
134 const CSurfaceLightGrid &igSurface= itIg->second.Grids[iSurf];
136 // Resize vector.
137 uint wVert= surface.Width;
138 uint hVert= surface.Height;
139 uint vId0= (uint)meshBuild.Vertices.size();
140 // Allocate vertices / colors
141 meshBuild.Vertices.resize(vId0 + wVert*hVert);
142 vector<CRGBA> colors;
143 colors.resize(wVert*hVert);
145 // Build vertices pos and colors.
146 uint x, y;
147 for(y=0;y<hVert; y++)
149 for(x=0;x<wVert; x++)
151 uint vId= y*wVert + x;
152 // Copy Pos.
153 meshBuild.Vertices[vId0 + vId]= surface.Cells[vId].CenterPos + deltaPos;
154 // init Color with idMultiplier in Blue (info!).
155 CRGBA &col= colors[vId];
156 col.set(0,0, idMultiplier, 255);
157 // store the compressed id of the light found in igOut.
158 nlassert( CSurfaceLightGrid::NumLightPerCorner>=2 );
159 uint idLight0= igSurface.Cells[vId].Light[0];
160 uint idLight1= igSurface.Cells[vId].Light[1];
161 // 255 means no light. If at least one light
162 if(idLight0<255)
164 uint v= (idLight0+1)*idMultiplier;
165 col.R= min(v, 255U);
166 // if second light
167 if(idLight1<255)
169 v= (idLight1+1)*idMultiplier;
170 col.G= min(v, 255U);
176 // Build faces
177 addDebugMeshFaces(meshBuild, surface, vId0, colors);
185 // ***************************************************************************
186 void CIGSurfaceLightBuild::addDebugMeshFaces(CMesh::CMeshBuild &meshBuild, CSurface &surface, uint vId0,
187 const std::vector<CRGBA> &colors)
189 // Resize faces.
190 uint wVert= surface.Width;
191 uint hVert= surface.Height;
192 uint wCell= wVert-1;
193 uint hCell= hVert-1;
194 // Allocate enough space for faces.
195 meshBuild.Faces.reserve(meshBuild.Faces.size() + wCell*hCell *2);
197 // Build faces
198 uint x,y;
199 for(y=0;y<hCell; y++)
201 for(x=0;x<wCell; x++)
203 uint v00= y*wVert + x;
204 uint v10= y*wVert + x+1;
205 uint v01= (y+1)*wVert + x;
206 uint v11= (y+1)*wVert + x+1;
208 // Skip this cell??
209 bool skip= false;
210 if(!surface.Cells[v00].InSurface && !surface.Cells[v00].Dilated) skip= true;
211 if(!surface.Cells[v10].InSurface && !surface.Cells[v10].Dilated) skip= true;
212 if(!surface.Cells[v01].InSurface && !surface.Cells[v01].Dilated) skip= true;
213 if(!surface.Cells[v11].InSurface && !surface.Cells[v11].Dilated) skip= true;
216 if(!skip)
218 // 1st triangle.
219 CMesh::CFace face0;
220 face0.MaterialId= 0;
221 face0.Corner[0].Vertex= vId0+ v00;
222 face0.Corner[0].Color= colors[v00];
223 face0.Corner[1].Vertex= vId0+ v10;
224 face0.Corner[1].Color= colors[v10];
225 face0.Corner[2].Vertex= vId0+ v01;
226 face0.Corner[2].Color= colors[v01];
228 // 2nd triangle.
229 CMesh::CFace face1;
230 face1.MaterialId= 0;
231 face1.Corner[0].Vertex= vId0+ v10;
232 face1.Corner[0].Color= colors[v10];
233 face1.Corner[1].Vertex= vId0+ v11;
234 face1.Corner[1].Color= colors[v11];
235 face1.Corner[2].Vertex= vId0+ v01;
236 face1.Corner[2].Color= colors[v01];
238 // Add 2 triangles
239 meshBuild.Faces.push_back(face0);
240 meshBuild.Faces.push_back(face1);
248 } // NL3D