summaryrefslogtreecommitdiffstats
path: root/src/RendererWorld.cpp
diff options
context:
space:
mode:
authorLaG1924 <lag1924@gmail.com>2021-11-19 07:48:37 +0100
committerLaG1924 <lag1924@gmail.com>2021-11-19 08:28:15 +0100
commit8ab6abaae252050bf0ee03cd2a988c0e4e502718 (patch)
tree6c96eb7314da49db1d2ac5a142623bc443e3ee33 /src/RendererWorld.cpp
parentOptimized entities rendering (diff)
downloadAltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar
AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.gz
AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.bz2
AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.lz
AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.xz
AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.zst
AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.zip
Diffstat (limited to '')
-rw-r--r--src/RendererWorld.cpp192
1 files changed, 137 insertions, 55 deletions
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp
index 4bec197..cadad3f 100644
--- a/src/RendererWorld.cpp
+++ b/src/RendererWorld.cpp
@@ -337,51 +337,48 @@ void RendererWorld::Render(RenderState & renderState) {
//Render sky
renderState.TimeOfDay = GetGameState()->GetTimeStatus().timeOfDay;
- Shader *skyShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get();
- skyShader->Activate();
- skyShader->SetUniform("projection", projection);
- skyShader->SetUniform("view", view);
- glm::mat4 model = glm::mat4(1.0);
- model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm());
- const float scale = 1000000.0f;
- model = glm::scale(model, glm::vec3(scale, scale, scale));
- float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f;
- if (shift < 0)
- shift *= -1.0f;
- model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f));
- model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f));
- skyShader->SetUniform("model", model);
-
- glCheckError();
-
- const int sunriseMin = 22000;
- const int sunriseMax = 23500;
- const int moonriseMin = 12000;
- const int moonriseMax = 13500;
- const float sunriseLength = sunriseMax - sunriseMin;
- const float moonriseLength = moonriseMax - moonriseMin;
-
- float mixLevel = 0;
- float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay;
- if (dayTime < 0)
- dayTime *= -1;
- while (dayTime > 24000)
- dayTime -= 24000;
- if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day
- mixLevel = 1.0;
- if (dayTime > moonriseMax && dayTime < sunriseMin) //night
- mixLevel = 0.0;
- if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise
- mixLevel = (dayTime - sunriseMin) / sunriseLength;
- if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise
- float timePassed = (dayTime - moonriseMin);
- mixLevel = 1.0 - (timePassed / moonriseLength);
- }
- skyShader->SetUniform("DayTime", mixLevel);
+ glm::mat4 model = glm::mat4(1.0);
+ model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm());
+ const float scale = 1000000.0f;
+ model = glm::scale(model, glm::vec3(scale, scale, scale));
+ float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f;
+ if (shift < 0)
+ shift *= -1.0f;
+ model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f));
+ model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f));
+
+ const int sunriseMin = 22000;
+ const int sunriseMax = 23500;
+ const int moonriseMin = 12000;
+ const int moonriseMax = 13500;
+ const float sunriseLength = sunriseMax - sunriseMin;
+ const float moonriseLength = moonriseMax - moonriseMin;
+
+ float mixLevel = 0;
+ float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay;
+ if (dayTime < 0)
+ dayTime *= -1;
+ while (dayTime > 24000)
+ dayTime -= 24000;
+ if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day
+ mixLevel = 1.0;
+ if (dayTime > moonriseMax && dayTime < sunriseMin) //night
+ mixLevel = 0.0;
+ if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise
+ mixLevel = (dayTime - sunriseMin) / sunriseLength;
+ if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise
+ float timePassed = (dayTime - moonriseMin);
+ mixLevel = 1.0 - (timePassed / moonriseLength);
+ }
+
+ skyPipeline->Activate();
+ skyPipeline->SetShaderParameter("projView", projView);
+ skyPipeline->SetShaderParameter("model", model);
+ skyPipeline->SetShaderParameter("DayTime", mixLevel);
+ skyPipelineInstance->Activate();
+ skyPipelineInstance->Render(0, 36);
- rendererSky.Render(renderState);
- glCheckError();
//Render sections
auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart);
@@ -433,6 +430,15 @@ void RendererWorld::PrepareRender() {
entitiesPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size());
}
+ std::string skyVertexSource, skyPixelSource;
+ {
+ auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/sky");
+ skyVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size());
+
+ auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/sky");
+ skyPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size());
+ }
+
auto gal = Gal::GetImplementation();
{
auto sectionsPLC = gal->CreatePipelineConfig();
@@ -623,18 +629,94 @@ void RendererWorld::PrepareRender() {
});
}
- TextureCoord sunTexture = AssetManager::GetTexture("/minecraft/textures/environment/sun");
- TextureCoord moonTexture = AssetManager::GetTexture("/minecraft/textures/environment/moon_phases");
- moonTexture.w /= 4.0f; //First phase will be fine for now
- moonTexture.h /= 2.0f;
-
- Shader *sky = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get();
- sky->Activate();
- sky->SetUniform("textureAtlas", 0);
- sky->SetUniform("sunTexture", glm::vec4(sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h));
- sky->SetUniform("sunTextureLayer", (float)sunTexture.layer);
- sky->SetUniform("moonTexture", glm::vec4(moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h));
- sky->SetUniform("moonTextureLayer", (float)moonTexture.layer);
+ {
+ auto skyPPC = gal->CreatePipelineConfig();
+ skyPPC->SetTarget(gal->GetDefaultFramebuffer());
+ skyPPC->AddShaderParameter("textureAtlas", Gal::Type::Int32);
+ skyPPC->AddShaderParameter("sunTexture", Gal::Type::Vec4);
+ skyPPC->AddShaderParameter("sunTextureLayer", Gal::Type::Float);
+ skyPPC->AddShaderParameter("moonTexture", Gal::Type::Vec4);
+ skyPPC->AddShaderParameter("moonTextureLayer", Gal::Type::Float);
+ skyPPC->AddShaderParameter("DayTime", Gal::Type::Float);
+ skyPPC->AddShaderParameter("projView", Gal::Type::Mat4);
+ skyPPC->AddShaderParameter("model", Gal::Type::Mat4);
+ skyPPC->SetVertexShader(gal->LoadVertexShader(skyVertexSource));
+ skyPPC->SetPixelShader(gal->LoadPixelShader(skyPixelSource));
+ auto skyPosUvBB = skyPPC->BindVertexBuffer({
+ {"position", Gal::Type::Vec3},
+ {"", Gal::Type::Vec2},
+ });
+
+ constexpr float vertices[] = {
+ //Z+ Positions UVs
+ -0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
+ -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
+ -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
+
+ //Z-
+ -0.5f, 0.5f, -0.5f, 1.0f, 0.0f,
+ -0.5f, -0.5f, -0.5f, 1.0f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Texture = AssetManager::GetTexture("/minecraft/textures/environment/moon_phases");
+ moonTexture.w /= 4.0f; // First phase will be fine for now
+ moonTexture.h /= 2.0f;
+
+ skyPipeline = gal->BuildPipeline(skyPPC);
+ skyPipeline->Activate();
+ skyPipeline->SetShaderParameter("textureAtlas", 0);
+ skyPipeline->SetShaderParameter("sunTexture", glm::vec4(sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h));
+ skyPipeline->SetShaderParameter("sunTextureLayer", static_cast<float>(sunTexture.layer));
+ skyPipeline->SetShaderParameter("moonTexture", glm::vec4(moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h));
+ skyPipeline->SetShaderParameter("moonTextureLayer", static_cast<float>(moonTexture.layer));
+
+ skyBuffer = gal->CreateBuffer();
+ skyBuffer->SetData({ reinterpret_cast<const std::byte*>(vertices), reinterpret_cast<const std::byte*>(vertices) + sizeof(vertices) });
+
+ skyPipelineInstance = skyPipeline->CreateInstance({
+ {skyPosUvBB, skyBuffer}
+ });
+ }
}
void RendererWorld::Update(double timeToUpdate) {