space-game001/PlanetObject.h
Vladislav Khorev af550f56a7 Cool working
2025-12-13 23:17:03 +03:00

79 lines
1.6 KiB
C++

#pragma once
#include "ZLMath.h"
#include "Renderer.h"
#include "TextureManager.h"
#include <vector>
#include <chrono>
#include <iostream>
#include <string>
#include <array>
#include <numeric>
#include <random>
#include <algorithm>
namespace ZL {
struct Triangle
{
std::array<Vector3f, 3> data;
Triangle(Vector3f p1, Vector3f p2, Vector3f p3)
: data{ p1, p2, p3 }
{
}
};
class PerlinNoise {
std::vector<int> p;
public:
PerlinNoise();
PerlinNoise(uint64_t seed);
float fade(float t);
float lerp(float t, float a, float b);
float grad(int hash, float x, float y, float z);
float noise(float x, float y, float z);
float getSurfaceHeight(Vector3f pos);
};
class PlanetObject {
private:
PerlinNoise perlin;
PerlinNoise colorPerlin;
void prepareDrawData();
VertexDataStruct planetMesh;
VertexRenderStruct planetRenderStruct;
VertexRenderStruct planetAtmosphere;
std::shared_ptr<Texture> sandTexture;
public:
PlanetObject();
void init();
void update(float deltaTimeMs);
void draw(Renderer& renderer);
void drawAtmosphere(Renderer& renderer);
bool planetIsFar();
float distanceToPlanetSurface();
private:
bool drawDataDirty = true;
std::vector<Triangle> subdivideTriangles(const std::vector<Triangle>& inputTriangles);
Vector3f calculateSurfaceNormal(Vector3f p_sphere);
VertexDataStruct trianglesToVertices(const std::vector<Triangle>& triangles);
VertexDataStruct generateSphere(int subdivisions);
};
} // namespace ZL