- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在考虑从 PyOpenGL 转向 ModernGL,而且我现在正在努力实现任何事情。
首先,我想尝试经典的“使用时间统一和正弦函数改变形状的三角形”,但我对如何写入统一感到困惑。
文档是这样说的:
A uniform is a global GLSL variable declared with the “uniform” storage qualifier. These act asparameters that the user of a shader program can pass to that program.
In ModernGL, Uniforms can be accessed using
Program.__getitem__()
orProgram.__iter__()
.# Set a vec4 uniform
uniform['color'] = 1.0, 1.0, 1.0, 1.0
# Optionally we can store references to a member and set the value directly
uniform = program['color']
uniform.value = 1.0, 0.0, 0.0, 0.0
uniform = program['cameraMatrix']
uniform.write(camera_matrix)
这是我的代码:
import moderngl as mgl
import glfw
import numpy as np
import time
from math import sin
glfw.init()
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window = glfw.create_window(800, 600, "__DELETEME__", None, None)
glfw.make_context_current(window)
context = mgl.create_context()
vertex_source = """
#version 330 core
in vec2 aPos;
uniform float time;
void main() {
gl_Position = vec4(aPos.x, aPos.y + sin(time), 0.0, 1.0);
}
"""
fragment_source = """
#version 330 core
out vec4 color;
void main(){
color = vec4(0.0, 0.0, 1.0, 1.0);
}
"""
program = context.program(vertex_shader=vertex_source, fragment_shader=fragment_source)
data = np.array([
0.5, 0,
-0.5, 0,
0, 0.5], dtype = "float32")
vbo = context.buffer(data.tobytes())
vao = context.vertex_array(program, vbo, "aPos")
uniform = program["time"]
uniform.value = 1.0
while not glfw.window_should_close(window):
now = time.time()
vao.render()
elapsed = time.time() - now
glfw.poll_events()
glfw.swap_buffers(window)
glfw.terminate()
现在它什么都不画了。我究竟做错了什么?谢谢!
最佳答案
耗时是开始时间和当前时间之间的差值。获取应用程序循环之前的开始时间,并计算每一帧循环中耗时:
start_time = time.time()
while not glfw.window_should_close(window):
elapsed = time.time() - start_time
# [...]
uniform "time"
的值必须在循环中不断更新:
while not glfw.window_should_close(window):
# [...]
uniform.value = elapsed
您必须在应用程序循环中清除每一帧中的显示(参见 ModernGL Context):
while not glfw.window_should_close(window):
# [...]
context.clear(0.0, 0.0, 0.0)
vao.render()
完整示例:
import moderngl as mgl
import glfw
import numpy as np
import time
from math import sin
glfw.init()
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window = glfw.create_window(800, 600, "__DELETEME__", None, None)
glfw.make_context_current(window)
context = mgl.create_context()
vertex_source = """
#version 330 core
in vec2 aPos;
uniform float time;
void main() {
gl_Position = vec4(aPos.x, aPos.y + sin(time), 0.0, 1.0);
}
"""
fragment_source = """
#version 330 core
out vec4 color;
void main(){
color = vec4(0.0, 0.0, 1.0, 1.0);
}
"""
program = context.program(vertex_shader=vertex_source, fragment_shader=fragment_source)
data = np.array([
0.5, 0,
-0.5, 0,
0, 0.5], dtype = "float32")
vbo = context.buffer(data.tobytes())
vao = context.vertex_array(program, vbo, "aPos")
uniform = program["time"]
uniform.value = 1.0
start_time = time.time()
while not glfw.window_should_close(window):
elapsed = time.time() - start_time
uniform.value = elapsed
context.clear(0.0, 0.0, 0.0)
vao.render()
glfw.poll_events()
glfw.swap_buffers(window)
glfw.terminate()
关于python - ModernGL 设置统一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63658326/
我正在考虑从 PyOpenGL 转向 ModernGL,而且我现在正在努力实现任何事情。 首先,我想尝试经典的“使用时间统一和正弦函数改变形状的三角形”,但我对如何写入统一感到困惑。 文档是这样说的:
此代码在没有抗锯齿 (samples=0) 时呈现彩色三角形。但是,当我打开抗锯齿 (samples=1...32) 时,它无法呈现任何内容。如何使其与抗锯齿一起使用?也许我无法直接从多重采样 fbo
此代码在没有抗锯齿 (samples=0) 时呈现彩色三角形。但是,当我打开抗锯齿 (samples=1...32) 时,它无法呈现任何内容。如何使其与抗锯齿一起使用?也许我无法直接从多重采样 fbo
我有 2 个 FBO 之一,我一直在使用它在 glsl 中进行一些计算,我需要将纹理数据(dtype='f4')读回一个 numpy 数组以进行进一步计算。我没有在说明如何执行此操作的文档中找到任何内
我是一名优秀的程序员,十分优秀!