Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create(self):
mesh = VAO("test", mode=moderngl.LINES)
mesh.buffer(numpy.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0], dtype='f4'), '3f', 'position')
mesh.buffer(numpy.array([0.0, 0.0, 1.0, 1.0, 0.0, 1.0], dtype='f4'), '3f', 'normal')
mesh.buffer(numpy.array([0.0, 0.0, 1.0, 1.0], dtype='f4'), '2f', 'uv')
# Ensure basic properties are correct
self.assertEqual(mesh.name, "test")
self.assertEqual(mesh.vertex_count, 2)
self.assertEqual(mesh.mode, moderngl.LINES)
# Ensure buffers are present
self.assertIsInstance(mesh.get_buffer_by_name('position'), BufferInfo)
self.assertIsInstance(mesh.get_buffer_by_name('normal'), BufferInfo)
self.assertIsInstance(mesh.get_buffer_by_name('uv'), BufferInfo)
self.assertEqual(mesh.get_buffer_by_name('something'), None)
# Create a progam using a subset of the buffers
def test_create(self):
mesh = VAO("test", mode=moderngl.LINES)
mesh.buffer(numpy.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0], dtype='f4'), '3f', 'position')
mesh.buffer(numpy.array([0.0, 0.0, 1.0, 1.0, 0.0, 1.0], dtype='f4'), '3f', 'normal')
mesh.buffer(numpy.array([0.0, 0.0, 1.0, 1.0], dtype='f4'), '2f', 'uv')
# Ensure basic properties are correct
self.assertEqual(mesh.name, "test")
self.assertEqual(mesh.vertex_count, 2)
self.assertEqual(mesh.mode, moderngl.LINES)
# Ensure buffers are present
self.assertIsInstance(mesh.get_buffer_by_name('position'), BufferInfo)
self.assertIsInstance(mesh.get_buffer_by_name('normal'), BufferInfo)
self.assertIsInstance(mesh.get_buffer_by_name('uv'), BufferInfo)
self.assertEqual(mesh.get_buffer_by_name('something'), None)
# Create a progam using a subset of the buffers
prog = self.createProgram()
vao = mesh.instance(prog)
self.assertIsInstance(vao, moderngl.VertexArray)
# Render directly with VAO and VertexArray instance
mesh.render(prog)
vao.render()
def draw(self, mvp, light, bright, scatter):
if not self.visible: return
self.ctx.line_width = self.width
mvp = np.dot(*mvp)
self.prog['Mvp'].write(mvp.astype(np.float32).tobytes())
self.prog['blend'].value = self.blend
self.prog['scatter'].value = scatter
self.prog['light'].value = tuple(light)
self.prog['bright'].value = bright
self.vao.render({'mesh':moderngl.TRIANGLES, 'grid':moderngl.LINES}[self.mode])
def render(self, time, frame_time):
self.move_camera()
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.DEPTH_TEST)
self.mvp.write((self.camera.mat_projection * self.camera.mat_lookat).astype('f4').tobytes())
self.vao.render(moderngl.LINES)
import numpy
import moderngl
import moderngl_window as mglw
from moderngl_window.opengl import types
# For sanity checking draw modes when creating the VAO
DRAW_MODES = {
moderngl.TRIANGLES: 'TRIANGLES',
moderngl.TRIANGLE_FAN: 'TRIANGLE_FAN',
moderngl.TRIANGLE_STRIP: 'TRIANGLE_STRIP',
moderngl.TRIANGLES_ADJACENCY: 'TRIANGLES_ADJACENCY',
moderngl.TRIANGLE_STRIP_ADJACENCY: 'TRIANGLE_STRIP_ADJACENCY',
moderngl.POINTS: 'POINTS',
moderngl.LINES: 'LINES',
moderngl.LINE_STRIP: 'LINE_STRIP',
moderngl.LINE_LOOP: 'LINE_LOOP',
moderngl.LINES_ADJACENCY: 'LINES_ADJACENCY',
}
class BufferInfo:
"""Container for a vbo with additional information"""
def __init__(self, buffer: moderngl.Buffer, buffer_format: str, attributes=None, per_instance=False):
"""
:param buffer: The vbo object
:param format: The format of the buffer
"""
self.buffer = buffer
self.attrib_formats = types.parse_attribute_formats(buffer_format)
self.attributes = attributes
def draw(self, mvp, light, bright, scatter):
if not self.visible: return
self.ctx.line_width = 2
self.prog['mv'].write(mvp[0].astype(np.float32).tobytes())
self.prog['proj'].write(mvp[1].astype(np.float32).tobytes())
self.prog['f_color'].write(np.array(self.color).astype(np.float32).tobytes())
self.prog['h'].value = self.h
self.vao.render(moderngl.LINES)
def render(self, time: float, frame_time: float):
self.ctx.clear(1.0, 1.0, 1.0)
self.vao.render(moderngl.LINES, 65 * 4)
def render(self, time, frame_time):
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.DEPTH_TEST)
proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
(40.0, 30.0, 30.0),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0),
)
self.mvp.write((proj * lookat).astype('f4').tobytes())
self.vao.render(moderngl.LINES)
import numpy
import moderngl
from demosys import context
from demosys.opengl import types
# For sanity checking draw modes when creating the VAO
DRAW_MODES = {
moderngl.TRIANGLES: 'TRIANGLES',
moderngl.TRIANGLE_FAN: 'TRIANGLE_FAN',
moderngl.TRIANGLE_STRIP: 'TRIANGLE_STRIP',
moderngl.TRIANGLES_ADJACENCY: 'TRIANGLES_ADJACENCY',
moderngl.TRIANGLE_STRIP_ADJACENCY: 'TRIANGLE_STRIP_ADJACENCY',
moderngl.POINTS: 'POINTS',
moderngl.LINES: 'LINES',
moderngl.LINE_STRIP: 'LINE_STRIP',
moderngl.LINE_LOOP: 'LINE_LOOP',
moderngl.LINES_ADJACENCY: 'LINES_ADJACENCY',
}
class BufferInfo:
"""Container for a vbo with additional information"""
def __init__(self, buffer: moderngl.Buffer, buffer_format: str, attributes=None, per_instance=False):
"""
:param buffer: The vbo object
:param format: The format of the buffer
"""
self.buffer = buffer
self.attrib_formats = types.parse_attribute_formats(buffer_format)
self.attributes = attributes
def render(self):
for i in range(len(self.YOLO_BOX_VAOS)):
self.CTX.disable(moderngl.DEPTH_TEST)
self.PROG["mode"].value = 2
self.PROG["box_rgb"].value = tuple(self.BOX_RGBS[i])
self.YOLO_BOX_VAOS[i].render(moderngl.LINES)
self.PROG["mode"].value = 1
self.YOLO_LABELS.use()
self.YOLO_LABEL_VAOS[i].render()
self.CTX.enable(moderngl.DEPTH_TEST)
self.PROG["mode"].value = 0