Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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't get GLSL per-pixel lighting to work.  (Read 2973 times)
0 Members and 1 Guest are viewing this topic.
Offline epicbo

Senior Newbie





« Posted 2012-04-22 01:14:16 »

I'm trying to implement per-pixel lighting with GLSL in my program and I just can't get it to work. Also, sorry for the long post.

Every face has the same color. This image of an ugly skull that I rendered with my shaders should explain the problem:



Here's my shaders. I removed everything unrelated.

Vertex shader

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
#version 130

in vec3 inPosition;
in vec4 inColor;
in vec3 inNormal;

smooth out vec4 color;
smooth out vec3 normal;
smooth out vec3 position;

uniform mat4 perspectiveMatrix;
uniform mat4 cameraTransformationMatrix;
uniform mat4 modelTransformationMatrix;

uniform vec3 lightPos;

void main()
{

   normal = inNormal;
   
   position = inPosition;
   
   color = inColor;
   
   vec4 pos  = cameraTransformationMatrix * vec4(inPosition, 1);
   
    gl_Position = perspectiveMatrix * pos;
 
}


Fragment shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
#version 130

smooth in vec4 color;
smooth in vec3 normal;
smooth in vec3 position;

uniform vec3 lightPos;

void main()
{

   vec3 lightDir = normalize(lightPos - position);

   float diffuseLightIntensity = max(0,dot(normalize(normal),lightDir));

   vec4 fragcolor = vec4(diffuseLightIntensity * color.rgb,color.a);
   
   fragcolor.rgb += vec3(0.2,0.2,0.2);

    gl_FragColor = fragcolor;
}


I've been trying to figure out what could cause this to happen. I've tried to change gl_FragColor to different things. For example, gl_FragColor = vec4(lightDir,1) draws the skull with interpolated colors (check image below).



This shows that lightDir varies with every fragment. So that can't be the problem. Now, if i put gl_FragColor = vec4(normalize(normal),1) I get this:



Which is expected, since every face has the same normal. Now, this part is what confuses me.

float diffuseLightIntensity = max(0,dot(normalize(normal),lightDir));

I take the dot product of the normal and the direction of the light. The normal remains the same for every part of the face, while the lightDir is different for every fragment of the face. Taking the dot product should give me a different value for each fragment, since lightDir is different for each fragment. Am I correct? Either way, diffuseLightIntensity remains the same for every fragment of the face, which can be seen on the first image.

I'm probably just misunderstanding something. If I'm doing something really stupid that I should be able to fix myself, then sorry for wasting your time. But hopefully I shouldn't make myself look like a fool.  Tongue

Any help is appreciated.  Smiley
Offline theagentd
« Reply #1 - Posted 2012-04-22 05:29:55 »

You're getting exactly what I expect you to get with those normals. You need to specify different normals per vertex so you can interpolate between them. If all vertices in a triangle have the same normal then "per-pixel" lighting is going to use that normal for all pixels --> same result as per vertex lighting.

The problem is the normal data you feed. The skull probably has 3 unique vertices for each triangle, and these vertices all have the same normal. I assume you loaded the model from file. If the format does not store vertex normals you need to calculate them yourself. One way to calculate the normal of a vertex is adding together the normals of all the triangles that share that vertex and then normalizing it (when loading the model, not in the shader).

The world need more shaders! Keep it up!

Myomyomyo.
Offline pitbuller
« Reply #2 - Posted 2012-04-22 09:00:46 »

Problem is allways in the normals.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline epicbo

Senior Newbie





« Reply #3 - Posted 2012-04-22 14:23:02 »

I get what you're saying but I don't believe that's the problem. I think I managed to smooth the normals, but the problem is still there. Please verify that I've understood everything about the lighting calculations correctly.

Currently I take the dot product of the normal (which remains constant for every face) and the normalized distance vector from the pixel to the light source. Take a look at this image:



Every black block represents a pixel, the yellow circle the light source, every red line a normal, and every green line the vector from the light source to the pixel.

The distance vector from the light source to the pixel will be different for every pixel since it's calculated in the fragment shader. The normal will of course remain the same for every pixel. As seen in the image, the angle between the two vectors varies so taking the dot product of the two should give different values for different pixels, right? Why isnt this the case?  Huh
Offline lhkbob

JGO Knight


Medals: 32



« Reply #4 - Posted 2012-04-22 15:55:37 »

Are you using a direction light or a point light?

Offline epicbo

Senior Newbie





« Reply #5 - Posted 2012-04-22 16:02:31 »

Point light.
Offline pitbuller
« Reply #6 - Posted 2012-04-22 16:26:42 »

It's the normals.
Offline theagentd
« Reply #7 - Posted 2012-04-22 16:47:14 »

Your shader code works perfectly fine. Try to put a light very very close to the skeleton and you´ll see that the color really varies per pixel.

Are you using an index buffer to draw the skeleton or not? I think you´re creating unique vertices for each triangle, am I correct? And you set the normal of all those vertices to the same value. Currently, the light direction vector is per pixel, but the vertex normals aren´t even per vertex, but per triange duplicated to all 3 vertices. "Smooth" interpolation between 3 equal values is still the same value.

Myomyomyo.
Offline epicbo

Senior Newbie





« Reply #8 - Posted 2012-04-22 16:59:46 »

Quote
Your shader code works perfectly fine. Try to put a light very very close to the skeleton and you´ll see that the color really varies per pixel.

You're right, it does work the way it should! Oh wow, now I feel really stupid. I guess the light was just too far away to make the lighting vary pixel by pixel.

Quote
Are you using an index buffer to draw the skeleton or not? I think you´re creating unique vertices for each triangle, am I correct?

You are correct, but I smoothed the normals the way you suggested earlier, by adding the normal of all the vertices that has the same position and then normalizing them. However, there must be something wrong with the code that does that. Anyway, thanks for you help. You've been really helpful  Smiley
Offline theagentd
« Reply #9 - Posted 2012-04-23 04:08:27 »

No problem. I went through the same problems when I did the same thing as you. It's really hard to actually see if the lighting is accurate...  Wink

Myomyomyo.
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.

BurntPizza (10 views)
2014-09-19 03:14:18

Dwinin (29 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (60 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!