0

我在将光泽因子发送到凹凸贴图着色器时遇到问题。结果总是这样:http: //i.imgur.com/unzdx.jpg

但是,如果我将着色器内的值硬编码为 0.0,它就可以正常工作。当我将 0.0 发送到着色器时,结果如上图所示。

有任何想法吗?

这是我的着色器

#version 110

uniform sampler2D tex;
uniform sampler2D bmap;

uniform bool boolBump;
uniform vec4 vecColor;
uniform bool onlyColor;

uniform float fTransparencyThresh;
uniform float fShininess;
uniform float alpha;

varying vec3 vecLight;
varying vec3 vecEye;
varying vec3 vecNormal;

vec4 getLighting()
{
    //Ambient part
    vec4 color = (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + (gl_LightSource[0].ambient * gl_FrontMaterial.ambient);

    //For bump mapping, the normal comes from the bump map texture lookup
    vec3 n = normalize(vecNormal);
    if(boolBump)
    {
         n = normalize(texture2D(bmap, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
    }

    vec3 l = normalize(vecLight);

    //Lambert term
    float NdotL = dot(n, l);

    if(NdotL > 0.0)
    {
        //Diffuse part
        color += gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * max(0.0, NdotL);

        //Specular part
        vec3 e = normalize(vecEye);
        vec3 r = normalize(-reflect(l,n));  

        float spec = pow(max(0.0, dot(r, e)), fShininess);

        color += gl_LightSource[0].specular * gl_FrontMaterial.specular * spec;
    }

    return color;
}

void main(void)
{
    vec4 texel = texture2D(tex, gl_TexCoord[0].st);
    if(texel.a < fTransparencyThresh)
        discard;

    //Get shading
        vec4 color = getLighting();

    //Color only mode?
    if(onlyColor)
    {
        color *= vecColor;
    }
    else
    {
        color *= texel;
    }   

    //Set fragment color, alpha comes from MTL file
    gl_FragColor = vec4(color.xyz, alpha);
}

编辑,OpenGL代码:

void MyClass::sendToShader(const OBJ::StelModel* pStelModel, Effect cur, bool& tangEnabled, int& tangLocation)
{
    int location;
    tangEnabled = false;

    if(cur != No)
    {
        location = curShader->uniformLocation("fTransparencyThresh");
        curShader->setUniform(location, fTransparencyThresh);

        location = curShader->uniformLocation("alpha");
        curShader->setUniform(location, pStelModel->pMaterial->alpha);

        location = curShader->uniformLocation("fShininess");
        curShader->setUniform(location, 0.0f);

...

编辑:即使这样也行不通:

GLint loc = glGetUniformLocation(curShader->program, "fShininess");
glUniform1f(loc, 0.0f);
4

2 回答 2

1

请注意 pow(0, 0) 是未定义的。这意味着spec未定义 ifdot(r, e) == 0fShininess == 0

于 2012-04-22T00:55:28.577 回答
0

当你调用 glUniform 时,你确定程序是主动绑定的吗?你在任何地方检查 glGetError 吗?

于 2012-04-21T20:27:55.890 回答