mirror of
https://github.com/thunderbrewhq/thunderbrew
synced 2025-12-12 03:02:30 +00:00
fix(daynight): improve DNSky::GenSphere
This commit is contained in:
parent
a886fe7d63
commit
15608f11e7
3 changed files with 78 additions and 40 deletions
|
|
@ -44,6 +44,11 @@ int32_t CGWorldFrame::OnLayerKeyDown(const CKeyEvent& evt) {
|
||||||
C3Vector& position = this->m_camera->m_position;
|
C3Vector& position = this->m_camera->m_position;
|
||||||
|
|
||||||
float step = 0.1f;
|
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) {
|
switch (evt.key) {
|
||||||
case KEY_W:
|
case KEY_W:
|
||||||
|
|
@ -64,10 +69,52 @@ int32_t CGWorldFrame::OnLayerKeyDown(const CKeyEvent& evt) {
|
||||||
case KEY_MINUS:
|
case KEY_MINUS:
|
||||||
position.x -= step;
|
position.x -= step;
|
||||||
break;
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->m_camera->SetFacing(yaw, pitch, roll);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,49 +37,37 @@ void DNSky::Render() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DNSky::GenSphere(float sphRadius) {
|
void DNSky::GenSphere(float sphRadius) {
|
||||||
const int32_t geoSize = 24;
|
const uint16_t totalSlices = 24;
|
||||||
const int32_t idxSize = 25 * 2;
|
const uint16_t totalIndices = (totalSlices + 1) * 2;
|
||||||
|
|
||||||
this->m_sphThetaTess = geoSize;
|
this->m_sphThetaTess = totalSlices;
|
||||||
this->m_geoVerts.SetCount(SKY_NUMBANDS * geoSize); // 168
|
this->m_geoVerts.SetCount(SKY_NUMBANDS * totalSlices); // 168
|
||||||
this->m_clrVerts.SetCount(SKY_NUMBANDS * geoSize); // 168
|
this->m_clrVerts.SetCount(SKY_NUMBANDS * totalSlices); // 168
|
||||||
this->m_indices.SetCount(idxSize * (SKY_NUMBANDS - 1)); // 300
|
this->m_indices.SetCount((SKY_NUMBANDS - 1) * totalIndices); // 300
|
||||||
|
|
||||||
uint16_t lastGeoIndex = 0;
|
|
||||||
uint16_t lastIndex = 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) {
|
for (int32_t i = 0; i < SKY_NUMBANDS; ++i) {
|
||||||
float phi = DNSky::m_stripSizes[i] * CMath::PI;
|
float phi = DNSky::m_stripSizes[i] * CMath::PI;
|
||||||
|
|
||||||
float v10 = 0.31830987f * phi;
|
float cosPhi = CMath::cos(phi);
|
||||||
int64_t v11 = static_cast<int64_t>(v10);
|
float sinPhi = CMath::sin(phi);
|
||||||
if (v10 <= 0.0f) {
|
|
||||||
v11 -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
float v12 = 1.0f - (v10 - v11) * ((6.0f - (v10 - v11) * 4.0f) * (v10 - v11));
|
thisRowIndex = lastRowIndex;
|
||||||
if (v11 & 1) {
|
|
||||||
v12 = -v12;
|
|
||||||
}
|
|
||||||
|
|
||||||
float v13 = 0.31830987f * phi - 0.5f;
|
for (uint16_t j = 0; j < totalSlices; ++j) {
|
||||||
int64_t v14 = static_cast<int64_t>(v13);
|
auto& vertex = this->m_geoVerts[lastRowIndex++];
|
||||||
if (v13 <= 0.0f) {
|
|
||||||
v14 -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
float v16 = 1.0f - (6.0f - 4.0f * (v13 - v14)) * (v13 - v14) * (v13 - v14);
|
float theta = static_cast<float>(j) / static_cast<float>(totalSlices) * CMath::TWO_PI;
|
||||||
if (v14 & 1) {
|
vertex.x = CMath::sin(theta) * sinPhi * sphRadius;
|
||||||
v16 = -v16;
|
vertex.y = CMath::cos(theta) * sinPhi * sphRadius;
|
||||||
}
|
vertex.z = cosPhi * sphRadius;
|
||||||
|
|
||||||
for (int32_t j = 0; j < geoSize; ++j) {
|
|
||||||
auto& vertex = this->m_geoVerts[lastGeoIndex++];
|
|
||||||
|
|
||||||
float v19 = static_cast<float>(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);
|
|
||||||
|
|
||||||
if (CMath::fequal(phi, 0.0f) || CMath::fequal(phi, CMath::PI)) {
|
if (CMath::fequal(phi, 0.0f) || CMath::fequal(phi, CMath::PI)) {
|
||||||
break;
|
break;
|
||||||
|
|
@ -87,16 +75,19 @@ void DNSky::GenSphere(float sphRadius) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
for (uint16_t k = 0; k < 25; ++k) {
|
for (uint16_t k = 0; k < totalIndices / 2; ++k) {
|
||||||
uint16_t idx1 = CMath::fequal(phi, 0.0f) ? 0 : (k % 24);
|
uint16_t idx1 = CMath::fequal(prevPhi, 0.0f) ? 0 : (k % totalSlices);
|
||||||
uint16_t idx2 = CMath::fequal(phi, CMath::PI) ? 0 : (k % 24);
|
uint16_t idx2 = CMath::fequal(phi, CMath::PI) ? 0 : (k % totalSlices);
|
||||||
this->m_indices[lastIndex++] = idx1 + lastGeoIndex;
|
this->m_indices[lastIndex++] = idx1 + prevRowIndex;
|
||||||
this->m_indices[lastIndex++] = idx2 + lastGeoIndex;
|
this->m_indices[lastIndex++] = idx2 + thisRowIndex;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->m_nVerts = lastGeoIndex;
|
prevPhi = phi;
|
||||||
|
prevRowIndex = thisRowIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->m_nVerts = lastRowIndex;
|
||||||
this->m_nIndices = lastIndex; // Should be always equal to 300
|
this->m_nIndices = lastIndex; // Should be always equal to 300
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ void RenderSky() {
|
||||||
CImVector clearColor = { 124, 125, 61, 0xFF };
|
CImVector clearColor = { 124, 125, 61, 0xFF };
|
||||||
GxSceneClear(3, clearColor);
|
GxSceneClear(3, clearColor);
|
||||||
|
|
||||||
//g_stars.Render();
|
g_stars.Render();
|
||||||
g_sky.Render();
|
g_sky.Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue