Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [GLSL] Normal/Specular Maps  (Read 3796 times)

0 Members and 1 Guest are viewing this topic.

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
[GLSL] Normal/Specular Maps
« on: February 27, 2011, 11:50:49 am »
Prerequisite: I'm Using SFML2 on Windows 7 Professional 64 bit (but compiling with 32)


Hey, Im finally trying to implement lighting in my game. What im trying to do is a simple test to see if i can load a simple brick diffuse texture, and display it with a  point light at the mouse position. I also want to use a Normal and Specular map to control how the light reacts to the Material.

The problem is when searching for examples on how to approach this, most tutorials are aimed for 3d Surfaces, so it involves a vertex shader as well as a fragment shader, but I would like to implement the lighting using only the  sf::Shader's class provided by SFML.

So far I have managed to load and feed the required maps to the shader, however im not sure where to continue from here, as im missing some data that the vertex shader is suppose to provide, here is a link to the original shader from the tutorial.

http://ciardhubh.de/node/18


another thing to note is, im not sure about is how im suppose to modify

Code: [Select]
uniform lightDataType light;
from my application, do I make a similar structure? What do I replace the vec4's with?

Any way after some reworking, I hacked up a version of the above that, Kind of, almost works, but not quite,
Code: [Select]

[source]



struct lightDataType
{
    vec4  ambient;
    vec4  diffuse;
    vec4  specular;
    vec4  position;
};


uniform float time;
uniform float mouseX;
uniform float mouseY;





uniform float shininess; // Shininess exponent for specular highlights

uniform sampler2D diffuseTexture;
uniform sampler2D normalMap;
uniform sampler2D specularMap;


void main(void)
{

vec4 light_ambient = vec4(.5, .5, .5, 1);
vec4 light_position = vec4(mouseX, mouseY, .1, 0);
vec4 light_diffuse = vec4(.5, .5, .5, 1);
vec4 light_specular = vec4(0.2, 0.2, 0.2, 1);


vec3 tbnDirToLight = normalize(light_position-texture2D(normalMap, gl_TexCoord[0].xy).xyz); // Direction to light in tangent space...I think
vec3 tbnHalfVector = normalize(vec3(0, 0, -1)); // Half vector in tangent space


    // Base colour from diffuse texture
    vec4 baseColour = texture2D(diffuseTexture, gl_TexCoord[0].xy);

    // Uncompress normal from normal map texture
    vec3 normal = normalize(texture2D(normalMap, gl_TexCoord[0].xy).xyz * 2.0 - 1.0);
    // Depending on the normal map's format, the normal's Y direction may have to be inverted to
    // achieve the correct result. This depends - for example - on how the normal map has been
    // created or how it was loaded by the engine. If the shader output seems wrong, uncomment
    // this line:
    // normal.y = -normal.y;
   
    // Ambient
    vec4 ambient = light_ambient * baseColour;
   
    // Diffuse
    // Normalize interpolated direction to light
    vec3 tbnNormDirToLight = normalize(tbnDirToLight);
    // Full strength if normal points directly at light
    float diffuseIntensity = max(dot(tbnNormDirToLight, normal), 0.0);
    vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);

    // Specular
    vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);
    // Only calculate specular light if light reaches the fragment.
    if (diffuseIntensity > 0.0) {
        // Colour of specular reflection
        vec4 specularColour = texture2D(specularMap, gl_TexCoord[0].xy);
        // Specular strength, Blinn–Phong shading model
        float specularModifier = max(dot(normal, normalize(tbnHalfVector)), 0.0);
        specular = light_specular * specularColour * pow(specularModifier, shininess);


diffuse = light_diffuse * baseColour * diffuseIntensity;


    }

    // Sum of all lights
    gl_FragColor = clamp(ambient + diffuse + specular, 0.0, 1.0);
   
    // Use the diffuse texture's alpha value.
    gl_FragColor.a = baseColour.a;
}


[/source]



The Result:

 
Not much Going on :S
The biggest problem probably is that Im not sure what to put for
Code: [Select]
vec3 tbnDirToLight
and
Code: [Select]
vec3 tbnHalfVector'



Id appreciate any help somebody with knowledge of GLSL can give me!
John Carmack can Divide by zer0.

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
[GLSL] Normal/Specular Maps
« Reply #1 on: March 03, 2011, 04:45:40 am »
anyone?
John Carmack can Divide by zer0.

 

anything