From 15608f11e72c17ac71469a0aa4a26fc523492d20 Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 29 Jul 2025 00:36:54 +0400 Subject: [PATCH] fix(daynight): improve DNSky::GenSphere --- src/gameui/CGWorldFrame.cpp | 47 ++++++++++++++++++++++ src/world/daynight/DNSky.cpp | 69 ++++++++++++++------------------- src/world/daynight/DayNight.cpp | 2 +- 3 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/gameui/CGWorldFrame.cpp b/src/gameui/CGWorldFrame.cpp index 0bac82d..55fa2b8 100644 --- a/src/gameui/CGWorldFrame.cpp +++ b/src/gameui/CGWorldFrame.cpp @@ -44,6 +44,11 @@ int32_t CGWorldFrame::OnLayerKeyDown(const CKeyEvent& evt) { C3Vector& position = this->m_camera->m_position; float step = 0.1f; + float astep = 0.1f; + + static float pitch = 0.0f; + static float yaw = 0.0f; + static float roll = 0.0f; switch (evt.key) { case KEY_W: @@ -64,10 +69,52 @@ int32_t CGWorldFrame::OnLayerKeyDown(const CKeyEvent& evt) { case KEY_MINUS: position.x -= step; break; + + case KEY_P: + position.Set(0.0f, 0.0f, 0.0f); + break; + + case KEY_R: + pitch = 0.0f; + yaw = 0.0f; + roll = 0.0f; + break; + + case KEY_Z: + position.Set(0.0f, 0.0f, 0.0f); + pitch = 0.0f; + yaw = 0.0f; + roll = 0.0f; + break; + + case KEY_LEFT: + roll -= astep; + break; + case KEY_RIGHT: + roll += astep; + break; + + case KEY_DOWN: + pitch -= astep; + break; + case KEY_UP: + pitch += astep; + break; + + case KEY_PAGEUP: + yaw += astep; + break; + case KEY_PAGEDOWN: + yaw -= astep; + break; + + default: break; } + this->m_camera->SetFacing(yaw, pitch, roll); + return 0; } diff --git a/src/world/daynight/DNSky.cpp b/src/world/daynight/DNSky.cpp index a6246d8..766e3ce 100644 --- a/src/world/daynight/DNSky.cpp +++ b/src/world/daynight/DNSky.cpp @@ -37,49 +37,37 @@ void DNSky::Render() { } void DNSky::GenSphere(float sphRadius) { - const int32_t geoSize = 24; - const int32_t idxSize = 25 * 2; + const uint16_t totalSlices = 24; + const uint16_t totalIndices = (totalSlices + 1) * 2; - this->m_sphThetaTess = geoSize; - this->m_geoVerts.SetCount(SKY_NUMBANDS * geoSize); // 168 - this->m_clrVerts.SetCount(SKY_NUMBANDS * geoSize); // 168 - this->m_indices.SetCount(idxSize * (SKY_NUMBANDS - 1)); // 300 + this->m_sphThetaTess = totalSlices; + this->m_geoVerts.SetCount(SKY_NUMBANDS * totalSlices); // 168 + this->m_clrVerts.SetCount(SKY_NUMBANDS * totalSlices); // 168 + this->m_indices.SetCount((SKY_NUMBANDS - 1) * totalIndices); // 300 - uint16_t lastGeoIndex = 0; uint16_t lastIndex = 0; + uint16_t lastRowIndex = 0; + uint16_t thisRowIndex = 0; + uint16_t prevRowIndex = 0; + + float prevPhi = 0.0f; + for (int32_t i = 0; i < SKY_NUMBANDS; ++i) { float phi = DNSky::m_stripSizes[i] * CMath::PI; - float v10 = 0.31830987f * phi; - int64_t v11 = static_cast(v10); - if (v10 <= 0.0f) { - v11 -= 1; - } + float cosPhi = CMath::cos(phi); + float sinPhi = CMath::sin(phi); - float v12 = 1.0f - (v10 - v11) * ((6.0f - (v10 - v11) * 4.0f) * (v10 - v11)); - if (v11 & 1) { - v12 = -v12; - } + thisRowIndex = lastRowIndex; - float v13 = 0.31830987f * phi - 0.5f; - int64_t v14 = static_cast(v13); - if (v13 <= 0.0f) { - v14 -= 1; - } + for (uint16_t j = 0; j < totalSlices; ++j) { + auto& vertex = this->m_geoVerts[lastRowIndex++]; - float v16 = 1.0f - (6.0f - 4.0f * (v13 - v14)) * (v13 - v14) * (v13 - v14); - if (v14 & 1) { - v16 = -v16; - } - - for (int32_t j = 0; j < geoSize; ++j) { - auto& vertex = this->m_geoVerts[lastGeoIndex++]; - - float v19 = static_cast(j) * 0.041666668f * CMath::TWO_PI; - vertex.x = CMath::sin(v19) * v16 * sphRadius; - vertex.y = v16 * CMath::cos(v19) * sphRadius; - vertex.z = sphRadius * v12 - CMath::cos(0.7853981852531433f); + float theta = static_cast(j) / static_cast(totalSlices) * CMath::TWO_PI; + vertex.x = CMath::sin(theta) * sinPhi * sphRadius; + vertex.y = CMath::cos(theta) * sinPhi * sphRadius; + vertex.z = cosPhi * sphRadius; if (CMath::fequal(phi, 0.0f) || CMath::fequal(phi, CMath::PI)) { break; @@ -87,16 +75,19 @@ void DNSky::GenSphere(float sphRadius) { } if (i > 0) { - for (uint16_t k = 0; k < 25; ++k) { - uint16_t idx1 = CMath::fequal(phi, 0.0f) ? 0 : (k % 24); - uint16_t idx2 = CMath::fequal(phi, CMath::PI) ? 0 : (k % 24); - this->m_indices[lastIndex++] = idx1 + lastGeoIndex; - this->m_indices[lastIndex++] = idx2 + lastGeoIndex; + for (uint16_t k = 0; k < totalIndices / 2; ++k) { + uint16_t idx1 = CMath::fequal(prevPhi, 0.0f) ? 0 : (k % totalSlices); + uint16_t idx2 = CMath::fequal(phi, CMath::PI) ? 0 : (k % totalSlices); + this->m_indices[lastIndex++] = idx1 + prevRowIndex; + this->m_indices[lastIndex++] = idx2 + thisRowIndex; } } + + prevPhi = phi; + prevRowIndex = thisRowIndex; } - this->m_nVerts = lastGeoIndex; + this->m_nVerts = lastRowIndex; this->m_nIndices = lastIndex; // Should be always equal to 300 } diff --git a/src/world/daynight/DayNight.cpp b/src/world/daynight/DayNight.cpp index 50e2164..3dd018e 100644 --- a/src/world/daynight/DayNight.cpp +++ b/src/world/daynight/DayNight.cpp @@ -95,7 +95,7 @@ void RenderSky() { CImVector clearColor = { 124, 125, 61, 0xFF }; GxSceneClear(3, clearColor); - //g_stars.Render(); + g_stars.Render(); g_sky.Render(); }