Skip to content

Commit

Permalink
Better config system
Browse files Browse the repository at this point in the history
  • Loading branch information
macbury committed Aug 27, 2015
1 parent 16cbd45 commit 1134489
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 124 deletions.
16 changes: 10 additions & 6 deletions core/assets/config_editor.json
Original file line number Diff line number Diff line change
@@ -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
},
}
24 changes: 18 additions & 6 deletions core/assets/config_game.json
Original file line number Diff line number Diff line change
@@ -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
},
}
25 changes: 21 additions & 4 deletions core/assets/graphics/shaders/helpers/shadow_map.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions core/assets/graphics/shaders/ps-bloom.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions core/assets/graphics/shaders/terrain-depthmap.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
2 changes: 0 additions & 2 deletions core/assets/graphics/shaders/terrain-depthmap.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +20,5 @@ void main() {

vec4 position = u_projectionMatrix * v_position;

v_depth = v_position.z * 0.5f + 0.5f;
gl_Position = position;
}
74 changes: 43 additions & 31 deletions core/src/macbury/forge/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,32 +12,39 @@
/**
* Created by macbury on 18.10.14.
*/
public class Config {

public class Config extends KVStorage<Config.Key> {
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();
Expand All @@ -47,17 +55,21 @@ public static Config load(String namespace) {
e.printStackTrace();
}
String text = new String(encoded);
return json.fromJson(Config.class, text);
ObjectMap<String, Object> 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);
}

}
2 changes: 1 addition & 1 deletion core/src/macbury/forge/graphics/batch/VoxelBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions core/src/macbury/forge/graphics/builders/TerrainBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down
13 changes: 7 additions & 6 deletions core/src/macbury/forge/graphics/fbo/FrameBufferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, FrameBuffer> all() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions core/src/macbury/forge/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Loading

0 comments on commit 1134489

Please sign in to comment.