Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can you explain these shaders?  (Read 1102 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC
« Posted 2013-09-17 03:39:32 »

I'm currently trying to learn how to add lights in LWJGL through shaders. Google search bought me these two but I don't understand them. Can you explain them to me please?

shader.vert

1  
2  
3  
4  
5  
6  
7  
8  
9  
varying vec3 normal;
varying vec3 vertex;

void main(void)  
{    
   vertex = vec3(gl_ModelViewMatrix * gl_Vertex);
   normal = normalize(gl_NormalMatrix * gl_Normal);
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
}

shader.frag

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
varying vec3 normal;
varying vec3 vertex;

void main (void)  
{  
   vec3 light = normalize(gl_LightSource[0].position.xyz - vertex);  
   vec3 eyeCoords = normalize(-vertex);
   vec3 reflection = normalize(-reflect(light, normal));  
 
   //calculate Ambient Term:  
   vec4 ambientTerm = gl_FrontLightProduct[0].ambient;    

   //calculate Diffuse Term:  
   vec4 diffuseTerm = gl_FrontLightProduct[0].diffuse * max(dot(normal, light), 0.0);
   diffuseTerm = clamp(diffuseTerm, 0.0, 1.0);    
   
   // calculate Specular Term:
   vec4 specularTerm = gl_FrontLightProduct[0].specular * pow(max(dot(reflection, eyeCoords),0.0), 0.3 * gl_FrontMaterial.shininess);
   specularTerm = clamp(specularTerm, 0.0, 1.0);
   
   // write Total Color:  
   gl_FragColor = gl_FrontLightModelProduct.sceneColor + ambientTerm + diffuseTerm + specularTerm;    
}

The main problem in them is the terms and how they works.

Offline kramin42
« Reply #1 - Posted 2013-09-17 04:22:04 »

light
is the direction of the light source from the vertex.
eyeCoords
is the direction of the camera from the vertex.
reflection
is the direction that the light would be reflected by the face.

1  
2  
   //calculate Ambient Term:  
   vec4 ambientTerm = gl_FrontLightProduct[0].ambient;  

The ambient term is always the same.

1  
2  
3  
   //calculate Diffuse Term:  
   vec4 diffuseTerm = gl_FrontLightProduct[0].diffuse * max(dot(normal, light), 0.0);
   diffuseTerm = clamp(diffuseTerm, 0.0, 1.0);

The diffuse term depends on how aligned the face is with the light source; if the face's normal vector is in the same direction as the light source, it is fully lit. If it is perpendicular then is is not lit at all. The dot product essentially gives a measure of how aligned
normal
and
light
are.

1  
2  
3  
   // calculate Specular Term:
   vec4 specularTerm = gl_FrontLightProduct[0].specular * pow(max(dot(reflection, eyeCoords),0.0), 0.3 * gl_FrontMaterial.shininess);
   specularTerm = clamp(specularTerm, 0.0, 1.0);

The specular term depends on how aligned the camera is with the reflected light. The calculation is similar to above except the effect is transformed with a power to adjust the size of the spot you see, so that a matt sphere would have a larger bright spot than a shiny one.

Hopefully that makes it a bit clearer, check out this wikipedia page too:
http://en.wikipedia.org/wiki/Diffuse_reflection

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline davedes
« Reply #2 - Posted 2013-09-17 04:22:46 »

It's a standard lambertian illumination model. More info here:
https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6

I also list some 3D-specific links toward the bottom.

If you're working with shaders I'd suggest using custom attributes instead of relying on gl_Normal, gl_Vertex, gl_Light and so forth. These are deprecated in the programmable pipeline.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-09-17 10:43:03 »

Doing lighting correct is quite a difficult undertaking. The old way of just adding some phong specular to the dot product of the normal and light vector has proven to be not really correct and difficult for artists to control.
Already some years now something called physical correct lighting is the standard, but no so easy to set up. There are quite a lot of papers and presentations out there for the ones interested.

Here is my code I use which works quite well for me: github gist

Here some really good blog posts explain everything:
http://seblagarde.wordpress.com/2011/08/17/hello-world
http://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/
Offline quew8

JGO Coder


Medals: 33



« Reply #4 - Posted 2013-09-17 18:54:12 »

If you're looking for an introduction to shaders, then I recommend Lighthouse3D: http://www.lighthouse3d.com/ It's under tutorials and then either GLSL or GLSL core.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!