Specular Map support?

Hi, I just wanted to know if there are some build that support specular maps, and where can I get it.

I´m working with normal maps, but lacks specularity

http://salvador.mata.com.mx/cosas/NormalMap.jpg

Thanks <img>

woah that looks nice, how did you make normal maps for that?

I am assuming you already have a GLSL shader for your Normal-Mapping allready written and working correct?

Well first of all it looks to me like you have some Tangent vector artifacts going on. Make shure your tangents are getting calculated correctly first.

To do a specular just create a single channel greyscale that containes your specular values for given areas. Then put that into the alpha channel of your diffuse texture. In your GLSL shader add some standard specular code and multiply the result of that by you specular channel

soo…
specPass *= diffuse.a;
gl_FragColor = diffPass + specPass;

Does that make sense??? It would be easier for me to help you if you post your shader code (both vertex and fragment shaders) on here so I can correct it and tell you where to add the specular code.

Hello Spike… <img>

Well, I’m using the script that comes in a blend file test I’ve downloaded some time ago. here is it:

import GameLogic as g
objlist = g.getCurrentScene().getObjectList()
g.setLogicTicRate(60.0)

-------------------------------------

ShaderObjects = [ objlist[‘OBCube’] ]
MaterialIndexList = [0,1]
GlobalAmbient = [1.39,8.35,3.32,1]
AmbF = 3.5

-------------------------------------

VertexShader = “”"
uniform mat4 mvi;
attribute vec4 Tangent;

varying vec4 vcol;

varying vec3 lvec,vvec;

vec3 TBN(in vec3 by)
{
vec3 tangent = (Tangent.xyz);
vec3 binormal = cross(gl_Normal, Tangent.xyz)*Tangent.w;
vec3 normal = gl_Normal;

vec3 tvec;
tvec.x = dot(tangent,  by);
tvec.y = dot(binormal, by);
tvec.z = dot(normal,   by);
return tvec;

}

void main() {
gl_Position = ftransform();

vec4 light0    = (mvi*gl_LightSource[0].position)-gl_Vertex;
vec4 view = mvi[3]-gl_Vertex;
lvec = TBN(light0.xyz);
vvec = TBN(view.xyz);
gl_TexCoord[0] = gl_MultiTexCoord0;
vcol =gl_Color;

}
“”"

FragmentShader = “”"
varying vec4 vcol;
varying vec3 lvec,vvec,hvec;

uniform sampler2D color;
uniform sampler2D bump;

void main() {
vec4 diffuse_color = vcol*gl_LightSource[0].diffuse;
vec4 specular_color = gl_FrontMaterial.specular;
vec4 colormap = texture2D(color, gl_TexCoord[0].st);

vec3 lv     = normalize(lvec);
vec3 vv     = normalize(vvec);
vec3 nv     = normalize(2.0*texture2D(bump, gl_TexCoord[0].st).xyz-1.0);

float diff     = max(dot(lv, nv), 0.0);
float spec    = pow(max(dot(reflect(-vv, nv), lv),0.0), gl_FrontMaterial.shininess);
vec4 diff_pass = colormap*(diff*diffuse_color);
vec4 spec_pass = spec*specular_color;    
gl_FragColor = vec4(0.1,0.1,0.1,1.0)+diff_pass+spec_pass;

}
“”"

def MainLoop ():
# for each object
for obj in ShaderObjects:

    mesh_index = 0
    mesh = obj.getMesh(mesh_index)
    while mesh != None:
        for mat in mesh.materials:
            
            # regular TexFace materials do NOT have this function
            if not hasattr(mat, "getMaterialIndex"):
                return
            
            mat_index = mat.getMaterialIndex()
            # find an index                
            found = 0
            for i in range(len(MaterialIndexList)):
                if mat_index == MaterialIndexList[i]:
                    found=1
                    break
            if not found: continue
            shader = mat.getShader()
            if shader != None:
                if not shader.isValid():
                    shader.setSource(VertexShader, FragmentShader,1)
                shader.setAttrib(g.SHD_TANGENT)
                shader.setUniformDef('mvi', g.MODELVIEWMATRIX_INVERSE)
                shader.setSampler('color', 0)
                shader.setSampler('bump', 1)
        mesh_index += 1
        mesh = obj.getMesh(mesh_index)

-------------------------------------

MainLoop()

-------------------------------------

And yes, I have the specular map ready, it works fine in 3dmax, but I would like to get workig in blender :slight_smile:
I write a comment in my blog some time ago,

http://salvador.mata.com.mx/blog/index.php/2006/06/09/un-mueble/

I hope it helps <img>, thanks in advance <img>

Mmmm…does anybody got some idea? <img>

That’s simply amazing. And no… I don’t know how to solve your problem… sorry…

replace FragmentShader with the following…

Oh btw. You need to have your specular map in the alpha channel of your diffuse map for this to work. I also fixed the ambient light part of the shader to look better since the other way is ugly. Areas in bold are the parts I changed/added.

FragmentShader = “”"
varying vec4 vcol;
varying vec3 lvec,vvec,hvec;

uniform sampler2D color;
uniform sampler2D bump;

void main() {
vec4 diffuse_color = vcol*gl_LightSource[0].diffuse;
vec4 specular_color = gl_FrontMaterial.specular;
vec4 colormap = texture2D(color, gl_TexCoord[0].st);

vec3 lv     = normalize(lvec);
vec3 vv     = normalize(vvec);
vec3 nv     = normalize(2.0*texture2D(bump, gl_TexCoord[0].st).xyz-1.0);

float diff     = <b>dot(lv, nv)</b>;
float spec    = pow(max(dot(reflect(-vv, nv), lv),0.0), gl_FrontMaterial.shininess);
vec4 diff_pass = colormap*(diff*diffuse_color);
vec4 spec_pass = spec*specular_color<b>*colormap.a</b>;
gl_FragColor = <b>mix(colormap,diff_pass,0.1)</b>+spec_pass;

}
“”"

Thanks a lot spike1907;735584 :smiley:

I changed the fragment shader and than put the specular inside of the alpha channel, and it looks great :O, the object get more realistic :eek:

But, in the specular map, I have some colors, and that colors were intended to affect the specular color too, but when the specular is in the Alpha channel (as it should happen) gets in gray scale, and there are no more specular color, only specular level :confused:

but any way is very very usefull for me :wink: , so thanks a lot again, spike1907;735584 :smiley:

For the best results you want your specular color to be the exact same color as your diffuse color.

just replace the vec4 spec_pass line with
vec4 spec_pass = specspecular_colorcolormap.a*colormap.rgb;

Thanks :smiley:

I’ll check it :slight_smile: