3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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.
23 #include "../constants.h" // BS, MAP_BLOCKSIZE
24 #include "../noise.h" // PseudoRandom, PcgRandom
25 #include "../threading/mutex_auto_lock.h"
29 UNORDERED_MAP<u16, std::vector<v3s16> > FacePositionCache::m_cache;
30 Mutex FacePositionCache::m_cache_mutex;
31 // Calculate the borders of a "d-radius" cube
32 // TODO: Make it work without mutex and data races, probably thread-local
33 std::vector<v3s16> FacePositionCache::getFacePositions(u16 d)
35 MutexAutoLock cachelock(m_cache_mutex);
36 if (m_cache.find(d) != m_cache.end())
39 generateFacePosition(d);
44 void FacePositionCache::generateFacePosition(u16 d)
46 m_cache[d] = std::vector<v3s16>();
48 m_cache[d].push_back(v3s16(0,0,0));
53 This is an optimized sequence of coordinates.
55 m_cache[d].push_back(v3s16( 0, 1, 0)); // top
56 m_cache[d].push_back(v3s16( 0, 0, 1)); // back
57 m_cache[d].push_back(v3s16(-1, 0, 0)); // left
58 m_cache[d].push_back(v3s16( 1, 0, 0)); // right
59 m_cache[d].push_back(v3s16( 0, 0,-1)); // front
60 m_cache[d].push_back(v3s16( 0,-1, 0)); // bottom
62 m_cache[d].push_back(v3s16(-1, 0, 1)); // back left
63 m_cache[d].push_back(v3s16( 1, 0, 1)); // back right
64 m_cache[d].push_back(v3s16(-1, 0,-1)); // front left
65 m_cache[d].push_back(v3s16( 1, 0,-1)); // front right
66 m_cache[d].push_back(v3s16(-1,-1, 0)); // bottom left
67 m_cache[d].push_back(v3s16( 1,-1, 0)); // bottom right
68 m_cache[d].push_back(v3s16( 0,-1, 1)); // bottom back
69 m_cache[d].push_back(v3s16( 0,-1,-1)); // bottom front
70 m_cache[d].push_back(v3s16(-1, 1, 0)); // top left
71 m_cache[d].push_back(v3s16( 1, 1, 0)); // top right
72 m_cache[d].push_back(v3s16( 0, 1, 1)); // top back
73 m_cache[d].push_back(v3s16( 0, 1,-1)); // top front
75 m_cache[d].push_back(v3s16(-1, 1, 1)); // top back-left
76 m_cache[d].push_back(v3s16( 1, 1, 1)); // top back-right
77 m_cache[d].push_back(v3s16(-1, 1,-1)); // top front-left
78 m_cache[d].push_back(v3s16( 1, 1,-1)); // top front-right
79 m_cache[d].push_back(v3s16(-1,-1, 1)); // bottom back-left
80 m_cache[d].push_back(v3s16( 1,-1, 1)); // bottom back-right
81 m_cache[d].push_back(v3s16(-1,-1,-1)); // bottom front-left
82 m_cache[d].push_back(v3s16( 1,-1,-1)); // bottom front-right
87 // Take blocks in all sides, starting from y=0 and going +-y
88 for(s16 y=0; y<=d-1; y++) {
89 // Left and right side, including borders
90 for(s16 z=-d; z<=d; z++) {
91 m_cache[d].push_back(v3s16(d,y,z));
92 m_cache[d].push_back(v3s16(-d,y,z));
94 m_cache[d].push_back(v3s16(d,-y,z));
95 m_cache[d].push_back(v3s16(-d,-y,z));
98 // Back and front side, excluding borders
99 for(s16 x=-d+1; x<=d-1; x++) {
100 m_cache[d].push_back(v3s16(x,y,d));
101 m_cache[d].push_back(v3s16(x,y,-d));
103 m_cache[d].push_back(v3s16(x,-y,d));
104 m_cache[d].push_back(v3s16(x,-y,-d));
109 // Take the bottom and top face with borders
110 // -d<x<d, y=+-d, -d<z<d
111 for(s16 x=-d; x<=d; x++)
112 for(s16 z=-d; z<=d; z++) {
113 m_cache[d].push_back(v3s16(x,-d,z));
114 m_cache[d].push_back(v3s16(x,d,z));
126 return g_pcgrand.next();
129 void mysrand(unsigned int seed)
131 g_pcgrand.seed(seed);
134 void myrand_bytes(void *out, size_t len)
136 g_pcgrand.bytes(out, len);
139 int myrand_range(int min, int max)
141 return g_pcgrand.range(min, max);
146 64-bit unaligned version of MurmurHash
148 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed)
150 const u64 m = 0xc6a4a7935bd1e995ULL;
152 u64 h = seed ^ (len * m);
154 const u64 *data = (const u64 *)key;
155 const u64 *end = data + (len / 8);
157 while (data != end) {
159 memcpy(&k, data, sizeof(u64));
170 const unsigned char *data2 = (const unsigned char *)data;
172 case 7: h ^= (u64)data2[6] << 48;
173 case 6: h ^= (u64)data2[5] << 40;
174 case 5: h ^= (u64)data2[4] << 32;
175 case 4: h ^= (u64)data2[3] << 24;
176 case 3: h ^= (u64)data2[2] << 16;
177 case 2: h ^= (u64)data2[1] << 8;
178 case 1: h ^= (u64)data2[0];
190 blockpos_b: position of block in block coordinates
191 camera_pos: position of camera in nodes
192 camera_dir: an unit vector pointing to camera direction
194 distance_ptr: return location for distance from the camera
196 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
197 f32 camera_fov, f32 range, f32 *distance_ptr)
199 // Maximum radius of a block. The magic number is
200 // sqrt(3.0) / 2.0 in literal form.
201 const f32 block_max_radius = 0.866025403784 * MAP_BLOCKSIZE * BS;
203 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
205 // Block center position
207 ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
208 ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
209 ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
212 // Block position relative to camera
213 v3f blockpos_relative = blockpos - camera_pos;
216 f32 d = MYMAX(0, blockpos_relative.getLength() - block_max_radius);
221 // If block is far away, it's not in sight
225 // If block is (nearly) touching the camera, don't
226 // bother validating further (that is, render it anyway)
230 // Adjust camera position, for purposes of computing the angle,
231 // such that a block that has any portion visible with the
232 // current camera position will have the center visible at the
234 f32 adjdist = block_max_radius / cos((M_PI - camera_fov) / 2);
236 // Block position relative to adjusted camera
237 v3f blockpos_adj = blockpos - (camera_pos - camera_dir * adjdist);
239 // Distance in camera direction (+=front, -=back)
240 f32 dforward = blockpos_adj.dotProduct(camera_dir);
242 // Cosine of the angle between the camera direction
243 // and the block direction (camera_dir is an unit vector)
244 f32 cosangle = dforward / blockpos_adj.getLength();
246 // If block is not in the field of view, skip it
247 // HOTFIX: use sligthly increased angle (+10%) to fix too agressive
248 // culling. Somebody have to find out whats wrong with the math here.
249 // Previous value: camera_fov / 2
250 if(cosangle < cos(camera_fov * 0.55))