-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLargeVoxelSphere.cpp
100 lines (79 loc) · 2.76 KB
/
LargeVoxelSphere.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyrighted under the MPLv2 and LGPLv3 by Jesse Johnson.
#include "LargeVoxelSphere.h"
#include <cstdlib>
#include <vector>
#include "PolyVox/Mesh.h"
LargeVoxelSphere::LargeVoxelSphere(const Ogre::String& name):
Ogre::ManualObject(name)
{
}
LargeVoxelSphere::~LargeVoxelSphere()
{
}
void LargeVoxelSphere::VoxelSpherePager::pageIn(
const PolyVox::Region& region,
PolyVox::PagedVolume<BYTE>::Chunk* chunk)
{
for (int z = region.getLowerCorner().getZ();
z <= region.getUpperCorner().getZ(); z++) {
for (int y = region.getLowerCorner().getY();
y <= region.getUpperCorner().getY(); y++) {
for (int x = region.getLowerCorner().getX();
x <= region.getUpperCorner().getX(); x++) {
PolyVox::Vector3DFloat current_pos(x,y,z);
PolyVox::Vector3DFloat volume_center(0,0,0);
float dist_to_center = (current_pos - volume_center).length();
BYTE voxel_value = 0;
if(dist_to_center <= 100) {
voxel_value = 255;
}
chunk->setVoxel(x, y, z, voxel_value);
}
}
}
}
void LargeVoxelSphere::VoxelSpherePager::pageOut(
const PolyVox::Region& region,
PolyVox::PagedVolume<BYTE>::Chunk* chunk)
{
// Ignore data which can no longer fit into memory.
//TODO Create save data for modified voxels.
std::cout << "Deleting volume." << std::endl;
}
void LargeVoxelSphere::generate(float radius)
{
mVolumeData = new PolyVox::LargeVolume<BYTE>(new VoxelSpherePager);
//mRadius = radius;
mesh();
}
void LargeVoxelSphere::mesh()
{
PolyVox::Region region(PolyVox::Vector3DInt32(-120,-120,-120),
PolyVox::Vector3DInt32(120, 120, 120));
auto encodedMesh = extractCubicMesh(mVolumeData, region);
auto mesh = decodeMesh(encodedMesh);
auto vertexOffset = static_cast<PolyVox::Vector3DFloat>(mesh.getOffset());
delete mVolumeData;
mVolumeData = nullptr;
// clear();
begin("BaseWhite", Ogre::RenderOperation::OT_TRIANGLE_LIST);
{
auto& vertices = mesh.getVertices();
auto& indices = mesh.getIndices();
for (auto index : indices) {
auto& vertex = vertices[index];
auto vertexPos = vertex.position + vertexOffset;
position(vertexPos.getX(), vertexPos.getY(), vertexPos.getZ());
// uint8_t mat = vertex.getMaterial() + 0.5;
// uint8_t red = mat & 0xF0;
// uint8_t green = mat & 0x03;
// uint8_t blue = mat & 0x0C;
// colour(red*2, green*4, blue*4);
colour(1, 0, 0, 0.5);
}
}
end();
}