Skip to content

Commit

Permalink
Move lightdirection calculation to CPU.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwando committed Sep 9, 2014
1 parent 3d294a3 commit 25157a8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
4 changes: 1 addition & 3 deletions assets/DMonkey/AmbientLight.j3md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ MaterialDef AmbientLight {
Texture2D DepthBuffer
Texture2D NormalBuffer
Texture2D DiffuseBuffer
Vector3 LightDir
Vector4Array DirectionalColors
Vector3Array Directions
Vector3 ViewLightDir

Float LightIntensity
Int DirectionalLights
Expand Down
29 changes: 21 additions & 8 deletions assets/DMonkey/Shaders/AmbientLight.frag
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
#define SPECULAR
#define GAMMA_CORRECT
#import "DMonkey/Shaders/DM_Light.glsllib"
uniform vec4 m_Color;
uniform float m_LightIntensity;
uniform vec3 m_LightDir;
uniform vec3 m_ViewLightDir;

const vec3 lightDirection = vec3(0.1, 0.3, 0.0);
const float GAMMA = 2.2;

vec3 FilmicMain(vec3 texColor){
float range = 0.2;
texColor *= range;
vec3 x = max(vec3(0.0),texColor-0.004); // Filmic Curve
vec3 retColor = (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);
return retColor;
}
void main() {
dm_decode();

vec3 albedo = GBuffer.albedo;
#ifdef GAMMA_CORRECT
albedo = gamma(GBuffer.albedo, GAMMA);
#endif


vec3 lightDir = normalize((g_ViewMatrix * vec4(m_LightDir, 0.0)).xyz);
//lightDir = normalize(m_LightDir);
vec3 albedo = gamma(GBuffer.albedo, GAMMA);

Light light = Light(vec4(gamma(m_Color.rgb, GAMMA), 0.0), vec3(0.0), lightDir);
Light light = Light(vec4(gamma(m_Color.rgb, GAMMA), 0.0), vec3(0.0), m_ViewLightDir);
vec4 color = ComputeLighting(light);

color += vec4(0.05, 0.05, 0.1, 0.0);

gl_FragColor.rgb = albedo * color.rgb;
gl_FragColor.rgb = albedo * color.rgb * 1.5;

//gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb);
#ifdef GAMMA_CORRECT
gl_FragColor.rgb = gamma(gl_FragColor.rgb, 1.0 / GAMMA);
#endif
}

17 changes: 0 additions & 17 deletions assets/Materials/Generated/test_object-test_object_geom_0.j3m

This file was deleted.

1 change: 1 addition & 0 deletions src/me/merciless/dmonkey3/DeferredRenderState.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jme3.app.state.AppStateManager;
import com.jme3.math.ColorRGBA;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.FXAAFilter;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
Expand Down
10 changes: 9 additions & 1 deletion src/me/merciless/dmonkey3/LightRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix4f;
import com.jme3.math.Vector3f;
import com.jme3.math.Vector4f;
import com.jme3.post.SceneProcessor;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
Expand Down Expand Up @@ -94,7 +96,13 @@ public void postQueue(RenderQueue rq) {
public void postFrame(FrameBuffer out) {
updateMaterial();
vsLightDir.set(camera.getViewMatrix().mult(dl.getDirection().normalizeLocal()).normalize());
material.setVector3("LightDir", dl.getDirection());

Vector3f vec3 = dl.getDirection();
Vector4f vec4 = new Vector4f(vec3.x, vec3.y, vec3.z, 0);
camera.getViewMatrix().mult(vec4, vec4);

vec3 = new Vector3f(vec4.x, vec4.y, vec4.z);
material.setVector3("ViewLightDir", vec3);
renderManager.getRenderer().setFrameBuffer(null);
renderManager.getRenderer().clearBuffers(true, true, true);
renderManager.renderGeometry(fullscreenQuad);
Expand Down
6 changes: 6 additions & 0 deletions src/me/merciless/dmonkey3/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.texture.Texture;
Expand Down Expand Up @@ -66,6 +67,11 @@ public void simpleInitApp() {
((Texture)(groundPlane.getMaterial().getTextureParam("NormalTex").getValue())).setWrap(Texture.WrapMode.Repeat);

deferred.getRootNode().attachChild(groundPlane);

Spatial spat = assetManager.loadModel("Models/Tracy/Tracy.j3o");
//spat.setMaterial(cylinder.getMaterial());
spat.move(-2, -2, 1);
deferred.getRootNode().attachChild(spat);
}

public static void main(String[] arg) {
Expand Down

0 comments on commit 25157a8

Please sign in to comment.