2014-01-08 69 views
0

所以我一直在嘗試實施凹凸貼圖一段時間,並且我以某種方式工作。所以它使紋理和陰影正確,但不會隨着光源四處移動而改變,我確定它應用光源從源(0,0)移動而不是在光源處於世界的位置。我如何確定着色器中片段的世界位置?我現在有點卡住,任何幫助將不勝感激。glsl中的頂點世界位置,JOGL

--vertex着色器

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

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

--fragment着色器

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

親切的問候 Johandre

回答

0

首先,確保你真的需要那個。一旦完成,您可以在片段着色器中創建一個varying vec3,該片段着色器從包含世界位置的頂點着色器中插入。爲了做到這一點,確保你有單獨的模型視圖矩陣和投影矩陣。 (我更喜歡目前爲止製作的遊戲只有一個投影矩陣)。使用varying vec3的模型視圖矩陣的輸出。