如何在 python 中使用 moderngl 将基元设置为 GL_LINES
How to set primitives to GL_LINES with moderngl in python
我使用以下代码在屏幕上渲染一个矩形,使用 moderngl 和 moderngl_window。这主要来自他们的例子:
import moderngl
import moderngl_window as mglw
import glfw
import numpy as np
import OpenGL.GL
import OpenGL.GLUT
import OpenGL.GLU
class MyWin(mglw.WindowConfig):
gl_version = (3, 3)
title = "Hello World"
window_size = (1280, 720)
aspect_ratio = 16/9;
resizable = True
samples = 4
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.prog = self.ctx.program(
vertex_shader='''
#version 330
in vec2 vert;
void main() {
gl_Position = vec4(vert.x, vert.y*1.7777, 0.0, 1.0);
}
''',
fragment_shader='''
#version 330
out vec4 color;
void main() {
color = vec4(0.3, 0.5, 1.0, 1.0);
}
''',
)
self.vertices = np.array([
-0.8, 0.8,
-0.8, -0.8,
0.8, -0.8,
0.8, 0.8,
-0.8, 0.8,
0.8,-0.8
])
self.vbo = self.ctx.buffer(self.vertices.astype('f4').tobytes())
# self.texCoordBuffer = self.ctx.buffer(texCoord.astype('f4').tobytes());
self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'vert');
# self.vao.bind(1,'f',self.texCoordBuffer,'2f');
# self.time = self.prog['time'];
@classmethod
def setVertices(self, vertices):
print("vertices have been set")
self.vertices = vertices
@classmethod
def run(cls):
mglw.run_window_config(cls);
def render(self, time, frame_time):
# self.time.value = time
self.ctx.clear(0.0, 0.0, 0.0)
self.vao.render()
但我不知道我需要用 moderngl 调用什么函数来将渲染模式设置为 GL_LINES
而不是 GL_TRIANGLES
我想我需要调用 glDrawArrays
但我找不到如何使用 moderngl 访问它。
感谢您的帮助!
您可以选择Primitive type by setting the mode argument when you invoke moderngl.VertexArray.render()。默认参数是 TRIANGLES
。为原始类型设置模式 LINES
GL_LINES
:
self.vao.render()
self.vao.render(moderngl.LINES)
我使用以下代码在屏幕上渲染一个矩形,使用 moderngl 和 moderngl_window。这主要来自他们的例子:
import moderngl
import moderngl_window as mglw
import glfw
import numpy as np
import OpenGL.GL
import OpenGL.GLUT
import OpenGL.GLU
class MyWin(mglw.WindowConfig):
gl_version = (3, 3)
title = "Hello World"
window_size = (1280, 720)
aspect_ratio = 16/9;
resizable = True
samples = 4
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.prog = self.ctx.program(
vertex_shader='''
#version 330
in vec2 vert;
void main() {
gl_Position = vec4(vert.x, vert.y*1.7777, 0.0, 1.0);
}
''',
fragment_shader='''
#version 330
out vec4 color;
void main() {
color = vec4(0.3, 0.5, 1.0, 1.0);
}
''',
)
self.vertices = np.array([
-0.8, 0.8,
-0.8, -0.8,
0.8, -0.8,
0.8, 0.8,
-0.8, 0.8,
0.8,-0.8
])
self.vbo = self.ctx.buffer(self.vertices.astype('f4').tobytes())
# self.texCoordBuffer = self.ctx.buffer(texCoord.astype('f4').tobytes());
self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'vert');
# self.vao.bind(1,'f',self.texCoordBuffer,'2f');
# self.time = self.prog['time'];
@classmethod
def setVertices(self, vertices):
print("vertices have been set")
self.vertices = vertices
@classmethod
def run(cls):
mglw.run_window_config(cls);
def render(self, time, frame_time):
# self.time.value = time
self.ctx.clear(0.0, 0.0, 0.0)
self.vao.render()
但我不知道我需要用 moderngl 调用什么函数来将渲染模式设置为 GL_LINES
而不是 GL_TRIANGLES
我想我需要调用 glDrawArrays
但我找不到如何使用 moderngl 访问它。
感谢您的帮助!
您可以选择Primitive type by setting the mode argument when you invoke moderngl.VertexArray.render()。默认参数是 TRIANGLES
。为原始类型设置模式 LINES
GL_LINES
:
self.vao.render()
self.vao.render(moderngl.LINES)