diff --git a/core/assets/config_editor.json b/core/assets/config_editor.json index f1c34c9..4f86963 100644 --- a/core/assets/config_editor.json +++ b/core/assets/config_editor.json @@ -1,8 +1,12 @@ { - generateWireframe: true, - renderStaticOctree: false, - renderBoundingBox: false, - debug: true, - nearShadowMapSize: 1024, - farShadowMapSize: 1024, + Debug: true, + GenerateWireframe: true, + NearShadowMapSize: { + class: java.lang.Integer, + value: 1024 + }, + FarShadowMapSize: { + class: java.lang.Integer, + value: 1024 + }, } diff --git a/core/assets/config_game.json b/core/assets/config_game.json index 5927e55..88b1d3a 100644 --- a/core/assets/config_game.json +++ b/core/assets/config_game.json @@ -1,8 +1,20 @@ { - debug: false, - resolutionWidth: 1360, - resolutionHeight: 768, - fullscreen: false, - nearShadowMapSize: 1024, - farShadowMapSize: 1024, + Debug: false, + ResolutionWidth: { + class: java.lang.Integer, + value: 1360 + }, + ResolutionHeight: { + class: java.lang.Integer, + value: 768 + }, + Fullscreen: false, + NearShadowMapSize: { + class: java.lang.Integer, + value: 1024 + }, + FarShadowMapSize: { + class: java.lang.Integer, + value: 1024 + }, } diff --git a/core/assets/graphics/shaders/helpers/shadow_map.glsl b/core/assets/graphics/shaders/helpers/shadow_map.glsl index 6b7145e..12de79b 100644 --- a/core/assets/graphics/shaders/helpers/shadow_map.glsl +++ b/core/assets/graphics/shaders/helpers/shadow_map.glsl @@ -21,15 +21,32 @@ vec4 transformFrom0To1Range(vec4 vector) { return vector * 0.5f + 0.5f; } +float chebshevComputeQuantity(vec4 positionInLightSpace, sampler2D depthMap, float bias) { + float currentDepth = (positionInLightSpace.z / positionInLightSpace.w )* 0.5f + 0.5f; + vec2 projCoords = (positionInLightSpace.xy / positionInLightSpace.w) * 0.5f + 0.5f; + // get two moments + vec2 moments = texture2D(depthMap, projCoords.xy).rg; + if (currentDepth <= moments.x) { + return 1.0f; + } else { + float E_x2 = moments.y; + float Ex_2 = moments.x * moments.x; + float variance = E_x2 - (Ex_2); + float t = currentDepth - moments.x; + float pMax = variance / (variance + t*t); + return pMax; + } +} + float shadowCalculation(vec4 positionInLightSpace, sampler2D depthMap, float bias) { // perform perspective divide - vec3 projCoords = positionInLightSpace.xyz / positionInLightSpace.w; + vec3 projCoords = (positionInLightSpace.xyz / positionInLightSpace.w) * 0.5f + 0.5f; // Transform to [0,1] range - positionInLightSpace = transformFrom0To1Range(positionInLightSpace); + //projCoords = transformFrom0To1Range(positionInLightSpace); // Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) - float closestDepth = step(positionInLightSpace.z, unpack(texture2D(depthMap, positionInLightSpace.xy))); + float closestDepth = step(projCoords.z, unpack(texture2D(depthMap, projCoords.xy))); // Get depth of current fragment from light's perspective - float currentDepth = positionInLightSpace.z; + float currentDepth = projCoords.z; // Check whether current frag pos is in shadow /* vec2 texelSize = vec2(0.0009765f); diff --git a/core/assets/graphics/shaders/ps-bloom.frag.glsl b/core/assets/graphics/shaders/ps-bloom.frag.glsl index 60b8bce..0a276e6 100644 --- a/core/assets/graphics/shaders/ps-bloom.frag.glsl +++ b/core/assets/graphics/shaders/ps-bloom.frag.glsl @@ -2,27 +2,27 @@ varying vec2 v_texCoords; void main() { vec4 sum = vec4(0.0); - float blurSize = 0.002f; -/* - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 4.0*blurSize)) * 0.05; + float blurSize = 0.003f; + + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 4.0*blurSize)) * 0.05; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 3.0*blurSize)) * 0.09; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 2.0*blurSize)) * 0.12; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 2.0*blurSize)) * 0.12; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - blurSize)) * 0.15; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y)) * 0.16; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y)) * 0.16; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + blurSize)) * 0.15; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 2.0*blurSize)) * 0.12; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 2.0*blurSize)) * 0.12; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 3.0*blurSize)) * 0.09; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 4.0*blurSize)) * 0.05; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 4.0*blurSize)) * 0.05; + -*/ sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 4.0*blurSize, v_texCoords.y)) * 0.05; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 3.0*blurSize, v_texCoords.y)) * 0.1; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 3.0*blurSize, v_texCoords.y)) * 0.1; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 2.0*blurSize, v_texCoords.y)) * 0.12; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - blurSize, v_texCoords.y)) * 0.15; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - blurSize, v_texCoords.y)) * 0.15; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y)) * 0.16; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + blurSize, v_texCoords.y)) * 0.15; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + blurSize, v_texCoords.y)) * 0.15; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 2.0*blurSize, v_texCoords.y)) * 0.12; - sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 3.0*blurSize, v_texCoords.y)) * 0.1; + //sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 3.0*blurSize, v_texCoords.y)) * 0.1; sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 4.0*blurSize, v_texCoords.y)) * 0.05; gl_FragColor = sum; diff --git a/core/assets/graphics/shaders/terrain-depthmap.frag.glsl b/core/assets/graphics/shaders/terrain-depthmap.frag.glsl index 6961889..7e9955d 100644 --- a/core/assets/graphics/shaders/terrain-depthmap.frag.glsl +++ b/core/assets/graphics/shaders/terrain-depthmap.frag.glsl @@ -3,7 +3,6 @@ varying vec2 v_uvStart; varying vec2 v_uvMul; varying vec2 v_textCoord; -varying float v_depth; void main() { vec2 tilingTextCord = (fract(v_textCoord) * v_uvMul) + v_uvStart; @@ -12,5 +11,18 @@ void main() { discard; } - gl_FragColor = pack(v_depth); + //homogeneus to texture cordinate system([-1,1]) + + float depth = v_position.z / v_position.w; + depth = depth * 0.5f + 0.5f; + + float M1 = depth; //moment 1 + float M2 = depth * depth; + + float dx = dFdx(depth); + float dy = dFdy(depth); + + M2 += 0.25f*(dx*dx+dy*dy); + + gl_FragColor = vec4(M1, M2, 0.0f, 1.0f); } diff --git a/core/assets/graphics/shaders/terrain-depthmap.vert.glsl b/core/assets/graphics/shaders/terrain-depthmap.vert.glsl index 05b83c0..f2fb383 100644 --- a/core/assets/graphics/shaders/terrain-depthmap.vert.glsl +++ b/core/assets/graphics/shaders/terrain-depthmap.vert.glsl @@ -8,7 +8,6 @@ varying vec4 v_position; varying vec2 v_uvStart; varying vec2 v_uvMul; -varying float v_depth; void main() { float waviness = a_material.a; @@ -21,6 +20,5 @@ void main() { vec4 position = u_projectionMatrix * v_position; - v_depth = v_position.z * 0.5f + 0.5f; gl_Position = position; } diff --git a/core/src/macbury/forge/Config.java b/core/src/macbury/forge/Config.java index ef3fcb7..4035844 100644 --- a/core/src/macbury/forge/Config.java +++ b/core/src/macbury/forge/Config.java @@ -2,6 +2,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.utils.Json; +import com.badlogic.gdx.utils.ObjectMap; import macbury.forge.utils.KVStorage; import java.io.IOException; @@ -11,32 +12,39 @@ /** * Created by macbury on 18.10.14. */ -public class Config { - +public class Config extends KVStorage { + public enum Key { + ResolutionWidth, ResolutionHeight, Fullscreen, Debug, NearShadowMapSize, FarShadowMapSize, BloomTextureSize, ReflectionBufferSize, RefractionBufferSize, + GenerateWireframe, NearShadowDistance, RenderDebug, RenderDynamicOctree, RenderStaticOctree, RenderBoundingBox, RenderBulletDebug, Editor, + } public enum RenderDebug { Textured, Wireframe, Normals, Lighting } - public int bloomTextureSize = 256; - public int resolutionWidth = 1360; - public int resolutionHeight = 768; - public boolean fullscreen = false; - - public int reflectionBufferSize = 512; - public int refractionBufferSize = 512; - public int farShadowMapSize = 512; - public int nearShadowMapSize = 1024; - public float nearShadowDistance = 10; - - public boolean generateWireframe = false; - public boolean debug = false; - public RenderDebug renderDebug = RenderDebug.Textured; - public boolean renderDynamicOctree = false; - public boolean renderStaticOctree = false; - public boolean renderBoundingBox = false; - public boolean cacheGeometry = false; - public boolean renderBulletDebug = false; - public String editor = "atom"; + + @Override + public void setDefaults() { + putInt(Key.NearShadowMapSize, 1024); + putInt(Key.NearShadowMapSize, 1024); + putInt(Key.NearShadowMapSize, 1024); + putInt(Key.FarShadowMapSize, 1024); + putInt(Key.ResolutionWidth, 1360); + putInt(Key.ResolutionHeight, 768); + putInt(Key.BloomTextureSize, 512); + putInt(Key.ReflectionBufferSize, 512); + putInt(Key.RefractionBufferSize, 512); + putInt(Key.NearShadowDistance, 10); + putBool(Key.Fullscreen, false); + putBool(Key.Debug, false); + putBool(Key.RenderDynamicOctree, false); + putBool(Key.RenderStaticOctree, false); + putBool(Key.RenderBoundingBox, false); + putBool(Key.RenderBulletDebug, false); + putBool(Key.GenerateWireframe, false); + putString(Key.Editor, "atom"); + putRenderDebug(RenderDebug.Textured); + } + public static Config load(String namespace) { Json json = new Json(); @@ -47,17 +55,21 @@ public static Config load(String namespace) { e.printStackTrace(); } String text = new String(encoded); - return json.fromJson(Config.class, text); + ObjectMap values = json.fromJson(ObjectMap.class, text); + Config config = new Config(); + for(String rawKey : values.keys()) { + Key key = Key.valueOf(rawKey); + config.putObject(key, values.get(rawKey)); + } + return config; } - public void setRenderDebugTo(RenderDebug debug) { - renderDebug = debug; - Gdx.app.postRunnable(new Runnable() { - @Override - public void run() { - ForgE.shaders.reload(); - } - }); + public void putRenderDebug(RenderDebug renderDebug) { + putObject(Key.RenderDebug, renderDebug); + } + public RenderDebug getRenderDebug() { + return (RenderDebug)getObject(Key.RenderDebug); } + } diff --git a/core/src/macbury/forge/graphics/batch/VoxelBatch.java b/core/src/macbury/forge/graphics/batch/VoxelBatch.java index 31d3bb0..c359fa1 100644 --- a/core/src/macbury/forge/graphics/batch/VoxelBatch.java +++ b/core/src/macbury/forge/graphics/batch/VoxelBatch.java @@ -112,7 +112,7 @@ public void add(final RenderableProvider renderableProvider) { public void render(LevelEnv env) { if (camera == null) throw new GdxRuntimeException("Call begin() first."); sortUnlessNotSorted(); - if (ForgE.config.renderDebug == Config.RenderDebug.Wireframe) { + if (ForgE.config.getRenderDebug() == Config.RenderDebug.Wireframe) { renderWireframe(); } else { renderTextured(env); diff --git a/core/src/macbury/forge/graphics/builders/TerrainBuilder.java b/core/src/macbury/forge/graphics/builders/TerrainBuilder.java index f719a73..e3295f7 100644 --- a/core/src/macbury/forge/graphics/builders/TerrainBuilder.java +++ b/core/src/macbury/forge/graphics/builders/TerrainBuilder.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.GdxRuntimeException; +import macbury.forge.Config; import macbury.forge.ForgE; import macbury.forge.blocks.Block; import macbury.forge.graphics.batch.renderable.VoxelChunkRenderable; @@ -117,7 +118,7 @@ private VoxelChunkRenderableFactory buildFactoryFor(Chunk chunk, VoxelsAssembler voxelChunkRenderableFactory.material = material; voxelChunkRenderableFactory.primitiveType = GL30.GL_TRIANGLES; - if (ForgE.config.generateWireframe) + if (ForgE.config.getBool(Config.Key.GenerateWireframe)) voxelChunkRenderableFactory.wireframe = assembler.wireframe(); voxelChunkRenderableFactory.triangleCount = assembler.getTriangleCount(); voxelChunkRenderableFactory.attributes = MeshVertexInfo.voxelTypes(); @@ -145,7 +146,7 @@ private VoxelChunkRenderable buildFaceForChunkWithAssembler(Chunk chunk, VoxelsA VoxelChunkRenderable renderable = new VoxelChunkRenderable(); renderable.primitiveType = GL30.GL_TRIANGLES; - if (ForgE.config.generateWireframe) + if (ForgE.config.getBool(Config.Key.GenerateWireframe)) renderable.wireframe = assembler.wireframe(); renderable.triangleCount = assembler.getTriangleCount(); renderable.meshFactory = assembler.meshFactory(MeshVertexInfo.voxelTypes()); diff --git a/core/src/macbury/forge/graphics/fbo/FrameBufferManager.java b/core/src/macbury/forge/graphics/fbo/FrameBufferManager.java index 53653af..7cc893f 100644 --- a/core/src/macbury/forge/graphics/fbo/FrameBufferManager.java +++ b/core/src/macbury/forge/graphics/fbo/FrameBufferManager.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.GdxRuntimeException; import com.badlogic.gdx.utils.ObjectMap; +import macbury.forge.Config; import macbury.forge.ForgE; import macbury.forge.level.env.LevelEnv; import macbury.forge.shaders.CopyFrameBufferShader; @@ -186,12 +187,12 @@ public void begin(String fbIdn) { public void createDefaultFrameBuffers() { create(Fbo.FRAMEBUFFER_FINAL); create(Fbo.FRAMEBUFFER_MAIN_COLOR); - create(Fbo.FRAMEBUFFER_DOWN_SAMPLED_MAIN, Pixmap.Format.RGBA8888, ForgE.config.bloomTextureSize, ForgE.config.bloomTextureSize, false, Texture.TextureWrap.MirroredRepeat, Texture.TextureFilter.Linear); - create(Fbo.FRAMEBUFFER_BLOOM, Pixmap.Format.RGBA8888, ForgE.config.bloomTextureSize, ForgE.config.bloomTextureSize, false, Texture.TextureWrap.MirroredRepeat, Texture.TextureFilter.Linear); - create(Fbo.FRAMEBUFFER_REFLECTIONS, Pixmap.Format.RGBA8888, ForgE.config.reflectionBufferSize, ForgE.config.reflectionBufferSize, true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear); - create(Fbo.FRAMEBUFFER_REFRACTIONS, Pixmap.Format.RGBA8888, ForgE.config.refractionBufferSize, ForgE.config.refractionBufferSize, true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear); - createFloat(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH, ForgE.config.farShadowMapSize, ForgE.config.farShadowMapSize, true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Linear); - createFloat(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, ForgE.config.farShadowMapSize, ForgE.config.nearShadowMapSize, true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Linear); + create(Fbo.FRAMEBUFFER_DOWN_SAMPLED_MAIN, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.BloomTextureSize), ForgE.config.getInt(Config.Key.BloomTextureSize), false, Texture.TextureWrap.MirroredRepeat, Texture.TextureFilter.Linear); + create(Fbo.FRAMEBUFFER_BLOOM, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.BloomTextureSize), ForgE.config.getInt(Config.Key.BloomTextureSize), false, Texture.TextureWrap.MirroredRepeat, Texture.TextureFilter.Linear); + create(Fbo.FRAMEBUFFER_REFLECTIONS, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.ReflectionBufferSize), ForgE.config.getInt(Config.Key.ReflectionBufferSize), true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear); + create(Fbo.FRAMEBUFFER_REFRACTIONS, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.RefractionBufferSize), ForgE.config.getInt(Config.Key.RefractionBufferSize), true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear); + createFloat(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH, ForgE.config.getInt(Config.Key.FarShadowMapSize), ForgE.config.getInt(Config.Key.FarShadowMapSize), true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Linear); + createFloat(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, ForgE.config.getInt(Config.Key.NearShadowMapSize), ForgE.config.getInt(Config.Key.NearShadowMapSize), true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Linear); } public ObjectMap all() { diff --git a/core/src/macbury/forge/graphics/light/OrthographicDirectionalLight.java b/core/src/macbury/forge/graphics/light/OrthographicDirectionalLight.java index 2c61be8..fc324c0 100644 --- a/core/src/macbury/forge/graphics/light/OrthographicDirectionalLight.java +++ b/core/src/macbury/forge/graphics/light/OrthographicDirectionalLight.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.Disposable; +import macbury.forge.Config; import macbury.forge.ForgE; import macbury.forge.graphics.camera.GameCamera; import macbury.forge.graphics.camera.ICamera; @@ -59,7 +60,7 @@ public void begin(GameCamera mainCamera) { public void beginFar(GameCamera mainCamera) { cacheNearFar(mainCamera); - mainCamera.near = ForgE.config.nearShadowDistance; + mainCamera.near = ForgE.config.getInt(Config.Key.NearShadowDistance); mainCamera.update(true); update(mainCamera); } @@ -77,7 +78,7 @@ public void end(GameCamera mainCamera) { public void beginNear(GameCamera mainCamera) { cacheNearFar(mainCamera); - mainCamera.far = ForgE.config.nearShadowDistance; + mainCamera.far = ForgE.config.getInt(Config.Key.NearShadowDistance); mainCamera.update(true); update(mainCamera); nearMatrix.set(shadowCamera.combined); diff --git a/core/src/macbury/forge/level/Level.java b/core/src/macbury/forge/level/Level.java index d30fb52..781b727 100644 --- a/core/src/macbury/forge/level/Level.java +++ b/core/src/macbury/forge/level/Level.java @@ -75,8 +75,8 @@ public Level(LevelState state, TerrainGeometryProvider geometryProvider) { octree.setBounds(terrainMap.getBounds(ChunkMap.TERRAIN_TILE_SIZE)); ui.addActor(new FullScreenFrameBufferResult(Fbo.FRAMEBUFFER_FINAL)); - //ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_BLOOM, 256, 0, 0)); - // ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, 256, 256, 0)); + ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH, 256, 0, 0)); + ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, 256, 256, 0)); // ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_REFLECTIONS, 256, 0, 0)); //ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_REFRACTIONS, 256, 256, 0)); } diff --git a/core/src/macbury/forge/shaders/uniforms/UniformNearShadowDistance.java b/core/src/macbury/forge/shaders/uniforms/UniformNearShadowDistance.java index 87815b2..5563b3c 100644 --- a/core/src/macbury/forge/shaders/uniforms/UniformNearShadowDistance.java +++ b/core/src/macbury/forge/shaders/uniforms/UniformNearShadowDistance.java @@ -3,21 +3,24 @@ import com.badlogic.gdx.graphics.Camera; import com.badlogic.gdx.graphics.g3d.utils.RenderContext; import com.badlogic.gdx.graphics.glutils.ShaderProgram; +import macbury.forge.Config; import macbury.forge.ForgE; import macbury.forge.level.env.LevelEnv; import macbury.forge.shaders.utils.BaseUniform; +import macbury.forge.utils.KVStorage; /** * Created by macbury on 21.08.15. */ -public class UniformNearShadowDistance extends BaseUniform { +public class UniformNearShadowDistance extends BaseUniform implements KVStorage.OnChangeListener { public static final String UNIFORM_NEAR_SHADOW_DISTANCE = "u_nearShadowDistance"; private static final float SHADOW_OFFSET = 2; - private final float distance; + private float distance; public UniformNearShadowDistance() { super(); - this.distance = ForgE.config.nearShadowDistance + SHADOW_OFFSET; + ForgE.config.addListener(this); + updateDistance(); } @Override @@ -32,6 +35,15 @@ public void bind(ShaderProgram shader, LevelEnv env, RenderContext context, Came @Override public void dispose() { + ForgE.config.removeListener(this); + } + + @Override + public void onKeyChange(Object key, KVStorage storage) { + updateDistance(); + } + private void updateDistance() { + this.distance = ForgE.config.getInt(Config.Key.NearShadowDistance) + SHADOW_OFFSET; } } diff --git a/core/src/macbury/forge/shaders/utils/BaseShader.java b/core/src/macbury/forge/shaders/utils/BaseShader.java index a36452e..ccf825a 100644 --- a/core/src/macbury/forge/shaders/utils/BaseShader.java +++ b/core/src/macbury/forge/shaders/utils/BaseShader.java @@ -137,7 +137,7 @@ private void loadGlobalAndLocalUniforms() { private String applyDebugPrefixes() { String out = ""; - switch (ForgE.config.renderDebug) { + switch (ForgE.config.getRenderDebug()) { case Normals: out += "#define normalsDebugFlag\n"; break; diff --git a/core/src/macbury/forge/shaders/utils/ShadersManager.java b/core/src/macbury/forge/shaders/utils/ShadersManager.java index 79df7d8..c1530d3 100644 --- a/core/src/macbury/forge/shaders/utils/ShadersManager.java +++ b/core/src/macbury/forge/shaders/utils/ShadersManager.java @@ -4,6 +4,8 @@ import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Json; +import macbury.forge.ForgE; +import macbury.forge.utils.KVStorage; import java.io.File; import java.io.FilenameFilter; @@ -13,7 +15,7 @@ /** * Created by macbury on 16.10.14. */ -public class ShadersManager { +public class ShadersManager implements KVStorage.OnChangeListener { public static final String SHADERS_PATH = "graphics/shaders/"; public static final String SHADER_HELPERS_PATH = SHADERS_PATH + "helpers/"; public static final String SHADER_STRUCTS_PATH = SHADERS_PATH + "structs/"; @@ -26,6 +28,7 @@ public ShadersManager() { this.shaderReloadListeners = new Array(); this.shaders = new HashMap(); this.shaderList = new Array(); + ForgE.config.addListener(this); reload(); } @@ -108,4 +111,14 @@ private void triggerOnShaderError(BaseShader program) { public Array all() { return shaderList; } + + @Override + public void onKeyChange(Object key, KVStorage storage) { + Gdx.app.postRunnable(new Runnable() { + @Override + public void run() { + reload(); + } + }); + } } diff --git a/core/src/macbury/forge/systems/DebugSystem.java b/core/src/macbury/forge/systems/DebugSystem.java index 88d5389..c84a501 100644 --- a/core/src/macbury/forge/systems/DebugSystem.java +++ b/core/src/macbury/forge/systems/DebugSystem.java @@ -13,6 +13,7 @@ import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.collision.BoundingBox; import com.badlogic.gdx.utils.Disposable; +import macbury.forge.Config; import macbury.forge.ForgE; import macbury.forge.components.CursorComponent; import macbury.forge.components.PositionComponent; @@ -68,7 +69,7 @@ public DebugSystem(Level level) { @Override public boolean checkProcessing() { - return ForgE.config.debug; + return ForgE.config.getBool(Config.Key.Debug); } @Override @@ -76,7 +77,7 @@ public void processEntity(Entity entity, float deltaTime) { PositionComponent positionComponent = pm.get(entity); CursorComponent cursorComponent = cm.get(entity); - if (ForgE.config.renderBoundingBox) { + if (ForgE.config.getBool(Config.Key.RenderBoundingBox)) { //positionComponent.getBoundingBox(tempBox); tempVec.set(positionComponent.size).scl(0.5f); tempVec2.set(positionComponent.vector).sub(tempVec); @@ -105,7 +106,7 @@ public void update(float deltaTime) { batch.shapeRenderer.setColor(BOUNDING_BOX_COLOR); batch.shapeRenderer.identity(); super.update(deltaTime); - if (ForgE.config.renderBoundingBox) { + if (ForgE.config.getBool(Config.Key.RenderBoundingBox)) { batch.shapeRenderer.identity(); for (int i = 0; i < terrain.chunks.size; i++) { Chunk chunk = terrain.chunks.get(i); @@ -118,14 +119,14 @@ public void update(float deltaTime) { } batch.shapeRenderer.setColor(OCTREE_BOUNDS_COLOR); - if (ForgE.config.renderDynamicOctree) { + if (ForgE.config.getBool(Config.Key.RenderDynamicOctree)) { DebugShape.cullledOctree(batch.shapeRenderer, dynamicOctree, camera.normalOrDebugFrustrum()); } - if (ForgE.config.renderStaticOctree) { + if (ForgE.config.getBool(Config.Key.RenderStaticOctree)) { DebugShape.cullledOctree(batch.shapeRenderer, terrainOctree, camera.normalOrDebugFrustrum()); } - if (ForgE.config.debug) { + if (ForgE.config.getBool(Config.Key.Debug)) { renderStartPosition(); } @@ -135,7 +136,7 @@ public void update(float deltaTime) { context.end(); batch.render(level.env); batch.end(); - if (ForgE.config.renderBulletDebug) { + if (ForgE.config.getBool(Config.Key.RenderBulletDebug)) { context.begin(); { context.setDepthTest(GL30.GL_LEQUAL); context.setCullFace(GL30.GL_BACK); diff --git a/core/src/macbury/forge/systems/WorldRenderingSystem.java b/core/src/macbury/forge/systems/WorldRenderingSystem.java index 47bca19..0e7d499 100644 --- a/core/src/macbury/forge/systems/WorldRenderingSystem.java +++ b/core/src/macbury/forge/systems/WorldRenderingSystem.java @@ -80,12 +80,7 @@ public void update(float deltaTime) { private void renderSunDepth() { env.water.clipMode = LevelEnv.ClipMode.None; OrthographicDirectionalLight sunLight = env.mainLight; - sunLight.begin(mainCamera); { - ForgE.fb.begin(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH); { - renderBucketWith(false, false, sunLight.getShadowCamera()); - } ForgE.fb.end(); - } sunLight.end(mainCamera); -/* + sunLight.beginFar(mainCamera); { ForgE.fb.begin(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH); { renderBucketWith(false, false, sunLight.getShadowCamera()); @@ -96,7 +91,7 @@ private void renderSunDepth() { ForgE.fb.begin(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH); { renderBucketWith(false, false, sunLight.getShadowCamera()); } ForgE.fb.end(); - } sunLight.endNear(mainCamera);*/ + } sunLight.endNear(mainCamera); } private void renderReflections() { diff --git a/core/src/macbury/forge/time/TimeManager.java b/core/src/macbury/forge/time/TimeManager.java index 80014cb..654b88d 100644 --- a/core/src/macbury/forge/time/TimeManager.java +++ b/core/src/macbury/forge/time/TimeManager.java @@ -24,7 +24,7 @@ public class TimeManager implements Disposable { public static final float DAY_END_HOUR = 20 * HOUR_IN_SECONDS + 35 * MINUTE_IN_SECONDS; public static final float DAY_LENGTH = DAY_END_HOUR - DAY_START_HOUR; public static final float NIGHT_LENGTH = DAY_IN_SECONDS - DAY_END_HOUR + DAY_START_HOUR; - public static final float DEFAULT_TIME = 19 * HOUR_IN_SECONDS; + public static final float DEFAULT_TIME = 12 * HOUR_IN_SECONDS; private float sateliteRotation; private float sateliteProgress; diff --git a/core/src/macbury/forge/utils/KVStorage.java b/core/src/macbury/forge/utils/KVStorage.java index a9d900e..e986aa3 100644 --- a/core/src/macbury/forge/utils/KVStorage.java +++ b/core/src/macbury/forge/utils/KVStorage.java @@ -3,18 +3,22 @@ import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.ObjectMap; +import macbury.forge.Config; /** * Created by macbury on 25.08.15. */ -public class KVStorage implements Disposable { - private ObjectMap values; +public abstract class KVStorage implements Disposable { + private ObjectMap values; private Array listeners; public KVStorage() { - this.values = new ObjectMap(); + this.values = new ObjectMap(); this.listeners = new Array(); + setDefaults(); } + public abstract void setDefaults(); + public void addListener(OnChangeListener listener) { if (!listeners.contains(listener, true)) listeners.add(listener); @@ -24,43 +28,47 @@ public void removeListener(OnChangeListener listener) { listeners.removeValue(listener, true); } - public void putObject(String key, Object value) { + public void putObject(K key, Object value) { values.put(key, value); triggerChange(key); } - public void putInt(String key, int value) { + public void putInt(K key, int value) { values.put(key, value); triggerChange(key); } - public void putString(String key, String value) { + public void putString(K key, String value) { values.put(key, value); triggerChange(key); } - public void putBool(String key, boolean value) { + public void putBool(K key, boolean value) { values.put(key, value); triggerChange(key); } - public int getInt(String key) { + public int getInt(K key) { return (int)values.get(key); } - public String getString(String key) { + public String getString(K key) { return (String)values.get(key); } - public boolean getBool(String key) { + public boolean getBool(K key) { return (boolean)values.get(key); } - public Object getObject(String key) { + public Object getObject(K key) { return values.get(key); } - protected void triggerChange(String key) { + public float getFloat(K key) { + return (float)values.get(key); + } + + protected void triggerChange(K key) { for (OnChangeListener listener : listeners) { listener.onKeyChange(key, this); } @@ -74,7 +82,9 @@ public void dispose() { values = null; } - public interface OnChangeListener { - public void onKeyChange(String key, KVStorage storage); + + + public interface OnChangeListener { + public void onKeyChange(K key, KVStorage storage); } } diff --git a/desktop/src/macbury/forge/desktop/DesktopGame.java b/desktop/src/macbury/forge/desktop/DesktopGame.java index 472d877..9aa1658 100644 --- a/desktop/src/macbury/forge/desktop/DesktopGame.java +++ b/desktop/src/macbury/forge/desktop/DesktopGame.java @@ -28,9 +28,9 @@ public DesktopGame(String[] arg) { config.resizable = false; config.foregroundFPS = 30; - config.width = forgeConfig.resolutionWidth; - config.height = forgeConfig.resolutionHeight; - config.fullscreen = forgeConfig.fullscreen; + config.width = forgeConfig.getInt(Config.Key.ResolutionWidth); + config.height = forgeConfig.getInt(Config.Key.ResolutionHeight); + config.fullscreen = forgeConfig.getBool(Config.Key.Fullscreen); ForgE engine = new ForgE(forgeConfig); engine.addBootListener(this); diff --git a/editor/src/macbury/forge/editor/controllers/resources/ResourcesController.java b/editor/src/macbury/forge/editor/controllers/resources/ResourcesController.java index 6de91c7..6f44e84 100644 --- a/editor/src/macbury/forge/editor/controllers/resources/ResourcesController.java +++ b/editor/src/macbury/forge/editor/controllers/resources/ResourcesController.java @@ -1,6 +1,7 @@ package macbury.forge.editor.controllers.resources; import com.badlogic.gdx.Gdx; +import macbury.forge.Config; import macbury.forge.ForgE; import macbury.forge.shaders.utils.BaseShader; @@ -59,9 +60,9 @@ private void onResourceDbClick(int selRow, TreePath selPath) { private void openShaderToEdit(ResourcesModel.GameShaderNode node) { BaseShader shader = ForgE.shaders.get(node.getName()); try { - Runtime.getRuntime().exec(ForgE.config.editor + " " + shader.getJsonFile().getAbsolutePath()); - Runtime.getRuntime().exec(ForgE.config.editor + " " + shader.getFragmentFile().path()); - Runtime.getRuntime().exec(ForgE.config.editor + " " + shader.getVertexFile().path()); + Runtime.getRuntime().exec(ForgE.config.getString(Config.Key.Editor) + " " + shader.getJsonFile().getAbsolutePath()); + Runtime.getRuntime().exec(ForgE.config.getString(Config.Key.Editor) + " " + shader.getFragmentFile().path()); + Runtime.getRuntime().exec(ForgE.config.getString(Config.Key.Editor) + " " + shader.getVertexFile().path()); } catch (IOException e) { e.printStackTrace(); } diff --git a/editor/src/macbury/forge/editor/views/MainMenu.java b/editor/src/macbury/forge/editor/views/MainMenu.java index bc500fe..89043e3 100644 --- a/editor/src/macbury/forge/editor/views/MainMenu.java +++ b/editor/src/macbury/forge/editor/views/MainMenu.java @@ -116,15 +116,15 @@ public void setEditor(LevelEditorScreen levelEditorScreen) { } public void refresh() { - debugRenderDynamicOctree.setState(ForgE.config.renderDynamicOctree); - debugBoundingBox.setState(ForgE.config.renderBoundingBox); - debugRenderStaticOctree.setState(ForgE.config.renderStaticOctree); + debugRenderDynamicOctree.setState(ForgE.config.getBool(Config.Key.RenderDynamicOctree)); + debugBoundingBox.setState(ForgE.config.getBool(Config.Key.RenderBoundingBox)); + debugRenderStaticOctree.setState(ForgE.config.getBool(Config.Key.RenderStaticOctree)); if (editor == null) { debugRenderMenu.setVisible(false); } else { debugRenderMenu.setVisible(true); - switch (ForgE.config.renderDebug) { + switch (ForgE.config.getRenderDebug()) { case Normals: debugNormalsItem.setSelected(true); break; @@ -221,21 +221,21 @@ public void actionPerformed(ActionEvent e) { debugRenderDynamicOctree.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ForgE.config.renderDynamicOctree = debugRenderDynamicOctree.getState(); + ForgE.config.putBool(Config.Key.RenderDynamicOctree, debugRenderDynamicOctree.getState()); } }); debugRenderStaticOctree.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ForgE.config.renderStaticOctree = debugRenderStaticOctree.getState(); + ForgE.config.putBool(Config.Key.RenderStaticOctree, debugRenderStaticOctree.getState()); } }); debugBoundingBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ForgE.config.renderBoundingBox = debugBoundingBox.getState(); + ForgE.config.putBool(Config.Key.RenderBoundingBox, debugBoundingBox.getState()); } }); } @@ -273,7 +273,7 @@ public void actionPerformed(ActionEvent e) { } private void setRenderingMode(Config.RenderDebug renderType) { - ForgE.config.setRenderDebugTo(renderType); + ForgE.config.putRenderDebug(renderType); } @Override