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

View space bump mapping refuses to work

3 posts in this topic

Recently I was implementing an SSAO algorithm, for which my dummy scene was not enough, so I decided I'd replace it with the popular 'Sponza' model. As I loaded it in, I noticed that my bump mapping implementation wasn't working properly, so I decided I'd fix it. After roughly 10 hours, and after having tested every possible and impossible combination of calculations, I decided I'd ask for help, because I clearly cannot see what's wrong with my code.

 

I'm using a deferred renderer, do all the lighting calculations in view/camera space, so I store view space normals in the GBuffer. I'm using Assimp for tangent calculation.

 

Here are a few screenshots of my shader's output:

 

Normals (pre-mapping, abs): https://docs.google.com/file/d/0BzrnpA5nbZMJT3I2Yzc1WkRjcDA/edit

Tangents (abs): https://docs.google.com/file/d/0BzrnpA5nbZMJRHJESGpodVA0Mjg/edit

Normals (post-mapping, abs): https://docs.google.com/file/d/0BzrnpA5nbZMJYUNGcEt2eF90WEU/edit

Lit scene /w bump mapping: https://docs.google.com/file/d/0BzrnpA5nbZMJUkQ3U2I0N0NCR0U/edit

Lit scene w/o bump mapping: https://docs.google.com/file/d/0BzrnpA5nbZMJNDh5Q01waDl2LTQ/edit

Lit scene /w bump mapping and reversing normal when dot(normal,bumpedNormal)<0.0: https://docs.google.com/file/d/0BzrnpA5nbZMJeldHamZkckc3bzg/edit

 

The results are clearly wrong, and even when normals are reversed, only a change in light angle happens.

 

So here are my shaders:
 

// ... Deferred.glsl
//////////////////////////////////////////////////
//  Helpers
struct BasicData
{
    vec2 uv;
    vec3 posWorld;
    vec3 normalCamera;
    vec4 prevPos;
    vec4 currentPos;
    mat3 tbn;
};

//////////////////////////////////////////////////
//  Base pass
shader VS_base(in vec4 vPos: ATTRIBUTE_LOCATION_POSITION,
               in vec4 vNormal: ATTRIBUTE_LOCATION_NORMAL,
               in vec4 vTangent: ATTRIBUTE_LOCATION_TANGENT,
               in vec2 vUv: ATTRIBUTE_LOCATION_UV,
               out BasicData fromVS)
{
    fromVS.posWorld=(modelData.mModel*vPos).xyz;
    fromVS.currentPos=cameraParameters.mViewProjection*vec4(fromVS.posWorld,1);
    fromVS.prevPos=previousCameraParameters.mViewProjection*previousModelData.mModel*vPos;
    
    fromVS.normalCamera=normalize((modelData.mNormal*vNormal).xyz);
    vec3 tangent=normalize((modelData.mNormal*vTangent).xyz);
    tangent=normalize(tangent-dot(tangent,fromVS.normalCamera)*fromVS.normalCamera);
    vec3 bitangent=cross(tangent,fromVS.normalCamera);
    fromVS.tbn=mat3(tangent,bitangent,fromVS.normalCamera);
    
    fromVS.uv=vUv;
    
    gl_Position=fromVS.currentPos;
}

shader FS_base(in BasicData fromVS,
               out vec4 albedo: 0,
               out vec4 normal: 1,
               out vec4 velocity: 2)
{
    vec2 uv=fromVS.uv;

    //////////////////////////////////////////////////
    //  Bump & parallax mapping
    if (material.bHasBumpMap==1.0)
    {
        // never evaluates to true as .mtl doesn't support displacement textures
        if (material.bHasParallaxMap==1.0)
            uv=getDisplacedUV(uv,normalize(cameraParameters.vEye-fromVS.posWorld),fromVS.tbn);

        normal.rgb=getBumpedNormal(fromVS.tbn,uv);
        
        if (dot(normal.rgb,fromVS.normalCamera.rgb)<0.0)
            normal.rgb=-normal.rgb;
    }
    else
    {
        normal.rgb=normalize(fromVS.normalCamera);
    }

    //////////////////////////////////////////////////
    //  Albedo
    if (material.bHasDiffuse==1.0)
        albedo.rgb=textureGamma(sDiffuse,uv).rgb;
    else
        albedo.rgb=vec3(1,1,1);
    
    //////////////////////////////////////////////////
    //  Specular
    if (material.bHasSpecularMap==1.0)
    {
        albedo.a=2.0+material.fGlossiness/256.0;
        normal.a=textureGamma(sSpecularMap,uv).r;
    }
    else
    {
        albedo.a=material.fGlossiness/256.0;
    }
    
    //albedo.rgb=abs(normal.rgb);
    //normal.rgb=normalize(fromVS.normalCamera);
    
    //////////////////////////////////////////////////
    //  Velocity
    vec2 currentPos=fromVS.currentPos.xy/fromVS.currentPos.w;
    vec2 prevPos=fromVS.prevPos.xy/fromVS.prevPos.w;
    velocity=packVelocityData(currentPos,prevPos,gl_FragCoord.z);
}

// ... Material.glsl

//////////////////////////////////////////////////
//  Bump mapping
vec3 getBumpedNormal(mat3 tbn, vec2 uv)
{
    return normalize(tbn*(2.0*textureGamma(sBumpMap,uv).xyz-1.0));
}

The normals, tangents and the normal matrix are okay, as it can been on the screenshots (without bumping lighting is right), so the problem must be somewhere else.

 

Any help is greatly appreciated, I'd much prefer not to waste any more time on such a basic and easy feature/algorithm.

Edited by luorax
0

Share this post


Link to post
Share on other sites

I didn't have time to read through the entire code, but multiplying a tangent space normal map with the TBN matrix converts it to world space normal, unless I am mistaken.

Can you show us how you do your lighting? 

0

Share this post


Link to post
Share on other sites

TBN converts to whatever space the column vectors (tangent, bitangent, normals) are. In the above code they're multiplied by the normal matrix (inverse_transpose(view*model)), which takes them to view space, so that TBN should be tangent -> view. Lighting is also fine without normal mapping, or at least it seems so, which makes me pretty sure the problem is not related to the light calculation.

 

Just for the sake of completeness though, here is my point light code (the scene only has a few point lights I quickly put down to lit the 2 floors) - don't mind the variable name inconsistencies, I plan on making a refactor on some of these codes once I finish shadow mapping and such.

 

What's more interesting is that by looking at demo codes of various postprocessing effect I have on my HDD that came with an engine, I've found a couple of shaders that, aside from variable names, work exactly like my code, yet it works for them.
 

vec3 computePointLight(PointLight lightData,
                       sampler2D shadowSampler,
                       vec4 worldPos, 
                       vec4 eyePos, 
                       vec3 normalCamera, 
                       vec3 albedo, 
                       vec3 specularColor, 
                       float specularPower)
{
    //////////////////////////////////////////////////
    //  Make sure this fragment is lit
    vec3 toLight=lightData.vSourceEye.xyz-eyePos.xyz;
    float distSqr=dot(toLight,toLight);
    float dist=sqrt(distSqr);
    toLight/=dist;
    
    if (dist>lightData.fRadius)
        return vec3(0.0);
    
    //////////////////////////////////////////////////
    //  Calculate light
    float angle=max(dot(normalCamera,toLight),0.0);

    float diffuseIntensity=angle*lightData.fDiffuseIntensity;
    float specularIntensity=0.0;
    
    float attenuation=1.0;
    float attFactor=1.0f/(lightData.fAttenuationFactor+1.0f);
    
    attenuation=distSqr/(lightData.fRadiusSquare);
    attenuation=1.0f/(attenuation*lightData.fAttenuationFactor+1.0f);
    attenuation-=attFactor;
    attenuation/=1.0f-attFactor;
        
    if (lightData.fSpecularIntensity>0.0)
    {
        vec3 halfVector=normalize(toLight+normalize(-eyePos.xyz)); 
        
        specularIntensity+=pow(max(dot(halfVector,normalCamera),0.0),specularPower)*lightData.fSpecularIntensity;
    }
    
    //////////////////////////////////////////////////
    //  Shadow
    float shadowFactor=1.0;
    
    if (lightData.bShadow!=0.0)
    {
        vec3 lightSpacePos=worldPos.xyz-lightData.vSourceEye.xyz;
        
        if (shadowMap.iQuality==SHADOW_QUALITY_HIGH)
            shadowFactor=shadowFactor3DVSM(shadowSampler,lightSpacePos,cameraParameters.fNear,cameraParameters.fFar);
        else
            shadowFactor=shadowFactor3DSimple(shadowSampler,lightSpacePos,cameraParameters.fNear,cameraParameters.fFar,0.00001);
    }
    
    //////////////////////////////////////////////////
    //  Composite
    float shadowedAttenuation=attenuation*shadowFactor;

    return albedo*(lightData.vColor.rgb*diffuseIntensity*shadowedAttenuation)+
           albedo*(specularColor*specularIntensity*shadowedAttenuation);
}
Edited by luorax
0

Share this post


Link to post
Share on other sites

Turns out the material has actual bump textures and not normal ones, so that's what causing the problems. I'll try to convert them to normal maps somehow and see if it works.

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