Phong Shading

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)

:smiley: wow, thanks man.

Nice stuff! Added to JME Related videos :slight_smile:



That's just flat textured ground the horse is standing on right? I suppose this wasn't intentional, but your version seems to add a hint of change in depth in the cracks as the light moves from one side to another.



Also I'm wondering, are these extremes we're looking at or is this how you imagine it would show in a game? I am merely referring to the last few seconds of the video, where it looks like the horse is about to be toast at the hand of a giant magnifying glass' focused sunbeam ;D

erlend_sh said:

Nice stuff! Added to JME Related videos :)

That's just flat textured ground the horse is standing on right? I suppose this wasn't intentional, but your version seems to add a hint of change in depth in the cracks as the light moves from one side to another.

Also I'm wondering, are these extremes we're looking at or is this how you imagine it would show in a game? I am merely referring to the last few seconds of the video, where it looks like the horse is about to be toast at the hand of a giant magnifying glass' focused sunbeam ;D


About the ground - not intentionally, and i didn't even notice it until now.
Lol, yeah they're extremes, it wouldn't be wize to kill my horse by burning it ingame now would it? lol  :P
I'm thinking on saving the bloom effect for the metal gear that the character will be wearing in my game, along with a reflection shader, i guess, or for special items(make them glow).

About my project, i intend to program a type of MMORPG(yep, another one of those...) as an Applet.
I've got a team working on the models and 2D art, and we're planning to finish a "mock-up" and show it to investors in about 2 months from now, hopefuly, to get funded and hire more programmers(since currently i'm the only one in the team).

I'm using the new java plug-in system with a JNLP file (like webstart) to download and link to the native libs.(the LWJGL applet launcher gave me tons of errors).

I'll update on future progress  :wink:

and again,
Thanks for this great Engine!

wow, the non-bloom phong shader is absolutely beautiful!

sbook said:

wow, the non-bloom phong shader is absolutely beautiful!


Yeah I agree. The bloom examples I think had a bit too much bloom :/