• 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
BenjaminSergent

Problems with Normal Mapping

6 posts in this topic

Hello,

 

I'm trying to implement lighting with normal maps in my 2D prototype. I'm stuck on a strange issue where the lighting fades out below my lights. It looks like the dot product between the light direction and the normal must be negative below the light, but I can't get a grasp on why that's happening.

 

Here is an image of the problem:

d2wjxL1.png

 

No matter where I place the light(s), the area below each light stays dark.

This is the code for my frag shader. I've simplified it to try and narrow the problem down:

#version 130

in vec2 texCoords;
in vec4 color;

uniform float numPointLights;
uniform sampler2D texture;
uniform sampler2D textureN;
uniform vec4 ambient;
uniform vec4 pointLightInfo[100];
uniform vec2 resolution;

void main()                                  
{
       vec4 diffuseColor = texture2D(texture, texCoords) * color;
	
	vec3 normalMap = texture2D(textureN, texCoords).rgb;
	normalMap.g = -normalMap.g; // Our normal map program produces an inverted y axis.
	
	normalMap = normalMap * 2 - 1; // Normalize to [-1, 1]
	
	vec2 screenPos = gl_FragCoord.xy;
	screenPos.x /= resolution.x;
	screenPos.y /= resolution.y;	
	screenPos = screenPos * 2 - 1; // Convert to NDC
	
	vec3 finalColor = (ambient.rgb * ambient.a) * diffuseColor.rgb;		
	for(int index = 0; index < numPointLights; index++) {		
                vec4 lightScreenPos = pointLightInfo[2*index]; // Pre-transformed to NDC
		vec3 lightVector = vec3(lightScreenPos.xy - screenPos.xy, lightScreenPos.z);	
		lightVector.x *= resolution.x / resolution.y;
		
		float lightDist = length(lightVector);
		
		vec3 normal = normalize(normalMap);
		vec3 lightDir = lightVector / lightDist;
		
		vec4 lightColor = pointLightInfo[2*index+1];
                float normalIntensity = max(dot(normal, lightDir), 0.0);
		vec3 diffuse = (lightColor.rgb * lightColor.a) * normalIntensity;
		
		vec3 falloff = vec3(0.009,0.09,0.5);
		
		float attenuation = 1.0 / ( falloff.x + (falloff.y*lightDist) + (falloff.z*lightDist*lightDist) );
		
	        vec3 intensity = diffuse * attenuation;
	        finalColor += diffuseColor.rgb * intensity;
    }

    gl_FragColor = vec4(finalColor, diffuseColor.a);
}

In case it's relevant, here is the level's normal map:

ODY3wtF.jpg


I'm at a bit of a loss for how to proceed. I'm hoping I made a silly error that will be quickly obvious to someone. Thanks in advance for any help.

0

Share this post


Link to post
Share on other sites

 

vec2 screenPos = gl_FragCoord.xy;
    screenPos.x /= resolution.x;
    screenPos.y /= resolution.y;    
    screenPos
= screenPos * 2 - 1; // Convert to NDC

In case gl_FragCoord is not in projection space (NDC) after vertex function, this is just ok, but consider that.

 

 

vec4 lightScreenPos = pointLightInfo[2*index]; // Pre-transformed to NDC
        vec3 lightVector = vec3(lightScreenPos.xy - screenPos.xy, lightScreenPos.z);    
        lightVector
.x *= resolution.x / resolution.y;

if the comment is true, you should keep light vector in NDC and not scale it to screen aspect, since the normal direction is in NDC=texture space right?

 

But those things do not seem as the couse to me though. What seems as possible couse to me is this

 

 

normalMap.g = -normalMap.g; // Our normal map program produces an inverted y axis.
    
    normalMap
= normalMap * 2 - 1; // Normalize to [-1, 1]

try switching those two lines - first make normal -1,1 and then switch sign of g component. You could also try to dot the light vector with a constant (0,0,1) vector to see if it is purely the sampled normal problem.

 

 

1

Share this post


Link to post
Share on other sites

That's definitely a bug. If you negate the Y component first, then apply 0-1 to -1-1 scale, then a negative normal of -1 (0 in the texture) will be -3 instead of -1. This is, as you'd expect, incorrect. smile.png

Edited by Styves
1

Share this post


Link to post
Share on other sites

Thank you for the replies.
 

Inverting the g channel first was definitely an issue. It led me to realising my light positions z-values were also too small, so I increased those and adjusted the falloff.

 

Now I have something much closer to my goal, but there is one more issue. Now I have a problem where only normals that are facing almost directly ahead seem to get lit:

 

t2zH7Jl.png

 

I tried encircling one of the spots that isn't lighting, and it didn't change:

 

fkVsFsC.png

 

Then I tried moving the z- value of the light around and got a confusing result. If the z-value is very small (the background is effectively at z = 0), then some of the spots that were dark becomes lit to the up and right of the light while the normals facing the screen become darker in that area.

 

OwWo3po.png

 

I'd be grateful for any ideas or suggestions.

0

Share this post


Link to post
Share on other sites

Almost immediately after posting, I found a solution; however, I don't understand why I works.

 

I changed the line:

 

normalMap = normalMap * 2 - 1; // Normalize to [-1, 1]

To

normalMap.xy = normalMap.xy * 2 - 1; // Normalize to [-1, 1]

So that the z value would stay in the range [0,1]. Now it looks like this:

DPOdoRL.png

 

If someone knows why that change worked and if it while cause problems later, I would be grateful for the insight.

0

Share this post


Link to post
Share on other sites

seeing this, your normal texture seems to contain negative z for that spot (a z <0.5  in texture) though in texture you posted it does not seem so.

 

Let me aware you that many normal map generating software does not produce z component with relation to -1,1 scale- due to compressing efficiency accuracy and the fact negative z component is realy not desired for a texture space normal, but x and y are. You can also compute z component youself after having -1,1 x and y components- such as z=sqrt(1.0-x*x+y*y) - what is even more effective than normalizing the sampled normal and allowing you to have 2 normal maps in one texture.

0

Share this post


Link to post
Share on other sites

What means that your solution is not corrupt, as you may have been thinking. Just make an order in your production system in first place!

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