WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Headlight only when bump mapping? (https://forum.worldviz.com/showthread.php?t=4833)

TopazFrost 09-24-2013 03:12 PM

Headlight only when bump mapping?
 
I have another lighting question. I already had bump mapping working when I started working on better lights. Any node that doesn't have a bump map is working fine, but any bump mapped node is not taking the new light(s). It is painfully obvious it is the head light that is being used (and it looks horrible with any shiny surface).

Turning it off didn't work. I even tried setting the head light color to [0,0,0]! So, is there something special I have to do to a bump mapped node to use user defined lighting?

TopazFrost 09-24-2013 03:51 PM

I forgot something.

I did find the last parameter with the light number, but that only uses one light. That is not enough with I am bump mapping the inside walls.

TopazFrost 10-01-2013 11:14 AM

Ok....The built in bump mapping still isn't working, but I found a work around. I made my own bump shader. For those looking for a solution here is the code. This uses 2 hard coded directional light positions, colors, and ambient light. That was all I needed.

Attaching the shader:
Code:

norm = viz.add(textureFilePath)
norm.wrap(viz.WRAP_T, viz.REPEAT)
norm.wrap(viz.WRAP_S, viz.REPEAT)
                       
node.texture(norm,unit=1)

shader = viz.addShader( vert='bump.vert', frag='bump.frag')
node.apply(shader)
node.apply(viz.addUniformInt('normalMap',1))

The vertex shader (bump.vert):
Code:

attribute float lightNum1;
attribute float lightNum2;

varying vec3 lightVec;
varying vec3 lightVec2;
varying vec3 vPosition;
varying vec3 eyeVec;
varying vec2 texCoord;

void main(void) 
{   
        //lights positions and color  <----------------------Change this  code block if you want to use GL specified lights
        vec3 LightLocation = vec3(2,2,1);
        vec3 LightLocation2 = vec3(-1,2,-2);

        //put vertex in final render position, and grab texture coordinate for later use in fragment shader
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        texCoord = gl_MultiTexCoord0.xy;

        //get everything normalized and in place for later
        vPosition = normalize(vec3(gl_ModelViewMatrix *  gl_Vertex));
        lightVec = normalize(gl_NormalMatrix * LightLocation);            //since these are directional light, we only need the orientation
        lightVec2 = normalize(gl_NormalMatrix * LightLocation2);       

        //the rest of this is getting the space ready for bump mapping which requires a different transform space

        // Building the transform matrix Eye Space (camera) -> Tangent Space (bump map)
        mat3 CameraToTangent;
        vec3 c1 = cross( gl_NormalMatrix * gl_Normal, vec3(0.0, 0.0, 1.0) );
        vec3 c2 = cross( gl_NormalMatrix * gl_Normal, vec3(0.0, 1.0, 0.0) );
        if( length(c1)>length(c2) )
        {
                CameraToTangent[0] = normalize(c1);
        }
        else
        {
                CameraToTangent[0] = normalize(c2);
        }
        CameraToTangent[2] = normalize (gl_NormalMatrix * gl_Normal);
        CameraToTangent[1] = cross(CameraToTangent[2],CameraToTangent[0]);
       
        // transform light and eye position vectors into tangent/bump space
        eyeVec = -normalize(vPosition * CameraToTangent);
        lightVec = normalize (lightVec * CameraToTangent);
        lightVec2 = normalize (lightVec2 * CameraToTangent);
}

The fragment shader (bump.frag):
Code:

uniform sampler2D texMap;
uniform sampler2D normalMap;

varying vec3 lightVec;
varying vec3 lightVec2;
varying vec3 vPosition;
varying vec3 eyeVec;
varying vec2 texCoord;

void main (void)
{
        //lights positions and color <------------------Change this  code block if you want to use GL specified lights
        float I = .8;
        float I2 = .6;
        vec4 LightColor = vec4(I,I,I, 1);
        vec4 LightColor2 = vec4(I2,I2,I2, 1);

        gl_FragColor = vec4(0.3,0.3,0.3,1.0);        //add in the ambient right away

        vec3 bump = normalize( texture2D(normalMap, texCoord).xyz * 2.0 - 1.0 );                //the bump mapped normal vector
       
        //calculate the diffuse color for each light (dot for the percentage applied and max to make sure it isn't negative)
        vec4 diffuse = gl_FrontMaterial.diffuse * LightColor *  max (dot (lightVec, bump), 0.0) ;
        vec4 diffuse2 = gl_FrontMaterial.diffuse * LightColor2 *  max (dot (lightVec2, bump), 0.0) ;

        //get the specular values for each light
        float specular = pow(clamp(dot(-reflect(lightVec, bump), eyeVec), 0.0, 1.0),  gl_FrontMaterial.shininess );
        float specular2 = pow(clamp(dot(-reflect(lightVec2, bump), eyeVec), 0.0, 1.0),  gl_FrontMaterial.shininess );
        vec4 spec =  gl_FrontMaterial.specular * LightColor * specular;
        vec4 spec2 = gl_FrontMaterial.specular * LightColor2 * specular2;

        //sum all light sources together
        gl_FragColor += diffuse + diffuse2 ;                       
        gl_FragColor += spec + spec2;                                       
       
        gl_FragColor *= texture2D(texMap, texCoord);        //multiply in the texture
        gl_FragColor = clamp(gl_FragColor, 0.0, 1.0);
}



All times are GMT -7. The time now is 11:54 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC