unit cube, better spherification method

This commit is contained in:
Evert Prants 2021-11-06 14:25:13 +02:00
parent 567dd856cb
commit b90dd89e2e
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 28 additions and 27 deletions

View File

@ -164,7 +164,7 @@ void Application::run()
SDL_ShowCursor(SDL_DISABLE);
Input::getInstance().setMouseCoords(m_width/2, m_height/2);
PlanetNoiseParams noise{128.f, 10.f, 2.f, 0.5f};
PlanetNoiseParams noise{15.f, 15.f, .8f, 1.f/.8f};
Planet* pl = new Planet(glm::vec3(0.0f,0.0f,0.0f), 500.0f, noise);
// Create the shader and link them together
Shader& chunkShader = Shader::createShader("data/shaders/chunk.vert", "data/shaders/chunk.frag");

View File

@ -226,7 +226,7 @@ void Camera::updateProjection(void)
{
// Recalculate the projection matrix
glm::vec2 screenDims = Application::getInstance().getScreenDimensions();
m_projection = glm::perspective(getFOV(), (GLfloat)screenDims.x/(GLfloat)screenDims.y, 0.1f, 1000.0f);
m_projection = glm::perspective(getFOV(), (GLfloat)screenDims.x/(GLfloat)screenDims.y, 0.1f, 10000.0f);
}
void Camera::updateCameraVectors(void)

View File

@ -1,5 +1,6 @@
#include "planet/PlanetFace.h"
#include <time.h>
#include <math.h>
// Correlates directly to Face enum
const glm::vec3 FACE_NORMALS[6] = {
@ -18,13 +19,6 @@ PlanetFaceNode::PlanetFaceNode(Planet* planet, PlanetFace* face, glm::vec3 posit
m_left = glm::vec3(normal.y, normal.z, normal.x);
m_forward = glm::cross(normal, m_left);
if (level == 0)
{
float radius = m_planet->getRadius() / 2.0f;
m_pos = m_pos - (m_left * radius);
m_pos = m_pos - (m_forward * radius);
}
generate();
}
@ -106,22 +100,30 @@ void PlanetFaceNode::generate()
{
for (int j = 0; j < RESOLUTION; j++)
{
float iindex = i / (RESOLUTION - 1.0);
float jindex = j / (RESOLUTION - 1.0);
glm::vec2 index = glm::vec2(i, j) / (RESOLUTION - 1.0f);
// From the left and forward vectors, we can calculate an oriented vertex
glm::vec3 iv = ((m_left * iindex) * radius) / divisionLevel;
glm::vec3 jv = ((m_forward * jindex) * radius) / divisionLevel;
// From the left and forward vectors, we can calculate an oriented unit vertex
glm::vec3 iv = (m_forward * (2.0f * index.x - 1.0f)) / divisionLevel;
glm::vec3 jv = (m_left * (2.0f * index.y - 1.0f)) / divisionLevel;
// Add the scaled left and forward to the centered origin
glm::vec3 vertex = m_pos + (iv + jv);
// Add the unit left and forward vectors to the origin
glm::vec3 vertex = m_pos + jv + iv;
// Normalize and multiply by radius to create a spherical mesh
glm::vec3 vertNormal = glm::normalize(vertex);
glm::vec3 pos = (m_planet->getNoise().fractal(m_level + 1, vertNormal.x, vertNormal.y, vertNormal.z) + radius) * vertNormal;
// glm::vec3 vertNormal = glm::normalize(vertex);
float x2 = vertex.x * vertex.x;
float y2 = vertex.y * vertex.y;
float z2 = vertex.z * vertex.z;
glm::vec3 point = glm::vec3(
vertex.x * sqrt(1.0f - ((y2 + z2) / 2.0f) + ((y2 * z2) / 3.0f)),
vertex.y * sqrt(1.0f - ((z2 + x2) / 2.0f) + ((z2 * x2) / 3.0f)),
vertex.z * sqrt(1.0f - ((x2 + y2) / 2.0f) + ((x2 * y2) / 3.0f))
);
glm::vec3 pos = -(m_planet->getNoise().fractal(8, point.x, point.y, point.z) * 20.0f + radius) * point;
// Add vertex
vertices.push_back({ pos, vertNormal, glm::vec2(j * (1.0 / RESOLUTION), i * (1.0 / RESOLUTION)) });
vertices.push_back({ pos, point, glm::vec2(j * (1.0 / RESOLUTION), i * (1.0 / RESOLUTION)) });
// Set center
if ((i == RESOLUTION / 2 && j == RESOLUTION / 2))
@ -147,15 +149,14 @@ bool PlanetFaceNode::subdivide()
return false;
int lv = m_level + 1;
float radius = m_planet->getRadius();
glm::vec3 stepLeft = m_left * (radius / (float)pow(2, lv));
glm::vec3 stepForward = m_forward * (radius / (float)pow(2, lv));
glm::vec3 stepLeft = m_left * (1.0f / (float)pow(2, lv));
glm::vec3 stepForward = m_forward * (1.0f / (float)pow(2, lv));
m_children[TOP_LEFT] = new PlanetFaceNode(m_planet, m_planetFace, m_pos, TOP_LEFT, lv);
m_children[TOP_RIGHT] = new PlanetFaceNode(m_planet, m_planetFace, m_pos + stepForward, TOP_RIGHT, lv);
m_children[BOTTOM_RIGHT] = new PlanetFaceNode(m_planet, m_planetFace, (m_pos + stepLeft) + stepForward, BOTTOM_RIGHT, lv);
m_children[BOTTOM_LEFT] = new PlanetFaceNode(m_planet, m_planetFace, (m_pos + stepLeft), BOTTOM_LEFT, lv);
m_children[TOP_LEFT] = new PlanetFaceNode(m_planet, m_planetFace, m_pos + stepForward - stepLeft, TOP_LEFT, lv);
m_children[TOP_RIGHT] = new PlanetFaceNode(m_planet, m_planetFace, m_pos - stepForward - stepLeft, TOP_RIGHT, lv);
m_children[BOTTOM_RIGHT] = new PlanetFaceNode(m_planet, m_planetFace, m_pos - stepForward + stepLeft, BOTTOM_RIGHT, lv);
m_children[BOTTOM_LEFT] = new PlanetFaceNode(m_planet, m_planetFace, m_pos + stepForward + stepLeft, BOTTOM_LEFT, lv);
m_leaf = false;
@ -317,7 +318,7 @@ PlanetFace::PlanetFace(Planet* planet, const unsigned int face) :
m_planet(planet), m_face(face)
{
m_normal = FACE_NORMALS[m_face];
m_lod = new PlanetFaceNode(planet, this, m_normal * (m_planet->getRadius() / 2.0f), face, 0);
m_lod = new PlanetFaceNode(planet, this, m_normal, face, 0);
}
PlanetFace::~PlanetFace()