Skip to content

Commit 1134489

Browse files
committed
Better config system
1 parent 16cbd45 commit 1134489

File tree

22 files changed

+214
-124
lines changed

22 files changed

+214
-124
lines changed

core/assets/config_editor.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
2-
generateWireframe: true,
3-
renderStaticOctree: false,
4-
renderBoundingBox: false,
5-
debug: true,
6-
nearShadowMapSize: 1024,
7-
farShadowMapSize: 1024,
2+
Debug: true,
3+
GenerateWireframe: true,
4+
NearShadowMapSize: {
5+
class: java.lang.Integer,
6+
value: 1024
7+
},
8+
FarShadowMapSize: {
9+
class: java.lang.Integer,
10+
value: 1024
11+
},
812
}

core/assets/config_game.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
{
2-
debug: false,
3-
resolutionWidth: 1360,
4-
resolutionHeight: 768,
5-
fullscreen: false,
6-
nearShadowMapSize: 1024,
7-
farShadowMapSize: 1024,
2+
Debug: false,
3+
ResolutionWidth: {
4+
class: java.lang.Integer,
5+
value: 1360
6+
},
7+
ResolutionHeight: {
8+
class: java.lang.Integer,
9+
value: 768
10+
},
11+
Fullscreen: false,
12+
NearShadowMapSize: {
13+
class: java.lang.Integer,
14+
value: 1024
15+
},
16+
FarShadowMapSize: {
17+
class: java.lang.Integer,
18+
value: 1024
19+
},
820
}

core/assets/graphics/shaders/helpers/shadow_map.glsl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,32 @@ vec4 transformFrom0To1Range(vec4 vector) {
2121
return vector * 0.5f + 0.5f;
2222
}
2323

24+
float chebshevComputeQuantity(vec4 positionInLightSpace, sampler2D depthMap, float bias) {
25+
float currentDepth = (positionInLightSpace.z / positionInLightSpace.w )* 0.5f + 0.5f;
26+
vec2 projCoords = (positionInLightSpace.xy / positionInLightSpace.w) * 0.5f + 0.5f;
27+
// get two moments
28+
vec2 moments = texture2D(depthMap, projCoords.xy).rg;
29+
if (currentDepth <= moments.x) {
30+
return 1.0f;
31+
} else {
32+
float E_x2 = moments.y;
33+
float Ex_2 = moments.x * moments.x;
34+
float variance = E_x2 - (Ex_2);
35+
float t = currentDepth - moments.x;
36+
float pMax = variance / (variance + t*t);
37+
return pMax;
38+
}
39+
}
40+
2441
float shadowCalculation(vec4 positionInLightSpace, sampler2D depthMap, float bias) {
2542
// perform perspective divide
26-
vec3 projCoords = positionInLightSpace.xyz / positionInLightSpace.w;
43+
vec3 projCoords = (positionInLightSpace.xyz / positionInLightSpace.w) * 0.5f + 0.5f;
2744
// Transform to [0,1] range
28-
positionInLightSpace = transformFrom0To1Range(positionInLightSpace);
45+
//projCoords = transformFrom0To1Range(positionInLightSpace);
2946
// Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
30-
float closestDepth = step(positionInLightSpace.z, unpack(texture2D(depthMap, positionInLightSpace.xy)));
47+
float closestDepth = step(projCoords.z, unpack(texture2D(depthMap, projCoords.xy)));
3148
// Get depth of current fragment from light's perspective
32-
float currentDepth = positionInLightSpace.z;
49+
float currentDepth = projCoords.z;
3350
// Check whether current frag pos is in shadow
3451
/*
3552
vec2 texelSize = vec2(0.0009765f);

core/assets/graphics/shaders/ps-bloom.frag.glsl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ varying vec2 v_texCoords;
22
void main() {
33
vec4 sum = vec4(0.0);
44

5-
float blurSize = 0.002f;
6-
/*
7-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 4.0*blurSize)) * 0.05;
5+
float blurSize = 0.003f;
6+
7+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 4.0*blurSize)) * 0.05;
88
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 3.0*blurSize)) * 0.09;
9-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 2.0*blurSize)) * 0.12;
9+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - 2.0*blurSize)) * 0.12;
1010
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y - blurSize)) * 0.15;
11-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y)) * 0.16;
11+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y)) * 0.16;
1212
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + blurSize)) * 0.15;
13-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 2.0*blurSize)) * 0.12;
13+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 2.0*blurSize)) * 0.12;
1414
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 3.0*blurSize)) * 0.09;
15-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 4.0*blurSize)) * 0.05;
15+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y + 4.0*blurSize)) * 0.05;
16+
1617

17-
*/
1818
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 4.0*blurSize, v_texCoords.y)) * 0.05;
19-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 3.0*blurSize, v_texCoords.y)) * 0.1;
19+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 3.0*blurSize, v_texCoords.y)) * 0.1;
2020
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - 2.0*blurSize, v_texCoords.y)) * 0.12;
21-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - blurSize, v_texCoords.y)) * 0.15;
21+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x - blurSize, v_texCoords.y)) * 0.15;
2222
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x, v_texCoords.y)) * 0.16;
23-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + blurSize, v_texCoords.y)) * 0.15;
23+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + blurSize, v_texCoords.y)) * 0.15;
2424
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 2.0*blurSize, v_texCoords.y)) * 0.12;
25-
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 3.0*blurSize, v_texCoords.y)) * 0.1;
25+
//sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 3.0*blurSize, v_texCoords.y)) * 0.1;
2626
sum += texture2D(u_downSampledMainTexture, vec2(v_texCoords.x + 4.0*blurSize, v_texCoords.y)) * 0.05;
2727

2828
gl_FragColor = sum;

core/assets/graphics/shaders/terrain-depthmap.frag.glsl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ varying vec2 v_uvStart;
33
varying vec2 v_uvMul;
44
varying vec2 v_textCoord;
55

6-
varying float v_depth;
76

87
void main() {
98
vec2 tilingTextCord = (fract(v_textCoord) * v_uvMul) + v_uvStart;
@@ -12,5 +11,18 @@ void main() {
1211
discard;
1312
}
1413

15-
gl_FragColor = pack(v_depth);
14+
//homogeneus to texture cordinate system([-1,1])
15+
16+
float depth = v_position.z / v_position.w;
17+
depth = depth * 0.5f + 0.5f;
18+
19+
float M1 = depth; //moment 1
20+
float M2 = depth * depth;
21+
22+
float dx = dFdx(depth);
23+
float dy = dFdy(depth);
24+
25+
M2 += 0.25f*(dx*dx+dy*dy);
26+
27+
gl_FragColor = vec4(M1, M2, 0.0f, 1.0f);
1628
}

core/assets/graphics/shaders/terrain-depthmap.vert.glsl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ varying vec4 v_position;
88
varying vec2 v_uvStart;
99
varying vec2 v_uvMul;
1010

11-
varying float v_depth;
1211

1312
void main() {
1413
float waviness = a_material.a;
@@ -21,6 +20,5 @@ void main() {
2120

2221
vec4 position = u_projectionMatrix * v_position;
2322

24-
v_depth = v_position.z * 0.5f + 0.5f;
2523
gl_Position = position;
2624
}

core/src/macbury/forge/Config.java

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.badlogic.gdx.Gdx;
44
import com.badlogic.gdx.utils.Json;
5+
import com.badlogic.gdx.utils.ObjectMap;
56
import macbury.forge.utils.KVStorage;
67

78
import java.io.IOException;
@@ -11,32 +12,39 @@
1112
/**
1213
* Created by macbury on 18.10.14.
1314
*/
14-
public class Config {
15-
15+
public class Config extends KVStorage<Config.Key> {
16+
public enum Key {
17+
ResolutionWidth, ResolutionHeight, Fullscreen, Debug, NearShadowMapSize, FarShadowMapSize, BloomTextureSize, ReflectionBufferSize, RefractionBufferSize,
18+
GenerateWireframe, NearShadowDistance, RenderDebug, RenderDynamicOctree, RenderStaticOctree, RenderBoundingBox, RenderBulletDebug, Editor,
19+
}
1620

1721
public enum RenderDebug {
1822
Textured, Wireframe, Normals, Lighting
1923
}
20-
public int bloomTextureSize = 256;
21-
public int resolutionWidth = 1360;
22-
public int resolutionHeight = 768;
23-
public boolean fullscreen = false;
24-
25-
public int reflectionBufferSize = 512;
26-
public int refractionBufferSize = 512;
27-
public int farShadowMapSize = 512;
28-
public int nearShadowMapSize = 1024;
29-
public float nearShadowDistance = 10;
30-
31-
public boolean generateWireframe = false;
32-
public boolean debug = false;
33-
public RenderDebug renderDebug = RenderDebug.Textured;
34-
public boolean renderDynamicOctree = false;
35-
public boolean renderStaticOctree = false;
36-
public boolean renderBoundingBox = false;
37-
public boolean cacheGeometry = false;
38-
public boolean renderBulletDebug = false;
39-
public String editor = "atom";
24+
25+
@Override
26+
public void setDefaults() {
27+
putInt(Key.NearShadowMapSize, 1024);
28+
putInt(Key.NearShadowMapSize, 1024);
29+
putInt(Key.NearShadowMapSize, 1024);
30+
putInt(Key.FarShadowMapSize, 1024);
31+
putInt(Key.ResolutionWidth, 1360);
32+
putInt(Key.ResolutionHeight, 768);
33+
putInt(Key.BloomTextureSize, 512);
34+
putInt(Key.ReflectionBufferSize, 512);
35+
putInt(Key.RefractionBufferSize, 512);
36+
putInt(Key.NearShadowDistance, 10);
37+
putBool(Key.Fullscreen, false);
38+
putBool(Key.Debug, false);
39+
putBool(Key.RenderDynamicOctree, false);
40+
putBool(Key.RenderStaticOctree, false);
41+
putBool(Key.RenderBoundingBox, false);
42+
putBool(Key.RenderBulletDebug, false);
43+
putBool(Key.GenerateWireframe, false);
44+
putString(Key.Editor, "atom");
45+
putRenderDebug(RenderDebug.Textured);
46+
}
47+
4048

4149
public static Config load(String namespace) {
4250
Json json = new Json();
@@ -47,17 +55,21 @@ public static Config load(String namespace) {
4755
e.printStackTrace();
4856
}
4957
String text = new String(encoded);
50-
return json.fromJson(Config.class, text);
58+
ObjectMap<String, Object> values = json.fromJson(ObjectMap.class, text);
59+
Config config = new Config();
60+
for(String rawKey : values.keys()) {
61+
Key key = Key.valueOf(rawKey);
62+
config.putObject(key, values.get(rawKey));
63+
}
64+
return config;
5165
}
5266

53-
public void setRenderDebugTo(RenderDebug debug) {
54-
renderDebug = debug;
55-
Gdx.app.postRunnable(new Runnable() {
56-
@Override
57-
public void run() {
58-
ForgE.shaders.reload();
59-
}
60-
});
67+
public void putRenderDebug(RenderDebug renderDebug) {
68+
putObject(Key.RenderDebug, renderDebug);
69+
}
6170

71+
public RenderDebug getRenderDebug() {
72+
return (RenderDebug)getObject(Key.RenderDebug);
6273
}
74+
6375
}

core/src/macbury/forge/graphics/batch/VoxelBatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void add(final RenderableProvider renderableProvider) {
112112
public void render(LevelEnv env) {
113113
if (camera == null) throw new GdxRuntimeException("Call begin() first.");
114114
sortUnlessNotSorted();
115-
if (ForgE.config.renderDebug == Config.RenderDebug.Wireframe) {
115+
if (ForgE.config.getRenderDebug() == Config.RenderDebug.Wireframe) {
116116
renderWireframe();
117117
} else {
118118
renderTextured(env);

core/src/macbury/forge/graphics/builders/TerrainBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.badlogic.gdx.math.Vector3;
77
import com.badlogic.gdx.utils.Array;
88
import com.badlogic.gdx.utils.GdxRuntimeException;
9+
import macbury.forge.Config;
910
import macbury.forge.ForgE;
1011
import macbury.forge.blocks.Block;
1112
import macbury.forge.graphics.batch.renderable.VoxelChunkRenderable;
@@ -117,7 +118,7 @@ private VoxelChunkRenderableFactory buildFactoryFor(Chunk chunk, VoxelsAssembler
117118
voxelChunkRenderableFactory.material = material;
118119
voxelChunkRenderableFactory.primitiveType = GL30.GL_TRIANGLES;
119120

120-
if (ForgE.config.generateWireframe)
121+
if (ForgE.config.getBool(Config.Key.GenerateWireframe))
121122
voxelChunkRenderableFactory.wireframe = assembler.wireframe();
122123
voxelChunkRenderableFactory.triangleCount = assembler.getTriangleCount();
123124
voxelChunkRenderableFactory.attributes = MeshVertexInfo.voxelTypes();
@@ -145,7 +146,7 @@ private VoxelChunkRenderable buildFaceForChunkWithAssembler(Chunk chunk, VoxelsA
145146
VoxelChunkRenderable renderable = new VoxelChunkRenderable();
146147
renderable.primitiveType = GL30.GL_TRIANGLES;
147148

148-
if (ForgE.config.generateWireframe)
149+
if (ForgE.config.getBool(Config.Key.GenerateWireframe))
149150
renderable.wireframe = assembler.wireframe();
150151
renderable.triangleCount = assembler.getTriangleCount();
151152
renderable.meshFactory = assembler.meshFactory(MeshVertexInfo.voxelTypes());

core/src/macbury/forge/graphics/fbo/FrameBufferManager.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.badlogic.gdx.utils.Disposable;
1111
import com.badlogic.gdx.utils.GdxRuntimeException;
1212
import com.badlogic.gdx.utils.ObjectMap;
13+
import macbury.forge.Config;
1314
import macbury.forge.ForgE;
1415
import macbury.forge.level.env.LevelEnv;
1516
import macbury.forge.shaders.CopyFrameBufferShader;
@@ -186,12 +187,12 @@ public void begin(String fbIdn) {
186187
public void createDefaultFrameBuffers() {
187188
create(Fbo.FRAMEBUFFER_FINAL);
188189
create(Fbo.FRAMEBUFFER_MAIN_COLOR);
189-
create(Fbo.FRAMEBUFFER_DOWN_SAMPLED_MAIN, Pixmap.Format.RGBA8888, ForgE.config.bloomTextureSize, ForgE.config.bloomTextureSize, false, Texture.TextureWrap.MirroredRepeat, Texture.TextureFilter.Linear);
190-
create(Fbo.FRAMEBUFFER_BLOOM, Pixmap.Format.RGBA8888, ForgE.config.bloomTextureSize, ForgE.config.bloomTextureSize, false, Texture.TextureWrap.MirroredRepeat, Texture.TextureFilter.Linear);
191-
create(Fbo.FRAMEBUFFER_REFLECTIONS, Pixmap.Format.RGBA8888, ForgE.config.reflectionBufferSize, ForgE.config.reflectionBufferSize, true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear);
192-
create(Fbo.FRAMEBUFFER_REFRACTIONS, Pixmap.Format.RGBA8888, ForgE.config.refractionBufferSize, ForgE.config.refractionBufferSize, true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear);
193-
createFloat(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH, ForgE.config.farShadowMapSize, ForgE.config.farShadowMapSize, true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Linear);
194-
createFloat(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, ForgE.config.farShadowMapSize, ForgE.config.nearShadowMapSize, true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Linear);
190+
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);
191+
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);
192+
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);
193+
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);
194+
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);
195+
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);
195196
}
196197

197198
public ObjectMap<String, FrameBuffer> all() {

core/src/macbury/forge/graphics/light/OrthographicDirectionalLight.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.badlogic.gdx.math.Matrix4;
88
import com.badlogic.gdx.math.Vector3;
99
import com.badlogic.gdx.utils.Disposable;
10+
import macbury.forge.Config;
1011
import macbury.forge.ForgE;
1112
import macbury.forge.graphics.camera.GameCamera;
1213
import macbury.forge.graphics.camera.ICamera;
@@ -59,7 +60,7 @@ public void begin(GameCamera mainCamera) {
5960

6061
public void beginFar(GameCamera mainCamera) {
6162
cacheNearFar(mainCamera);
62-
mainCamera.near = ForgE.config.nearShadowDistance;
63+
mainCamera.near = ForgE.config.getInt(Config.Key.NearShadowDistance);
6364
mainCamera.update(true);
6465
update(mainCamera);
6566
}
@@ -77,7 +78,7 @@ public void end(GameCamera mainCamera) {
7778

7879
public void beginNear(GameCamera mainCamera) {
7980
cacheNearFar(mainCamera);
80-
mainCamera.far = ForgE.config.nearShadowDistance;
81+
mainCamera.far = ForgE.config.getInt(Config.Key.NearShadowDistance);
8182
mainCamera.update(true);
8283
update(mainCamera);
8384
nearMatrix.set(shadowCamera.combined);

core/src/macbury/forge/level/Level.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public Level(LevelState state, TerrainGeometryProvider geometryProvider) {
7575
octree.setBounds(terrainMap.getBounds(ChunkMap.TERRAIN_TILE_SIZE));
7676

7777
ui.addActor(new FullScreenFrameBufferResult(Fbo.FRAMEBUFFER_FINAL));
78-
//ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_BLOOM, 256, 0, 0));
79-
// ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, 256, 256, 0));
78+
ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH, 256, 0, 0));
79+
ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_SUN_NEAR_DEPTH, 256, 256, 0));
8080
// ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_REFLECTIONS, 256, 0, 0));
8181
//ui.addActor(DebugFrameBufferResult.build(Fbo.FRAMEBUFFER_REFRACTIONS, 256, 256, 0));
8282
}

0 commit comments

Comments
 (0)