Sky: support GLES2

IrrLicht built-in shader is broken, have to write my own
This commit is contained in:
numzero 2020-11-22 18:25:41 +03:00 committed by lhofhansl
parent be59668f47
commit cdcf7dca7c
5 changed files with 31 additions and 10 deletions

@ -0,0 +1,6 @@
uniform vec4 starColor;
void main(void)
{
gl_FragColor = starColor;
}

@ -0,0 +1,4 @@
void main(void)
{
gl_Position = mWorldViewProj * inVertexPosition;
}

@ -424,6 +424,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
CachedVertexShaderSetting<float> m_animation_timer_vertex; CachedVertexShaderSetting<float> m_animation_timer_vertex;
CachedPixelShaderSetting<float> m_animation_timer_pixel; CachedPixelShaderSetting<float> m_animation_timer_pixel;
CachedPixelShaderSetting<float, 3> m_day_light; CachedPixelShaderSetting<float, 3> m_day_light;
CachedPixelShaderSetting<float, 4> m_star_color;
CachedPixelShaderSetting<float, 3> m_eye_position_pixel; CachedPixelShaderSetting<float, 3> m_eye_position_pixel;
CachedVertexShaderSetting<float, 3> m_eye_position_vertex; CachedVertexShaderSetting<float, 3> m_eye_position_vertex;
CachedPixelShaderSetting<float, 3> m_minimap_yaw; CachedPixelShaderSetting<float, 3> m_minimap_yaw;
@ -456,6 +457,7 @@ public:
m_animation_timer_vertex("animationTimer"), m_animation_timer_vertex("animationTimer"),
m_animation_timer_pixel("animationTimer"), m_animation_timer_pixel("animationTimer"),
m_day_light("dayLight"), m_day_light("dayLight"),
m_star_color("starColor"),
m_eye_position_pixel("eyePosition"), m_eye_position_pixel("eyePosition"),
m_eye_position_vertex("eyePosition"), m_eye_position_vertex("eyePosition"),
m_minimap_yaw("yawVec"), m_minimap_yaw("yawVec"),
@ -507,6 +509,10 @@ public:
sunlight.b }; sunlight.b };
m_day_light.set(dnc, services); 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; u32 animation_timer = porting::getTimeMs() % 1000000;
float animation_timer_f = (float)animation_timer / 100000.f; float animation_timer_f = (float)animation_timer / 100000.f;
m_animation_timer_vertex.set(&animation_timer_f, services); m_animation_timer_vertex.set(&animation_timer_f, services);
@ -1363,7 +1369,7 @@ bool Game::createClient(const GameStartData &start_data)
/* Skybox /* Skybox
*/ */
sky = new Sky(-1, texture_src); sky = new Sky(-1, texture_src, shader_src);
scsf->setSky(sky); scsf->setSky(sky);
skybox = NULL; // This is used/set later on in the main run loop skybox = NULL; // This is used/set later on in the main run loop

@ -51,7 +51,7 @@ static video::SMaterial baseMaterial() {
return mat; return mat;
}; };
Sky::Sky(s32 id, ITextureSource *tsrc) : Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(), scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
RenderingEngine::get_scene_manager(), id) RenderingEngine::get_scene_manager(), id)
{ {
@ -59,10 +59,12 @@ Sky::Sky(s32 id, ITextureSource *tsrc) :
m_box.MaxEdge.set(0, 0, 0); m_box.MaxEdge.set(0, 0, 0);
m_box.MinEdge.set(0, 0, 0); m_box.MinEdge.set(0, 0, 0);
m_enable_shaders = g_settings->getBool("enable_shaders");
// Create materials // Create materials
m_materials[0] = baseMaterial(); 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].Lighting = true;
m_materials[0].ColorMaterial = video::ECM_NONE; 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 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; float starbrightness = (0.25f - fabsf(tod)) * 20.0f;
int alpha = clamp<int>(starbrightness * m_star_params.starcolor.getAlpha(), 0, 255); m_star_color = m_star_params.starcolor;
if (!alpha) // Stars are only drawn when not fully transparent 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; return;
m_materials[0].DiffuseColor = m_materials[0].EmissiveColor = m_star_color.toSColor();
m_materials[0].DiffuseColor = video::SColor(alpha, 0, 0, 0);
m_materials[0].EmissiveColor = m_star_params.starcolor;
auto sky_rotation = core::matrix4().setRotationAxisRadians(2.0f * M_PI * (wicked_time_of_day - 0.25f), v3f(0.0f, 0.0f, 1.0f)); 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); auto world_matrix = driver->getTransform(video::ETS_WORLD);
driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation); driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation);

@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "camera.h" #include "camera.h"
#include "irrlichttypes_extrabloated.h" #include "irrlichttypes_extrabloated.h"
#include "irr_ptr.h" #include "irr_ptr.h"
#include "shader.h"
#include "skyparams.h" #include "skyparams.h"
#pragma once #pragma once
@ -35,7 +36,7 @@ class Sky : public scene::ISceneNode
{ {
public: public:
//! constructor //! constructor
Sky(s32 id, ITextureSource *tsrc); Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc);
virtual void OnRegisterSceneNode(); virtual void OnRegisterSceneNode();
@ -102,6 +103,8 @@ public:
void clearSkyboxTextures() { m_sky_params.textures.clear(); } void clearSkyboxTextures() { m_sky_params.textures.clear(); }
void addTextureToSkybox(std::string texture, int material_id, void addTextureToSkybox(std::string texture, int material_id,
ITextureSource *tsrc); ITextureSource *tsrc);
const video::SColorf &getCurrentStarColor() const { return m_star_color; }
private: private:
aabb3f m_box; aabb3f m_box;
video::SMaterial m_materials[SKY_MATERIAL_COUNT]; video::SMaterial m_materials[SKY_MATERIAL_COUNT];
@ -155,6 +158,7 @@ private:
bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
bool m_directional_colored_fog; bool m_directional_colored_fog;
bool m_in_clouds = true; // Prevent duplicating bools to remember old values 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_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); video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
@ -181,6 +185,7 @@ private:
u64 m_seed = 0; u64 m_seed = 0;
irr_ptr<scene::SMeshBuffer> m_stars; irr_ptr<scene::SMeshBuffer> m_stars;
video::SColorf m_star_color;
video::ITexture *m_sun_texture; video::ITexture *m_sun_texture;
video::ITexture *m_moon_texture; video::ITexture *m_moon_texture;
@ -188,7 +193,6 @@ private:
video::ITexture *m_moon_tonemap; video::ITexture *m_moon_tonemap;
void updateStars(); void updateStars();
void updateStarsColor(video::SColor color);
void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor, void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
const video::SColor &suncolor2, float wicked_time_of_day); const video::SColor &suncolor2, float wicked_time_of_day);