]> git.localhorst.tv Git - blank.git/commitdiff
fix this whole sky box mess
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 24 Sep 2015 10:37:15 +0000 (12:37 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 24 Sep 2015 10:37:15 +0000 (12:37 +0200)
src/app/app.cpp
src/client/InteractiveState.hpp
src/client/client.cpp
src/graphics/CubeMap.hpp
src/graphics/render.cpp
src/graphics/shader.cpp
src/graphics/viewport.cpp

index 6e7201a6f9b2239405255d2413aa0f06dca35007..4d2d2c098afa17bcde96083ddf384aeddacaa86b 100644 (file)
@@ -385,32 +385,66 @@ CubeMap AssetLoader::LoadCubeMap(const string &name) const {
        string front = full + "-front.png";
 
        CubeMap cm;
        string front = full + "-front.png";
 
        CubeMap cm;
+       cm.Bind();
        SDL_Surface *srf;
 
        if (!(srf = IMG_Load(right.c_str()))) throw SDLError("IMG_Load");
        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");
        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");
        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");
        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");
        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");
        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);
 
        SDL_FreeSurface(srf);
 
+       cm.FilterNearest();
+       cm.WrapEdge();
+
        return cm;
 }
 
        return cm;
 }
 
index 8cf9246a56d61a58c9be8fa5a04b4387fadd25b3..5a43cd89e63b18757d4675bb7a001e0775db7884 100644 (file)
@@ -5,6 +5,7 @@
 #include "ChunkRequester.hpp"
 #include "../app/IntervalTimer.hpp"
 #include "../app/State.hpp"
 #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"
 #include "../io/WorldSave.hpp"
 #include "../model/Skeletons.hpp"
 #include "../ui/Interface.hpp"
@@ -56,6 +57,8 @@ private:
        Skeletons skeletons;
        IntervalTimer loop_timer;
 
        Skeletons skeletons;
        IntervalTimer loop_timer;
 
+       SkyBox sky;
+
        struct PlayerHistory {
                EntityState state;
                int delta_t;
        struct PlayerHistory {
                EntityState state;
                int delta_t;
index 7dac3ad0991a2438350a0f15c755370ab42da786..521b149ae824185cd44fda750f9ec38c48917ba7 100644 (file)
@@ -120,6 +120,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , chunk_renderer(*interface.GetPlayer().chunks)
 , skeletons()
 , loop_timer(16)
 , chunk_renderer(*interface.GetPlayer().chunks)
 , skeletons()
 , loop_timer(16)
+, sky(master.GetEnv().loader.LoadCubeMap("skybox"))
 , player_hist() {
        if (!save.Exists()) {
                save.Write(master.GetWorldConf());
 , 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);
        viewport.WorldPosition(player.Transform(player.ChunkCoords()));
        chunk_renderer.Render(viewport);
        world.Render(viewport);
+       sky.Render(viewport);
        interface.Render(viewport);
 }
 
        interface.Render(viewport);
 }
 
index b328196304095b6ee9d2eb3c7a8d16f3e935bbba..033894b811a767d6f8bd7958798d5e03cb0007b3 100644 (file)
@@ -35,7 +35,7 @@ public:
        CubeMap &operator =(const CubeMap &) = delete;
 
 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;
 
 };
        void Data(Face, GLsizei w, GLsizei h, const Format &, GLvoid *data) noexcept;
 
 };
index fdf751e4248390e6f2b37a80a9707b139953e5cc..9468b68b794b8640558c986870f885c5e171c26d 100644 (file)
@@ -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<SDL_Surface *>(&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 {
 }
 
 void CubeMap::Data(Face face, GLsizei w, GLsizei h, const Format &f, GLvoid *data) noexcept {
index 98da25ab348fb7210ac66eb774c6909eb30d39d7..5540dea4ef4b9454279332d17e33435ee7aed1f5 100644 (file)
@@ -521,6 +521,7 @@ SkyBoxShader::SkyBoxShader()
                "out vec3 color;\n"
                "void main() {\n"
                        "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
                "out vec3 color;\n"
                "void main() {\n"
                        "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
+                       //"color = vec3(1,0,0);\n"
                "}\n"
        );
        program.Link();
                "}\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);
        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);
 }
 
        program.Uniform(vp_handle, vp);
 }
 
index 181c97cd7a9d8a5ba9859920abe26c2081aed844..ddc609baad6599a88012ba8b8dd4128c31e600a2 100644 (file)
@@ -148,14 +148,9 @@ void Viewport::Resize(int w, int h) noexcept {
        cam.Aspect(fw, fh);
        canv.Resize(fw, fh);
 
        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 {
 }
 
 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));
 
 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());
 }
 
 }
 }
 
 }