2013-08-29 11:45:22 +09:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 yvt
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
This file is part of OpenSpades.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
OpenSpades is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
OpenSpades is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
*/
|
2013-08-18 16:18:06 +09:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <cstdint>
|
|
|
|
|
2016-12-03 18:49:07 +09:00
|
|
|
#include <Core/Debug.h>
|
2013-08-18 16:18:06 +09:00
|
|
|
#include "IStream.h"
|
|
|
|
#include "Math.h"
|
2013-09-14 13:28:19 +09:00
|
|
|
#include <Core/RefCountedObject.h>
|
2013-08-18 16:18:06 +09:00
|
|
|
|
|
|
|
namespace spades {
|
2016-12-03 18:23:47 +09:00
|
|
|
class VoxelModel : public RefCountedObject {
|
2013-08-18 16:18:06 +09:00
|
|
|
Vector3 origin;
|
|
|
|
int width, height, depth;
|
|
|
|
uint64_t *solidBits;
|
|
|
|
uint32_t *colors;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-09-14 13:28:19 +09:00
|
|
|
protected:
|
2017-01-05 03:48:11 +09:00
|
|
|
~VoxelModel();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
public:
|
|
|
|
VoxelModel(int width, int height, int depth);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-25 00:28:43 +09:00
|
|
|
void HollowFill();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
static VoxelModel *LoadKV6(IStream *);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
const uint64_t &GetSolidBitsAt(int x, int y) const {
|
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < width);
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < height);
|
2013-08-18 16:18:06 +09:00
|
|
|
return solidBits[x + y * width];
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
uint64_t &GetSolidBitsAt(int x, int y) {
|
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < width);
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < height);
|
2013-08-18 16:18:06 +09:00
|
|
|
return solidBits[x + y * width];
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
const uint32_t &GetColor(int x, int y, int z) const {
|
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < width);
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < height);
|
|
|
|
SPAssert(z >= 0);
|
|
|
|
SPAssert(z < depth);
|
2013-08-18 16:18:06 +09:00
|
|
|
return colors[(x + y * width) * depth + z];
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
uint32_t &GetColor(int x, int y, int z) {
|
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < width);
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < height);
|
|
|
|
SPAssert(z >= 0);
|
|
|
|
SPAssert(z < depth);
|
2013-08-18 16:18:06 +09:00
|
|
|
return colors[(x + y * width) * depth + z];
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
bool IsSolid(int x, int y, int z) const {
|
2016-12-03 18:23:47 +09:00
|
|
|
if (z < 0 || z >= depth)
|
2013-08-18 16:18:06 +09:00
|
|
|
return false;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (x < 0 || y < 0 || x >= width || y >= height)
|
2013-08-18 16:18:06 +09:00
|
|
|
return false;
|
|
|
|
uint64_t bits = GetSolidBitsAt(x, y);
|
|
|
|
return (bits >> z) & 1;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
void SetAir(int x, int y, int z) {
|
2016-12-03 18:23:47 +09:00
|
|
|
SPAssert(z >= 0);
|
|
|
|
SPAssert(z < depth);
|
2013-08-18 16:18:06 +09:00
|
|
|
uint64_t mask = 1ULL << z;
|
|
|
|
GetSolidBitsAt(x, y) &= ~mask;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
void SetSolid(int x, int y, int z, uint32_t color) {
|
|
|
|
uint64_t mask = 1ULL << z;
|
|
|
|
GetSolidBitsAt(x, y) |= mask;
|
|
|
|
GetColor(x, y, z) = color;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
Vector3 GetOrigin() { return origin; }
|
|
|
|
|
|
|
|
void SetOrigin(Vector3 v) { origin = v; }
|
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
int GetWidth() const { return width; }
|
|
|
|
int GetHeight() const { return height; }
|
|
|
|
int GetDepth() const { return depth; }
|
|
|
|
};
|
|
|
|
}
|