【发布时间】:2018-05-05 17:34:26
【问题描述】:
在 kivy 中创建 fbo 时,它接受 "with_depthbuffer" 作为参数。在文档中,它说这将导致 fbo 被分配一个 z 缓冲区。我想尝试使用它来制作灯光效果。
我的问题是,我在哪里可以找到和使用该缓冲区? 我可以选择清除它,但查看源代码并没有帮助。 我注意到 fbo.texture 有一个 bufferfmt 属性。深度缓冲区是否存储在图像纹理中?我可以在 glsl 中访问它以获取像素的深度吗?
提前致谢
编辑:
我找到了一种保存深度信息的“hacky”方法,方法是将 gl_FragColor 分配给 vec4(lambert_value, -1.0 / vertex_pos.z, 1.0, 1.0) 并在后处理中使用这些值。但显然我不能在实际应用中使用它
编辑:
好的,所以我想我想出的临时方法似乎非常准确。
我访问了这个页面:https://github.com/kivy/kivy/blob/master/kivy/graphics/fbo.pyx 在第 230 行,如果 with_depthbuffer == True,则绑定渲染缓冲区。
我查看了渲染缓冲区是什么,显然它们只能在当前着色器通道中使用。这意味着我从 fbo 获取这些信息的目标行不通。
然后我意识到我永远不需要从相机的角度来看的深度信息,只需要光线。所以我只是使用灯光的着色器生成深度信息。我觉得我好笨。但事情就是这样发展的。我想我可以弄清楚这一点。我将继续更新此页面,以便提供更多信息。感谢阅读
如果您想对其进行测试,只需单击/拖动以更改灯光位置。 还有一些注释行使用我的临时方法来保存深度信息,这样你就可以看到我想要做什么。 您还需要某种目标文件,我使用了搅拌机猴子
main.py
post_shader = '''
#ifdef GL_ES
precision highp float;
#endif
/* Outputs from the vertex shader */
varying vec4 frag_color;
varying vec2 tex_coord0;
/* uniform texture samplers */
uniform sampler2D texture0;
uniform vec2 mouse_pos;
void main(void)
{
float distance = length(gl_FragCoord.xy - mouse_pos) * .02;
vec4 pixel = texture2D(texture0, tex_coord0);
vec3 color = pixel.rgb / distance;
gl_FragColor = vec4(color, 1.0);
// uncomment for desired results
// float brightness = clamp(pixel.r / distance, .1, .8) * pixel.g;
// gl_FragColor = vec4(brightness, brightness, brightness, 1.0);
}
'''
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.resources import resource_find
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics.opengl import *
from kivy.graphics import *
from objloader import ObjFile
class Renderer(Widget):
def __init__(self, **kwargs):
self.canvas = RenderContext(use_parent_projection=True)
# self.canvas.shader.fs = resource_find('post_shader.glsl')
self.canvas.shader.fs = post_shader
scene = ObjFile(resource_find("Models/monkey.obj"))
m = list(scene.objects.values())[0]
self.z_pos = 0
self.d = 1
super(Renderer, self).__init__(**kwargs)
with self.canvas:
self.fbo = Fbo(compute_normal_matrix=True,
with_depthbuffer=True)
self.fbo.shader.source = resource_find('simple.glsl')
self.display = Rectangle()
with self.fbo:
self.cb = Callback(self.setup_gl_context)
ClearColor(1, 0, 0, 0)
ClearBuffers(clear_depth=True)
PushMatrix()
self.translation = Translate(0, 0, -3)
self.rot = Rotate(1, 0, 1, 0)
UpdateNormalMatrix()
Color(rgb=(0, 0, 0))
self.mesh = Mesh(vertices=m.vertices,
indices=m.indices,
fmt=m.vertex_format,
mode='triangles')
PopMatrix()
self.cb = Callback(self.reset_gl_context)
self.display.texture = self.fbo.texture
Clock.schedule_interval(self.update, 1 / 60.)
def setup_gl_context(self, *args):
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
def reset_gl_context(self, *args):
glDisable(GL_DEPTH_TEST)
glDisable(GL_CULL_FACE)
def on_touch_move(self, touch):
self.canvas['mouse_pos'] = touch.pos
def update(self, dt):
self.z_pos += self.d * dt
if self.d > 0:
if self.z_pos > -2:
self.d *= -1
elif self.z_pos < -15:
self.d *= -1
self.translation.z = self.z_pos
self.rot.angle += dt * 10
def on_size(self, *args):
self.display.size = self.size
self.fbo['projection_mat'].perspective(90, self.width / float(self.height), .1, 25)
class RendererApp(App):
def build(self):
root = FloatLayout()
root.add_widget(Renderer())
return root
if __name__ == "__main__":
RendererApp().run()
simple.glsl
/* simple.glsl
simple diffuse lighting based on laberts cosine law; see e.g.:
http://en.wikipedia.org/wiki/Lambertian_reflectance
http://en.wikipedia.org/wiki/Lambert%27s_cosine_law
*/
---VERTEX SHADER-------------------------------------------------------
#ifdef GL_ES
precision highp float;
#endif
attribute vec3 v_pos;
attribute vec3 v_normal;
uniform mat4 modelview_mat;
uniform mat4 projection_mat;
varying vec4 normal_vec;
varying vec4 vertex_pos;
void main (void) {
//compute vertex position in eye_space and normalize normal vector
vec4 pos = modelview_mat * vec4(v_pos,1.0);
vertex_pos = pos;
normal_vec = vec4(v_normal, 0.0);
gl_Position = projection_mat * pos;
}
---FRAGMENT SHADER-----------------------------------------------------
#ifdef GL_ES
precision highp float;
#endif
varying vec4 normal_vec;
varying vec4 vertex_pos;
uniform mat4 normal_mat;
void main (void){
//correct normal, and compute light vector (assume light at the eye)
vec4 v_normal = normalize( normal_mat * normal_vec ) ;
vec4 v_light = normalize( vec4(0,0,0,1) - vertex_pos );
//reflectance based on lamberts law of cosine
float theta = clamp(dot(v_normal, v_light), 0.0, 1.0);
gl_FragColor = vec4(theta, theta, theta, 1.0);
// uncomment for desired results
// gl_FragColor = vec4(theta, -3.0 / vertex_pos.z, 1.0, 1.0);
}
【问题讨论】:
标签: 3d glsl kivy buffer lighting