3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
25 #include "irrlichtwrapper.h"
28 TimeTaker::TimeTaker(const char *name, u32 *result)
33 m_time1 = getTimeMs();
36 u32 TimeTaker::stop(bool quiet)
40 u32 time2 = getTimeMs();
41 u32 dtime = time2 - m_time1;
49 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
57 u32 TimeTaker::getTime()
59 u32 time2 = getTimeMs();
60 u32 dtime = time2 - m_time1;
64 const v3s16 g_26dirs[26] =
66 // +right, +top, +back
67 v3s16( 0, 0, 1), // back
68 v3s16( 0, 1, 0), // top
69 v3s16( 1, 0, 0), // right
70 v3s16( 0, 0,-1), // front
71 v3s16( 0,-1, 0), // bottom
72 v3s16(-1, 0, 0), // left
74 v3s16(-1, 1, 0), // top left
75 v3s16( 1, 1, 0), // top right
76 v3s16( 0, 1, 1), // top back
77 v3s16( 0, 1,-1), // top front
78 v3s16(-1, 0, 1), // back left
79 v3s16( 1, 0, 1), // back right
80 v3s16(-1, 0,-1), // front left
81 v3s16( 1, 0,-1), // front right
82 v3s16(-1,-1, 0), // bottom left
83 v3s16( 1,-1, 0), // bottom right
84 v3s16( 0,-1, 1), // bottom back
85 v3s16( 0,-1,-1), // bottom front
87 v3s16(-1, 1, 1), // top back-left
88 v3s16( 1, 1, 1), // top back-right
89 v3s16(-1, 1,-1), // top front-left
90 v3s16( 1, 1,-1), // top front-right
91 v3s16(-1,-1, 1), // bottom back-left
92 v3s16( 1,-1, 1), // bottom back-right
93 v3s16(-1,-1,-1), // bottom front-left
94 v3s16( 1,-1,-1), // bottom front-right
98 const v3s16 g_27dirs[27] =
100 // +right, +top, +back
101 v3s16( 0, 0, 1), // back
102 v3s16( 0, 1, 0), // top
103 v3s16( 1, 0, 0), // right
104 v3s16( 0, 0,-1), // front
105 v3s16( 0,-1, 0), // bottom
106 v3s16(-1, 0, 0), // left
108 v3s16(-1, 1, 0), // top left
109 v3s16( 1, 1, 0), // top right
110 v3s16( 0, 1, 1), // top back
111 v3s16( 0, 1,-1), // top front
112 v3s16(-1, 0, 1), // back left
113 v3s16( 1, 0, 1), // back right
114 v3s16(-1, 0,-1), // front left
115 v3s16( 1, 0,-1), // front right
116 v3s16(-1,-1, 0), // bottom left
117 v3s16( 1,-1, 0), // bottom right
118 v3s16( 0,-1, 1), // bottom back
119 v3s16( 0,-1,-1), // bottom front
121 v3s16(-1, 1, 1), // top back-left
122 v3s16( 1, 1, 1), // top back-right
123 v3s16(-1, 1,-1), // top front-left
124 v3s16( 1, 1,-1), // top front-right
125 v3s16(-1,-1, 1), // bottom back-left
126 v3s16( 1,-1, 1), // bottom back-right
127 v3s16(-1,-1,-1), // bottom front-left
128 v3s16( 1,-1,-1), // bottom front-right
133 static unsigned long next = 1;
135 /* RAND_MAX assumed to be 32767 */
138 next = next * 1103515245 + 12345;
139 return((unsigned)(next/65536) % 32768);
142 void mysrand(unsigned seed)
151 // Float with distance
158 float PointAttributeList::getInterpolatedFloat(v2s16 p)
160 const u32 near_wanted_count = 5;
161 // Last is nearest, first is farthest
162 core::list<DFloat> near_list;
164 for(core::list<PointWithAttr>::Iterator
165 i = m_points.begin();
166 i != m_points.end(); i++)
168 PointWithAttr &pwa = *i;
169 u32 d = pwa.p.getDistanceFrom(p);
172 df.v = pwa.attr.getFloat();
175 // If near list is empty, add directly and continue
176 if(near_list.size() == 0)
178 near_list.push_back(df);
182 // Get distance of farthest in near list
184 if(near_list.size() > 0)
186 core::list<DFloat>::Iterator i = near_list.begin();
191 If point is closer than the farthest in the near list or
192 there are not yet enough points on the list
194 if(d < near_d || near_list.size() < near_wanted_count)
196 // Find the right place in the near list and put it there
198 // Go from farthest to near in the near list
199 core::list<DFloat>::Iterator i = near_list.begin();
200 for(; i != near_list.end(); i++)
202 // Stop when i is at the first nearer node
206 // Add df to before i
207 if(i == near_list.end())
208 near_list.push_back(df);
210 near_list.insert_before(i, df);
212 // Keep near list at right size
213 if(near_list.size() > near_wanted_count)
215 core::list<DFloat>::Iterator j = near_list.begin();
221 // Return if no values found
222 if(near_list.size() == 0)
226 20:58:29 < tejeez> joka pisteelle a += arvo / etäisyys^6; b += 1 / etäisyys^6; ja
227 lopuks sit otetaan a/b
232 for(core::list<DFloat>::Iterator i = near_list.begin();
233 i != near_list.end(); i++)
238 //float dd = pow((float)i->d, 6);
239 float dd = pow((float)i->d, 5);
241 //dstream<<"dd="<<dd<<", v="<<v<<std::endl;
250 float PointAttributeList::getInterpolatedFloat(v3s16 p)
252 const u32 near_wanted_count = 2;
253 const u32 nearest_wanted_count = 2;
255 core::list<DFloat> near;
257 for(core::list<PointWithAttr>::Iterator
258 i = m_points.begin();
259 i != m_points.end(); i++)
261 PointWithAttr &pwa = *i;
262 u32 d = pwa.p.getDistanceFrom(p);
265 df.v = pwa.attr.getFloat();
268 // If near list is empty, add directly and continue
269 if(near_list.size() == 0)
271 near_list.push_back(df);
275 // Get distance of farthest in near list
277 if(near_list.size() > 0)
279 core::list<DFloat>::Iterator i = near_list.begin();
284 If point is closer than the farthest in the near list or
285 there are not yet enough points on the list
287 if(d < near_d || near_list.size() < near_wanted_count)
289 // Find the right place in the near list and put it there
291 // Go from farthest to near in the near list
292 core::list<DFloat>::Iterator i = near_list.begin();
293 for(; i != near_list.end(); i++)
295 // Stop when i is at the first nearer node
299 // Add df to before i
300 if(i == near_list.end())
301 near_list.push_back(df);
303 near_list.insert_before(i, df);
305 // Keep near list at right size
306 if(near_list.size() > near_wanted_count)
308 core::list<DFloat>::Iterator j = near_list.begin();
314 // Return if no values found
315 if(near_list.size() == 0)
322 u32 nearest_count = nearest_wanted_count;
323 if(nearest_count > near_list.size())
324 nearest_count = near_list.size();
325 core::list<DFloat> nearest;
327 core::list<DFloat>::Iterator i = near_list.getLast();
328 for(u32 j=0; j<nearest_count; j++)
330 nearest.push_front(*i);
337 20:58:29 < tejeez> joka pisteelle a += arvo / etäisyys^6; b += 1 / etäisyys^6; ja
338 lopuks sit otetaan a/b
342 Get total distance to nearest points
345 float nearest_d_sum = 0;
346 for(core::list<DFloat>::Iterator i = nearest.begin();
347 i != nearest.end(); i++)
349 nearest_d_sum += (float)i->d;
353 Interpolate a value between the first ones
356 dstream<<"nearest.size()="<<nearest.size()<<std::endl;
358 float interpolated = 0;
360 for(core::list<DFloat>::Iterator i = nearest.begin();
361 i != nearest.end(); i++)
364 if(nearest_d_sum > 0.001)
365 weight = (float)i->d / nearest_d_sum;
367 weight = 1. / nearest.size();
368 /*dstream<<"i->d="<<i->d<<" nearest_d_sum="<<nearest_d_sum
369 <<" weight="<<weight<<std::endl;*/
370 interpolated += weight * i->v;
378 blockpos: position of block in block coordinates
379 camera_pos: position of camera in nodes
380 camera_dir: an unit vector pointing to camera direction
383 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range)
385 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
387 // Block center position
389 ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
390 ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
391 ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
394 // Block position relative to camera
395 v3f blockpos_relative = blockpos - camera_pos;
397 // Distance in camera direction (+=front, -=back)
398 f32 dforward = blockpos_relative.dotProduct(camera_dir);
401 f32 d = blockpos_relative.getLength();
403 // If block is far away, it's not in sight
407 // Maximum radius of a block
408 f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
410 // If block is (nearly) touching the camera, don't
411 // bother validating further (that is, render it anyway)
412 if(d > block_max_radius * 1.5)
414 // Cosine of the angle between the camera direction
415 // and the block direction (camera_dir is an unit vector)
416 f32 cosangle = dforward / d;
418 // Compensate for the size of the block
419 // (as the block has to be shown even if it's a bit off FOV)
420 // This is an estimate.
421 cosangle += block_max_radius / dforward;
423 // If block is not in the field of view, skip it
424 //if(cosangle < cos(FOV_ANGLE/2))
425 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))