]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Sky: support GLES2
authornumzero <numzer0@yandex.ru>
Sun, 22 Nov 2020 15:25:41 +0000 (18:25 +0300)
committerlhofhansl <larsh@apache.org>
Thu, 26 Nov 2020 20:49:10 +0000 (12:49 -0800)
IrrLicht built-in shader is broken, have to write my own

client/shaders/stars_shader/opengl_fragment.glsl [new file with mode: 0644]
client/shaders/stars_shader/opengl_vertex.glsl [new file with mode: 0644]
src/client/game.cpp
src/client/sky.cpp
src/client/sky.h

diff --git a/client/shaders/stars_shader/opengl_fragment.glsl b/client/shaders/stars_shader/opengl_fragment.glsl
new file mode 100644 (file)
index 0000000..a9ed741
--- /dev/null
@@ -0,0 +1,6 @@
+uniform vec4 starColor;
+
+void main(void)
+{
+       gl_FragColor = starColor;
+}
diff --git a/client/shaders/stars_shader/opengl_vertex.glsl b/client/shaders/stars_shader/opengl_vertex.glsl
new file mode 100644 (file)
index 0000000..77c401f
--- /dev/null
@@ -0,0 +1,4 @@
+void main(void)
+{
+       gl_Position = mWorldViewProj * inVertexPosition;
+}
index b7bb0a330df7e41fddb117d03d4c29977ff8b30d..2001f0487b3448ef70c97e0213883f392068ce3f 100644 (file)
@@ -424,6 +424,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
        CachedVertexShaderSetting<float> m_animation_timer_vertex;
        CachedPixelShaderSetting<float> m_animation_timer_pixel;
        CachedPixelShaderSetting<float, 3> m_day_light;
+       CachedPixelShaderSetting<float, 4> m_star_color;
        CachedPixelShaderSetting<float, 3> m_eye_position_pixel;
        CachedVertexShaderSetting<float, 3> m_eye_position_vertex;
        CachedPixelShaderSetting<float, 3> m_minimap_yaw;
@@ -456,6 +457,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
                m_animation_timer_vertex("animationTimer"),
                m_animation_timer_pixel("animationTimer"),
                m_day_light("dayLight"),
+               m_star_color("starColor"),
                m_eye_position_pixel("eyePosition"),
                m_eye_position_vertex("eyePosition"),
                m_minimap_yaw("yawVec"),
@@ -507,6 +509,10 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
                        sunlight.b };
                m_day_light.set(dnc, services);
 
+               video::SColorf star_color = m_sky->getCurrentStarColor();
+               float clr[4] = {star_color.r, star_color.g, star_color.b, star_color.a};
+               m_star_color.set(clr, services);
+
                u32 animation_timer = porting::getTimeMs() % 1000000;
                float animation_timer_f = (float)animation_timer / 100000.f;
                m_animation_timer_vertex.set(&animation_timer_f, services);
@@ -1363,7 +1369,7 @@ bool Game::createClient(const GameStartData &start_data)
 
        /* Skybox
         */
-       sky = new Sky(-1, texture_src);
+       sky = new Sky(-1, texture_src, shader_src);
        scsf->setSky(sky);
        skybox = NULL;  // This is used/set later on in the main run loop
 
index dda59dd11b8fc850f0259f4b68a2054b07751ff6..3fc5a95b4d29e73f593a262bff4f641d1fa4d64f 100644 (file)
@@ -51,7 +51,7 @@ static video::SMaterial baseMaterial() {
        return mat;
 };
 
-Sky::Sky(s32 id, ITextureSource *tsrc) :
+Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
                scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
                        RenderingEngine::get_scene_manager(), id)
 {
@@ -59,10 +59,12 @@ Sky::Sky(s32 id, ITextureSource *tsrc) :
        m_box.MaxEdge.set(0, 0, 0);
        m_box.MinEdge.set(0, 0, 0);
 
+       m_enable_shaders = g_settings->getBool("enable_shaders");
+
        // Create materials
 
        m_materials[0] = baseMaterial();
-       m_materials[0].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+       m_materials[0].MaterialType = ssrc->getShaderInfo(ssrc->getShader("stars_shader", TILE_MATERIAL_ALPHA, 0)).material;
        m_materials[0].Lighting = true;
        m_materials[0].ColorMaterial = video::ECM_NONE;
 
@@ -694,12 +696,11 @@ void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
 
        float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
        float starbrightness = (0.25f - fabsf(tod)) * 20.0f;
-       int alpha = clamp<int>(starbrightness * m_star_params.starcolor.getAlpha(), 0, 255);
-       if (!alpha) // Stars are only drawn when not fully transparent
+       m_star_color = m_star_params.starcolor;
+       m_star_color.a = clamp(starbrightness * m_star_color.a, 0.0f, 1.0f);
+       if (m_star_color.a <= 0.0f) // Stars are only drawn when not fully transparent
                return;
-
-       m_materials[0].DiffuseColor = video::SColor(alpha, 0, 0, 0);
-       m_materials[0].EmissiveColor = m_star_params.starcolor;
+       m_materials[0].DiffuseColor = m_materials[0].EmissiveColor = m_star_color.toSColor();
        auto sky_rotation = core::matrix4().setRotationAxisRadians(2.0f * M_PI * (wicked_time_of_day - 0.25f), v3f(0.0f, 0.0f, 1.0f));
        auto world_matrix = driver->getTransform(video::ETS_WORLD);
        driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation);
index 9f859f961bf606b7187796eb3e82ce925552abba..10e1cd97639b154524c9b425d525594822c68030 100644 (file)
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "camera.h"
 #include "irrlichttypes_extrabloated.h"
 #include "irr_ptr.h"
+#include "shader.h"
 #include "skyparams.h"
 
 #pragma once
@@ -35,7 +36,7 @@ class Sky : public scene::ISceneNode
 {
 public:
        //! constructor
-       Sky(s32 id, ITextureSource *tsrc);
+       Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc);
 
        virtual void OnRegisterSceneNode();
 
@@ -102,6 +103,8 @@ class Sky : public scene::ISceneNode
        void clearSkyboxTextures() { m_sky_params.textures.clear(); }
        void addTextureToSkybox(std::string texture, int material_id,
                ITextureSource *tsrc);
+       const video::SColorf &getCurrentStarColor() const { return m_star_color; }
+
 private:
        aabb3f m_box;
        video::SMaterial m_materials[SKY_MATERIAL_COUNT];
@@ -155,6 +158,7 @@ class Sky : public scene::ISceneNode
        bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
        bool m_directional_colored_fog;
        bool m_in_clouds = true; // Prevent duplicating bools to remember old values
+       bool m_enable_shaders = false;
 
        video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
        video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
@@ -181,6 +185,7 @@ class Sky : public scene::ISceneNode
 
        u64 m_seed = 0;
        irr_ptr<scene::SMeshBuffer> m_stars;
+       video::SColorf m_star_color;
 
        video::ITexture *m_sun_texture;
        video::ITexture *m_moon_texture;
@@ -188,7 +193,6 @@ class Sky : public scene::ISceneNode
        video::ITexture *m_moon_tonemap;
 
        void updateStars();
-       void updateStarsColor(video::SColor color);
 
        void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
                const video::SColor &suncolor2, float wicked_time_of_day);