1 /* 2 Copyright (c) 2018-2019 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.math.vector; 32 import dlib.geometry.sphere; 33 import dlib.geometry.triangle; 34 35 import dagon.core.ownership; 36 import dagon.core.interfaces; 37 import dagon.graphics.mesh; 38 import dagon.graphics.heightmap; 39 import dagon.logics.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(RenderingContext* rc) 119 { 120 mesh.render(rc); 121 } 122 123 void refreshChanges() 124 { 125 mesh.generateNormals(); 126 mesh.prepareVAO(); 127 } 128 129 TerrainSphereTraverseAggregate traverseBySphere(Sphere* sphere) 130 { 131 return TerrainSphereTraverseAggregate(this, sphere); 132 } 133 } 134 135 struct TerrainSphereTraverseAggregate 136 { 137 Terrain terrain; 138 Sphere* sphere; 139 140 int opApply(int delegate(ref Triangle) dg) 141 { 142 int result = 0; 143 144 uint x = 0; 145 uint y = 0; 146 147 Vector3f c = sphere.center; 148 // TODO: transform c with position and scale? 149 if (c.x > terrain.width - 1) x = terrain.width - 1; 150 else if (c.x < 0) x = 0; 151 else x = cast(uint)c.x; 152 153 if (c.z > terrain.height - 1) y = terrain.height - 1; 154 else if (c.z < 0) y = 0; 155 else y = cast(uint)c.z; 156 157 Triangle tri = terrain.mesh.getTriangle(y * terrain.width + x); 158 tri.barycenter = (tri.v[0] + tri.v[1] + tri.v[2]) / 3; 159 160 result = dg(tri); 161 162 return result; 163 } 164 } 165 166 bool entityIsTerrain(Entity e) 167 { 168 Drawable d = e.drawable; 169 if (d) 170 { 171 if (cast(Terrain)d) 172 return true; 173 } 174 return false; 175 }