Vertex world position in glsl, JOGL

494 views Asked by At

so i've been trying to implement bump mapping for some time and i have it working in some way. So it renders the texture and shadowing correct but does not change as the light source moves around I determined that it applies the light source moving around from the source (0,0) and not where the light source is in the world. How do i determine the world position of the fragment in the shader? I am a bit stuck at the moment, any help would be appreciated.

--vertex shader

    void main() 
{
    gl_TexCoord[0] = gl_MultiTexCoord0;

    // Set the position of the current vertex
    gl_Position =  gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}

--fragment shader

uniform sampler2D color_texture;
uniform sampler2D normal_texture;
uniform vec4 lightColor;
uniform vec3 falloff;
uniform vec3 lightPos;
uniform vec2 resolution;
uniform float ambience;
//uniform float lightDirection;

void main()
{
    // Extract the normal from the normal map
    vec3 normal = normalize(texture2D(normal_texture, gl_TexCoord[0].st).rgb * 2.0 - 1.0);

    // Determine where the light is positioned
    vec3 light_pos = normalize(lightPos);
    //vec3 light_pos = normalize(vec3(1.0, 1.0, 0.5));

    // Calculate the lighting diffuse value, the ambience is the darkness due to no light
    float diffuse = max(dot(normal, light_pos), 0.0);

    //direction
    float lightDir = length(vec3(lightPos.xy - (gl_FragCoord.xy / resolution.xy), lightPos.z));

    //calculate attenuation
    float attenuation = 1.0 / ( falloff.x + (falloff.y*lightDir) + (falloff.z*lightDir*lightDir) );

    //calculate the final color
    vec3 color = diffuse * texture2D(color_texture, gl_TexCoord[0].st).rgb;

    // Set the output color of our current pixel
    gl_FragColor = vec4(color, 1.0);
}

--jogl, java code hooking up the shader

int shaderProgram = ShaderControl.enableShader(gl, shaderName);

        //apply vars
        int diffuseTextureVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "color_texture");
        int normalColorVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "normal_texture");
        int lightPositionVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "lightPos");
        int lightColorVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "lightColor");
        int falloffVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "falloff");
        int resolutionVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "resolution");
        int ambienceVariableLocation = gl.getGL2().glGetUniformLocation(shaderProgram, "ambience");

        gl.getGL2().glUniform1i(diffuseTextureVariableLocation, 0);
        gl.getGL2().glUniform1i(normalColorVariableLocation, 1);
        gl.getGL2().glUniform3f(lightPositionVariableLocation, positionLight.x, positionLight.y, 1.5f);
        gl.getGL2().glUniform4f(lightColorVariableLocation, 1f, 1.0f, 1.0f, 1f);
        gl.getGL2().glUniform3f(falloffVariableLocation,.4f, 3f, 20f);
        gl.getGL2().glUniform2f(resolutionVariableLocation, Game._viewPortDimension.width, Game._viewPortDimension.height);
        gl.getGL2().glUniform1f(ambienceVariableLocation, 0.93f);

        gl.getGL2().glActiveTexture(GL2.GL_TEXTURE1);
        normalTexture.bind(gl);

        //bind diffuse color to texture unit 0
        gl.getGL2().glActiveTexture(GL2.GL_TEXTURE0);
        texture.bind(gl);

        //draw the texture and apply the bump mapping shader
        drawTexture(gl, worldOffsetX, worldOffsetY, x, y, depth, rotation, percentageToDraw, width, height, texture);

        ShaderControl.disableShader(gl);

Kind regards Johandre

1

There are 1 answers

0
Martijn Courteaux On

First, make sure you really need that. Once you are, you can create a varying vec3 in your fragment shaders that gets interpolated from the vertex shader that holds the world position. In order to do that, make sure you have separate modelview matrix and projection matrix. (I prefer having only a projection matrix for the games I made so far). Use the output of the modelview matrix for your varying vec3.