1 /*
2 Copyright (c) 2018-2020 Rafał Ziemniewski, Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.graphics.terrain;
29 
30 import dlib.core.memory;
31 import dlib.core.ownership;
32 import dlib.math.vector;
33 import dlib.geometry.sphere;
34 import dlib.geometry.triangle;
35 
36 import dagon.graphics.drawable;
37 import dagon.graphics.mesh;
38 import dagon.graphics.heightmap;
39 import dagon.graphics.entity;
40 
41 class Terrain: Owner, Drawable
42 {
43     uint width;
44     uint height;
45     Mesh mesh;
46     Mesh collisionMesh;
47     Heightmap heightmap;
48 
49     this(uint meshResolution, uint collisionMeshResolution, Heightmap heightmap, Owner owner)
50     {
51         super(owner);
52 
53         this.width = meshResolution;
54         this.height = meshResolution;
55         this.heightmap = heightmap;
56 
57         mesh = generateMesh(width, height, 1, owner);
58         mesh.dataReady = true;
59         mesh.prepareVAO();
60 
61         float scale = cast(float)meshResolution / collisionMeshResolution;
62         collisionMesh = generateMesh(collisionMeshResolution, collisionMeshResolution, scale, owner);
63     }
64 
65     this(uint meshResolution, Heightmap heightmap, Owner owner)
66     {
67         this(meshResolution, 80, heightmap, owner);
68     }
69 
70     Mesh generateMesh(uint w, uint h, float scale, Owner o)
71     {
72         Mesh mesh = New!Mesh(o);
73 
74         size_t numVerts = w * h;
75         mesh.vertices = New!(Vector3f[])(numVerts);
76         mesh.normals = New!(Vector3f[])(numVerts);
77         mesh.texcoords = New!(Vector2f[])(numVerts);
78         mesh.indices = New!(uint[3][])(numVerts * 2);
79 
80         int i = 0;
81         foreach(x; 0..w)
82         foreach(z; 0..h)
83         {
84             float y = heightmap.getHeight(
85                 cast(float)x / cast(float)(w-1),
86                 cast(float)z / cast(float)(h-1));
87             mesh.vertices[i] = Vector3f(x * scale, y, z * scale);
88             mesh.texcoords[i] = Vector2f(
89                 cast(float)x / cast(float)(w-1),
90                 cast(float)z / cast(float)(h-1));
91             i += 1;
92         }
93 
94         i = 0;
95         foreach(x; 0..w-1)
96         foreach(z; 0..h-1)
97         {
98             uint LU = x + z * w;
99             uint RU = x+1 + z * w;
100             uint LB = x + (z+1) * w;
101             uint RB = x+1 + (z+1) * w;
102 
103             mesh.indices[i] = [LU, RU, RB];
104             mesh.indices[i+1] = [LU, RB, LB];
105             i += 2;
106         }
107 
108         mesh.generateNormals();
109 
110         return mesh;
111     }
112 
113     void update(double dt)
114     {
115 
116     }
117 
118     void render(GraphicsState* state)
119     {
120         mesh.render(state);
121     }
122 
123     void refreshChanges()
124     {
125         mesh.generateNormals();
126         mesh.prepareVAO();
127     }
128     
129     float getHeight(Entity e, Vector3f pos)
130     {
131         Vector3f ts = (pos - e.position) / e.scaling;
132         float x = ts.x / width;
133         float z = ts.z / height;
134         float y = heightmap.getHeight(x, z);
135         return y * e.scaling.y;
136     }
137 
138     TerrainSphereTraverseAggregate traverseBySphere(Sphere* sphere)
139     {
140         return TerrainSphereTraverseAggregate(this, sphere);
141     }
142 }
143 
144 struct TerrainSphereTraverseAggregate
145 {
146     Terrain terrain;
147     Sphere* sphere;
148 
149     int opApply(int delegate(ref Triangle) dg)
150     {
151         int result = 0;
152 
153         uint x = 0;
154         uint y = 0;
155 
156         Vector3f c = sphere.center;
157         // TODO: transform c with position and scale?
158         if (c.x > terrain.width - 1) x = terrain.width - 1;
159         else if (c.x < 0) x = 0;
160         else x = cast(uint)c.x;
161 
162         if (c.z > terrain.height - 1) y = terrain.height - 1;
163         else if (c.z < 0) y = 0;
164         else y = cast(uint)c.z;
165 
166         Triangle tri = terrain.mesh.getTriangle(y * terrain.width + x);
167         tri.barycenter = (tri.v[0] + tri.v[1] + tri.v[2]) / 3;
168 
169         result = dg(tri);
170 
171         return result;
172     }
173 }
174 
175 bool entityIsTerrain(Entity e)
176 {
177     Drawable d = e.drawable;
178     if (d)
179     {
180         if (cast(Terrain)d)
181             return true;
182     }
183     return false;
184 }