|
| 1 | +/* |
| 2 | + * This file is part of BlueMap, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package de.bluecolored.bluemap.core.map.hires; |
| 26 | + |
| 27 | +import com.flowpowered.math.TrigMath; |
| 28 | + |
| 29 | +public class HiresTileModel { |
| 30 | + private static final double GROW_MULTIPLIER = 1.5; |
| 31 | + |
| 32 | + // attributes per-vertex * per-face |
| 33 | + private static final int |
| 34 | + FI_POSITION = 3 * 3, |
| 35 | + FI_UV = 2 * 3, |
| 36 | + FI_AO = 3, |
| 37 | + FI_COLOR = 3 , |
| 38 | + FI_SUNLIGHT = 1 , |
| 39 | + FI_BLOCKLIGHT = 1 , |
| 40 | + FI_MATERIAL_INDEX = 1 ; |
| 41 | + |
| 42 | + private int capacity; |
| 43 | + private int size; |
| 44 | + |
| 45 | + private double[] position; |
| 46 | + private float[] color, uv, ao; |
| 47 | + private byte[] sunlight, blocklight; |
| 48 | + private int[] materialIndex; |
| 49 | + |
| 50 | + public HiresTileModel(int initialCapacity) { |
| 51 | + if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity is negative"); |
| 52 | + setCapacity(initialCapacity); |
| 53 | + clear(); |
| 54 | + } |
| 55 | + |
| 56 | + public int size() { |
| 57 | + return size; |
| 58 | + } |
| 59 | + |
| 60 | + public int add(int count) { |
| 61 | + ensureCapacity(count); |
| 62 | + return this.size += count; |
| 63 | + } |
| 64 | + |
| 65 | + public void setPositions( |
| 66 | + int face, |
| 67 | + double x1, double y1, double z1, |
| 68 | + double x2, double y2, double z2, |
| 69 | + double x3, double y3, double z3 |
| 70 | + ){ |
| 71 | + int index = face * FI_POSITION; |
| 72 | + |
| 73 | + position[index ] = x1; |
| 74 | + position[index + 1] = y1; |
| 75 | + position[index + 2] = z1; |
| 76 | + |
| 77 | + position[index + 3 ] = x2; |
| 78 | + position[index + 3 + 1] = y2; |
| 79 | + position[index + 3 + 2] = z2; |
| 80 | + |
| 81 | + position[index + 6 ] = x3; |
| 82 | + position[index + 6 + 1] = y3; |
| 83 | + position[index + 6 + 2] = z3; |
| 84 | + } |
| 85 | + |
| 86 | + public void setUvs( |
| 87 | + int face, |
| 88 | + float u1, float v1, |
| 89 | + float u2, float v2, |
| 90 | + float u3, float v3 |
| 91 | + ){ |
| 92 | + int index = face * FI_UV; |
| 93 | + |
| 94 | + uv[index ] = u1; |
| 95 | + uv[index + 1] = v1; |
| 96 | + |
| 97 | + uv[index + 2 ] = u2; |
| 98 | + uv[index + 2 + 1] = v2; |
| 99 | + |
| 100 | + uv[index + 4 ] = u3; |
| 101 | + uv[index + 4 + 1] = v3; |
| 102 | + } |
| 103 | + |
| 104 | + public void setAOs( |
| 105 | + int face, |
| 106 | + float ao1, float ao2, float ao3 |
| 107 | + ) { |
| 108 | + int index = face * FI_AO; |
| 109 | + |
| 110 | + ao[index ] = ao1; |
| 111 | + ao[index + 1] = ao2; |
| 112 | + ao[index + 2] = ao3; |
| 113 | + } |
| 114 | + |
| 115 | + public void setColor( |
| 116 | + int face, |
| 117 | + float r, float g, float b |
| 118 | + ){ |
| 119 | + int index = face * FI_COLOR; |
| 120 | + |
| 121 | + color[index ] = r; |
| 122 | + color[index + 1] = g; |
| 123 | + color[index + 2] = b; |
| 124 | + } |
| 125 | + |
| 126 | + public void setSunlight(int face, int sl) { |
| 127 | + sunlight[face * FI_SUNLIGHT] = (byte) sl; |
| 128 | + } |
| 129 | + |
| 130 | + public void setBlocklight(int face, int bl) { |
| 131 | + blocklight[face * FI_BLOCKLIGHT] = (byte) bl; |
| 132 | + } |
| 133 | + |
| 134 | + public void setMaterialIndex(int face, int m) { |
| 135 | + materialIndex[face * FI_MATERIAL_INDEX] = m; |
| 136 | + } |
| 137 | + |
| 138 | + public void rotate( |
| 139 | + int start, int count, |
| 140 | + float angle, float axisX, float axisY, float axisZ |
| 141 | + ) { |
| 142 | + |
| 143 | + // create quaternion |
| 144 | + double halfAngle = Math.toRadians(angle) * 0.5; |
| 145 | + double q = TrigMath.sin(halfAngle) / Math.sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ); |
| 146 | + |
| 147 | + double //quaternion |
| 148 | + qx = axisX * q, |
| 149 | + qy = axisY * q, |
| 150 | + qz = axisZ * q, |
| 151 | + qw = TrigMath.cos(halfAngle), |
| 152 | + qLength = Math.sqrt(qx * qx + qy * qy + qz * qz + qw * qw); |
| 153 | + |
| 154 | + // normalize quaternion |
| 155 | + qx /= qLength; |
| 156 | + qy /= qLength; |
| 157 | + qz /= qLength; |
| 158 | + qw /= qLength; |
| 159 | + |
| 160 | + rotateWithQuaternion(start, count, qx, qy, qz, qw); |
| 161 | + } |
| 162 | + |
| 163 | + public void rotate( |
| 164 | + int start, int count, |
| 165 | + float pitch, float yaw, float roll |
| 166 | + ) { |
| 167 | + |
| 168 | + double |
| 169 | + halfYaw = Math.toRadians(yaw) * 0.5, |
| 170 | + qy1 = TrigMath.sin(halfYaw), |
| 171 | + qw1 = TrigMath.cos(halfYaw), |
| 172 | + |
| 173 | + halfPitch = Math.toRadians(pitch) * 0.5, |
| 174 | + qx2 = TrigMath.sin(halfPitch), |
| 175 | + qw2 = TrigMath.cos(halfPitch), |
| 176 | + |
| 177 | + halfRoll = Math.toRadians(roll) * 0.5, |
| 178 | + qz3 = TrigMath.sin(halfRoll), |
| 179 | + qw3 = TrigMath.cos(halfRoll); |
| 180 | + |
| 181 | + // multiply 1 with 2 |
| 182 | + double |
| 183 | + qxA = qw1 * qx2, |
| 184 | + qyA = qy1 * qw2, |
| 185 | + qzA = - qy1 * qx2, |
| 186 | + qwA = qw1 * qw2; |
| 187 | + |
| 188 | + // multiply with 3 |
| 189 | + double |
| 190 | + qx = qxA * qw3 + qyA * qz3, |
| 191 | + qy = qyA * qw3 - qxA * qz3, |
| 192 | + qz = qwA * qz3 + qzA * qw3, |
| 193 | + qw = qwA * qw3 - qzA * qz3; |
| 194 | + |
| 195 | + rotateWithQuaternion(start, count, qx, qy, qz, qw); |
| 196 | + } |
| 197 | + |
| 198 | + private void rotateWithQuaternion( |
| 199 | + int start, int count, |
| 200 | + double qx, double qy, double qz, double qw |
| 201 | + ) { |
| 202 | + double x, y, z, px, py, pz, pw; |
| 203 | + int end = start + count, index; |
| 204 | + for (int face = start; face < end; face++) { |
| 205 | + index = face * FI_COLOR; |
| 206 | + |
| 207 | + x = position[index ]; |
| 208 | + y = position[index + 1]; |
| 209 | + z = position[index + 2]; |
| 210 | + |
| 211 | + px = qw * x + qy * z - qz * y; |
| 212 | + py = qw * y + qz * x - qx * z; |
| 213 | + pz = qw * z + qx * y - qy * x; |
| 214 | + pw = -qx * x - qy * y - qz * z; |
| 215 | + |
| 216 | + position[index ] = pw * -qx + px * qw - py * qz + pz * qy; |
| 217 | + position[index + 1] = pw * -qy + py * qw - pz * qx + px * qz; |
| 218 | + position[index + 2] = pw * -qz + pz * qw - px * qy + py * qx; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + public void scale( |
| 223 | + int start, int count, |
| 224 | + float scale |
| 225 | + ) { |
| 226 | + int startIndex = start * FI_POSITION; |
| 227 | + int endIndex = count * FI_POSITION + startIndex; |
| 228 | + |
| 229 | + for (int i = startIndex; i < endIndex; i++) |
| 230 | + position[i] *= scale; |
| 231 | + } |
| 232 | + |
| 233 | + public void translate( |
| 234 | + int start, int count, |
| 235 | + double dx, double dy, double dz |
| 236 | + ) { |
| 237 | + int end = start + count, index; |
| 238 | + for (int face = start; face < end; face++) { |
| 239 | + index = face * FI_COLOR; |
| 240 | + for (int i = 0; i < 3; i++) { |
| 241 | + position[index ] += dx; |
| 242 | + position[index + 1] += dy; |
| 243 | + position[index + 2] += dz; |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + public void clear() { |
| 249 | + this.size = 0; |
| 250 | + } |
| 251 | + |
| 252 | + private void ensureCapacity(int count) { |
| 253 | + if (size + count > capacity){ |
| 254 | + double[] _position = position; |
| 255 | + float[] _color = color, _uv = uv, _ao = ao; |
| 256 | + byte[] _sunlight = sunlight, _blocklight = blocklight; |
| 257 | + int[] _materialIndex = materialIndex; |
| 258 | + |
| 259 | + int newCapacity = (int) (capacity * GROW_MULTIPLIER) + count; |
| 260 | + setCapacity(newCapacity); |
| 261 | + |
| 262 | + System.arraycopy(_position, 0, position, 0, size * FI_POSITION); |
| 263 | + System.arraycopy(_uv, 0, uv, 0, size * FI_UV); |
| 264 | + System.arraycopy(_ao, 0, ao, 0, size * FI_AO); |
| 265 | + |
| 266 | + System.arraycopy(_color, 0, color, 0, size * FI_COLOR); |
| 267 | + System.arraycopy(_sunlight, 0, sunlight, 0, size * FI_SUNLIGHT); |
| 268 | + System.arraycopy(_blocklight, 0, blocklight, 0, size * FI_BLOCKLIGHT); |
| 269 | + System.arraycopy(_materialIndex, 0, materialIndex, 0, size * FI_MATERIAL_INDEX); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + private void setCapacity(int capacity) { |
| 274 | + this.capacity = capacity; |
| 275 | + |
| 276 | + // attributes capacity * per-vertex * per-face |
| 277 | + position = new double [capacity * FI_POSITION]; |
| 278 | + uv = new float [capacity * FI_UV]; |
| 279 | + ao = new float [capacity * FI_AO]; |
| 280 | + |
| 281 | + color = new float [capacity * FI_COLOR]; |
| 282 | + sunlight = new byte [capacity * FI_SUNLIGHT]; |
| 283 | + blocklight = new byte [capacity * FI_BLOCKLIGHT]; |
| 284 | + materialIndex = new int [capacity * FI_MATERIAL_INDEX]; |
| 285 | + } |
| 286 | + |
| 287 | +} |
0 commit comments