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>
27 TimeTaker::TimeTaker(const char *name, u32 *result)
32 m_time1 = getTimeMs();
35 u32 TimeTaker::stop(bool quiet)
39 u32 time2 = getTimeMs();
40 u32 dtime = time2 - m_time1;
48 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
56 u32 TimeTaker::getTime()
58 u32 time2 = getTimeMs();
59 u32 dtime = time2 - m_time1;
63 const v3s16 g_6dirs[6] =
65 // +right, +top, +back
66 v3s16( 0, 0, 1), // back
67 v3s16( 0, 1, 0), // top
68 v3s16( 1, 0, 0), // right
69 v3s16( 0, 0,-1), // front
70 v3s16( 0,-1, 0), // bottom
71 v3s16(-1, 0, 0) // left
74 const v3s16 g_26dirs[26] =
76 // +right, +top, +back
77 v3s16( 0, 0, 1), // back
78 v3s16( 0, 1, 0), // top
79 v3s16( 1, 0, 0), // right
80 v3s16( 0, 0,-1), // front
81 v3s16( 0,-1, 0), // bottom
82 v3s16(-1, 0, 0), // left
84 v3s16(-1, 1, 0), // top left
85 v3s16( 1, 1, 0), // top right
86 v3s16( 0, 1, 1), // top back
87 v3s16( 0, 1,-1), // top front
88 v3s16(-1, 0, 1), // back left
89 v3s16( 1, 0, 1), // back right
90 v3s16(-1, 0,-1), // front left
91 v3s16( 1, 0,-1), // front right
92 v3s16(-1,-1, 0), // bottom left
93 v3s16( 1,-1, 0), // bottom right
94 v3s16( 0,-1, 1), // bottom back
95 v3s16( 0,-1,-1), // bottom front
97 v3s16(-1, 1, 1), // top back-left
98 v3s16( 1, 1, 1), // top back-right
99 v3s16(-1, 1,-1), // top front-left
100 v3s16( 1, 1,-1), // top front-right
101 v3s16(-1,-1, 1), // bottom back-left
102 v3s16( 1,-1, 1), // bottom back-right
103 v3s16(-1,-1,-1), // bottom front-left
104 v3s16( 1,-1,-1), // bottom front-right
108 const v3s16 g_27dirs[27] =
110 // +right, +top, +back
111 v3s16( 0, 0, 1), // back
112 v3s16( 0, 1, 0), // top
113 v3s16( 1, 0, 0), // right
114 v3s16( 0, 0,-1), // front
115 v3s16( 0,-1, 0), // bottom
116 v3s16(-1, 0, 0), // left
118 v3s16(-1, 1, 0), // top left
119 v3s16( 1, 1, 0), // top right
120 v3s16( 0, 1, 1), // top back
121 v3s16( 0, 1,-1), // top front
122 v3s16(-1, 0, 1), // back left
123 v3s16( 1, 0, 1), // back right
124 v3s16(-1, 0,-1), // front left
125 v3s16( 1, 0,-1), // front right
126 v3s16(-1,-1, 0), // bottom left
127 v3s16( 1,-1, 0), // bottom right
128 v3s16( 0,-1, 1), // bottom back
129 v3s16( 0,-1,-1), // bottom front
131 v3s16(-1, 1, 1), // top back-left
132 v3s16( 1, 1, 1), // top back-right
133 v3s16(-1, 1,-1), // top front-left
134 v3s16( 1, 1,-1), // top front-right
135 v3s16(-1,-1, 1), // bottom back-left
136 v3s16( 1,-1, 1), // bottom back-right
137 v3s16(-1,-1,-1), // bottom front-left
138 v3s16( 1,-1,-1), // bottom front-right
143 static unsigned long next = 1;
145 /* RAND_MAX assumed to be 32767 */
148 next = next * 1103515245 + 12345;
149 return((unsigned)(next/65536) % 32768);
152 void mysrand(unsigned seed)
158 blockpos: position of block in block coordinates
159 camera_pos: position of camera in nodes
160 camera_dir: an unit vector pointing to camera direction
163 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
166 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
168 // Block center position
170 ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
171 ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
172 ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
175 // Block position relative to camera
176 v3f blockpos_relative = blockpos - camera_pos;
178 // Distance in camera direction (+=front, -=back)
179 f32 dforward = blockpos_relative.dotProduct(camera_dir);
182 f32 d = blockpos_relative.getLength();
187 // If block is very close, it is always in sight
188 if(d < 1.44*1.44*MAP_BLOCKSIZE*BS/2)
191 // If block is far away, it's not in sight
195 // Maximum radius of a block
196 f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
198 // If block is (nearly) touching the camera, don't
199 // bother validating further (that is, render it anyway)
200 if(d > block_max_radius * 1.5)
202 // Cosine of the angle between the camera direction
203 // and the block direction (camera_dir is an unit vector)
204 f32 cosangle = dforward / d;
206 // Compensate for the size of the block
207 // (as the block has to be shown even if it's a bit off FOV)
208 // This is an estimate.
209 cosangle += block_max_radius / dforward;
211 // If block is not in the field of view, skip it
212 //if(cosangle < cos(FOV_ANGLE/2))
213 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))