Skip to content

Commit

Permalink
More changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwando committed Sep 14, 2014
1 parent 25157a8 commit c69b875
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
10 changes: 7 additions & 3 deletions assets/DMonkey/Shaders/AmbientLight.frag
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#define SPECULAR
#define GAMMA_CORRECT

uniform float g_Time;
#import "DMonkey/Shaders/DM_Light.glsllib"
uniform vec4 m_Color;
uniform float m_LightIntensity;
uniform vec3 m_ViewLightDir;


const vec3 lightDirection = vec3(0.1, 0.3, 0.0);
const float GAMMA = 2.2;
vec3 FilmicMain(vec3 texColor){
Expand All @@ -26,12 +29,13 @@ void main() {
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);
//color += vec4(0.05, 0.05, 0.1, 0.0);

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

//gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb);

#ifdef GAMMA_CORRECT
//gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb);
gl_FragColor.rgb = gamma(gl_FragColor.rgb, 1.0 / GAMMA);
#endif
}
Expand Down
41 changes: 37 additions & 4 deletions assets/DMonkey/Shaders/DM_Light.glsllib
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
#import "DMonkey/Shaders/GBuffer.glsllib"

const int QUALITY_LOW = 0,
QUALITY_MED = 1,
QUALITY_HIGH = 2;

const float PI = 3.14159265358979323846264;
const float ONE_OVER_PI = 1.0 / PI;
const float PI_OVER_FOUR = PI / 4.0;
const float PI_OVER_TWO = PI / 2.0;

// Use this instead of Lambert for the diffuse part
float OrenNayar(vec3 normal, vec3 lightDir, vec3 viewDir, float roughness)
{
float vdotl = max(0.0, dot(viewDir, lightDir));
float ndotv = max(0.0, dot(normal, viewDir));
float ndotl = max(0.0, dot(normal, lightDir));

// theta_o = arcsin ndotv (as angle)
// theta_i = arcsin ndotl
float sigma2 = roughness * roughness;
float A = 1.0 - (0.5 * sigma2) / (sigma2 + 0.33);
float B = (0.45 * sigma2) / (sigma2 + 0.09);
float alpha = max(ndotl, ndotv);
float beta = min(ndotl, ndotv);
return (ndotl * ONE_OVER_PI) * (A + B * vdotl * sin(alpha) * tan(beta));
}

float Lambert(vec3 normal, vec3 lightDir){
return max(dot(normal, lightDir), 0.0);
}


vec3 gamma(vec3 color, float gamma){
color.r = pow(color.r, gamma);
color.g = pow(color.g, gamma);
Expand All @@ -20,12 +51,14 @@ float fastFresnel(vec3 ViewDir, vec3 Normal){
return R0 + (1.0 - R0)*pow((1.0 - dot(ViewDir, Normal)),5.0);
}
vec4 ComputeLighting(Light light) {
float Roughness = 128.0;

float lambert = max(dot(GBuffer.normal, light.direction), 0.0);
float lambert = Lambert(GBuffer.normal, light.direction);
lambert = OrenNayar(GBuffer.normal, light.direction, normalize(GBuffer.position), mod(g_Time, 1.0));

vec4 DiffuseColor = vec4(0, 0, 0, 0);
vec4 SpecularColor = vec4(0, 0, 0, 0);
float Roughness = 1.0;

float specPower = pow(2.0, (10.0 * (Roughness / 255.0) + 1.0));


Expand All @@ -34,7 +67,7 @@ vec4 ComputeLighting(Light light) {
#ifdef SPECULAR
vec3 vsCameraDir = normalize(GBuffer.position);
vec3 reflection = reflect(light.direction, GBuffer.normal);
float specular = max(dot(normalize(vsCameraDir), normalize(reflection)), 0.0);
float specular = max(dot(vsCameraDir, normalize(reflection)), 0.0);
specular = pow(specular, specPower);
if (specular > 0.0) {
SpecularColor = vec4(1.0) * GBuffer.specular * specular;
Expand All @@ -44,7 +77,7 @@ vec4 ComputeLighting(Light light) {
#endif


return DiffuseColor + SpecularColor*0.0;
return DiffuseColor + SpecularColor * 0.0;
}

// Other uses.. later on
Expand Down
12 changes: 6 additions & 6 deletions src/me/merciless/dmonkey3/LightRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public class LightRenderer implements GBufferListener, SceneProcessor {
private Material material;
private boolean gbufferChanged = false;
private Camera camera;

private Vector3f vsLightDir = new Vector3f();

private DirectionalLight dl = new DirectionalLight();

private LightRenderer(AssetManager assets) {
this.fullscreenQuad = new Geometry("LightRenderQuad", new Quad(1, 1));
material = new Material(assets, "DMonkey/AmbientLight.j3md");
material.setColor("Color", dl.getColor());
dl.setColor(new ColorRGBA(CSSColor.LightSkyBlue).interpolateLocal(ColorRGBA.White, 0.5f));


dl.setColor(ColorRGBA.White.clone());
fullscreenQuad.setMaterial(material);
fullscreenQuad.setCullHint(Spatial.CullHint.Never);

dl.setDirection(Vector3f.UNIT_XYZ);
}

Expand Down Expand Up @@ -96,11 +96,11 @@ public void postQueue(RenderQueue rq) {
public void postFrame(FrameBuffer out) {
updateMaterial();
vsLightDir.set(camera.getViewMatrix().mult(dl.getDirection().normalizeLocal()).normalize());

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);
Expand Down
2 changes: 2 additions & 0 deletions src/me/merciless/dmonkey3/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
Expand All @@ -29,6 +30,7 @@ public class TestCase extends SimpleApplication {
@Override
public void simpleInitApp() {
stateManager.attach(deferred = new DeferredRenderState());
flyCam.setMoveSpeed(10);

material = assetManager.loadMaterial("TestLightMaterial.j3m");
Geometry geom = new Geometry("BoxNode", new Box(1, 1, 1));
Expand Down

0 comments on commit c69b875

Please sign in to comment.