为了账号安全,请及时绑定邮箱和手机立即绑定

ModernGL套装制服

ModernGL套装制服

扬帆大鱼 2023-07-05 16:36:31
我正在考虑切换到 ModernGL 而不是 PyOpenGL,而且我现在正在努力实现任何东西。首先,我想尝试经典的“使用时间均匀和正弦函数改变形状的三角形”,但我一直不知道如何写入均匀。以下是文档对此的说明:统一是使用“统一”存储限定符声明的全局 GLSL 变量。这些充当着色器程序的用户可以传递给该程序的参数。在 ModernGL 中,可以使用Program.__getitem__()或访问制服Program.__iter__()。# Set a vec4 uniformuniform['color'] = 1.0, 1.0, 1.0, 1.0# Optionally we can store references to a member and set the value directlyuniform = program['color']uniform.value = 1.0, 0.0, 0.0, 0.0uniform = program['cameraMatrix']uniform.write(camera_matrix)这是我的代码:import moderngl as mglimport glfwimport numpy as npimport timefrom math import singlfw.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 corein vec2 aPos;uniform float time;void main() {    gl_Position = vec4(aPos.x, aPos.y + sin(time), 0.0, 1.0);}"""fragment_source = """#version 330 coreout 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.0while not glfw.window_should_close(window):    now = time.time()    vao.render()    elapsed = time.time() - now    glfw.poll_events()    glfw.swap_buffers(window)glfw.terminate()现在它什么也没画。我究竟做错了什么?谢谢!
查看完整描述

1 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

经过的时间是开始时间和当前时间之间的差值。获取应用程序循环之前的开始时间并计算每帧循环中经过的时间:


start_time = time.time()

while not glfw.window_should_close(window):

    elapsed = time.time() - start_time


    # [...]

制服的值"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()


查看完整回答
反对 回复 2023-07-05
  • 1 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信