A short time ago I was testing Shadow Mapping using OpenGL, and it worked. But when I tried to change the window size to full screen, the Shadow-Mapping size also changed, I thought about using the window height and width in the size
variable inside the shader to see if I could modify the position of the shadow-mapping along with the size of the window, but I'm not sure how to do it and I ended up doing some really weird things and I didn't get anywhere, can you help me?
Normal:
When I change the window size:
I tried to use the size inside texture (shadow, vec2 (.5) + o.xy / 80) .r
and something came out when I did texture (shadow, vec2 (.5 ) * vec2 (size.x / size.y, 1) + o.xy / 80) .r
, but it was not a very viable solution ...
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()
When you resize the window, then the size of the (shadow) framebuffer doesn't change. You have to fit the viewport rectangle to the size of the frambuffer:
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)
# [...]
Additionally you can recreate the framebuffer in the on_resize
event:
@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)