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/ig_surface_light_build.h"
20 #include "nel/3d/scene_group.h"
23 using namespace NLMISC
;
34 // ***************************************************************************
35 void CIGSurfaceLightBuild::buildSunDebugMesh(CMesh::CMeshBuild
&meshBuild
,
36 CMeshBase::CMeshBaseBuild
&meshBaseBuild
, const CVector
&deltaPos
)
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
;
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
];
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
);
62 colors
.resize(wVert
*hVert
);
64 // Build vertices pos and colors.
70 uint vId
= y
*wVert
+ x
;
72 meshBuild
.Vertices
[vId0
+ vId
]= surface
.Cells
[vId
].CenterPos
+ deltaPos
;
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
)
86 addDebugMeshFaces(meshBuild
, surface
, vId0
, colors
);
94 // ***************************************************************************
95 void CIGSurfaceLightBuild::buildPLDebugMesh(CMesh::CMeshBuild
&meshBuild
, CMeshBase::CMeshBaseBuild
&meshBaseBuild
, const CVector
&deltaPos
, const CInstanceGroup
&igOut
)
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
;
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
);
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() );
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
];
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.
147 for(y
=0;y
<hVert
; y
++)
149 for(x
=0;x
<wVert
; x
++)
151 uint vId
= y
*wVert
+ x
;
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
164 uint v
= (idLight0
+1)*idMultiplier
;
169 v
= (idLight1
+1)*idMultiplier
;
177 addDebugMeshFaces(meshBuild
, surface
, vId0
, colors
);
185 // ***************************************************************************
186 void CIGSurfaceLightBuild::addDebugMeshFaces(CMesh::CMeshBuild
&meshBuild
, CSurface
&surface
, uint vId0
,
187 const std::vector
<CRGBA
> &colors
)
190 uint wVert
= surface
.Width
;
191 uint hVert
= surface
.Height
;
194 // Allocate enough space for faces.
195 meshBuild
.Faces
.reserve(meshBuild
.Faces
.size() + wCell
*hCell
*2);
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;
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;
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
];
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
];
239 meshBuild
.Faces
.push_back(face0
);
240 meshBuild
.Faces
.push_back(face1
);