Skip to content
Permalink
Browse files

Sky: support GLES2

IrrLicht built-in shader is broken, have to write my own
  • Loading branch information
numberZero authored and lhofhansl committed Nov 26, 2020
1 parent be59668 commit cdcf7dca7c9afb329d49f2016964f77ac379ed67
@@ -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;
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

@@ -51,18 +51,20 @@ 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)
{
setAutomaticCulling(scene::EAC_OFF);
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);
@@ -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,14 +185,14 @@ 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;
video::ITexture *m_sun_tonemap;
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);

0 comments on commit cdcf7dc

Please sign in to comment.