Skip to content

Commit cdcf7dc

Browse files
numberZerolhofhansl
authored andcommittedNov 26, 2020
Sky: support GLES2
IrrLicht built-in shader is broken, have to write my own
1 parent be59668 commit cdcf7dc

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
uniform vec4 starColor;
2+
3+
void main(void)
4+
{
5+
gl_FragColor = starColor;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
void main(void)
2+
{
3+
gl_Position = mWorldViewProj * inVertexPosition;
4+
}

‎src/client/game.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
424424
CachedVertexShaderSetting<float> m_animation_timer_vertex;
425425
CachedPixelShaderSetting<float> m_animation_timer_pixel;
426426
CachedPixelShaderSetting<float, 3> m_day_light;
427+
CachedPixelShaderSetting<float, 4> m_star_color;
427428
CachedPixelShaderSetting<float, 3> m_eye_position_pixel;
428429
CachedVertexShaderSetting<float, 3> m_eye_position_vertex;
429430
CachedPixelShaderSetting<float, 3> m_minimap_yaw;
@@ -456,6 +457,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
456457
m_animation_timer_vertex("animationTimer"),
457458
m_animation_timer_pixel("animationTimer"),
458459
m_day_light("dayLight"),
460+
m_star_color("starColor"),
459461
m_eye_position_pixel("eyePosition"),
460462
m_eye_position_vertex("eyePosition"),
461463
m_minimap_yaw("yawVec"),
@@ -507,6 +509,10 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
507509
sunlight.b };
508510
m_day_light.set(dnc, services);
509511

512+
video::SColorf star_color = m_sky->getCurrentStarColor();
513+
float clr[4] = {star_color.r, star_color.g, star_color.b, star_color.a};
514+
m_star_color.set(clr, services);
515+
510516
u32 animation_timer = porting::getTimeMs() % 1000000;
511517
float animation_timer_f = (float)animation_timer / 100000.f;
512518
m_animation_timer_vertex.set(&animation_timer_f, services);
@@ -1363,7 +1369,7 @@ bool Game::createClient(const GameStartData &start_data)
13631369

13641370
/* Skybox
13651371
*/
1366-
sky = new Sky(-1, texture_src);
1372+
sky = new Sky(-1, texture_src, shader_src);
13671373
scsf->setSky(sky);
13681374
skybox = NULL; // This is used/set later on in the main run loop
13691375

‎src/client/sky.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,20 @@ static video::SMaterial baseMaterial() {
5151
return mat;
5252
};
5353

54-
Sky::Sky(s32 id, ITextureSource *tsrc) :
54+
Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
5555
scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
5656
RenderingEngine::get_scene_manager(), id)
5757
{
5858
setAutomaticCulling(scene::EAC_OFF);
5959
m_box.MaxEdge.set(0, 0, 0);
6060
m_box.MinEdge.set(0, 0, 0);
6161

62+
m_enable_shaders = g_settings->getBool("enable_shaders");
63+
6264
// Create materials
6365

6466
m_materials[0] = baseMaterial();
65-
m_materials[0].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67+
m_materials[0].MaterialType = ssrc->getShaderInfo(ssrc->getShader("stars_shader", TILE_MATERIAL_ALPHA, 0)).material;
6668
m_materials[0].Lighting = true;
6769
m_materials[0].ColorMaterial = video::ECM_NONE;
6870

@@ -694,12 +696,11 @@ void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
694696

695697
float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
696698
float starbrightness = (0.25f - fabsf(tod)) * 20.0f;
697-
int alpha = clamp<int>(starbrightness * m_star_params.starcolor.getAlpha(), 0, 255);
698-
if (!alpha) // Stars are only drawn when not fully transparent
699+
m_star_color = m_star_params.starcolor;
700+
m_star_color.a = clamp(starbrightness * m_star_color.a, 0.0f, 1.0f);
701+
if (m_star_color.a <= 0.0f) // Stars are only drawn when not fully transparent
699702
return;
700-
701-
m_materials[0].DiffuseColor = video::SColor(alpha, 0, 0, 0);
702-
m_materials[0].EmissiveColor = m_star_params.starcolor;
703+
m_materials[0].DiffuseColor = m_materials[0].EmissiveColor = m_star_color.toSColor();
703704
auto sky_rotation = core::matrix4().setRotationAxisRadians(2.0f * M_PI * (wicked_time_of_day - 0.25f), v3f(0.0f, 0.0f, 1.0f));
704705
auto world_matrix = driver->getTransform(video::ETS_WORLD);
705706
driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation);

‎src/client/sky.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include "camera.h"
2323
#include "irrlichttypes_extrabloated.h"
2424
#include "irr_ptr.h"
25+
#include "shader.h"
2526
#include "skyparams.h"
2627

2728
#pragma once
@@ -35,7 +36,7 @@ class Sky : public scene::ISceneNode
3536
{
3637
public:
3738
//! constructor
38-
Sky(s32 id, ITextureSource *tsrc);
39+
Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc);
3940

4041
virtual void OnRegisterSceneNode();
4142

@@ -102,6 +103,8 @@ class Sky : public scene::ISceneNode
102103
void clearSkyboxTextures() { m_sky_params.textures.clear(); }
103104
void addTextureToSkybox(std::string texture, int material_id,
104105
ITextureSource *tsrc);
106+
const video::SColorf &getCurrentStarColor() const { return m_star_color; }
107+
105108
private:
106109
aabb3f m_box;
107110
video::SMaterial m_materials[SKY_MATERIAL_COUNT];
@@ -155,6 +158,7 @@ class Sky : public scene::ISceneNode
155158
bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
156159
bool m_directional_colored_fog;
157160
bool m_in_clouds = true; // Prevent duplicating bools to remember old values
161+
bool m_enable_shaders = false;
158162

159163
video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
160164
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
181185

182186
u64 m_seed = 0;
183187
irr_ptr<scene::SMeshBuffer> m_stars;
188+
video::SColorf m_star_color;
184189

185190
video::ITexture *m_sun_texture;
186191
video::ITexture *m_moon_texture;
187192
video::ITexture *m_sun_tonemap;
188193
video::ITexture *m_moon_tonemap;
189194

190195
void updateStars();
191-
void updateStarsColor(video::SColor color);
192196

193197
void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
194198
const video::SColor &suncolor2, float wicked_time_of_day);

0 commit comments

Comments
 (0)
Please sign in to comment.