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

GLSL spotlight+scale problem

2 posts in this topic

Hi everyone,

I have a problem with my shaders. I implemented a phong lightning. In the shader vertices get transformed by position, rotation and scale.
The problems is that when the scale is not (1, 1, 1) I get wrong results (displaced lighting). It's really noticeable for example with scale (100, 20, 100). I see this bug when I was testing with spotlight, so directional light and point light can also be wrong.

I think it's scale+normals problem, but I can't find it. Normal Matrix has scale in accound? How can I deal with this problem? If someone can help me I will be really apreciated. Any other advice not related to this problem it will be wellcome.
If it will help I can upload some images.

(SpotFallOff its usless, I use SpotInnerCutOff and SpotOuterCutOff)

Sorry for my bad English.

Here is my Shader:
[CODE]
<Shader>
<ShaderPart type="Vertex">
<ShaderCode>
<![CDATA[
#version 420
uniform mat4 ModelViewMatrix;
uniform mat4 MVP;
uniform mat3 NormalMatrix;

in vec3 in_Position;
in vec3 in_Normal;
in vec3 in_Tangent;
in vec3 in_Bitangent;
in vec2 in_TexCoord;

out mat3 TBN;
out vec2 pTexCoord;
out vec3 pViewDir;
out vec3 pPosition;
out vec3 pVertexPos;

void main(void)
{
pTexCoord = in_TexCoord;

vec3 n = normalize(NormalMatrix * in_Normal);
vec3 t = normalize(NormalMatrix * in_Tangent);
vec3 b = -normalize( cross(n, t) );
TBN = mat3(
t.x, b.x, n.x,
t.y, b.y, n.y,
t.z, b.z, n.z );

pPosition = vec3( ModelViewMatrix * vec4(in_Position, 1.0) );
pViewDir = TBN * vec3(-pPosition);
pVertexPos = in_Position;
gl_Position = MVP * vec4(in_Position, 1.0);
}
]]>
</ShaderCode>
</ShaderPart>
<ShaderPart type="Fragment" >
<ShaderCode>
<![CDATA[
#version 420

uniform sampler2D Texture0; // Diffuse
uniform sampler2D Texture1; // Normal
uniform sampler2D Texture2; // Specular

uniform float SpecularPower;

uniform struct
{
vec3 ambient;
vec3 diffuse;
vec3 specular;

float constantAtt;
float linearAtt;
float quadraticAtt;

float spotInnerCutOff;
float spotOuterCutOff;
float spotFallOff;

vec3 position;
vec3 direction; // For Spot and Directional lights

int type; // 0-> Point, 1-> Spot, 2-> Directional
}
Lights[16];
uniform int NumLights;

in mat3 TBN;
in vec2 pTexCoord;
in vec3 pViewDir;
in vec3 pPosition;
in vec3 pVertexPos;

out vec4 fragColor;

vec4 computePhongPointLight(in int index)
{
vec3 color = vec3(0.0);

vec4 difColor = texture2D( Texture0, pTexCoord );
vec3 normColor = normalize(texture2D( Texture1, pTexCoord ).xyz * 2.0 - 1.0);
vec3 specColor = texture2D( Texture2, pTexCoord ).rgb;

vec3 lightDir = TBN * (Lights[index].position.xyz - pPosition);

// Normalized lightDir
vec3 lightDirNorm = normalize( lightDir );

vec3 r = reflect( -lightDirNorm, normColor );

float sDotN = max( dot(lightDirNorm, normColor), 0.0 );

// Compute att for point and spot lights. Directional lights has att = 1.0
float att = 1.0;
float distSqr = dot(lightDir, lightDir);
float invAtt = (Lights[index].constantAtt + (Lights[index].linearAtt*sqrt(distSqr)) + (Lights[index].quadraticAtt*distSqr));
att = 0.0;
if (invAtt != 0.0)
att = 1.0/invAtt;

vec3 diffuse = difColor.rgb * Lights[index].diffuse * sDotN;
vec3 ambient = difColor.rgb * Lights[index].ambient; // Cheat here
// Specular power
vec3 specular = vec3(0.0);
if ( sDotN > 0.0 )
{
specular = Lights[index].specular.rgb * specColor * pow( max( dot(r, normalize(pViewDir)), 0.0), SpecularPower );
}

color = (diffuse + specular + ambient)*att;
return vec4(color, difColor.a);
}

vec4 computePhongSpotLight(in int index)
{
vec3 color = vec3(0.0);

vec4 difColor = texture( Texture0, pTexCoord );
vec3 normColor = normalize(texture( Texture1, pTexCoord ).xyz * 2.0 - 1.0);
vec3 specColor = texture( Texture2, pTexCoord ).rgb;

vec3 lightDir = TBN * (Lights[index].position.xyz - pPosition);

// Normalized lightDir
vec3 lightDirNorm = normalize( lightDir );

vec3 r = reflect( -lightDirNorm, normColor );

float sDotN = max( dot(lightDirNorm, normColor), 0.0 );

// Compute att for point and spot lights. Directional lights has att = 1.0
float att = 1.0;
float distSqr = dot(lightDir, lightDir);
float invAtt = (Lights[index].constantAtt + (Lights[index].linearAtt*sqrt(distSqr)) + (Lights[index].quadraticAtt*distSqr));
att = 0.0;
if (invAtt != 0.0)
att = 1.0/invAtt;

// Compute spot
float cos_cur_angle = dot(-lightDirNorm, normalize(TBN * Lights[index].direction));
float cos_inner_cone_angle = cos(Lights[index].spotInnerCutOff);
float cos_outer_cone_angle = cos(Lights[index].spotOuterCutOff);
float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
float spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);

vec3 diffuse = difColor.xyz * Lights[index].diffuse * sDotN;
vec3 ambient = difColor.rgb * Lights[index].ambient; // Cheat here

// Specular power
vec3 specular = vec3(0.0);
if ( sDotN > 0.0 )
{
specular = Lights[index].specular.rgb * specColor * pow( max( dot(r, normalize(pViewDir)), 0.0), SpecularPower );
}

color = (diffuse + specular + ambient)*att*spot;
return vec4(color, difColor.a);
}

vec4 computePhongDirectionalLight(in int index)
{
vec3 color = vec3(0.0);

vec4 difColor = texture( Texture0, pTexCoord );
vec3 normColor = normalize(texture( Texture1, pTexCoord ).xyz * 2.0 - 1.0);
vec3 specColor = texture( Texture2, pTexCoord ).rgb;

vec3 lightDir = TBN * Lights[index].direction;

// Normalized lightDir
vec3 lightDirNorm = normalize( lightDir );

vec3 r = reflect( -lightDirNorm, normColor );

float sDotN = max( dot(lightDirNorm, normColor), 0.0 );

vec3 diffuse = difColor.rgb * Lights[index].diffuse * sDotN;
vec3 ambient = difColor.rgb * Lights[index].ambient; // Cheat here

// Specular power
vec3 specular = vec3(0.0);
if ( sDotN > 0.0 )
{
specular = Lights[index].specular.rgb * specColor * pow( max( dot(r, normalize(pViewDir)), 0.0), SpecularPower );
}

color = diffuse + specular + ambient;
return vec4(color, difColor.a);
}

vec4 computeLight(in int index)
{
if (Lights[index].type == 0)
return computePhongPointLight(index);
if (Lights[index].type == 1)
return computePhongSpotLight(index);
else if (Lights[index].type == 2)
return computePhongDirectionalLight(index);
}
void main(void)
{
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (int i = 0; i < NumLights; i++)
fragColor += computeLight(i);

if (fragColor.a < 0.5)
discard;
}
]]>
</ShaderCode>
</ShaderPart>
</Shader>
[/CODE]
0

Share this post


Link to post
Share on other sites
Well you are probably right. It depends on how you calculate this matrix.
When matrix scale is non uniform normal matrix is not simple 3x3 matrix from your 4x4 modelview matrix

You should check this:
[url="http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/"]http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/[/url]
1

Share this post


Link to post
Share on other sites
I think it's well calculated normal matrix.
I don't know where is the bug, I can't find it.

Here is my normal matrix calculation:
[CODE]
_currentRenderableInfo.renderablePos = actor->getDerivedPosition();
_currentRenderableInfo.renderableRot = actor->getDerivedRotation();
_currentRenderableInfo.renderableScale = actor->getDerivedScale();
_currentRenderableInfo.modelMatrix = actor->getFullTransform();
_currentRenderableInfo.modelViewMatrix = _currentViewInfo.viewMatrix * _currentRenderableInfo.modelMatrix;
_currentRenderableInfo.mvpMatrix = _currentViewInfo.projectionMatrix * _currentRenderableInfo.modelViewMatrix;
_currentRenderableInfo.normalMatrix = Core::transpose(Core::inverse(Core::mat3(_currentRenderableInfo.modelViewMatrix)));
[/CODE]
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