• 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
Jack Shannon

Strange specular with normal map (BlinnPhong)

1 post in this topic

SOLVED

 

Please can someone take a look at my normal map code, the diffuse is fine.

 

Without normal map:

2GiIt.png

 

With normal map:

2GiCw.png

 

Vertex shader:

/* The following can be defined:
 *      TEXTURED
 *      NORMALMAPPED
 */

in vec3 v_position;
#if defined(TEXTURED) || defined(NORMALMAPPED)
in vec2 v_uv;
#endif
in vec3 v_normal;
#ifdef NORMALMAPPED
in vec4 v_tangent;
#endif

layout(std140) uniform Transform
{
	mat4 t_model_view_proj;
	mat4 t_model_view;
	mat3 t_normal;
};

#if defined(TEXTURED) || defined(NORMALMAPPED)
out vec2 f_uv;
#endif

#ifdef NORMALMAPPED
layout(std140) uniform Light
{
    int l_type;
    vec3 l_orientation;
    float l_attenuation;
    vec3 l_ambient;
    vec3 l_diffuse;
    vec3 l_specular;
};
out vec3 tbn_lightDirection;
out vec3 tbn_viewDirection;
#else
out vec3 f_position;
out vec3 f_normal;
out vec3 f_viewDirection;
#endif

void main()
{
    gl_Position = t_model_view_proj * vec4(v_position, 1.0);

    #if defined(TEXTURED) || defined(NORMALMAPPED)
    f_uv = v_uv;
    #endif

    #ifdef NORMALMAPPED
    vec3 v_bitangent = v_tangent.z * cross(v_normal, v_tangent.xyz); // tangent.z stores m value which is the determinant of the object space to tangent space matrix

    mat3 TBN = transpose(mat3(v_tangent.xyz,
                              v_bitangent,
                              v_normal));

    tbn_lightDirection = normalize(TBN * l_orientation);
    tbn_viewDirection = -normalize(TBN * v_position);
    #else
    f_position = (t_model_view * vec4(v_position, 1.0)).xyz;
    f_normal = normalize(t_normal * v_normal);
    f_viewDirection = -normalize(t_model_view * vec4(v_position, 1.0)).xyz;
    #endif
}

 

Fragment shader:

/* The following can be defined:
 *      TEXTURED
 *      NORMALMAPPED
 */

#define LTYPE_DIRECTIONAL 0
#define LTYPE_POINT 1

#if defined(TEXTURED) || defined(NORMALMAPPED)
in vec2 f_uv;
#endif
#ifdef TEXTURED
uniform sampler2D diffuseTexture;
#endif

#ifdef NORMALMAPPED
uniform sampler2D normalTexture;
in vec3 tbn_lightDirection;
in vec3 tbn_viewDirection;
#else
in vec3 f_position;
in vec3 f_normal;
in vec3 f_viewDirection;
#endif

layout(std140) uniform Light
{
    int l_type;
    vec3 l_orientation;
    float l_attenuation;
    vec3 l_ambient;
    vec3 l_diffuse;
    vec3 l_specular;
};

layout(std140) uniform Material
{
    vec3 m_ambient;
    vec3 m_diffuse;
    vec3 m_specular; // scale specular by shininess strength in external tool
    float m_shininess;
};

out vec4 fragment;

float lambert(vec3 lightDirection, vec3 normal)
{
    float lambertTerm = dot(lightDirection, normal);
    lambertTerm = clamp(lambertTerm, 0, 1);
    return lambertTerm;
}

float blinnPhong(vec3 lightDirection, vec3 normal, vec3 viewDirection)
{
    vec3 halfwayDirection = normalize(lightDirection + viewDirection);
    float blinnTerm = dot(normal, halfwayDirection);
    blinnTerm = clamp(blinnTerm, 0, 1);
    blinnTerm = pow(blinnTerm, m_shininess);
    return blinnTerm;
}

// not normalized
vec3 getL()
{
    #ifdef NORMALMAPPED
        return normalize(tbn_lightDirection);
    #else
    if (l_type == LTYPE_DIRECTIONAL) {
        return l_orientation;
    }
    else if (l_type == LTYPE_POINT) {
        return l_orientation - f_position;
    }
    #endif
}

vec3 getN()
{
    #ifdef NORMALMAPPED
    vec3 tbnNormal = texture(normalTexture, f_uv).rgb * 2.0 - 1.0;
    return normalize(tbnNormal);
    #else
    return normalize(f_normal);
    #endif
}

vec3 getV()
{
    #ifdef NORMALMAPPED
        return normalize(tbn_viewDirection);
    #else
    return normalize(f_viewDirection);
    #endif
}

void main()
{
    vec3 lightDiffuse, lightSpecular = vec3(0);

    vec3 lightVector = getL();
    vec3 l = normalize(lightVector);
    vec3 n = getN();

    float lambertTerm = lambert(l, n);

    lightDiffuse = l_diffuse * lambertTerm;
    if (lambertTerm > 0) {
        vec3 v = getV();
        lightSpecular = l_specular * blinnPhong(l, n, v);
    }

    #ifdef TEXTURED
    vec3 diffuse = texture(diffuseTexture, f_uv).xyz * lightDiffuse;
    #else
    vec3 diffuse = m_diffuse * lightDiffuse;
    #endif
    vec3 specular = m_specular * lightSpecular;

    float attenuation;
    if (l_type == LTYPE_DIRECTIONAL) {
        attenuation = 1;
    }
    else if (l_type == LTYPE_POINT) {
        float distanceToLight = length(lightVector);
        float attenuation = 1.0 / (1.0 + l_attenuation * pow(distanceToLight, 2));
    }

    fragment = vec4(m_ambient + attenuation * (diffuse + specular), 1.0);
    //fragment = vec4(diffuse + specular, 1.0);
}
Edited by JackShannon
0

Share this post


Link to post
Share on other sites

It turns out that the speculars are correct, but are only being rendered on half of the pane. This demonstrates it more clearly: 2H4q0.png

 

Why could this be?

 

 

EDIT: solved! It turned out that I shouldn't have normalised tbn_viewDirection, this fixed it:

2H5bl.png

Edited by JackShannon
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