所以我一直试图实现凹凸贴图一段时间,我让它以某种方式工作。所以它渲染纹理和阴影是正确的,但是当光源移动时不会改变我确定它应用从光源(0,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);
亲切的问候 Johandre
答案 0 :(得分:0)
首先,确保你真的需要它。完成后,您可以在片段着色器中创建varying vec3
,从包含世界位置的顶点着色器进行插值。为此,请确保您具有单独的模型视图矩阵和投影矩阵。 (我更喜欢到目前为止我只制作了一个投影矩阵)。使用模型视图矩阵的输出作为varying vec3
。