--- /dev/null
+trans_alphach_ref
--- /dev/null
+\r
+uniform sampler2D myTexture;\r
+uniform vec4 skyBgColor;\r
+uniform float fogDistance;\r
+\r
+varying vec3 vPosition;\r
+\r
+void main (void)\r
+{\r
+ //vec4 col = vec4(1.0, 0.0, 0.0, 1.0);\r
+ vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0]));\r
+ float a = col.a;\r
+ col *= gl_Color;\r
+ col = col * col; // SRGB -> Linear\r
+ col *= 1.8;\r
+ col.r = 1.0 - exp(1.0 - col.r) / exp(1.0);\r
+ col.g = 1.0 - exp(1.0 - col.g) / exp(1.0);\r
+ col.b = 1.0 - exp(1.0 - col.b) / exp(1.0);\r
+ col = sqrt(col); // Linear -> SRGB\r
+ if(fogDistance != 0.0){\r
+ float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));\r
+ col = mix(col, skyBgColor, d);\r
+ }\r
+ gl_FragColor = vec4(col.r, col.g, col.b, a);\r
+}\r
--- /dev/null
+\r
+uniform mat4 mWorldViewProj;\r
+uniform mat4 mInvWorld;\r
+uniform mat4 mTransWorld;\r
+\r
+varying vec3 vPosition;\r
+\r
+void main(void)\r
+{\r
+ gl_Position = mWorldViewProj * gl_Vertex;\r
+\r
+ vPosition = (mWorldViewProj * gl_Vertex).xyz;\r
+\r
+ if(gl_Normal.y > 0.5)\r
+ gl_FrontColor = gl_BackColor = gl_Color;\r
+ else\r
+ gl_FrontColor = gl_BackColor = gl_Color * 0.7;\r
+\r
+ /*if(gl_Normal.y > 0.5)\r
+ gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0);\r
+ else\r
+ gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0) * 0.7;*/\r
+\r
+ gl_TexCoord[0] = gl_MultiTexCoord0;\r
+}\r
--- /dev/null
+trans_alphach
--- /dev/null
+\r
+uniform sampler2D myTexture;\r
+uniform float fogDistance;\r
+\r
+varying vec3 vPosition;\r
+\r
+void main (void)\r
+{\r
+ vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0]));\r
+ col *= gl_Color;\r
+ float a = gl_Color.a;\r
+ col = col * col; // SRGB -> Linear\r
+ col *= 1.8;\r
+ col.r = 1.0 - exp(1.0 - col.r) / exp(1.0);\r
+ col.g = 1.0 - exp(1.0 - col.g) / exp(1.0);\r
+ col.b = 1.0 - exp(1.0 - col.b) / exp(1.0);\r
+ col = sqrt(col); // Linear -> SRGB\r
+ if(fogDistance != 0.0){\r
+ float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));\r
+ a = mix(a, 0.0, d);\r
+ }\r
+ gl_FragColor = vec4(col.r, col.g, col.b, a);\r
+}\r
--- /dev/null
+\r
+uniform mat4 mWorldViewProj;\r
+uniform mat4 mInvWorld;\r
+uniform mat4 mTransWorld;\r
+\r
+varying vec3 vPosition;\r
+\r
+void main(void)\r
+{\r
+ vec4 pos = gl_Vertex;\r
+ pos.y -= 2.0;\r
+ gl_Position = mWorldViewProj * pos;\r
+\r
+ vPosition = (mWorldViewProj * gl_Vertex).xyz;\r
+\r
+ gl_FrontColor = gl_BackColor = gl_Color;\r
+ //gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0);\r
+\r
+ gl_TexCoord[0] = gl_MultiTexCoord0;\r
+}\r
+++ /dev/null
-!!ARBfp1.0\r
-\r
-#Input\r
-ATTRIB inTexCoord = fragment.texcoord; # texture coordinates\r
-ATTRIB inColor = fragment.color.primary; # interpolated diffuse color\r
-\r
-#Output\r
-OUTPUT outColor = result.color;\r
-\r
-TEMP texelColor;\r
-TXP texelColor, inTexCoord, texture, 2D; \r
-MUL texelColor, texelColor, inColor; # multiply with color \r
-SUB outColor, {1.0,1.0,1.0,1.0}, texelColor;\r
-MOV outColor.w, 1.0;\r
-\r
-END\r
-\r
+++ /dev/null
-\r
-uniform sampler2D myTexture;\r
-\r
-void main (void)\r
-{\r
- vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0]));\r
- col *= gl_Color;\r
- gl_FragColor = vec4(1.0-col.r, 1.0-col.g, 1.0-col.b, 1.0);\r
-}\r
+++ /dev/null
-!!ARBvp1.0\r
-\r
-#input\r
-ATTRIB InPos = vertex.position;\r
-ATTRIB InColor = vertex.color;\r
-ATTRIB InNormal = vertex.normal;\r
-ATTRIB InTexCoord = vertex.texcoord;\r
-\r
-#output\r
-OUTPUT OutPos = result.position;\r
-OUTPUT OutColor = result.color;\r
-OUTPUT OutTexCoord = result.texcoord;\r
-\r
-PARAM MVP[4] = { state.matrix.mvp }; # modelViewProjection matrix.\r
-TEMP Temp;\r
-TEMP TempColor;\r
-TEMP TempCompare;\r
-\r
-#transform position to clip space \r
-DP4 Temp.x, MVP[0], InPos;\r
-DP4 Temp.y, MVP[1], InPos;\r
-DP4 Temp.z, MVP[2], InPos;\r
-DP4 Temp.w, MVP[3], InPos;\r
-\r
-# check if normal.y > 0.5\r
-SLT TempCompare, InNormal, {0.5,0.5,0.5,0.5};\r
-MUL TempCompare.z, TempCompare.y, 0.5;\r
-SUB TempCompare.x, 1.0, TempCompare.z;\r
-MOV TempCompare.y, TempCompare.x;\r
-MOV TempCompare.z, TempCompare.x;\r
-\r
-# calculate light color\r
-MUL OutColor, InColor, TempCompare;\r
-MOV OutColor.w, 1.0; # we want alpha to be always 1\r
-MOV OutTexCoord, InTexCoord; # store texture coordinate\r
-MOV OutPos, Temp;\r
-\r
-END\r
+++ /dev/null
-\r
-uniform mat4 mWorldViewProj;\r
-uniform mat4 mInvWorld;\r
-uniform mat4 mTransWorld;\r
-\r
-void main(void)\r
-{\r
- gl_Position = mWorldViewProj * gl_Vertex;\r
- \r
- if(gl_Normal.y > 0.5)\r
- gl_FrontColor = gl_BackColor = gl_Color;\r
- else\r
- gl_FrontColor = gl_BackColor = gl_Color * 0.5;\r
-\r
- gl_TexCoord[0] = gl_MultiTexCoord0;\r
-}\r
#include <IGUIButton.h>
#include <IGUIStaticText.h>
#include <IGUIFont.h>
+#include <IMaterialRendererServices.h>
#include "client.h"
#include "server.h"
#include "guiPauseMenu.h"
}
};
+class GameGlobalShaderConstantSetter : public IShaderConstantSetter
+{
+ Sky *m_sky;
+ bool *m_force_fog_off;
+ f32 *m_fog_range;
+
+public:
+ GameGlobalShaderConstantSetter(Sky *sky, bool *force_fog_off,
+ f32 *fog_range):
+ m_sky(sky),
+ m_force_fog_off(force_fog_off),
+ m_fog_range(fog_range)
+ {}
+ ~GameGlobalShaderConstantSetter() {}
+
+ virtual void onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel)
+ {
+ if(!is_highlevel)
+ return;
+
+ // Background color
+ video::SColor bgcolor = m_sky->getBgColor();
+ video::SColorf bgcolorf(bgcolor);
+ float bgcolorfa[4] = {
+ bgcolorf.r,
+ bgcolorf.g,
+ bgcolorf.b,
+ bgcolorf.a,
+ };
+ services->setPixelShaderConstant("skyBgColor", bgcolorfa, 4);
+
+ // Fog distance
+ float fog_distance = *m_fog_range;
+ if(*m_force_fog_off)
+ fog_distance = 10000*BS;
+ services->setPixelShaderConstant("fogDistance", &fog_distance, 1);
+ }
+
+private:
+ IrrlichtDevice *m_device;
+};
+
void the_game(
bool &kill,
bool random_input,
bool show_hud = true;
bool show_chat = true;
bool force_fog_off = false;
+ f32 fog_range = 100*BS;
bool disable_camera_update = false;
bool show_debug = g_settings->getBool("show_debug");
bool show_profiler_graph = false;
float time_of_day = 0;
float time_of_day_smooth = 0;
+ /*
+ Shader constants
+ */
+ shsrc->addGlobalConstantSetter(
+ new GameGlobalShaderConstantSetter(sky, &force_fog_off, &fog_range));
+
/*
Main loop
*/
Fog range
*/
- f32 fog_range;
if(farmesh)
{
fog_range = BS*farmesh_range;
Convert MeshCollector to SMesh
Also store animation info
*/
- video::E_MATERIAL_TYPE shadermat = m_gamedef->getShaderSource()->
- getShader("the_darkness_of_light").material;
+ bool enable_shaders = (g_settings->getS32("enable_shaders") > 0);
+ video::E_MATERIAL_TYPE shadermat1 = m_gamedef->getShaderSource()->
+ getShader("test_shader_1").material;
+ video::E_MATERIAL_TYPE shadermat2 = m_gamedef->getShaderSource()->
+ getShader("test_shader_2").material;
for(u32 i = 0; i < collector.prebuffers.size(); i++)
{
PreMeshBuffer &p = collector.prebuffers[i];
material.setTexture(0, p.tile.texture.atlas);
p.tile.applyMaterialOptions(material);
- //if(material.MaterialType == video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF)
- material.MaterialType = shadermat;
+ if(enable_shaders){
+ if(material.MaterialType == video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF)
+ material.MaterialType = shadermat1;
+ if(material.MaterialType == video::EMT_TRANSPARENT_VERTEX_ALPHA)
+ material.MaterialType = shadermat2;
+ }
// Create meshbuffer
#include "EShaderTypes.h"
#include "log.h"
#include "gamedef.h"
+#include "strfnd.h" // trim()
/*
A cache from shader name to shader path
ShaderCallback: Sets constants that can be used in shaders
*/
+class IShaderConstantSetterRegistry
+{
+public:
+ virtual ~IShaderConstantSetterRegistry(){};
+ virtual void onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel, const std::string &name) = 0;
+};
+
class ShaderCallback : public video::IShaderConstantSetCallBack
{
+ IShaderConstantSetterRegistry *m_scsr;
+ std::string m_name;
+
public:
- ShaderCallback(IrrlichtDevice *device): m_device(device) {}
+ ShaderCallback(IShaderConstantSetterRegistry *scsr, const std::string &name):
+ m_scsr(scsr),
+ m_name(name)
+ {}
~ShaderCallback() {}
virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData)
bool is_highlevel = userData;
+ m_scsr->onSetConstants(services, is_highlevel, m_name);
+ }
+};
+
+/*
+ MainShaderConstantSetter: Set basic constants required for almost everything
+*/
+
+class MainShaderConstantSetter : public IShaderConstantSetter
+{
+public:
+ MainShaderConstantSetter(IrrlichtDevice *device):
+ m_device(device)
+ {}
+ ~MainShaderConstantSetter() {}
+
+ virtual void onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel)
+ {
+ video::IVideoDriver *driver = services->getVideoDriver();
+ assert(driver);
+
// set inverted world matrix
core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);
invWorld.makeInverse();
ShaderSource
*/
-class ShaderSource : public IWritableShaderSource
+class ShaderSource : public IWritableShaderSource, public IShaderConstantSetterRegistry
{
public:
ShaderSource(IrrlichtDevice *device);
// Shall be called from the main thread.
void rebuildShaders();
+ void addGlobalConstantSetter(IShaderConstantSetter *setter)
+ {
+ m_global_setters.push_back(setter);
+ }
+
+ void onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel, const std::string &name);
+
private:
// The id of the thread that is allowed to use irrlicht directly
// Queued shader fetches (to be processed by the main thread)
RequestQueue<std::string, u32, u8, u8> m_get_shader_queue;
+
+ // Global constant setters
+ // TODO: Delete these in the destructor
+ core::array<IShaderConstantSetter*> m_global_setters;
};
IWritableShaderSource* createShaderSource(IrrlichtDevice *device)
{
assert(m_device);
- m_shader_callback = new ShaderCallback(device);
+ m_shader_callback = new ShaderCallback(this, "default");
m_shaderinfo_cache_mutex.Init();
// Add a dummy ShaderInfo as the first index, named ""
m_shaderinfo_cache.push_back(ShaderInfo());
m_name_to_id[""] = 0;
+
+ // Add main global constant setter
+ addGlobalConstantSetter(new MainShaderConstantSetter(device));
}
ShaderSource::~ShaderSource()
m_shader_callback, &m_sourcecache);
}
}
+
+void ShaderSource::onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel, const std::string &name)
+{
+ for(u32 i=0; i<m_global_setters.size(); i++){
+ IShaderConstantSetter *setter = m_global_setters[i];
+ setter->onSetConstants(services, is_highlevel);
+ }
+}
ShaderInfo generate_shader(std::string name, IrrlichtDevice *device,
video::IShaderConstantSetCallBack *callback,
/*
Get the base material
*/
- std::string base_material_name = sourcecache->getOrLoad(name, "base.txt");
+ std::string base_material_name =
+ trim(sourcecache->getOrLoad(name, "base.txt"));
for(s32 i = 0; video::sBuiltInMaterialTypeNames[i] != 0; i++){
if(video::sBuiltInMaterialTypeNames[i] == base_material_name){
shaderinfo.material = (video::E_MATERIAL_TYPE) i;
ShaderInfo(): name(""), material(video::EMT_SOLID) {}
};
+/*
+ Setter of constants for shaders
+*/
+
+namespace irr { namespace video {
+ class IMaterialRendererServices;
+} }
+
+class IShaderConstantSetter
+{
+public:
+ virtual ~IShaderConstantSetter(){};
+ virtual void onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel) = 0;
+};
+
/*
ShaderSource creates and caches shaders.
*/
virtual void insertSourceShader(const std::string &name_of_shader,
const std::string &filename, const std::string &program)=0;
virtual void rebuildShaders()=0;
+ virtual void addGlobalConstantSetter(IShaderConstantSetter *setter)=0;
};
IWritableShaderSource* createShaderSource(IrrlichtDevice *device);