# blobs [![Build Status](https://travis-ci.org/HolySmoke86/blobs.svg?branch=master)](https://travis-ci.org/HolySmoke86/blobs) [![codecov.io](https://codecov.io/github/HolySmoke86/blobs/coverage.svg?branch=master)](https://codecov.io/github/HolySmoke86/blobs?branch=master)
blobs
+
+Many thanks for the sky box to [StumpyStrust from opengameart.org](https://opengameart.org/content/space-skyboxes-0)
-Subproject commit f2587b6b95d73b9b2d2d871e9a8f5b5dae702965
+Subproject commit 7c4c2fb847cfdf25d90d6b2c46442b8cf899a96a
#include "../graphics/ArrayTexture.hpp"
#include "../graphics/Canvas.hpp"
#include "../graphics/CreatureSkin.hpp"
+#include "../graphics/CubeMap.hpp"
#include "../graphics/Font.hpp"
#include "../graphics/PlanetSurface.hpp"
+#include "../graphics/SkyBox.hpp"
#include "../graphics/SunSurface.hpp"
#include "../math/GaloisLFSR.hpp"
#include "../world/Resource.hpp"
std::string data_path;
std::string font_path;
std::string skin_path;
+ std::string sky_path;
std::string tile_path;
math::GaloisLFSR random;
struct {
graphics::ArrayTexture tiles;
graphics::ArrayTexture skins;
+ graphics::CubeMap sky;
} textures;
struct {
graphics::PlanetSurface planet_surface;
graphics::SunSurface sun_surface;
graphics::CreatureSkin creature_skin;
+ graphics::SkyBox sky_box;
} shaders;
Assets();
void LoadTileTexture(const std::string &name, graphics::ArrayTexture &, int layer) const;
void LoadSkinTexture(const std::string &name, graphics::ArrayTexture &, int layer) const;
+ void LoadSkyTexture(const std::string &name, graphics::CubeMap &) const;
};
, data_path(path + "data/")
, font_path(path + "fonts/")
, skin_path(path + "skins/")
+, sky_path(path + "skies/")
, tile_path(path + "tiles/")
, random(0x6283B64CEFE57925)
, fonts{
LoadSkinTexture("spots", textures.skins, 4);
LoadSkinTexture("circles", textures.skins, 5);
textures.skins.FilterTrilinear();
+
+ textures.sky.Bind();
+ LoadSkyTexture("blue", textures.sky);
+ textures.sky.FilterTrilinear();
+ textures.sky.WrapEdge();
}
Assets::~Assets() {
SDL_FreeSurface(srf);
}
+void Assets::LoadSkyTexture(const string &name, graphics::CubeMap &cm) const {
+ string full = sky_path + name;
+ string right = full + "-right.png";
+ string left = full + "-left.png";
+ string top = full + "-top.png";
+ string bottom = full + "-bottom.png";
+ string back = full + "-back.png";
+ string front = full + "-front.png";
+
+ SDL_Surface *srf = nullptr;
+
+ if (!(srf = IMG_Load(right.c_str()))) throw SDLError("IMG_Load");
+ try {
+ cm.Data(graphics::CubeMap::RIGHT, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+
+ if (!(srf = IMG_Load(left.c_str()))) throw SDLError("IMG_Load");
+ try {
+ cm.Data(graphics::CubeMap::LEFT, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+
+ if (!(srf = IMG_Load(top.c_str()))) throw SDLError("IMG_Load");
+ try {
+ cm.Data(graphics::CubeMap::TOP, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+
+ if (!(srf = IMG_Load(bottom.c_str()))) throw SDLError("IMG_Load");
+ try {
+ cm.Data(graphics::CubeMap::BOTTOM, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+
+ if (!(srf = IMG_Load(back.c_str()))) throw SDLError("IMG_Load");
+ try {
+ cm.Data(graphics::CubeMap::BACK, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+
+ if (!(srf = IMG_Load(front.c_str()))) throw SDLError("IMG_Load");
+ try {
+ cm.Data(graphics::CubeMap::FRONT, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+}
+
}
}
assets.shaders.sun_surface.SetVP(cam.View(), cam.Projection());
assets.shaders.creature_skin.Activate();
assets.shaders.creature_skin.SetVP(cam.View(), cam.Projection());
+ assets.shaders.sky_box.Activate();
+ assets.shaders.sky_box.SetVP(cam.View() * cam.Universe(), cam.Projection());
}
void MasterState::OnUpdate(int dt) {
cam.LookAt(glm::vec3(cam_pos), glm::vec3(cam_focus), glm::vec3(cam_up));
assets.shaders.planet_surface.Activate();
assets.shaders.planet_surface.SetV(cam.View());
+ assets.shaders.sky_box.Activate();
+ assets.shaders.sky_box.SetV(cam.View() * cam.Universe());
assets.shaders.sun_surface.Activate();
assets.shaders.sun_surface.SetV(cam.View());
assets.shaders.creature_skin.Activate();
c->Draw(viewport);
}
+ assets.shaders.sky_box.Activate();
+ assets.shaders.sky_box.SetTexture(assets.textures.sky);
+ assets.shaders.sky_box.Draw();
+
viewport.ClearDepth();
bp.Draw(viewport);
cp.Draw(viewport);
const glm::mat4 &Projection() const noexcept { return projection; }
const glm::mat4 &View() const noexcept { return view; }
glm::mat4 Model(const world::Body &) const noexcept;
+ glm::mat4 Universe() const noexcept;
private:
void UpdateProjection() noexcept;
--- /dev/null
+#ifndef BLOBS_GRAPHICS_SKYBOX_HPP_
+#define BLOBS_GRAPHICS_SKYBOX_HPP_
+
+#include "Program.hpp"
+#include "SimpleVAO.hpp"
+
+#include <cstdint>
+
+
+namespace blobs {
+namespace graphics {
+
+class CubeMap;
+
+class SkyBox {
+
+public:
+ SkyBox();
+ ~SkyBox();
+
+ SkyBox(const SkyBox &) = delete;
+ SkyBox &operator =(const SkyBox &) = delete;
+
+ SkyBox(SkyBox &&) = delete;
+ SkyBox &operator =(SkyBox &&) = delete;
+
+public:
+ void Activate() noexcept;
+
+ void SetV(const glm::mat4 &v) noexcept;
+ void SetP(const glm::mat4 &p) noexcept;
+ void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept;
+ void SetTexture(CubeMap &) noexcept;
+
+ const glm::mat4 &V() const noexcept { return v; }
+ const glm::mat4 &P() const noexcept { return p; }
+ const glm::mat4 &VP() const noexcept { return vp; }
+
+ void Draw() const noexcept;
+
+private:
+ Program prog;
+
+ glm::mat4 v;
+ glm::mat4 p;
+ glm::mat4 vp;
+
+ GLuint vp_handle;
+ GLuint sampler_handle;
+
+ SimpleVAO<glm::vec3, std::uint8_t> vao;
+
+};
+
+}
+}
+
+#endif
#include "PlanetSurface.hpp"
#include "Program.hpp"
#include "Shader.hpp"
+#include "SkyBox.hpp"
#include "SunSurface.hpp"
#include "ArrayTexture.hpp"
+#include "CubeMap.hpp"
#include "Texture.hpp"
#include "../app/init.hpp"
}
+SkyBox::SkyBox()
+: prog()
+, v(1.0f)
+, p(1.0f)
+, vp(1.0f) {
+ prog.LoadShader(
+ GL_VERTEX_SHADER,
+ "#version 330 core\n"
+
+ "layout(location = 0) in vec3 vtx_position;\n"
+
+ "uniform mat4 VP;\n"
+
+ "out vec3 vtx_viewspace;\n"
+
+ "void main() {\n"
+ "gl_Position = VP * vec4(vtx_position, 1.0);\n"
+ "gl_Position.z = gl_Position.w;\n"
+ "vtx_viewspace = vtx_position;\n"
+ "}\n"
+ );
+ prog.LoadShader(
+ GL_FRAGMENT_SHADER,
+ "#version 330 core\n"
+
+ "in vec3 vtx_viewspace;\n"
+
+ "uniform samplerCube tex_sampler;\n"
+
+ "out vec3 color;\n"
+
+ "void main() {\n"
+ "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
+ "}\n"
+ );
+ prog.Link();
+ if (!prog.Linked()) {
+ prog.Log(std::cerr);
+ throw std::runtime_error("link program");
+ }
+ vp_handle = prog.UniformLocation("VP");
+ sampler_handle = prog.UniformLocation("tex_sampler");
+
+ vao.Bind();
+ vao.BindAttributes();
+ vao.EnableAttribute(0);
+ vao.AttributePointer<glm::vec3>(0, false, 0);
+ vao.ReserveAttributes(8, GL_STATIC_DRAW);
+ {
+ auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
+ attrib[0] = glm::vec3(-1.0f, -1.0f, -1.0f);
+ attrib[1] = glm::vec3(-1.0f, -1.0f, 1.0f);
+ attrib[2] = glm::vec3(-1.0f, 1.0f, -1.0f);
+ attrib[3] = glm::vec3(-1.0f, 1.0f, 1.0f);
+ attrib[4] = glm::vec3( 1.0f, -1.0f, -1.0f);
+ attrib[5] = glm::vec3( 1.0f, -1.0f, 1.0f);
+ attrib[6] = glm::vec3( 1.0f, 1.0f, -1.0f);
+ attrib[7] = glm::vec3( 1.0f, 1.0f, 1.0f);
+ }
+ vao.BindElements();
+ vao.ReserveElements(14, GL_STATIC_DRAW);
+ {
+ auto element = vao.MapElements(GL_WRITE_ONLY);
+ element[ 0] = 1;
+ element[ 1] = 0;
+ element[ 2] = 3;
+ element[ 3] = 2;
+ element[ 4] = 6;
+ element[ 5] = 0;
+ element[ 6] = 4;
+ element[ 7] = 1;
+ element[ 8] = 5;
+ element[ 9] = 3;
+ element[10] = 7;
+ element[11] = 6;
+ element[12] = 5;
+ element[13] = 4;
+ }
+ vao.Unbind();
+}
+
+SkyBox::~SkyBox() {
+}
+
+void SkyBox::Activate() noexcept {
+ prog.Use();
+ glEnable(GL_DEPTH_TEST);
+ glDepthFunc(GL_LEQUAL);
+ glDisable(GL_CULL_FACE);
+ glDisable(GL_BLEND);
+}
+
+void SkyBox::SetV(const glm::mat4 &vv) noexcept {
+ v = vv;
+ v[0].w = 0.0f;
+ v[1].w = 0.0f;
+ v[2].w = 0.0f;
+ v[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
+ vp = p * v;
+ prog.Uniform(vp_handle, vp);
+}
+
+void SkyBox::SetP(const glm::mat4 &pp) noexcept {
+ p = pp;
+ vp = p * v;
+ prog.Uniform(vp_handle, vp);
+}
+
+void SkyBox::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
+ p = pp;
+ SetV(vv);
+}
+
+void SkyBox::SetTexture(CubeMap &cm) noexcept {
+ glActiveTexture(GL_TEXTURE0);
+ cm.Bind();
+ prog.Uniform(sampler_handle, GLint(0));
+}
+
+void SkyBox::Draw() const noexcept {
+ vao.Bind();
+ vao.DrawTriangleStrip(14);
+}
+
+
SunSurface::SunSurface()
: prog() {
prog.LoadShader(
}
}
+glm::mat4 Camera::Universe() const noexcept {
+ return glm::mat4(track_orient
+ ? ref->InverseTransform() * ref->ToUniverse()
+ : ref->ToUniverse());
+}
+
void Camera::UpdateProjection() noexcept {
projection = glm::infinitePerspective(fov, aspect, near);
}