]> git.lizzy.rs Git - minetest.git/blob - src/farmesh.cpp
Added an experimental "far view" thing. Doesn't work exactly like it should and not...
[minetest.git] / src / farmesh.cpp
1 /*
2 Part of Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 /*
21         A quick messy implementation of terrain rendering for a long
22         distance according to map seed
23 */
24
25 #include "farmesh.h"
26 #include "constants.h"
27 #include "debug.h"
28 #include "noise.h"
29 #include "map.h"
30
31 FarMesh::FarMesh(
32                 scene::ISceneNode* parent,
33                 scene::ISceneManager* mgr,
34                 s32 id,
35                 u64 seed
36 ):
37         scene::ISceneNode(parent, mgr, id),
38         m_seed(seed),
39         m_camera_pos(0,0),
40         m_time(0)
41 {
42         dstream<<__FUNCTION_NAME<<std::endl;
43
44         m_material.setFlag(video::EMF_LIGHTING, false);
45         m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
46         m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
47         m_material.setFlag(video::EMF_FOG_ENABLE, false);
48         //m_material.setFlag(video::EMF_ANTI_ALIASING, true);
49         //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
50
51         m_box = core::aabbox3d<f32>(-BS*1000000,-BS*31000,-BS*1000000,
52                         BS*1000000,BS*31000,BS*1000000);
53
54 }
55
56 FarMesh::~FarMesh()
57 {
58         dstream<<__FUNCTION_NAME<<std::endl;
59 }
60
61 void FarMesh::OnRegisterSceneNode()
62 {
63         if(IsVisible)
64         {
65                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
66                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
67                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
68         }
69
70         ISceneNode::OnRegisterSceneNode();
71 }
72
73 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
74
75 // Temporary hack
76 core::map<v2s16, float> g_heights;
77
78 float ground_height(u64 seed, v2s16 p2d)
79 {
80         core::map<v2s16, float>::Node *n = g_heights.find(p2d);
81         if(n)
82                 return n->getValue();
83         float avg_mud_amount = 4;
84         float gh = BS*base_rock_level_2d(seed, p2d) + avg_mud_amount*BS;
85         //gh *= 10;
86         g_heights[p2d] = gh;
87         return gh;
88 }
89
90 void FarMesh::render()
91 {
92         video::IVideoDriver* driver = SceneManager->getVideoDriver();
93
94         /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
95                 return;*/
96         /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
97                 return;*/
98         if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SKY_BOX)
99                 return;
100
101         driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
102         driver->setMaterial(m_material);
103         
104         const s16 grid_radius_i = 12;
105         const float grid_size = BS*50;
106         const v2f grid_speed(-BS*0, 0);
107         
108         // Position of grid noise origin in world coordinates
109         v2f world_grid_origin_pos_f(0,0);
110         // Position of grid noise origin from the camera
111         v2f grid_origin_from_camera_f = world_grid_origin_pos_f - m_camera_pos;
112         // The center point of drawing in the noise
113         v2f center_of_drawing_in_noise_f = -grid_origin_from_camera_f;
114         // The integer center point of drawing in the noise
115         v2s16 center_of_drawing_in_noise_i(
116                 MYROUND(center_of_drawing_in_noise_f.X / grid_size),
117                 MYROUND(center_of_drawing_in_noise_f.Y / grid_size)
118         );
119         // The world position of the integer center point of drawing in the noise
120         v2f world_center_of_drawing_in_noise_f = v2f(
121                 center_of_drawing_in_noise_i.X * grid_size,
122                 center_of_drawing_in_noise_i.Y * grid_size
123         ) + world_grid_origin_pos_f;
124
125         for(s16 zi=-grid_radius_i; zi<grid_radius_i; zi++)
126         for(s16 xi=-grid_radius_i; xi<grid_radius_i; xi++)
127         {
128                 v2s16 p_in_noise_i(
129                         xi+center_of_drawing_in_noise_i.X,
130                         zi+center_of_drawing_in_noise_i.Y
131                 );
132
133                 /*if((p_in_noise_i.X + p_in_noise_i.Y)%2==0)
134                         continue;*/
135                 /*if((p_in_noise_i.X/2 + p_in_noise_i.Y/2)%2==0)
136                         continue;*/
137
138                 v2f p0 = v2f(xi,zi)*grid_size + world_center_of_drawing_in_noise_f;
139                 
140                 /*double noise[4];
141                 double d = 100*BS;
142                 noise[0] = d*noise2d_perlin(
143                                 (float)(p_in_noise_i.X+0)*grid_size/BS/100,
144                                 (float)(p_in_noise_i.Y+0)*grid_size/BS/100,
145                                 m_seed, 3, 0.5);
146                 
147                 noise[1] = d*noise2d_perlin(
148                                 (float)(p_in_noise_i.X+0)*grid_size/BS/100,
149                                 (float)(p_in_noise_i.Y+1)*grid_size/BS/100,
150                                 m_seed, 3, 0.5);
151                 
152                 noise[2] = d*noise2d_perlin(
153                                 (float)(p_in_noise_i.X+1)*grid_size/BS/100,
154                                 (float)(p_in_noise_i.Y+1)*grid_size/BS/100,
155                                 m_seed, 3, 0.5);
156                 
157                 noise[3] = d*noise2d_perlin(
158                                 (float)(p_in_noise_i.X+1)*grid_size/BS/100,
159                                 (float)(p_in_noise_i.Y+0)*grid_size/BS/100,
160                                 m_seed, 3, 0.5);*/
161                 
162                 float noise[4];
163                 noise[0] = ground_height(m_seed, v2s16(
164                                 (p_in_noise_i.X+0)*grid_size/BS,
165                                 (p_in_noise_i.Y+0)*grid_size/BS));
166                 noise[1] = ground_height(m_seed, v2s16(
167                                 (p_in_noise_i.X+0)*grid_size/BS,
168                                 (p_in_noise_i.Y+1)*grid_size/BS));
169                 noise[2] = ground_height(m_seed, v2s16(
170                                 (p_in_noise_i.X+1)*grid_size/BS,
171                                 (p_in_noise_i.Y+1)*grid_size/BS));
172                 noise[3] = ground_height(m_seed, v2s16(
173                                 (p_in_noise_i.X+1)*grid_size/BS,
174                                 (p_in_noise_i.Y+0)*grid_size/BS));
175
176                 float h_min = BS*65535;
177                 float h_max = -BS*65536;
178                 for(u32 i=0; i<4; i++)
179                 {
180                         if(noise[i] < h_min)
181                                 h_min = noise[i];
182                         if(noise[i] > h_max)
183                                 h_max = noise[i];
184                 }
185                 float steepness = (h_max - h_min)/grid_size;
186                 
187                 float h_avg = (noise[0]+noise[1]+noise[2]+noise[3])/4.0;
188                 float light_f = noise[0]+noise[1]-noise[2]-noise[3];
189                 light_f /= 100;
190                 if(light_f < -1.0) light_f = -1.0;
191                 if(light_f > 1.0) light_f = 1.0;
192                 //light_f += 1.0;
193                 //light_f /= 2.0;
194                 
195                 v2f p1 = p0 + v2f(1,1)*grid_size;
196
197                 video::SColor c;
198                 // Detect water
199                 if(h_avg < WATER_LEVEL*BS && h_max < (WATER_LEVEL+5)*BS)
200                 {
201                         c = video::SColor(128,59,86,146);
202                         /*// Set to water level
203                         for(u32 i=0; i<4; i++)
204                         {
205                                 if(noise[i] < BS*WATER_LEVEL)
206                                         noise[i] = BS*WATER_LEVEL;
207                         }*/
208                         light_f = 0;
209                 }
210                 else if(steepness > 2.0)
211                         c = video::SColor(128,128,128,128);
212                 else
213                         c = video::SColor(128,107,134,51);
214                 
215                 // Set to water level
216                 for(u32 i=0; i<4; i++)
217                 {
218                         if(noise[i] < BS*WATER_LEVEL)
219                                 noise[i] = BS*WATER_LEVEL;
220                 }
221
222                 float b = m_brightness + light_f*0.1*m_brightness;
223                 if(b < 0) b = 0;
224                 if(b > 2) b = 2;
225                 
226                 c = video::SColor(128, b*c.getRed(), b*c.getGreen(), b*c.getBlue());
227
228                 video::S3DVertex vertices[4] =
229                 {
230                         video::S3DVertex(p0.X,noise[0],p0.Y, 0,0,0, c, 0,1),
231                         video::S3DVertex(p0.X,noise[1],p1.Y, 0,0,0, c, 1,1),
232                         video::S3DVertex(p1.X,noise[2],p1.Y, 0,0,0, c, 1,0),
233                         video::S3DVertex(p1.X,noise[3],p0.Y, 0,0,0, c, 0,0),
234                 };
235                 u16 indices[] = {0,1,2,2,3,0};
236                 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
237                                 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
238         }
239
240         driver->clearZBuffer();
241 }
242
243 void FarMesh::step(float dtime)
244 {
245         m_time += dtime;
246 }
247
248 void FarMesh::update(v2f camera_p, float brightness)
249 {
250         m_camera_pos = camera_p;
251         m_brightness = brightness;
252 }
253