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

specular light flickers when a ray is parallel to the lit surface

9 posts in this topic

Hi everyone

I'm writing a deferred renderer using DX9.

I ran to a problem with specular reflections produced by directional lights. when the ray of light is parallel to the lit surface, specular reflection produced by the light starts flickering as I move the camera.

[attachment=11458:flicker_1.png]
[attachment=11459:flicker_2.png]

I made sure that all normals were normalized after they came through the rasterizer. I'm even renormalizing them once they've been sampled from the normals texture.

I think it's got something to do with the precision loss as the normals go through the rasterizer, but the weird thing is that if I decrease the light angle even by a fraction,the artifact disappears altogether and the reflection becomes stable ( which would suggest that it's not a precision issue after all ).

I'm storing my normals uncompressed in a DXT5 texture, but I tried different formats as well, including a 32bit color texture. I'm sampling it using a POINT filter ( tried linear, but nothing changes as well ).

I'd be grateful for all suggestions.

Cheers,
Paksas
0

Share this post


Link to post
Share on other sites
The dot product between two perpendicular vectors is zero, check if you divide some value by the dot product between the surface normal and the light direction.

Can you show your shaders? I can't be more specific without them.
0

Share this post


Link to post
Share on other sites
No division by zero there.

Here are the shaders:

[b]Object shader:[/b]

[CODE]
//--------------------------------------------------------------------------------------

// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor; // Material's ambient color
float4 g_MaterialDiffuseColor; // Material's diffuse color
bool g_DiffuseTexSet;
bool g_NormalTexSet;
bool g_SpecularTexSet;
float g_farZ = 1024;
int g_materialIdx;
//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler g_DiffuseTexture;
sampler g_NormalTexture;
sampler g_SpecularTexture;
//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
// <psMain>
DEFERRED_PS_OUTPUT main( VS_OUTPUT In )
{
DEFERRED_PS_OUTPUT output = (DEFERRED_PS_OUTPUT)0;
// calculate the color component
output.Color = g_MaterialDiffuseColor;
if ( g_DiffuseTexSet )
{
float4 texColor = tex2D( g_DiffuseTexture, In.TextureUV );
output.Color = texColor + g_MaterialDiffuseColor * ( 1 - texColor.a );
}
// calculate normal
float3 surfaceNormalViewSpace = 0;
if ( g_NormalTexSet )
{
float3 normalVS = normalize( In.Normal );
float3 tangentVS = normalize( In.Tangent );
float3 binormalVS = cross( normalVS, tangentVS );
float3x3 mtx = float3x3( tangentVS, binormalVS, normalVS );
float3 surfaceNormalTanSpace = tex2D( g_NormalTexture, In.TextureUV ) * 2.0 - 1.0;
// I noticed the other day that normal maps exported from Blender have y coordinate flipped.
// So I'm unflipping it here - but keep that fact in mind and pay attention to other
// normal maps - perhaps I got something wrong in the code
surfaceNormalTanSpace.y *= -1.0;

surfaceNormalViewSpace = mul( surfaceNormalTanSpace, mtx );
surfaceNormalViewSpace = normalize( surfaceNormalViewSpace );
}
else
{
surfaceNormalViewSpace = normalize( In.Normal );
}

// Flip the Z direction, so that Z values decrease as they get further away from the camera.
// It's required in order for our normals compression algorithm to work correctly
output.NormalSpec.rgb = surfaceNormalViewSpace * float3( 0.5, 0.5, -0.5 ) + 0.5;
output.DepthNormal = encodeDepthAndNormal( surfaceNormalViewSpace, In.PositionCS.z, g_farZ );
// calculate the specular color ( store info about the specularity in albedo's alpha channel
if ( g_SpecularTexSet )
{
output.NormalSpec.a = tex2D( g_SpecularTexture, In.TextureUV ).r;
}
else
{
// full specularity
output.NormalSpec.a = 1;
}
// store the material index and the UV coordinates
output.MatIdx = storeMaterialIndex( g_materialIdx );
return output;
}
[/CODE]


[b]Directional light shader:[/b]
[CODE]
// -------------------------------------------------------------------------------
// constants
// -------------------------------------------------------------------------------
float4 g_halfPixel;
float4 g_lightDirVS; // light direction in view space
float4 g_lightColor; // light color
float g_strength; // light strength
bool g_drawShadows; // should we take the shadow map into account while rendering the light?
int g_materialsTexSize;
//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler g_Normals;
sampler g_Specular;
sampler g_SceneColor;
sampler g_ShadowMap;
sampler g_MaterialIndices;
sampler g_MaterialsDescr;
//--------------------------------------------------------------------------------------
// Helper structure that describes light parameters at the sampled point of the screen
//--------------------------------------------------------------------------------------
struct LightValues
{
float3 m_direction;
float m_reflectionFactor;
float3 m_reflectionVector;
float4 m_color;
};
//--------------------------------------------------------------------------------------
// Calculates the color of the pixel lit by the light described by the specified values
//--------------------------------------------------------------------------------------
float4 calcLightContribution( float2 UV, LightValues lightValues, sampler sceneColorTex, sampler materialIndices, sampler materialsDescr, int materialsTexSize )
{
// get the material's ambient color
float4 encodedMaterialIdx = tex2D( materialIndices, UV );
int materialIdx = readMaterialIndex( encodedMaterialIdx );
float4 matDiffuseCol = getMaterialComponent( materialsDescr, materialIdx, DIFFUSE_COLOR, materialsTexSize );
float4 matSpecularCol = getMaterialComponent( materialsDescr, materialIdx, SPECULAR_COLOR, materialsTexSize );
// I don't remember encoding the value of specular power, but for some reason
// it's encoded in the same way as the normals - investigate!
const float matShininess = matSpecularCol.a * 2 - 1;
matSpecularCol.a = 1;
float4 baseColor = tex2D( sceneColorTex, UV );
float4 outputColor = float4( 0, 0, 0, 1 );
if ( lightValues.m_reflectionFactor > 0.0 )
{
// calculate the specular factor
float specularValue = dot( lightValues.m_reflectionVector, float3( 0, 0, 1 ) );

float specularFactor = pow( max( specularValue, 1.0 ), matShininess );
// shininess occurs only where we allow it to be, and in our case that is regulated
// by the contents of the albedo texture alpha channel
specularFactor *= baseColor.a;
// calculate component albedo contributions for both the specular and alpha channels
float3 specularColor = matSpecularCol * specularFactor;
float3 diffuseColor = matDiffuseCol * baseColor.rgb * lightValues.m_reflectionFactor;
// calculate the final color by adding the two albedo colors and modulating them using the light's color
outputColor.rgb += ( diffuseColor + specularColor ) * lightValues.m_color.rgb;
}
return saturate( outputColor );
}
//--------------------------------------------------------------------------------------
// This method calculates light parameters values
//--------------------------------------------------------------------------------------
LightValues calculateDirectionalLightValue( float2 UV )
{
LightValues Out = (LightValues)0;

// set light ray direction
Out.m_direction = g_lightDirVS.xyz * float3( -1, -1, 1 );

// calculate the reflection factor of the light ray hitting the lit surface
float3 pixelNormalVS = normalize( tex2D( g_Normals, UV ) * 2.0 - 1.0 );
Out.m_reflectionFactor = dot( pixelNormalVS, Out.m_direction );
Out.m_reflectionVector = normalize( -reflect( Out.m_direction.xyz, pixelNormalVS ) );

// set the color of the light ( no attenuation requires, since directional lights have the same strength
// at every point in space )
Out.m_color = g_lightColor * g_strength;
return Out;
}
// -------------------------------------------------------------------------------
// main function
// -------------------------------------------------------------------------------
LIGHT_PS_OUT main( float2 UV : TEXCOORD0 )
{
LIGHT_PS_OUT Out = (LIGHT_PS_OUT)0;
// align texture coordinates
UV -= g_halfPixel.xy;

float lightPercentage = 1.0f;
if( g_drawShadows )
{
// use the shadow map to determine if we should render there
lightPercentage = tex2D( g_ShadowMap, UV ).r;
}
// color light and direction are constant across the entire space
if ( lightPercentage > 0.0f )
{
LightValues lightValue = calculateDirectionalLightValue( UV );
// calculate the light contribution to the scene color
Out.vColor = calcLightContribution( UV, lightValue, g_SceneColor, g_MaterialIndices, g_MaterialsDescr, g_materialsTexSize );
// modulate the output color by the light value
Out.vColor *= lightPercentage;
}
else
{
// save precious cycles - if the light doesn't reach the spot, don't calculate its contribution
Out.vColor = 0.0f;
}

return Out;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
@Ashaman73 - I tried, but that doesn't change anything. As a matter of fact if you look at the code you pasted ( the original one ), you can see that I'm actually maxin' it to 1.0,
before I pass it to the 'pow' function:

[CODE]
float specularFactor = pow( max( specularValue, 1.0 ), matShininess );
[/CODE]
1

Share this post


Link to post
Share on other sites
[quote name='Paksas' timestamp='1348660556' post='4983950']
@Ashaman73 - I tried, but that doesn't change anything. As a matter of fact if you look at the code you pasted ( the original one ), you can see that I'm actually maxin' it to 1.0,
before I pass it to the 'pow' function:
[/quote]
Actually, by doing that you minimized it. Look at what you wrote: take the maximum of the specular value and 1.0. This means the input to pow will always be >= 1, which is probably not what you wanted. You are probably looking for saturate() or clamp(). Edited by Bacterius
1

Share this post


Link to post
Share on other sites
oops.. I missread max and min...which would even make less sense ..[img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img] Edited by Ashaman73
0

Share this post


Link to post
Share on other sites
Ok - found it.
There were two problems hidden in the directional light shader:

1.) I don't know if I mentioned that I'm running all calculations in camera ( view ) space.
Anyway - the light direction I passed to calculate the reflection was flipped

2.) You guys were barking at the right tree with the whole min/max thing - right tree, wrong branch ;)

That's the original code:
[CODE]
const float matShininess = matSpecularCol.a * 2 - 1;
/// ...
float specularFactor = pow( max( specularValue, 1.0 ), matShininess );
[/CODE]

and here's the corrected code:
[CODE]
const float matShininess = matSpecularCol.a * 255.0;
/// ...
float specularFactor = pow( max( specularValue, 1.0 ), max( matShininess, 1.0 ) );
[/CODE]

I was decoding the shininess in the wrong way, having the value divided by 255 in the engine code ( that I didn't paste here, so you guys couldn't have possibly known ) so that I can output it in the alpha channel of my normals texture, but then I forgot to invert the operation before I calculated the factor, so in fact I was calculating a power with a factor in range ( 0..1 ).

The shininess value in my engine can be equal 0, if the material's not supposed to be shiny at all - I forgot to filter that case out.


Anyway - thank you all for help.
Cheers :)
0

Share this post


Link to post
Share on other sites
Backing up a bit - turns out the problem's still there.

It just became less visible, because the specular power is now calculated correctly, but that doesn't change the fact that when a light is perpendicular to object's surface, the highlight flickers.

Any ides?
0

Share this post


Link to post
Share on other sites
What is the problem, this

[quote name='Paksas' timestamp='1348651367' post='4983926']
when the ray of light is parallel to the lit surface
[/quote]

or this

[quote name='Paksas' timestamp='1348698150' post='4984144']
the fact that when a light is perpendicular to object's surface
[/quote]

?
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