Hi guys, it hasn’t been long since I’ve joined the forums, I’ve been working with JME2 for some time now, and I have to say that its a great engine, easy yet powerful and fast.
I’ve implemented Phong Shading into my JME2 Applet:
http://www.youtube.com/watch?v=7r6fQ6ph6Tg
These are the effect files:
vert-
varying vec3 v_N;
void main () {
gl_Position = ftransform();
v_N = gl_NormalMatrix * gl_Normal;
gl_TexCoord[0] = gl_MultiTexCoord0;
}
frag-
varying vec3 v_N;
uniform float amb;
uniform sampler2D tex;
void main () {
vec3 N = normalize(v_N);
vec3 L = normalize(gl_LightSource[0].position.xyz);
vec3 H = normalize(gl_LightSource[0].halfVector.xyz);
vec3 ambient = vec3(1,1,1) * amb;
vec3 diffuse = vec3(1,1,1) * (1.0 - amb) * max(dot(L, N), 0.0);
vec3 specular = vec3(1, 1, 1) * pow(max(dot(H, N), 0.0), 16.0);
vec4 texcolor = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = vec4((ambient+diffuse+specular),1.0)* texcolor ;
}
all you have to do is load the shader files this way:
private void setPhong(Node model)
{
GLSLShaderObjectsState glslshader = display.getRenderer().createGLSLShaderObjectsState();
glslshader.load(getClass().getResource("phong.vert"),getClass().getResource("phong.frag")); //used getClass() becase its an applet
glslshader.setUniform("amb", 0.1f); // controls the ambient
glslshader.setUniform("tex", 0); // 0 being the Texture number from the TextureState you've connected to the model
model.setRenderState(glslshader);
}
I hope it'll help some of you, since when I've searched the forums for anything about Phong Shading i came up with nothing that was really helpful.
EDIT:
ATI Cards couldn't load the old frag file, i edited it now and it works.
Seems like Nvidia had no problem before - though i've made a mistake in the shader itself(trying to multiply Vec4 with Vec3 and not casting floats into vec3)