From: Daniel Karbach Date: Thu, 24 Sep 2015 10:37:15 +0000 (+0200) Subject: fix this whole sky box mess X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=2e3774eb3f2d5d23a08731175b168566457e2192;p=blank.git fix this whole sky box mess --- diff --git a/src/app/app.cpp b/src/app/app.cpp index 6e7201a..4d2d2c0 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -385,32 +385,66 @@ CubeMap AssetLoader::LoadCubeMap(const string &name) const { string front = full + "-front.png"; CubeMap cm; + cm.Bind(); SDL_Surface *srf; if (!(srf = IMG_Load(right.c_str()))) throw SDLError("IMG_Load"); - cm.Data(CubeMap::RIGHT, *srf); + try { + cm.Data(CubeMap::RIGHT, *srf); + } catch (...) { + SDL_FreeSurface(srf); + throw; + } SDL_FreeSurface(srf); if (!(srf = IMG_Load(left.c_str()))) throw SDLError("IMG_Load"); - cm.Data(CubeMap::LEFT, *srf); + try { + cm.Data(CubeMap::LEFT, *srf); + } catch (...) { + SDL_FreeSurface(srf); + throw; + } SDL_FreeSurface(srf); if (!(srf = IMG_Load(top.c_str()))) throw SDLError("IMG_Load"); - cm.Data(CubeMap::TOP, *srf); + try { + cm.Data(CubeMap::TOP, *srf); + } catch (...) { + SDL_FreeSurface(srf); + throw; + } SDL_FreeSurface(srf); if (!(srf = IMG_Load(bottom.c_str()))) throw SDLError("IMG_Load"); - cm.Data(CubeMap::BOTTOM, *srf); + try { + cm.Data(CubeMap::BOTTOM, *srf); + } catch (...) { + SDL_FreeSurface(srf); + throw; + } SDL_FreeSurface(srf); if (!(srf = IMG_Load(back.c_str()))) throw SDLError("IMG_Load"); - cm.Data(CubeMap::BACK, *srf); + try { + cm.Data(CubeMap::BACK, *srf); + } catch (...) { + SDL_FreeSurface(srf); + throw; + } SDL_FreeSurface(srf); if (!(srf = IMG_Load(front.c_str()))) throw SDLError("IMG_Load"); - cm.Data(CubeMap::FRONT, *srf); + try { + cm.Data(CubeMap::FRONT, *srf); + } catch (...) { + SDL_FreeSurface(srf); + throw; + } SDL_FreeSurface(srf); + cm.FilterNearest(); + cm.WrapEdge(); + return cm; } diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index 8cf9246..5a43cd8 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -5,6 +5,7 @@ #include "ChunkRequester.hpp" #include "../app/IntervalTimer.hpp" #include "../app/State.hpp" +#include "../graphics/SkyBox.hpp" #include "../io/WorldSave.hpp" #include "../model/Skeletons.hpp" #include "../ui/Interface.hpp" @@ -56,6 +57,8 @@ private: Skeletons skeletons; IntervalTimer loop_timer; + SkyBox sky; + struct PlayerHistory { EntityState state; int delta_t; diff --git a/src/client/client.cpp b/src/client/client.cpp index 7dac3ad..521b149 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -120,6 +120,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) , chunk_renderer(*interface.GetPlayer().chunks) , skeletons() , loop_timer(16) +, sky(master.GetEnv().loader.LoadCubeMap("skybox")) , player_hist() { if (!save.Exists()) { save.Write(master.GetWorldConf()); @@ -277,6 +278,7 @@ void InteractiveState::Render(Viewport &viewport) { viewport.WorldPosition(player.Transform(player.ChunkCoords())); chunk_renderer.Render(viewport); world.Render(viewport); + sky.Render(viewport); interface.Render(viewport); } diff --git a/src/graphics/CubeMap.hpp b/src/graphics/CubeMap.hpp index b328196..033894b 100644 --- a/src/graphics/CubeMap.hpp +++ b/src/graphics/CubeMap.hpp @@ -35,7 +35,7 @@ public: CubeMap &operator =(const CubeMap &) = delete; public: - void Data(Face, const SDL_Surface &) noexcept; + void Data(Face, const SDL_Surface &); void Data(Face, GLsizei w, GLsizei h, const Format &, GLvoid *data) noexcept; }; diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index fdf751e..9468b68 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -424,8 +424,28 @@ CubeMap &CubeMap::operator =(CubeMap &&other) noexcept { } -void CubeMap::Data(Face f, const SDL_Surface &srf) noexcept { - Data(f, srf.w, srf.h, Format(*srf.format), srf.pixels); +void CubeMap::Data(Face f, const SDL_Surface &srf) { + Format format; + Format fmt(*srf.format); + if (format.Compatible(fmt)) { + Data(f, srf.w, srf.h, fmt, srf.pixels); + } else { + SDL_Surface *converted = SDL_ConvertSurface( + const_cast(&srf), + &format.sdl_format, + 0 + ); + if (!converted) { + throw SDLError("SDL_ConvertSurface"); + } + Format new_fmt(*converted->format); + if (!format.Compatible(new_fmt)) { + SDL_FreeSurface(converted); + throw std::runtime_error("unable to convert texture input"); + } + Data(f, converted->w, converted->h, new_fmt, converted->pixels); + SDL_FreeSurface(converted); + } } void CubeMap::Data(Face face, GLsizei w, GLsizei h, const Format &f, GLvoid *data) noexcept { diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 98da25a..5540dea 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -521,6 +521,7 @@ SkyBoxShader::SkyBoxShader() "out vec3 color;\n" "void main() {\n" "color = texture(tex_sampler, vtx_viewspace).rgb;\n" + //"color = vec3(1,0,0);\n" "}\n" ); program.Link(); @@ -556,7 +557,7 @@ void SkyBoxShader::SetView(const glm::mat4 &v) noexcept { view[1].w = 0.0f; view[2].w = 0.0f; view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); - vp = projection * v; + vp = projection * view; program.Uniform(vp_handle, vp); } diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index 181c97c..ddc609b 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -148,14 +148,9 @@ void Viewport::Resize(int w, int h) noexcept { cam.Aspect(fw, fh); canv.Resize(fw, fh); - chunk_prog.SetProjection(Perspective()); - if (active_prog == HUD) { - entity_prog.SetProjection(Ortho()); - } else { - entity_prog.SetProjection(Perspective()); - } - sky_prog.SetProjection(Perspective()); - sprite_prog.SetProjection(Ortho()); + ChunkProgram().SetProjection(Perspective()); + SkyBoxProgram().SetProjection(Perspective()); + SpriteProgram().SetProjection(Ortho()); } void Viewport::Clear() noexcept { @@ -262,8 +257,9 @@ BlendedSprite &Viewport::SpriteProgram() noexcept { void Viewport::WorldPosition(const glm::mat4 &t) noexcept { cam.View(glm::inverse(t)); - chunk_prog.SetView(cam.View()); - sky_prog.SetView(cam.View()); + ChunkProgram().SetView(cam.View()); + sky_prog.Activate(); + SkyBoxProgram().SetView(cam.View()); } }