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 const v3s16 g_26dirs[26] =
59 // +right, +top, +back
60 v3s16( 0, 0, 1), // back
61 v3s16( 0, 1, 0), // top
62 v3s16( 1, 0, 0), // right
63 v3s16( 0, 0,-1), // front
64 v3s16( 0,-1, 0), // bottom
65 v3s16(-1, 0, 0), // left
67 v3s16(-1, 1, 0), // top left
68 v3s16( 1, 1, 0), // top right
69 v3s16( 0, 1, 1), // top back
70 v3s16( 0, 1,-1), // top front
71 v3s16(-1, 0, 1), // back left
72 v3s16( 1, 0, 1), // back right
73 v3s16(-1, 0,-1), // front left
74 v3s16( 1, 0,-1), // front right
75 v3s16(-1,-1, 0), // bottom left
76 v3s16( 1,-1, 0), // bottom right
77 v3s16( 0,-1, 1), // bottom back
78 v3s16( 0,-1,-1), // bottom front
80 v3s16(-1, 1, 1), // top back-left
81 v3s16( 1, 1, 1), // top back-right
82 v3s16(-1, 1,-1), // top front-left
83 v3s16( 1, 1,-1), // top front-right
84 v3s16(-1,-1, 1), // bottom back-left
85 v3s16( 1,-1, 1), // bottom back-right
86 v3s16(-1,-1,-1), // bottom front-left
87 v3s16( 1,-1,-1), // bottom front-right
91 static unsigned long next = 1;
93 /* RAND_MAX assumed to be 32767 */
96 next = next * 1103515245 + 12345;
97 return((unsigned)(next/65536) % 32768);
100 void mysrand(unsigned seed)
109 // Float with distance
116 float PointAttributeList::getInterpolatedFloat(v2s16 p)
118 const u32 near_wanted_count = 5;
119 // Last is nearest, first is farthest
120 core::list<DFloat> near_list;
122 for(core::list<PointWithAttr>::Iterator
123 i = m_points.begin();
124 i != m_points.end(); i++)
126 PointWithAttr &pwa = *i;
127 u32 d = pwa.p.getDistanceFrom(p);
130 df.v = pwa.attr.getFloat();
133 // If near list is empty, add directly and continue
134 if(near_list.size() == 0)
136 near_list.push_back(df);
140 // Get distance of farthest in near list
142 if(near_list.size() > 0)
144 core::list<DFloat>::Iterator i = near_list.begin();
149 If point is closer than the farthest in the near list or
150 there are not yet enough points on the list
152 if(d < near_d || near_list.size() < near_wanted_count)
154 // Find the right place in the near list and put it there
156 // Go from farthest to near in the near list
157 core::list<DFloat>::Iterator i = near_list.begin();
158 for(; i != near_list.end(); i++)
160 // Stop when i is at the first nearer node
164 // Add df to before i
165 if(i == near_list.end())
166 near_list.push_back(df);
168 near_list.insert_before(i, df);
170 // Keep near list at right size
171 if(near_list.size() > near_wanted_count)
173 core::list<DFloat>::Iterator j = near_list.begin();
179 // Return if no values found
180 if(near_list.size() == 0)
184 20:58:29 < tejeez> joka pisteelle a += arvo / etäisyys^6; b += 1 / etäisyys^6; ja
185 lopuks sit otetaan a/b
190 for(core::list<DFloat>::Iterator i = near_list.begin();
191 i != near_list.end(); i++)
196 //float dd = pow((float)i->d, 6);
197 float dd = pow((float)i->d, 5);
199 //dstream<<"dd="<<dd<<", v="<<v<<std::endl;
208 float PointAttributeList::getInterpolatedFloat(v3s16 p)
210 const u32 near_wanted_count = 2;
211 const u32 nearest_wanted_count = 2;
213 core::list<DFloat> near;
215 for(core::list<PointWithAttr>::Iterator
216 i = m_points.begin();
217 i != m_points.end(); i++)
219 PointWithAttr &pwa = *i;
220 u32 d = pwa.p.getDistanceFrom(p);
223 df.v = pwa.attr.getFloat();
226 // If near list is empty, add directly and continue
227 if(near_list.size() == 0)
229 near_list.push_back(df);
233 // Get distance of farthest in near list
235 if(near_list.size() > 0)
237 core::list<DFloat>::Iterator i = near_list.begin();
242 If point is closer than the farthest in the near list or
243 there are not yet enough points on the list
245 if(d < near_d || near_list.size() < near_wanted_count)
247 // Find the right place in the near list and put it there
249 // Go from farthest to near in the near list
250 core::list<DFloat>::Iterator i = near_list.begin();
251 for(; i != near_list.end(); i++)
253 // Stop when i is at the first nearer node
257 // Add df to before i
258 if(i == near_list.end())
259 near_list.push_back(df);
261 near_list.insert_before(i, df);
263 // Keep near list at right size
264 if(near_list.size() > near_wanted_count)
266 core::list<DFloat>::Iterator j = near_list.begin();
272 // Return if no values found
273 if(near_list.size() == 0)
280 u32 nearest_count = nearest_wanted_count;
281 if(nearest_count > near_list.size())
282 nearest_count = near_list.size();
283 core::list<DFloat> nearest;
285 core::list<DFloat>::Iterator i = near_list.getLast();
286 for(u32 j=0; j<nearest_count; j++)
288 nearest.push_front(*i);
295 20:58:29 < tejeez> joka pisteelle a += arvo / etäisyys^6; b += 1 / etäisyys^6; ja
296 lopuks sit otetaan a/b
300 Get total distance to nearest points
303 float nearest_d_sum = 0;
304 for(core::list<DFloat>::Iterator i = nearest.begin();
305 i != nearest.end(); i++)
307 nearest_d_sum += (float)i->d;
311 Interpolate a value between the first ones
314 dstream<<"nearest.size()="<<nearest.size()<<std::endl;
316 float interpolated = 0;
318 for(core::list<DFloat>::Iterator i = nearest.begin();
319 i != nearest.end(); i++)
322 if(nearest_d_sum > 0.001)
323 weight = (float)i->d / nearest_d_sum;
325 weight = 1. / nearest.size();
326 /*dstream<<"i->d="<<i->d<<" nearest_d_sum="<<nearest_d_sum
327 <<" weight="<<weight<<std::endl;*/
328 interpolated += weight * i->v;
336 blockpos: position of block in block coordinates
337 camera_pos: position of camera in nodes
338 camera_dir: an unit vector pointing to camera direction
341 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range)
343 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
345 // Block center position
347 ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
348 ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
349 ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
352 // Block position relative to camera
353 v3f blockpos_relative = blockpos - camera_pos;
355 // Distance in camera direction (+=front, -=back)
356 f32 dforward = blockpos_relative.dotProduct(camera_dir);
359 f32 d = blockpos_relative.getLength();
361 // If block is far away, it's not in sight
365 // Maximum radius of a block
366 f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
368 // If block is (nearly) touching the camera, don't
369 // bother validating further (that is, render it anyway)
370 if(d > block_max_radius * 1.5)
372 // Cosine of the angle between the camera direction
373 // and the block direction (camera_dir is an unit vector)
374 f32 cosangle = dforward / d;
376 // Compensate for the size of the block
377 // (as the block has to be shown even if it's a bit off FOV)
378 // This is an estimate.
379 cosangle += block_max_radius / dforward;
381 // If block is not in the field of view, skip it
382 //if(cosangle < cos(FOV_ANGLE/2))
383 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))