]> git.lizzy.rs Git - minetest.git/blobdiff - src/client/sky.cpp
Restore Irrlicht 1.9 support
[minetest.git] / src / client / sky.cpp
index cc9fb7d36f9aab3f43842c466d24f5755918bf3a..caf695e7a28363b71b5a50226af1733fd6da2481 100644 (file)
@@ -35,15 +35,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "config.h"
 using namespace irr::core;
 
-static video::SMaterial baseMaterial() {
+static video::SMaterial baseMaterial()
+{
        video::SMaterial mat;
        mat.Lighting = false;
-#if ENABLE_GLES
+#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
        mat.ZBuffer = video::ECFN_DISABLED;
+       mat.ZWriteEnable = video::EZW_OFF;
 #else
+       mat.ZWriteEnable = false;
        mat.ZBuffer = video::ECFN_NEVER;
 #endif
-       mat.ZWriteEnable = false;
        mat.AntiAliasing = 0;
        mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
        mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
@@ -51,7 +53,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 +61,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)).material;
        m_materials[0].Lighting = true;
        m_materials[0].ColorMaterial = video::ECM_NONE;
 
@@ -209,7 +213,7 @@ void Sky::render()
 
                const f32 t = 1.0f;
                const f32 o = 0.0f;
-               static const u16 indices[4] = {0, 1, 2, 3};
+               static const u16 indices[6] = {0, 1, 2, 0, 2, 3};
                video::S3DVertex vertices[4];
 
                driver->setMaterial(m_materials[1]);
@@ -251,7 +255,7 @@ void Sky::render()
                                                vertex.Pos.rotateXZBy(180);
                                        }
                                }
-                               driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                               driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                        }
                }
 
@@ -277,7 +281,7 @@ void Sky::render()
                                                // Switch from -Z (south) to +Z (north)
                                                vertex.Pos.rotateXZBy(-180);
                                }
-                               driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                               driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                        }
                }
 
@@ -308,7 +312,7 @@ void Sky::render()
                                        // Switch from -Z (south) to -X (west)
                                        vertex.Pos.rotateXZBy(-90);
                        }
-                       driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                       driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                }
 
                // Draw sun
@@ -344,7 +348,7 @@ void Sky::render()
                                                // Switch from -Z (south) to +Z (north)
                                                vertex.Pos.rotateXZBy(-180);
                                }
-                               driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                               driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                        }
 
                        // Draw bottom far cloudy fog thing in front of sun, moon and stars
@@ -353,7 +357,7 @@ void Sky::render()
                        vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
                        vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
                        vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
-                       driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                       driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                }
        }
 }
@@ -597,7 +601,7 @@ void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SCol
         * wicked_time_of_day: current time of day, to know where should be the sun in the sky
         */
 {
-       static const u16 indices[4] = {0, 1, 2, 3};
+       static const u16 indices[] = {0, 1, 2, 0, 2, 3};
        std::array<video::S3DVertex, 4> vertices;
        if (!m_sun_texture) {
                driver->setMaterial(m_materials[1]);
@@ -615,7 +619,7 @@ void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SCol
                for (int i = 0; i < 4; i++) {
                        draw_sky_body(vertices, -sunsizes[i], sunsizes[i], colors[i]);
                        place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
-                       driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                       driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                }
        } else {
                driver->setMaterial(m_materials[3]);
@@ -627,7 +631,7 @@ void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SCol
                        c = video::SColor(255, 255, 255, 255);
                draw_sky_body(vertices, -d, d, c);
                place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
-               driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+               driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
        }
 }
 
@@ -644,7 +648,7 @@ void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SC
        * the sky
        */
 {
-       static const u16 indices[4] = {0, 1, 2, 3};
+       static const u16 indices[] = {0, 1, 2, 0, 2, 3};
        std::array<video::S3DVertex, 4> vertices;
        if (!m_moon_texture) {
                driver->setMaterial(m_materials[1]);
@@ -668,7 +672,7 @@ void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SC
                for (int i = 0; i < 4; i++) {
                        draw_sky_body(vertices, moonsizes_1[i], moonsizes_2[i], colors[i]);
                        place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
-                       driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+                       driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
                }
        } else {
                driver->setMaterial(m_materials[4]);
@@ -694,12 +698,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, 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);
@@ -835,7 +838,8 @@ void Sky::setStarCount(u16 star_count, bool force_update)
        }
 }
 
-void Sky::updateStars() {
+void Sky::updateStars()
+{
        m_stars.reset(new scene::SMeshBuffer());
        // Stupid IrrLicht doesn’t allow non-indexed rendering, and indexed quad
        // rendering is slow due to lack of hardware support. So as indices are
@@ -848,6 +852,7 @@ void Sky::updateStars() {
        m_stars->Vertices.reallocate(4 * m_star_params.count);
        m_stars->Indices.reallocate(6 * m_star_params.count);
 
+       video::SColor fallback_color = m_star_params.starcolor; // used on GLES 2 “without shaders”
        PcgRandom rgen(m_seed);
        float d = (0.006 / 2) * m_star_params.scale;
        for (u16 i = 0; i < m_star_params.count; i++) {
@@ -866,10 +871,10 @@ void Sky::updateStars() {
                a.rotateVect(p1);
                a.rotateVect(p2);
                a.rotateVect(p3);
-               m_stars->Vertices.push_back(video::S3DVertex(p, {}, {}, {}));
-               m_stars->Vertices.push_back(video::S3DVertex(p1, {}, {}, {}));
-               m_stars->Vertices.push_back(video::S3DVertex(p2, {}, {}, {}));
-               m_stars->Vertices.push_back(video::S3DVertex(p3, {}, {}, {}));
+               m_stars->Vertices.push_back(video::S3DVertex(p, {}, fallback_color, {}));
+               m_stars->Vertices.push_back(video::S3DVertex(p1, {}, fallback_color, {}));
+               m_stars->Vertices.push_back(video::S3DVertex(p2, {}, fallback_color, {}));
+               m_stars->Vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
        }
        for (u16 i = 0; i < m_star_params.count; i++) {
                m_stars->Indices.push_back(i * 4 + 0);