package com.haxademic.demo.draw.filters.shaders;

import com.haxademic.core.app.P;
import com.haxademic.core.app.PAppletHax;
import com.haxademic.core.data.constants.PRenderers;
import com.haxademic.core.debug.DebugView;
import com.haxademic.core.draw.context.PG;
import com.haxademic.core.file.FileUtil;
import com.haxademic.core.hardware.mouse.Mouse;
import com.haxademic.core.media.DemoAssets;

import processing.core.PGraphics;
import processing.core.PImage;
import processing.opengl.PShader;

public class Demo_DisplacementMapShader
extends PAppletHax {
	public static void main(String args[]) { arguments = args; PAppletHax.main(Thread.currentThread().getStackTrace()[1].getClassName()); }

	PImage base;
	PImage mapSource;
	PGraphics map;
	PShader texShader;
	int mode = 0;

	protected void firstFrame() {
		base = DemoAssets.textureJupiter();
		mapSource = DemoAssets.textureNebula();
		map = p.createGraphics(p.width, p.height, PRenderers.P3D);
		texShader = loadShader(FileUtil.getPath("haxademic/shaders/filters/displacement-map.glsl"));
	}

	protected void drawApp() {
		background(255);
		
		// rotate map for visibility
		map.beginDraw();
		PG.setDrawCenter(map);
		PG.setCenterScreen(map);
		map.rotate(p.frameCount * 0.01f);
		map.image(mapSource, 0, 0, mapSource.width * 2, mapSource.height * 2);
		map.endDraw();

		// set mode
		mode = P.round(Mouse.yNorm * 7);
		texShader.set("map", map );
		texShader.set("mode", mode );
		texShader.set("amp", Mouse.xNorm/10f);
		
		// draw image and apply displacement map
		p.image(base, 0, 0);
		p.filter(texShader); 
		
		// debug
		DebugView.setValue("mode", mode);
		DebugView.setTexture("base", base);
		DebugView.setTexture("map", map);
		DebugView.setTexture("mapSource", mapSource);
	}
}


@@ -287,21 +287,50 @@
 ore.
-file.FileUtil
+draw.filters.pshader.DisplacementMapFilter
 ;%0Aim
@@ -488,42 +488,8 @@
 age;
-%0Aimport processing.opengl.PShader;
 %0A%0Apu
@@ -738,28 +738,8 @@
 ap;%0A
-%09PShader texShader;%0A
 %09int
@@ -926,103 +926,8 @@
 D);%0A
-%09%09texShader = loadShader(FileUtil.getPath(%22haxademic/shaders/filters/displacement-map.glsl%22));%0A
 %09%7D%0A%0A
@@ -1214,247 +1214,357 @@
 %09// 
-set mode%0A%09%09mode = P.round(Mouse.yNorm * 7);%0A%09%09texShader.set(%22map%22, 
+draw image to screen, to be displaced next%0A%09%09p.image(base, 0, 0);%0A%0A%09%09// set displace shader params & apply%0A%09%09mode = P.round(Mouse.yNorm * 7);%0A%09%09DisplacementMapFilter.instance(p).setMap(
 map
- 
 );%0A%09%09
-texShader.set(%22mode%22, 
+DisplacementMapFilter.instance(p).setMode(
 mode
- 
 );%0A%09%09
-texShader.set(%22amp%22, Mouse.xNorm/10f);%0A%09%09%0A%09%09// draw image and apply displacement map%0A%09%09p.image(base, 0, 0);%0A%09%09p.filter(texShader
+DisplacementMapFilter.instance(p).setAmp(Mouse.xNorm/10f);%0A%09%09DisplacementMapFilter.instance.applyTo(p.g
 );
- 
 %0A%09%09%0A

Update Demo_DisplacementMapShader.java