Sign in to follow this  
Geometrian

OpenGL Shader Colors

Recommended Posts

Hello, I recently got basic shaders to work, using various examples. I see how they work, but I'm having some problems with lighting. The shader code I'm using is a slightly modified tutorial piece: Vertex shader:
varying vec4 eyePosition;
    varying vec3 diffuseColor;
    varying vec3 specularColor;
    varying vec3 emissiveColor;
    varying vec3 ambientColor;
    varying float shininess;
    varying vec3 normal;
    void main()
    {
        gl_Position = ftransform();
        /*
        ftransform performs OpenGL’s fixed functionality transform.
        Should be the same as doing
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        */
        eyePosition = gl_ModelViewMatrix * gl_Vertex;
        normal = gl_NormalMatrix * gl_Normal;
        diffuseColor = vec3(gl_FrontMaterial.diffuse);
        specularColor = vec3(gl_FrontMaterial.specular);
        emissiveColor = vec3(gl_FrontMaterial.emission);
        ambientColor = vec3(gl_FrontMaterial.ambient);
        shininess = gl_FrontMaterial.shininess;
    }

Fragment shader:
varying vec4 eyePosition;
    varying vec3 normal;
    varying vec3 diffuseColor;
    varying vec3 specularColor;
    varying vec3 emissiveColor;
    varying vec3 ambientColor;
    varying float shininess;
    void main()
    {
        const vec3 lightColor = vec3(1, 1, 1);
        const vec3 globalAmbient = vec3(0.2, 0.2, 0.0);
        
        vec3 emissive = emissiveColor;
        
        vec3 ambient = ambientColor * globalAmbient;
        
        vec3 P = vec3(eyePosition);
        vec3 N = normalize(normal);
        vec3 L = normalize(vec3(gl_LightSource[0].position) - P);
        float diffuseLight = max(dot(N, L), 0);
        vec3 diffuse = diffuseColor * lightColor * diffuseLight;

        vec3 V = normalize(-P);
        vec3 H = normalize(L + V);
        float specularLight = pow(max(dot(N, H),0), shininess);
        vec3 specular = specularColor * lightColor * specularLight;
        gl_FragColor.xyz = emissive + ambient + diffuse + specular;
        gl_FragColor.w = 1.0;
    }

The shader is then compiled and used like so:
glMaterialfv(GL_FRONT,GL_AMBIENT,(0.2,0.2,0.0,1.0))
glMaterialfv(GL_FRONT,GL_DIFFUSE,(0.8,0.8,0.8,1.0))
glMaterialfv(GL_FRONT,GL_SPECULAR,(1,1,1))
glMaterialfv(GL_FRONT,GL_EMISSION,(0,0,0,1))
glMaterialfv(GL_FRONT,GL_SHININESS,128)
glUseProgramObjectARB(program)
glutSolidTeapot(1.0)
This should draw a nice teapot with a weak specular, but changing any of the material values causes no change. Ideas? Thanks, -Geometrian

Share this post


Link to post
Share on other sites
hello:
I think your glsl code is wrong.
code error in max function ,the max function must receive two float(not int) parameter,the last parameter you passed is a integer value(0 you passed),it will success in c++,but wrong in glsl.So you must pass 0.0 to max function.

Share this post


Link to post
Share on other sites
I've gotten specular to work, but not the ambient or diffuse. I'm now using this tutorial: http://www.lighthouse3d.com/opengl/glsl/index.php?pointlight

Strangely, even though I set all the material parametres at once, the only things that appear to change are GL_SHININESS and GL_SPECULAR. Changes to GL_AMBIENT and GL_DIFFUSE don't change anything!

In the shader, no matter what I set these values to, they always appear to be (1,1,1,1)!

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