• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
OmarShehata

Simple diffuse light. Weird Behavior.

8 posts in this topic

So I'm trying to make a simple lighting effect. I have my normal map and my object posted below.

[img]http://4urentertainment.org/storage/Tile.png[/img][img]http://4urentertainment.org/storage/Normal.png[/img]

And my code in GLSL is written below:

[CODE]
uniform sampler2D normal;
uniform vec2 mouse;
uniform float Za;
vec4 effect(vec4 color,sampler2D tex,vec2 tc,vec2 pc)
{
vec4 img_color = texture2D( tex, tc );
vec4 normalColor = texture2D( normal, tc );
float X = (mouse.x - pc.x);
float Y = (mouse.y - pc.y);
float Z = Za;
float dotProduct = X * normalColor.r + Y * normalColor.g + Z * normalColor.b;
dotProduct /= sqrt(X * X + Y * Y + Z * Z) * sqrt(normalColor.r * normalColor.r + normalColor.g * normalColor.g + normalColor.b * normalColor.b);
float factor = dotProduct;
img_color.r = img_color.r + factor;
img_color.g = img_color.g + factor;
img_color.b = img_color.b + factor;
return img_color;
}
[/CODE]


So basically I had a few questions:

[b]Is my math correct?[/b]
What I'm trying to do is get the vector of the mouse (the light source for now) to the pixel, and the normal of that pixel, and then get the dot product between them, normalize that, and that is the brightness factor. However the angle [i]seems [/i]to be a bit off. I just wanted to double check if my questions were all right because debugging on the GPU doesn't seem to be very easy

[b]For the usual lighting methods, do you multiply by a distance factor?[/b]
Naturally when a light is further away the object should be dimmer. But this doesn't seem to be the case with this approach. Should I just multiply by some arbitrary distance factor?

Finally, I'm basically using 3D light for a 2D game. Although it seems pretty simple in the implementation though. I'm told the [b]Phong Reflection Model [/b]may be better suited in this case. Any thoughts on that?

Thanks in advance! Any help or tips on anything would be appreciated! Edited by OmarShehata
0

Share this post


Link to post
Share on other sites
First off, your glsl haa no line breaks making it incredible hard to read.

Then your left texture, what is it ? The diffuse map, in this case it would contain pseodo light information which will result in a weird look at the edges.

[quote name='OmarShehata' timestamp='1349855361' post='4988633']
For the usual lighting methods, do you multiply by a distance factor?
[/quote]
Yes, it is even sometimes useful to not only reduce the intensity with distance but to clamp it to a certain area (ie. sphere). A simple,smooth clamping function would be

[CODE]
normalized_distance = min(1.0, dist / max_dist);
final_intensity = (1.0 - normalized_distance * normalized_distance) * intensity;

[/CODE]

[quote name='OmarShehata' timestamp='1349855361' post='4988633']
I'm told the Phong Reflection Model may be better suited in this case.
[/quote]
Better suited compared to what ?
You should choose a diffuse model (ie lambert) and a specular model (ie phong or blinn).


Here you can see the [url="http://wiki.blender.org/index.php/Doc:2.4/Manual/Materials/Properties/Diffuse_Shaders"]diffuse lighting [/url]model demonstrated in blender and here the [url="http://wiki.blender.org/index.php/Doc:2.4/Manual/Materials/Properties/Specular_Shaders"]specular models[/url]. Lambert,phong,blinn are the easiest one, the others are more complicated. Edited by Ashaman73
1

Share this post


Link to post
Share on other sites
Thanks for the quick reply Ashaman!

[quote name='Ashaman73' timestamp='1349863121' post='4988661']
First off, your glsl haa no line breaks making it incredible hard to read.
[/quote]

Sorry about that, format messed up when I copied it. It should have line breaks now.

[quote name='Ashaman73' timestamp='1349863121' post='4988661']
Then your left texture, what is it ? The diffuse map, in this case it would contain pseodo light information which will result in a weird look at the edges.
[/quote]

Well the texture on the left right now is the actual image of the object that I'm applying lighting too (since it's a 2D game). And the normal map has the RGB values that are the XYZ of the normal at each pixel as I understand.

[quote name='Ashaman73' timestamp='1349863121' post='4988661']
[quote name='OmarShehata' timestamp='1349855361' post='4988633']
I'm told the Phong Reflection Model may be better suited in this case.
[/quote]
Better suited compared to what ?
You should choose a diffuse model (ie lambert) and a specular model (ie phong or blinn).
[/quote]

Well..compared to what I'm doing right now.

Speaking of which, everyone who looks at my shader code either says that my shading model is weird or incomplete, but it really makes sense to me.

I mean, I'm getting the angle between the light source and the normal, and the brightness is proportional to that, shouldn't that work on its own?

I was planning on implementing a well known light model but thought if I tried doing it myself first I'd be able to understand it better.
0

Share this post


Link to post
Share on other sites
[quote name='OmarShehata' timestamp='1349874179' post='4988700']
Sorry about that, format messed up when I copied it. It should have line breaks now.
[/quote]
The forum can't handle code inside the code-tag right now unfortunately, so it's still a single line :(

[quote name='OmarShehata' timestamp='1349874179' post='4988700']
Well the texture on the left right now is the actual image of the object that I'm applying lighting too (since it's a 2D game). And the normal map has the RGB values that are the XYZ of the normal at each pixel as I understand.
[/quote]
Like he said, it seems like it has some kind of lighting baked into it (hand painted though), which might look a bit odd when you're lighting it.

[quote name='OmarShehata' timestamp='1349874179' post='4988700']
I mean, I'm getting the angle between the light source and the normal, and the brightness is proportional to that, shouldn't that work on its own?
[/quote]
The brightness should in the simplest case be proportional to the cosine of the angle between the light source and the normal. Not quite sure what you're doing as the code is unreadable with this forum bug :(
You might want to paste it directly without the code-tag, change the font to Courier New and you might also want to color the syntax a bit, to help the reader :D
1

Share this post


Link to post
Share on other sites
[quote name='CryZe' timestamp='1349876346' post='4988713']
The brightness should in the simplest case be proportional to the cosine of the angle between the light source and the normal. Not quite sure what you're doing as the code is unreadable with this forum bug [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
You might want to paste it directly without the code-tag, change the font to Courier New and you might also want to color the syntax a bit, to help the reader [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
[/quote]

Yeah that's what I meant. After getting the dot product and normalizing I should have the cosine of the angle.

I put up the code on pastebin:

http://pastebin.com/W4UMH593
0

Share this post


Link to post
Share on other sites
Looks ok, as far as I can tell. But you should multiply the factor with the image color instead of adding it. You might want to add a constant "ambient term" to your factor before multiplying it to have some kind of ambient lighting, so that the non-lit area's are not completely black (spherical harmonics or environment mapping are better approaches for ambient lighting though).

Also, the normals in your image are stored in the range (0, 1). But what you actually want are normals in the range (-1, 1). So you have to load them like this:
vec4 normalColor = 2 * texture2D( normal, tc ) - 1;

As a distance attenuation factor you might want to divide the resulting illumination by the light vectors length squared. This would be the most physically correct distance attenuation factor. Edited by CryZe
1

Share this post


Link to post
Share on other sites
Here's your modified code, including shifting the texture into [-1..1] and an ambient term without specular (only diffuse yet).

[CODE]
uniform sampler2D normal;
uniform vec2 mouse;
uniform float Za;
vec4 effect(vec4 color,sampler2D tex,vec2 tc,vec2 pc)
{
vec4 img_color = texture2D( tex, tc );
vec4 normalColor = texture2D( normal, tc );
vec3 lightDir = normalize(vec3( (mouse.x - pc.x), (mouse.y - pc.y), Za));
vec3 normal = normlaize(normalColor.xyz*vec3(2.0)-vec3(1.0));
float dotProduct = dot(lightDir,normal);
float ambientFactor = 0.2;
img_color *= mix(vec3(1.0) , dotProduct, ambientFactor);

return img_color;
}
[/CODE]

Once this work you can adjust the high of the light, consider the light distance and add a specular term. Edited by Ashaman73
1

Share this post


Link to post
Share on other sites
[quote name='CryZe' timestamp='1349878267' post='4988727'](spherical harmonics or environment mapping are better approaches for ambient lighting though).[/quote]

Here's little code snippet for SH lighting:
[code]
varying vec3 Normal;

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

// Constants for Old Town Square lighting
const vec3 L00 = vec3( 0.871297, 0.875222, 0.864470);
const vec3 L1m1 = vec3( 0.175058, 0.245335, 0.312891);
const vec3 L10 = vec3( 0.034675, 0.036107, 0.037362);
const vec3 L11 = vec3(-0.004629, -0.029448, -0.048028);
const vec3 L2m2 = vec3(-0.120535, -0.121160, -0.117507);
const vec3 L2m1 = vec3( 0.003242, 0.003624, 0.007511);
const vec3 L20 = vec3(-0.028667, -0.024926, -0.020998);
const vec3 L21 = vec3(-0.077539, -0.086325, -0.091591);
const vec3 L22 = vec3(-0.161784, -0.191783, -0.219152);

void main()
{
vec3 AmbientColor = C1 * L22 * (Normal.x * Normal.x - Normal.y * Normal.y) +
C3 * L20 * Normal.z * Normal.z +
C4 * L00 -
C5 * L20 +
2.0 * C1 * L2m2 * Normal.x * Normal.y +
2.0 * C1 * L21 * Normal.x * Normal.z +
2.0 * C1 * L2m1 * Normal.y * Normal.z +
2.0 * C2 * L11 * Normal.x +
2.0 * C2 * L1m1 * Normal.y +
2.0 * C2 * L10 * Normal.z;

gl_FragCoord = AmbientColor;
}
[/code]

It was taken from some article about sh... I can't find it.

You can use only 2 sh bands and you will still get nice ambient light. Here's code if you want to use two bands:
[code]
varying vec3 Normal;

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

// Constants for Old Town Square lighting
const vec3 L00 = vec3( 0.871297, 0.875222, 0.864470);
const vec3 L1m1 = vec3( 0.175058, 0.245335, 0.312891);
const vec3 L10 = vec3( 0.034675, 0.036107, 0.037362);
const vec3 L11 = vec3(-0.004629, -0.029448, -0.048028);

void main()
{

vec3 AmbientColor = C4 * L00 -
2.0 * C2 * L11 * Normal.x +
2.0 * C2 * L1m1 * Normal.y +
2.0 * C2 * L10 * Normal.z;

gl_FragCoord = AmbientColor;
}
[/code] Edited by MrOMGWTF
1

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1349933700' post='4988967']
Here's your modified code, including shifting the texture into [-1..1] and an ambient term without specular (only diffuse yet).
[/quote]

Thanks for taking the time to write that Ashaman!

I still have a few questions though.

I'm not sure I understand what's going on in this line:

[CODE]vec3 normal = normalize(normalColor.xyz*vec3(2.0)-vec3(1.0));[/CODE]

Actually..reading it again, this is the part where you convert it from [0,1] to [-1,-1] right? I'm assuming this affects the angle of the light? Because that seems to be what fixes everything.

Also this line:

[CODE]img_color *= mix(vec3(1.0) , dotProduct, ambientFactor);[/CODE]

What's being mixed and why? And what's the purpose of that (1.0) ?

Finally, the reason I was adding to the color instead of multiplying is that, when I multiply, the color of the material or the object seems to turn greyish as it darkens. Like it loses its color. Which makes sense. But if I add the color then, with some tweaks, it looks a lot better because the object's color just gets brighter, more towards white, and darker, towards black, without really seeming to lose its color.

Do any lighting models add/subtract to the color instead of multiplying or is that just a bad idea?

[quote name='MrOMGWTF' timestamp='1350196241' post='4989987']
Here's little code snippet for SH lighting:
[/quote]

Thanks for this as well! I'll try to implement that and post again if I have any problems.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0