0

所以我一直在尝试实现凹凸映射一段时间,并且我让它以某种方式工作。因此它可以正确渲染纹理和阴影,但不会随着光源的移动而变化 如何确定着色器中片段的世界位置?我现在有点卡住了,任何帮助将不胜感激。

--顶点着色器

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

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

--片段着色器

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代码

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);

亲切的问候约翰德

4

1 回答 1

0

首先,确保你真的需要它。一旦你是,你可以varying vec3在你的片段着色器中创建一个从保持世界位置的顶点着色器中插值的片段着色器。为此,请确保您有单独的模型视图矩阵和投影矩阵。(我更喜欢只为我目前制作的游戏提供投影矩阵)。将模型视图矩阵的输出用于您的varying vec3.

于 2014-01-08T16:58:07.407 回答