-
Notifications
You must be signed in to change notification settings - Fork 0
/
Planet.h
139 lines (108 loc) · 2.81 KB
/
Planet.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include "Utility.h"
#include "Mesh.h"
#include "Camera.h"
#include "TriangleChunk.h"
#include "Common.h"
#include "Graphics.h"
#include <DirectXColors.h>
#include <vector>
#include <memory>
#include <map>
#include "FastNoiseLite.h"
class Node
{
private:
public:
Node* mParent;
std::vector<Node*> mSubnodes;
int mNumSubs = 0;
Triangle mTriangle = {0,0,0};
float mDistance = 0;
int mLevel = 0;
bool mCombine = false;
TriangleChunk* mTriangleChunk = nullptr;
Node() : mParent{ 0 } {};
Node(Node* parent) : mParent{ parent } {};
~Node()
{
if (mTriangleChunk) delete mTriangleChunk;
for (auto& subNode : mSubnodes)
{
mParent = nullptr;
delete subNode;
}
mSubnodes.clear();
}
Node* AddSub(Triangle triangle)
{
Node* newNode = new Node(this);
newNode->mTriangle = triangle;
mSubnodes.push_back(newNode);
mNumSubs++;
return newNode;
};
};
class Planet
{
public:
Planet(Graphics* graphics);
~Planet();
// Create a new planet
void CreatePlanet(float frequency, int octaves, int lod, int scale, int seed);
// Reset planet geometry to icosahedron
void ResetGeometry();
// Update planet
bool Update(Camera* camera, ID3D12GraphicsCommandList* commandList);
// Planet mesh
Mesh* mMesh;
// Max LOD to subdivide to
int mMaxLOD = 0;
// Scale of planet
int mScale = 1;
// Enable CLOD
bool mCLOD = false;
// List of chunks
std::vector<TriangleChunk*> mTriangleChunks;
private:
// Reference to the graphics class
Graphics* mGraphics;
ID3D12GraphicsCommandList* mCurrentCommandList;
// Geometry
std::vector<Vertex> mVertices;
std::vector<uint32_t> mIndices;
std::vector<Triangle> mTriangles;
std::vector<XMFLOAT3> mNormals;
std::map<std::pair<int, int>, int> mVertexMap;
// Geometry Quadtree
unique_ptr<Node> mTriangleTree;
// Radius of planet
float mRadius = 0.5f;
float mMaxDistance = 0.0f;
// Noise variables
float mFrequency;
int mOctaves;
FastNoiseLite* mNoise;
// Build indices to render the planet
void BuildIndices();
// Get triangles for indexing or chunks from maxLOD nodes
void GetTriangles(Node* node);
// Sort nodes by distance to the camera
void SortBaseNodes(XMFLOAT3 cameraPos);
// Check node distance to camera
float CheckNodeDistance(Node* node, XMFLOAT3 cameraPos);
// Subdivide node
bool Subdivide(Node* node, int level = 0);
// Get a vertex for triangle edge
int GetVertexForEdge(int v1, int v2);
// Subdivide triangle
std::vector<Triangle> SubdivideTriangle(Triangle triangle);
// Check each node for updates
bool CheckNodes(Camera* camera, Node* parentNode);
// Combine nodes if flagged
bool CombineNodes(Node* node);
// Apply noise to the geometry
void ApplyNoise(float frequency, int octaves, FastNoiseLite* noise, Vertex& vertex);
// Get size of triangle on screen UNUSED
float CheckNodeTriSize(Node* node, Camera* camera);
};