-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcloud_Shade.cpp
More file actions
309 lines (248 loc) · 8.88 KB
/
pcloud_Shade.cpp
File metadata and controls
309 lines (248 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright 2010-2016 by RomboStudios
* All rights reserved.
******************************************************************************
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the software's owners nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************
* Created: 13.11.00
* Module: pcloud_Shade
* Purpose: Point Based Shader
*
* Exports:
* pcloud_Shade
* pcloud_Shade_version
* pcloud_Shade_init
* pcloud_Shade_exit
*
*/
#include "_cpc_mentalray.h"
///////////////////////////////////////////////////////////////////////////////////////////////
// * the parameter struct for the shader
struct pcloud_Shade_paras
{
miBoolean verbose;
miTag map; // string-tag of the map to use
miTag map_attr; // stringtag of the map field/channel
miInteger attr_type; // color or float
miScalar mult; // color result multiplier
miInteger p_look; // points in lookup (opt:1)
miColor fg_bypass; // fg rays don't work well with pclouds
};
// * the cache structure for the shader
struct pcloud_Shade_cache
{
miTag tMap; // We get the map from file, then we store in DB init
// and access it directly from this tag in render loop
Map_field_id map_field_id;
char* map_attr_name;
miInteger map_attr_type;
miScalar multiply_factor;
miInteger points_in_lookup;
miBoolean isshaderball;
miBoolean disabled;
};
///////////////////////////////////////////////////////////////////////////////////////////////
// Shader Initialization
extern "C" DLLEXPORT int pcloud_Shade_version(void) {return(1);}
extern "C" DLLEXPORT void pcloud_Shade_init (
miState *state,
pcloud_Shade_paras *paras,
miBoolean *inst_init_req )
{
if ( ! paras ) {
*inst_init_req = miTRUE; return; // shader instance initialization
}
// inittialize cache
//pcloud_Shade_cache* cache = new pcloud_Shade_cache;
pcloud_Shade_cache* cache = (pcloud_Shade_cache*)
mi_mem_allocate(sizeof(pcloud_Shade_cache));
void **user;
mi_query(miQ_FUNC_USERPTR, state, 0, &user);
*user = cache;
// check if in hypershader
cache->isshaderball = miFALSE;
const miCamera * camera = state->camera;
if( camera->focal == 1.0 || camera->x_resolution<320){
cache->isshaderball = miTRUE;
return;
}
// check if in lightmap stage
// called during 2nd rays eval
miStage stage;
mi_query(miQ_STAGE, state, 0, &stage);
if(stage == miSTAGE_LIGHTMAP){
cache->disabled = miTRUE;
return;
}
miBoolean verbose = *mi_eval_boolean( ¶s->verbose );
if(verbose){
mi_info ( "-----------------------------------------------" );
mi_info ( "--- Reading point cloud" );
mi_info ( "-----------------------------------------------" );
}
// evaluates the tag of the map file name
miTag map_tag = *mi_eval_tag ( ¶s->map );
char *name = ( char * ) mi_db_access ( map_tag );
mi_db_unpin ( map_tag );
// evaluates the tag of the map field string
miTag map_attr_tag = *mi_eval_tag ( ¶s->map_attr );
char* map_attr_name = ( char * ) mi_db_access ( map_attr_tag );
mi_db_unpin ( map_attr_tag );
// access the map from file
Access_map map ( name );
if(map->is_empty()){ mi_info ( "--- Map -> is empty !" );cache->tMap = miNULLTAG;return;}
else {
if(verbose)
mi_info ( "--- Map -> n. of elements: %u" , map->size() );
}
// store the map in scene DB
cache->tMap = map.store();
// get a copy of the declaration
Map_declaration declaration ( map );
if(0){
mi_info ( "--- Map -> dimensions: %u" , declaration->get_dimension () );
mi_info ( "---" );
}
// cache map field to be used for lookup
cache->map_field_id = declaration->get_field_id ( map_attr_name );
cache->map_attr_type = *mi_eval_integer ( ¶s->attr_type );
// cache other parameters
cache->multiply_factor = *mi_eval_scalar ( ¶s->mult );
cache->points_in_lookup = *mi_eval_integer( ¶s->p_look );
// TEMP:DEBUG
if(0)
{
Access_map_iterator it ( map );
int ix = 0; miVector vPos; float area; miVector vDir;
for ( ; ! it->at_end () ; it->next () )
{
it->get_position( vPos );
if(ix%50000==0)
mi_info( "--- Map -> point : %d, position: %.2f %.2f %.2f, dir: %f %f %f, area: %f",
ix, vPos.x, vPos.y, vPos.z,
vDir.x, vDir.y, vDir.z, area);
ix ++;
}
mi_info ( "---" );
mi_info ( "---" );
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
// Shader Exit
extern "C" DLLEXPORT void pcloud_Shade_exit(
miState * state,
pcloud_Shade_paras * paras )
{
if (!paras) return;
void **user;
mi_query(miQ_FUNC_USERPTR, state, 0, &user);
pcloud_Shade_cache* cache = static_cast< pcloud_Shade_cache* >(*user);
if(cache->tMap) mi_db_delete(cache->tMap); // delete data map
mi_mem_release( *user ); // delete user cache
}
///////////////////////////////////////////////////////////////////////////////////////////////
// Shader Implementation
extern "C" DLLEXPORT miBoolean pcloud_Shade (
miColor *result,
miState *state,
struct pcloud_Shade_paras *paras)
{
result->r = result->g = result->b = 0.0f;
result->a = 1.0f;
// * Get shader cache
void **user;
mi_query(miQ_FUNC_USERPTR, state, 0, &user);
pcloud_Shade_cache* cache = static_cast< pcloud_Shade_cache* >(*user);
// exit earlier if ...
if(cache->isshaderball || cache->disabled)
{
if(mi_is_connected(state, ¶s->fg_bypass))
{
*result = *mi_eval_color(¶s->fg_bypass);
}
return miTRUE; // shaderball render
}
if( state->type==miRAY_FINALGATHER )
{
if(mi_is_connected(state, ¶s->fg_bypass))
{
*result = *mi_eval_color(¶s->fg_bypass);
}
return miTRUE;
}
// ** Setup map lookup
if(!cache->tMap) {
mi_error("No map saved to DB or invalid map tag !!");
return miFALSE;
}
Access_map map = cache->tMap;
if( map->is_empty() ){
mi_error("Map is empty !!");
return miFALSE;
}
// attach the lookup to the map
Map_lookup result_lookup ( map );
// runs a lookup inside the map
/*Map_status status = */result_lookup->search ( state->point, cache->points_in_lookup );
// accumulates the colors of all the elements which have been found
miUint num = 0;
if(cache->map_attr_type == 0) // color //////////////////////////////
{
miColor element_color;
for ( ; ! result_lookup->at_end () ; result_lookup->next () ) {
// retrieves the color of the current element in the lookup.
// It is assumed that all the maps have the same declaration, so
// 'map_color_id' is the id of the 'color' field for all the maps
result_lookup->get ( cache->map_field_id , element_color );
result->r += element_color.r;
result->g += element_color.g;
result->b += element_color.b;
}
}else // scalar ////////////////////////////
{
miScalar element_scalar;
for ( ; ! result_lookup->at_end () ; result_lookup->next () ) {
// retrieves the color of the current element in the lookup.
// It is assumed that all the maps have the same declaration, so
// 'map_color_id' is the id of the 'color' field for all the maps
result_lookup->get ( cache->map_field_id , element_scalar );
result->r += element_scalar;
result->g += element_scalar;
result->b += element_scalar;
}
}
// keeps track of how many elements were used in all
num = result_lookup->size ();
// divides by the total number of elements to get the average
if ( num > 0 ) {
result->r /= ( float ) num;
result->g /= ( float ) num;
result->b /= ( float ) num;
}
// scales the result by the color multiplier
result->r *= cache->multiply_factor;
result->g *= cache->multiply_factor;
result->b *= cache->multiply_factor;
result->a = 1.0;
return ( miTRUE );
}