纹理(shadowMap,coords).xyzw总是返回(1,1,1,1)

时间:2017-06-25 16:15:20

标签: java opengl glsl textures shadow-mapping

我正在尝试在我的OpenGL场景中实现阴影。但是,在我的着色器中,每当我尝试访问阴影贴图中的纹理值(我之前渲染)时,{{1}总是得到1或接近1的值{{1 }},xyz值。

问题在于我认为这些值应该代表阴影贴图/纹理中相应片段的w坐标(从太阳的角度来看)。

相关的启动代码(在JOGL中):

z

相关的抽奖/每帧代码:

gl.glEnable(GL2.GL_TEXTURE_2D);

// generate stuff
IntBuffer ib = IntBuffer.allocate(1);
gl.glGenFramebuffers(1, ib);
depthMapFBO = ib.get(0);

ib = IntBuffer.allocate(1);
gl.glGenTextures(1, ib);
depthMap = ib.get(0);

gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, depthMapFBO);
gl.glBindTexture(GL2.GL_TEXTURE_2D, depthMap);
gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL2.GL_DEPTH_COMPONENT, GL2.GL_FLOAT, null);
// prevents 'shadow acne'
gl.glPolygonOffset(2.5f, 0);
// prevents multiple shadows
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE);
// prevents (or expects!!!) pixel-y textures
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
// store one value in all four components of pixel
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_DEPTH_TEXTURE_MODE, GL2.GL_INTENSITY);

相关的片段着色器代码(用于调试值;在glsl 1.3中)

// render shadows
gl.glUseProgram(shadowProgram);

gl.glUniformMatrix4fv(lightMatrixLocShadow, 1, false, lightMatrix.getMatrix(), 0);  // yep (haha change viewMatrix -> lightMatrix)
gl.glUniformMatrix4fv(projMatrixLocShadow, 1, false, projMatrix.getMatrix(), 0);

gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, depthMapFBO);
gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT, GL2.GL_TEXTURE_2D, sha.depthMap, 0);
gl.glDrawBuffer(GL2.GL_NONE);
gl.glReadBuffer(GL2.GL_NONE);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);

gl.glViewport(0, 0, 1024, 1024);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, depthMapFBO);
gl.glClear(GL2.GL_DEPTH_BUFFER_BIT);
renderScene(gl, sunMatrix);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);

// render display (regular)
gl.glUseProgram(displayProgram);
gl.glDrawBuffer(GL2.GL_FRONT);
gl.glReadBuffer(GL2.GL_FRONT);

gl.glUniformMatrix4fv(viewMatrixLoc, 1, false, viewMatrix.getMatrix(), 0);
gl.glUniformMatrix4fv(projMatrixLocDisplay, 1, false, projMatrix.getMatrix(), 0);
gl.glUniformMatrix4fv(lightMatrixLocDisplay, 1, false, lightMatrix.getMatrix(), 0);
gl.glUniform4fv(sunPositionLoc, 1, sunWorldPosition, 0);    // send sun's position to shader
gl.glUniform1f(sunBrightnessLoc, sunBrightness);
gl.glUniform1i(shadowMapLoc, 0);

gl.glViewport(0, 0, screenWidth, screenHeight);
//gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, depthMapFBO);
// day-night cycle
float[] color = SkyManager.getSkyColor(time);
gl.glClearColor(color[0], color[1], color[2], 1);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, sha.depthMap);
//gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
renderScene(gl, sunMatrix);

gl_FragColor = vec4(vec3(texture2D(shadowMap, lightProjPosition.xy).z), 1.0); 调用的预期值当然是片段与光源的不同距离。 如果有人能找到我收到的原因texture(),我将不胜感激。

修改

以下是阴影着色器代码:

顶点:

1

片段:

#version 130

uniform mat4 light;
uniform mat4 proj;

out vec4 position;

void main() {
    position = proj * light * gl_Vertex;
    gl_Position = position;
}

0 个答案:

没有答案