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_6dirs[6] =
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
75 const v3s16 g_26dirs[26] =
77 // +right, +top, +back
78 v3s16( 0, 0, 1), // back
79 v3s16( 0, 1, 0), // top
80 v3s16( 1, 0, 0), // right
81 v3s16( 0, 0,-1), // front
82 v3s16( 0,-1, 0), // bottom
83 v3s16(-1, 0, 0), // left
85 v3s16(-1, 1, 0), // top left
86 v3s16( 1, 1, 0), // top right
87 v3s16( 0, 1, 1), // top back
88 v3s16( 0, 1,-1), // top front
89 v3s16(-1, 0, 1), // back left
90 v3s16( 1, 0, 1), // back right
91 v3s16(-1, 0,-1), // front left
92 v3s16( 1, 0,-1), // front right
93 v3s16(-1,-1, 0), // bottom left
94 v3s16( 1,-1, 0), // bottom right
95 v3s16( 0,-1, 1), // bottom back
96 v3s16( 0,-1,-1), // bottom front
98 v3s16(-1, 1, 1), // top back-left
99 v3s16( 1, 1, 1), // top back-right
100 v3s16(-1, 1,-1), // top front-left
101 v3s16( 1, 1,-1), // top front-right
102 v3s16(-1,-1, 1), // bottom back-left
103 v3s16( 1,-1, 1), // bottom back-right
104 v3s16(-1,-1,-1), // bottom front-left
105 v3s16( 1,-1,-1), // bottom front-right
109 const v3s16 g_27dirs[27] =
111 // +right, +top, +back
112 v3s16( 0, 0, 1), // back
113 v3s16( 0, 1, 0), // top
114 v3s16( 1, 0, 0), // right
115 v3s16( 0, 0,-1), // front
116 v3s16( 0,-1, 0), // bottom
117 v3s16(-1, 0, 0), // left
119 v3s16(-1, 1, 0), // top left
120 v3s16( 1, 1, 0), // top right
121 v3s16( 0, 1, 1), // top back
122 v3s16( 0, 1,-1), // top front
123 v3s16(-1, 0, 1), // back left
124 v3s16( 1, 0, 1), // back right
125 v3s16(-1, 0,-1), // front left
126 v3s16( 1, 0,-1), // front right
127 v3s16(-1,-1, 0), // bottom left
128 v3s16( 1,-1, 0), // bottom right
129 v3s16( 0,-1, 1), // bottom back
130 v3s16( 0,-1,-1), // bottom front
132 v3s16(-1, 1, 1), // top back-left
133 v3s16( 1, 1, 1), // top back-right
134 v3s16(-1, 1,-1), // top front-left
135 v3s16( 1, 1,-1), // top front-right
136 v3s16(-1,-1, 1), // bottom back-left
137 v3s16( 1,-1, 1), // bottom back-right
138 v3s16(-1,-1,-1), // bottom front-left
139 v3s16( 1,-1,-1), // bottom front-right
144 static unsigned long next = 1;
146 /* RAND_MAX assumed to be 32767 */
149 next = next * 1103515245 + 12345;
150 return((unsigned)(next/65536) % 32768);
153 void mysrand(unsigned seed)
159 blockpos: position of block in block coordinates
160 camera_pos: position of camera in nodes
161 camera_dir: an unit vector pointing to camera direction
164 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
167 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
169 // Block center position
171 ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
172 ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
173 ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
176 // Block position relative to camera
177 v3f blockpos_relative = blockpos - camera_pos;
179 // Distance in camera direction (+=front, -=back)
180 f32 dforward = blockpos_relative.dotProduct(camera_dir);
183 f32 d = blockpos_relative.getLength();
188 // If block is far away, it's not in sight
192 // Maximum radius of a block
193 f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
195 // If block is (nearly) touching the camera, don't
196 // bother validating further (that is, render it anyway)
197 if(d > block_max_radius * 1.5)
199 // Cosine of the angle between the camera direction
200 // and the block direction (camera_dir is an unit vector)
201 f32 cosangle = dforward / d;
203 // Compensate for the size of the block
204 // (as the block has to be shown even if it's a bit off FOV)
205 // This is an estimate.
206 cosangle += block_max_radius / dforward;
208 // If block is not in the field of view, skip it
209 //if(cosangle < cos(FOV_ANGLE/2))
210 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))