How to implements a custom GLSL Shader?

Hi all. Recently I by a question puzzle for a long time, that is how to implements a custom GLSL Shader?

I knew the jMonkeyEngine3 is a Shader Base Engine, and provide many fit shader(work in j3md file).Now I also would like to use j3md file and tools generated by the standard GLSL *.vert and *.frag file to implement custom Shader.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders

I have read this tutorial in detail.

I should be in accordance with this step to operate, but always unsuccess.

So…I need you help…

there are two file(Glass.frag;Glass.vert. from http://www.3dshaders.com).

What should I do to make them work correctly under the JME3?



my test:

glass.j3md

[xml]MaterialDef glass {
MaterialParameters {

Vector3 LightPos
Vector3 BaseColor
Float Depth
Float MixRatio
Float FrameWidth
Float FrameHeight
Texture2D EnvMap
Texture2D RefractionMap
}
Technique {

VertexShader GLSL120 : Common/MatDefs/Misc/Glass.vert
FragmentShader GLSL120 : Common/MatDefs/Misc/Glass.frag

WorldParameters {
WorldViewProjectionMatrix
NormalMatrix
WorldViewMatrix
}

RenderState {

}
}
}[/xml]
Glass.vert

[xml]//
// Vertex shader for environment mapping with an
// equirectangular 2D texture and refraction mapping
// with a background texture blended together using
// the fresnel terms
//
// Author: Jon Kennedy, based on the envmap shader by John Kessenich, Randi Rost
//
// Copyright (c) 2002-2006 3Dlabs Inc. Ltd.
//
// See 3Dlabs-License.txt for license information
//

uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_WorldViewMatrix;
uniform mat3 g_NormalMatrix;
uniform mat4 g_ViewMatrix;

varying vec3 Normal;
varying vec3 EyeDir;
varying vec4 EyePos;
varying float LightIntensity;

uniform vec3 m_LightPos;

attribute vec3 inPosition;
attribute vec3 inNormal;

void main(void)
{
gl_Position = ftransform();
Normal = normalize(g_NormalMatrix * inNormal);
vec4 pos = g_WorldViewMatrix * vec4(inPosition, 1.0);
EyeDir = pos.xyz;
EyePos = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
LightIntensity = max(dot(normalize(m_LightPos - EyeDir), Normal), 0.0);
}[/xml]
Glass.frag

[xml]
//
// Fragment shader for environment mapping with an
// equirectangular 2D texture and refraction mapping
// with a background texture blended together using
// the fresnel terms
//
// Author: Jon Kennedy, based on the envmap shader by John Kessenich, Randi Rost
//
// Copyright (c) 2002-2006 3Dlabs Inc. Ltd.
//
// See 3Dlabs-License.txt for license information
//

const vec3 Xunitvec = vec3 (1.0, 0.0, 0.0);
const vec3 Yunitvec = vec3 (0.0, 1.0, 0.0);

uniform vec3 m_BaseColor;
uniform float m_Depth;
uniform float m_MixRatio;

// need to scale our framebuffer - it has a fixed width/height of 2048
uniform float m_FrameWidth;
uniform float m_FrameHeight;

uniform sampler2D m_EnvMap;
uniform sampler2D m_RefractionMap;

varying vec3 Normal;
varying vec3 EyeDir;
varying vec4 EyePos;
varying float LightIntensity;

void main (void)
{
// Compute reflection vector
vec3 reflectDir = reflect(EyeDir, Normal);

// Compute altitude and azimuth angles

vec2 index;

index.y = dot(normalize(reflectDir), Yunitvec);
reflectDir.y = 0.0;
index.x = dot(normalize(reflectDir), Xunitvec) * 0.5;

// Translate index values into proper range

if (reflectDir.z >= 0.0)
index = (index + 1.0) * 0.5;
else
{
index.t = (index.t + 1.0) * 0.5;
index.s = (-index.s) * 0.5 + 1.0;
}

// if reflectDir.z >= 0.0, s will go from 0.25 to 0.75
// if reflectDir.z < 0.0, s will go from 0.75 to 1.25, and
// that's OK, because we've set the texture to wrap.

// Do a lookup into the environment map.

vec3 envColor = vec3 (texture2D(m_EnvMap, index));

// calc fresnels term. This allows a view dependant blend of reflection/refraction
float fresnel = abs(dot(normalize(EyeDir), Normal));
fresnel *= m_MixRatio;
fresnel = clamp(fresnel, 0.1, 0.9);

// calc refraction
vec3 refractionDir = normalize(EyeDir) - normalize(Normal);

// Scale the refraction so the z element is equal to depth
float depthVal = m_Depth / -refractionDir.z;

// perform the div by w
float recipW = 1.0 / EyePos.w;
vec2 eye = EyePos.xy * vec2(recipW);

// calc the refraction lookup
index.s = (eye.x + refractionDir.x * depthVal);
index.t = (eye.y + refractionDir.y * depthVal);

// scale and shift so we're in the range 0-1
index.s = index.s / 2.0 + 0.5;
index.t = index.t / 2.0 + 0.5;

// as we're looking at the framebuffer, we want it clamping at the edge of the rendered scene, not the edge of the texture,
// so we clamp before scaling to fit
float recip1k = 1.0 / 2048.0;
index.s = clamp(index.s, 0.0, 1.0 - recip1k);
index.t = clamp(index.t, 0.0, 1.0 - recip1k);

// scale the texture so we just see the rendered framebuffer
index.s = index.s * m_FrameWidth * recip1k;
index.t = index.t * m_FrameHeight * recip1k;

vec3 RefractionColor = vec3 (texture2D(m_RefractionMap, index));

// Add lighting to base color and mix
vec3 base = LightIntensity * m_BaseColor;
envColor = mix(envColor, RefractionColor, fresnel);
envColor = mix(envColor, base, 0.2);

gl_FragColor = vec4 (envColor, 1.0);
}
[/xml]
my test program

[java]import com.jme3.app.SimpleApplication;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class TestBox extends SimpleApplication {

public static void main(String[] args){
TestBox app = new TestBox();
app.start();
}

@Override
public void simpleInitApp() {

DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(-1,-1,-1));
rootNode.addLight(dl);

Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry("Box", b);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/glass.j3md");
mat.setVector3("LightPos", new Vector3f(0.0f, 0.0f, 4.0f));
mat.setVector3("BaseColor", new Vector3f(0.4f, 0.4f, 1.0f));
mat.setFloat("Depth", 0.1f);
mat.setFloat("MixRatio", 1.0f);
mat.setFloat("FrameWidth", 255.0f);
mat.setFloat("FrameHeight", 255.0f);
mat.setTexture("EnvMap", assetManager.loadTexture("Interface/Fonts/Default.png"));
mat.setTexture("RefractionMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));



geom.setMaterial(mat);
rootNode.attachChild(geom);
}

}
[/java]

after runing console info

[xml]
run:
2011-5-11 15:28:25 com.jme3.system.JmeSystem initialize
信息: Running on jMonkey Engine 3 Alpha 0.6
2011-5-11 15:28:25 com.jme3.system.Natives extractNativeLibs
信息: Extraction Directory #1: file:/E:/NetBeansProjects/3d/trunk/engine/build/classes/com/jme3/system/
2011-5-11 15:28:25 com.jme3.system.Natives extractNativeLibs
信息: Extraction Directory #2: E:NetBeansProjects3dtrunkengine
2011-5-11 15:28:25 com.jme3.system.Natives extractNativeLibs
信息: Extraction Directory #3: E:NetBeansProjects3dtrunkengine
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglAbstractDisplay run
信息: Using LWJGL 2.7.1
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglDisplay createContext
信息: Selected display mode: 800 x 600 x 0 @0Hz
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglContext printContextInitInfo
信息: Adapter: nv4_disp
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglContext printContextInitInfo
信息: Driver Version: 6.14.12.6099
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglContext printContextInitInfo
信息: Vendor: NVIDIA Corporation
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglContext printContextInitInfo
信息: OpenGL Version: 3.3.0
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglContext printContextInitInfo
信息: Renderer: GeForce GTS 250/PCI/SSE2
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglContext printContextInitInfo
信息: GLSL Ver: 3.30 NVIDIA via Cg compiler
2011-5-11 15:28:25 com.jme3.system.lwjgl.LwjglTimer <init>
信息: Timer resolution: 1000 ticks per second
2011-5-11 15:28:25 com.jme3.renderer.lwjgl.LwjglRenderer initialize
信息: Caps: [FrameBuffer, FrameBufferMRT, FrameBufferMultisample, TextureMultisample, OpenGL20, OpenGL21, OpenGL30, OpenGL31, OpenGL32, ARBprogram, GLSL100, GLSL110, GLSL120, GLSL130, GLSL140, GLSL150, VertexTextureFetch, TextureArray, TextureBuffer, FloatTexture, FloatColorBuffer, FloatDepthBuffer, PackedFloatTexture, SharedExponentTexture, PackedFloatColorBuffer, TextureCompressionLATC, NonPowerOfTwoTextures, MeshInstancing, VertexBufferArray]
2011-5-11 15:28:25 com.jme3.asset.DesktopAssetManager <init>
信息: DesktopAssetManager created.
2011-5-11 15:28:25 com.jme3.renderer.Camera <init>
信息: Camera created (W: 800, H: 600)
2011-5-11 15:28:25 com.jme3.renderer.Camera <init>
信息: Camera created (W: 800, H: 600)
2011-5-11 15:28:25 com.jme3.input.lwjgl.LwjglMouseInput initialize
信息: Mouse created.
2011-5-11 15:28:25 com.jme3.input.lwjgl.LwjglKeyInput initialize
信息: Keyboard created.
2011-5-11 15:28:25 com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
信息: AudioRenderer supports 64 channels
2011-5-11 15:28:25 com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
信息: Audio effect extension version: 1.0
2011-5-11 15:28:25 com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
信息: Audio max auxilary sends: 1
2011-5-11 15:28:25 com.jme3.material.MaterialDef <init>
信息: Loaded material definition: Unshaded
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Gui Node)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (BitmapFont) attached to this node (null)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (null) attached to this node (Statistics View)
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (Statistics View) attached to this node (Gui Node)
2011-5-11 15:28:25 com.jme3.material.MaterialDef <init>
信息: Loaded material definition: glass
2011-5-11 15:28:25 com.jme3.scene.Node attachChild
信息: Child (Box) attached to this node (Root Node)
2011-5-11 15:28:25 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation
信息: Uniform m_VertexColor is not declared in shader.

[/xml]
Can not see anything after startup. Please help me. Thank you.

Please dont double post:

http://hub.jmonkeyengine.org/groups/graphics/forum/topic/how-to-implements-a-custom-glsl-shader/