如何定位 Window 大小的阴影贴图?

How to position Shadow Mapping with Window Size?

不久前,我正在使用 OpenGL 测试阴影贴图,它成功了。但是当我尝试将 window 大小更改为全屏时,Shadow-Mapping 大小也发生了变化,我想到了使用着色器内部 size 变量中的 window 高度和宽度来看看我是否可以修改阴影贴图的位置以及 window 的大小,但我不确定该怎么做,最后我做了一些非常奇怪的事情,但我没有得到任何结果, 你能帮帮我吗?

正常:

当我更改 window 尺码时:

我尝试使用 texture (shadow, vec2 (.5) + o.xy / 80) .r 中的大小,当我这样做时出现了一些问题 texture (shadow, vec2 (.5 ) * vec2 (size.x / size.y, 1) + o.xy / 80) .r,但这不是一个非常可行的解决方案...

import pyglet, math, pyrr, ctypes
import numpy as np
from OpenGL.GL import *
from OpenGL.GL.shaders import *

app = pyglet.window.Window(resizable=True)

v = """
in layout(location=0) vec3 posicao;
uniform mat4 view;
uniform vec3 translate;
uniform float rot;
uniform float rot2;
uniform vec3 t2;
out vec3 outpos;
void main(){
    vec3 p = posicao;
    p = vec3(sin(rot)*p.x+cos(rot)*p.z,p.y,-sin(rot)*p.z+cos(rot)*p.x);
    p = translate+p+t2;
    p = vec3(p.x,sin(rot2)*p.z+cos(rot2)*p.y,-sin(rot2)*p.y+cos(rot2)*p.z);
    outpos = p;
    gl_Position = view*vec4(p,1);
}
"""

f = """
uniform vec3 cor;
uniform int modo;
uniform sampler2D shadow;
uniform vec2 size;
in vec3 outpos;
void main(){
    if(modo==0){
        float pi = 3.141592653589793;
        vec3 o = outpos+vec3(0,-30,-20);
        o = vec3(o.x,sin(-45*pi/180)*o.z+cos(-45*pi/180)*o.y,-sin(-45*pi/180)*o.y+cos(-45*pi/180)*o.z);
        o.z+=2;
        float d = texture(shadow,vec2(.5)+(o.xy/80)).r;
        float i = 1;
        if(d<-o.z/400){
            i = 0.5;
        }
        gl_FragColor = vec4(vec3(cor),1)*i;
        // gl_FragColor = texture(shadow,outpos.xy);
    }else{
        gl_FragColor = vec4(vec3(-outpos.z/400),1);
    }
}
"""

shader = compileProgram(compileShader(v,GL_VERTEX_SHADER),compileShader(f,GL_FRAGMENT_SHADER))

glUseProgram(shader)

tudo = [-100,-4,100,
        100,-4,100,
        100,-4,-100,
        -100,-4,-100,
        -2,-2,2,
        -2,-2,-2,
        2,-2,-2,
        2,-2,2,
        2,2,2,
        2,2,-2,
        -2,2,-2,
        -2,2,2,
        -2,2,2,
        -2,2,-2,
        -2,-2,-2,
        -2,-2,2,
        2,-2,2,
        2,-2,-2,
        2,2,-2,
        2,2,2,
        -2,2,-2,
        2,2,-2,
        2,-2,-2,
        -2,-2,-2,
        -2,-2,2,
        2,-2,2,
        2,2,2,
        -2,2,2]

tudo = np.array(tudo, dtype=np.float32)

CUBO = glGenBuffers(1)

glBindBuffer(GL_ARRAY_BUFFER, CUBO)
glBufferData(GL_ARRAY_BUFFER, len(tudo)*4, tudo, GL_STREAM_DRAW)

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)

view = pyrr.matrix44.create_perspective_projection_matrix(60, app.width/app.height, .1, 10000)
p = glGetUniformLocation(shader, "view")
glUniformMatrix4fv(p, 1, GL_FALSE, view)

glEnable(GL_DEPTH_TEST)

glEnable(GL_CULL_FACE)
glCullFace(GL_FRONT)

# Tentativa de Shadow Mapping

glEnable(GL_TEXTURE_2D)

tf = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, tf)
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, app.width, app.height, 0, GL_RED, GL_UNSIGNED_BYTE, None)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
f = glGenFramebuffers(1)
r = glGenRenderbuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, f)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tf, 0)
glBindRenderbuffer(GL_RENDERBUFFER, r)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, app.width, app.height)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, r)
glBindRenderbuffer(GL_RENDERBUFFER, 0)
glBindFramebuffer(GL_FRAMEBUFFER, 0)

pos = [0,-10,0]
comando = {"a":0,"w":0,"s":0,"d":0,"q":0,"e":0}
girar = -1

@app.event
def on_draw():
    global CUBO, shader, pos, comando, tudo, girar, f
    p = glGetUniformLocation(shader, "size")
    glUniform2f(p, app.width, app.height)
    glBindFramebuffer(GL_FRAMEBUFFER, f)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    # Luz Direcional
    view = pyrr.matrix44.create_orthogonal_projection_matrix(-app.width/16, app.width/16, -app.height/16, app.height/16, .1, 10000)
    p = glGetUniformLocation(shader, "view")
    glUniformMatrix4fv(p, 1, GL_FALSE, view)
    # Z-Buffer
    p = glGetUniformLocation(shader, "modo")
    glUniform1i(p, 1)
    p = glGetUniformLocation(shader, "rot2")
    glUniform1f(p, -45*math.pi/180)
    p = glGetUniformLocation(shader, "t2")
    glUniform3f(p, 0, -30, -20)
    # Movimento
    v = .04
    if comando["a"] == 1:
        pos[0]-=v
    if comando["d"] == 1:
        pos[0]+=v
    if comando["w"] == 1:
        pos[1]-=v
    if comando["s"] == 1:
        pos[1]+=v
    if comando["q"] == 1:
        pos[2]-=v
    if comando["e"] == 1:
        pos[2]+=v
    # fundo vermelho
    p = glGetUniformLocation(shader, "translate")
    glUniform3f(p, 0, 0, 0)
    p = glGetUniformLocation(shader, "rot")
    glUniform1f(p, 0)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 1, 0, 0)
    glDrawArrays(GL_QUADS, 0, 4)
    # objeto no meio
    p = glGetUniformLocation(shader, "translate")
    glUniform3f(p, -1, -5, -10)
    p = glGetUniformLocation(shader, "rot")
    glUniform1f(p, 0)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 0, 0, 1)
    glDrawArrays(GL_QUADS, 8, 4)
    # objeto azul
    p = glGetUniformLocation(shader, "translate")
    glUniform3f(p, pos[0], pos[2], pos[1])
    p = glGetUniformLocation(shader, "rot")
    girar+=.4
    glUniform1f(p, girar*math.pi/180)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 0, 0, 1)
    glDrawArrays(GL_QUADS, 4, 8)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 0, 1, 0)
    glDrawArrays(GL_QUADS, 12, 8)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 1, 1, 0)
    glDrawArrays(GL_QUADS, 20, 8)
    # gravar o Framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0)
    # reset
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    # Normal
    view = pyrr.matrix44.create_perspective_projection_matrix(60, app.width/app.height, .1, 10000)
    p = glGetUniformLocation(shader, "view")
    glUniformMatrix4fv(p, 1, GL_FALSE, view)
    # Z-Buffer tirando
    p = glGetUniformLocation(shader, "modo")
    glUniform1i(p, 0)
    p = glGetUniformLocation(shader, "rot2")
    glUniform1f(p, 0*math.pi/180)
    p = glGetUniformLocation(shader, "t2")
    glUniform3f(p, 0, 0, 0)
    # fundo vermelho
    p = glGetUniformLocation(shader, "translate")
    glUniform3f(p, 0, 0, 0)
    p = glGetUniformLocation(shader, "rot")
    glUniform1f(p, 0)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 1, 0, 0)
    glDrawArrays(GL_QUADS, 0, 4)
    # objeto no meio
    p = glGetUniformLocation(shader, "translate")
    glUniform3f(p, -1, -5, -10)
    p = glGetUniformLocation(shader, "rot")
    glUniform1f(p, 0)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 0, 0, 1)
    glDrawArrays(GL_QUADS, 8, 4)
    # objeto azul
    p = glGetUniformLocation(shader, "translate")
    glUniform3f(p, pos[0], pos[2], pos[1])
    p = glGetUniformLocation(shader, "rot")
    glUniform1f(p, girar*math.pi/180)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 0, 0, 1)
    glDrawArrays(GL_QUADS, 4, 8)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 0, 1, 0)
    glDrawArrays(GL_QUADS, 12, 8)
    p = glGetUniformLocation(shader, "cor")
    glUniform3f(p, 1, 1, 0)
    glDrawArrays(GL_QUADS, 20, 8)

@app.event
def on_key_press(k,m):
    global comando, grandeG
    if k == pyglet.window.key.A:
        comando["a"] = 1
    if k == pyglet.window.key.W:
        comando["w"] = 1
    if k == pyglet.window.key.S:
        comando["s"] = 1
    if k == pyglet.window.key.D:
        comando["d"] = 1
    if k == pyglet.window.key.Q:
        comando["q"] = 1
    if k == pyglet.window.key.E:
        comando["e"] = 1

@app.event
def on_key_release(k,m):
    global comando
    if k == pyglet.window.key.A:
        comando["a"] = 0
    if k == pyglet.window.key.W:
        comando["w"] = 0
    if k == pyglet.window.key.S:
        comando["s"] = 0
    if k == pyglet.window.key.D:
        comando["d"] = 0
    if k == pyglet.window.key.Q:
        comando["q"] = 0
    if k == pyglet.window.key.E:
        comando["e"] = 0

def SRO(dt):
    on_draw()

pyglet.clock.schedule(SRO)

pyglet.app.run()

当您调整 window 大小时,(影子)帧缓冲区的大小不会改变。您必须使视口矩形适合帧缓冲区的大小:

framebuffer_size = app.width, app.height
tf = glGenTextures(1)

# [...]

@app.event
def on_draw():
    # [...]

    glViewport(0, 0, framebuffer_size[0], framebuffer_size[1]) # <----
    glBindFramebuffer(GL_FRAMEBUFFER, f)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # [...]

    glViewport(0, 0, app.width, app.height) # <---
    glBindFramebuffer(GL_FRAMEBUFFER, 0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # [...]

此外,您可以在 on_resize 事件中重新创建帧缓冲区:

@app.event
def on_resize(width, height):
    global framebuffer_size
    framebuffer_size = app.width, app.height

    glBindTexture(GL_TEXTURE_2D, tf)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, app.width, app.height, 0, GL_RED, GL_UNSIGNED_BYTE, None)
    glBindFramebuffer(GL_FRAMEBUFFER, f)
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tf, 0)
    glBindRenderbuffer(GL_RENDERBUFFER, r)
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, app.width, app.height)
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, r)
    glBindRenderbuffer(GL_RENDERBUFFER, 0)
    glBindFramebuffer(GL_FRAMEBUFFER, 0)