1515 */
1616package org .terasology .rendering .shader ;
1717
18+ import org .lwjgl .opengl .GL11 ;
1819import org .lwjgl .opengl .GL13 ;
20+ import org .terasology .asset .Assets ;
1921import org .terasology .config .Config ;
2022import org .terasology .editor .EditorRange ;
2123import org .terasology .engine .CoreRegistry ;
2224import org .terasology .rendering .assets .material .Material ;
25+ import org .terasology .rendering .assets .texture .Texture ;
2326import org .terasology .rendering .cameras .Camera ;
2427import org .terasology .rendering .opengl .DefaultRenderingProcess ;
2528import org .terasology .rendering .world .WorldRenderer ;
2629import org .terasology .world .WorldProvider ;
2730
31+ import javax .vecmath .Matrix4f ;
2832import javax .vecmath .Vector3f ;
2933import javax .vecmath .Vector4f ;
3034
35+ import static org .lwjgl .opengl .GL11 .glBindTexture ;
36+
3137/**
3238 * Shader parameters for the Combine shader program.
3339 *
@@ -53,6 +59,15 @@ public class ShaderParametersCombine extends ShaderParametersBase {
5359 @ EditorRange (min = 0.01f , max = 1.0f )
5460 private float volFogHeightFalloff = 0.05f ;
5561
62+ @ EditorRange (min = 0.01f , max = 1.0f )
63+ private float volLightingDensity = 0.5f ;
64+ @ EditorRange (min = 0.001f , max = 0.1f )
65+ private float volLightingDecay = 0.005f ;
66+ @ EditorRange (min = -1.0f , max = -0.8f )
67+ private float volLightingScattering = -0.9f ;
68+ @ EditorRange (min = 0.0f , max = 10000.0f )
69+ private float volLightingPhi = 1000.0f ;
70+
5671 @ Override
5772 public void applyParameters (Material program ) {
5873 super .applyParameters (program );
@@ -83,18 +98,57 @@ public void applyParameters(Material program) {
8398 Camera activeCamera = CoreRegistry .get (WorldRenderer .class ).getActiveCamera ();
8499 if (activeCamera != null ) {
85100 program .setMatrix4 ("invViewProjMatrix" , activeCamera .getInverseViewProjectionMatrix (), true );
101+ }
102+
103+ Vector3f fogWorldPosition = new Vector3f (0.0f , 32.0f - activeCamera .getPosition ().y , 0.0f );
104+ program .setFloat3 ("fogWorldPosition" , fogWorldPosition .x , fogWorldPosition .y , fogWorldPosition .z , true );
86105
87- Vector3f fogWorldPosition = new Vector3f (activeCamera .getPosition ().x , 32.0f , activeCamera .getPosition ().y );
88- fogWorldPosition .sub (activeCamera .getPosition ());
89- program .setFloat3 ("fogWorldPosition" , fogWorldPosition .x , fogWorldPosition .y , fogWorldPosition .z , true );
106+ // Fog density is set according to the fog density provided by the world
107+ // TODO: The 50% percent limit shouldn't be hardcoded
108+ final float worldFog = Math .min (CoreRegistry .get (WorldProvider .class ).getFog (activeCamera .getPosition ()), 0.5f );
109+ program .setFloat4 ("volumetricFogSettings" , volFogDensityAtViewer , volFogGlobalDensity , volFogHeightFalloff , worldFog );
110+ }
90111
91- // Fog density is set according to the fog density provided by the world
92- // TODO: The 50% percent limit shouldn't be hardcoded
93- final float worldFog = Math .min (CoreRegistry .get (WorldProvider .class ).getFog (activeCamera .getPosition ()), 0.5f );
94- program .setFloat4 ("volumetricFogSettings" , volFogDensityAtViewer , volFogGlobalDensity , volFogHeightFalloff , worldFog );
112+ if (CoreRegistry .get (Config .class ).getRendering ().isVolumetricFog ()
113+ || CoreRegistry .get (Config .class ).getRendering ().isVolumetricLighting ()) {
114+ Camera activeCamera = CoreRegistry .get (WorldRenderer .class ).getActiveCamera ();
115+ if (activeCamera != null ) {
116+ program .setMatrix4 ("invViewProjMatrix" , activeCamera .getInverseViewProjectionMatrix (), true );
95117 }
118+ }
96119
120+ if (CoreRegistry .get (Config .class ).getRendering ().isVolumetricLighting ()) {
121+ GL13 .glActiveTexture (GL13 .GL_TEXTURE0 + texId );
122+ DefaultRenderingProcess .getInstance ().bindFboDepthTexture ("sceneShadowMap" );
123+ program .setInt ("texSceneShadowMap" , texId ++, true );
97124
125+ Camera activeCamera = CoreRegistry .get (WorldRenderer .class ).getActiveCamera ();
126+ Camera lightCamera = CoreRegistry .get (WorldRenderer .class ).getLightCamera ();
127+ if (lightCamera != null && activeCamera != null ) {
128+ program .setMatrix4 ("lightViewMatrix" , lightCamera .getViewMatrix (), true );
129+ program .setMatrix4 ("lightProjMatrix" , lightCamera .getProjectionMatrix (), true );
130+ program .setMatrix4 ("lightViewProjMatrix" , lightCamera .getViewProjectionMatrix (), true );
131+
132+ program .setMatrix4 ("viewMatrix" , activeCamera .getViewMatrix (), true );
133+
134+ Matrix4f invViewMatrix = new Matrix4f ();
135+ invViewMatrix .invert (activeCamera .getViewMatrix ());
136+
137+ program .setMatrix4 ("invViewMatrix" , invViewMatrix , true );
138+
139+ Vector3f activeCameraToLightSpace = new Vector3f ();
140+ activeCameraToLightSpace .sub (activeCamera .getPosition (), lightCamera .getPosition ());
141+ program .setFloat3 ("activeCameraToLightSpace" , activeCameraToLightSpace .x , activeCameraToLightSpace .y , activeCameraToLightSpace .z , true );
142+ }
143+
144+ program .setFloat4 ("volumetricLightingSettings" , volLightingDensity , volLightingDecay , volLightingPhi , volLightingScattering , true );
145+
146+ if (CoreRegistry .get (Config .class ).getRendering ().isCloudShadows ()) {
147+ Texture clouds = Assets .getTexture ("engine:perlinNoiseTileable" );
148+ GL13 .glActiveTexture (GL13 .GL_TEXTURE0 + texId );
149+ glBindTexture (GL11 .GL_TEXTURE_2D , clouds .getId ());
150+ program .setInt ("texSceneClouds" , texId ++, true );
151+ }
98152 }
99153
100154 DefaultRenderingProcess .FBO sceneTransparent = DefaultRenderingProcess .getInstance ().getFBO ("sceneTransparent" );
@@ -120,14 +174,16 @@ public void applyParameters(Material program) {
120174 program .setFloat ("outlineThickness" , outlineThickness , true );
121175 }
122176
123- GL13 .glActiveTexture (GL13 .GL_TEXTURE0 + texId );
124- DefaultRenderingProcess .getInstance ().bindFboTexture ("sceneSkyBand1" );
125- program .setInt ("texSceneSkyBand" , texId ++, true );
126-
127- Vector4f skyInscatteringSettingsFrag = new Vector4f ();
128- skyInscatteringSettingsFrag .y = skyInscatteringStrength ;
129- skyInscatteringSettingsFrag .z = skyInscatteringLength ;
130- skyInscatteringSettingsFrag .w = skyInscatteringThreshold ;
131- program .setFloat4 ("skyInscatteringSettingsFrag" , skyInscatteringSettingsFrag , true );
177+ if (CoreRegistry .get (Config .class ).getRendering ().isInscattering ()) {
178+ GL13 .glActiveTexture (GL13 .GL_TEXTURE0 + texId );
179+ DefaultRenderingProcess .getInstance ().bindFboTexture ("sceneSkyBand1" );
180+ program .setInt ("texSceneSkyBand" , texId ++, true );
181+
182+ Vector4f skyInscatteringSettingsFrag = new Vector4f ();
183+ skyInscatteringSettingsFrag .y = skyInscatteringStrength ;
184+ skyInscatteringSettingsFrag .z = skyInscatteringLength ;
185+ skyInscatteringSettingsFrag .w = skyInscatteringThreshold ;
186+ program .setFloat4 ("skyInscatteringSettingsFrag" , skyInscatteringSettingsFrag , true );
187+ }
132188 }
133189}
0 commit comments