1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
from pathlib import Path
from pyrr import Matrix44
import numpy
import moderngl
import moderngl_window
from moderngl_window import geometry
from base import CameraWindow
class LinesDemo(CameraWindow):
"""Rendering thick lines with geometry shader
Example is basic and incomplete, but shows how
one could use the geometry shader to create thick lines.
"""
gl_version = (3, 3)
title = "Thick Lines"
resource_dir = (Path(__file__) / '../resources').absolute()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.wnd.mouse_exclusivity = True
self.prog = self.load_program('programs/lines/lines.glsl')
self.prog['color'].value = (1.0, 1.0, 1.0, 1.0)
self.prog['m_model'].write(Matrix44.from_translation((0.0, 0.0, -3.5), dtype='f4'))
N = 10
# Create lines geometry
def gen_lines():
for i in range(N):
# A
yield -1.0
yield 1.0 - i * 2.0 / N
yield 0.0
# b
yield 1.0
yield 1.0 - i * 2.0 / N
yield 0.0
buffer = self.ctx.buffer(numpy.fromiter(gen_lines(), dtype='f4', count=N * 6).tobytes())
self.lines = self.ctx.vertex_array(
self.prog,
[
(buffer, '3f', 'in_position'),
],
)
def render(self, time, frametime):
# self.ctx.enable_only(moderngl.DEPTH_TEST)
self.prog['m_proj'].write(self.camera.projection.matrix)
self.prog['m_cam'].write(self.camera.matrix)
self.lines.render(mode=moderngl.LINES)
if __name__ == '__main__':
moderngl_window.run_window_config(LinesDemo)
|