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_attributes(self):
sampler = self.ctx.sampler()
# Default values
assert sampler.anisotropy == 1.0
assert sampler.repeat_x is True
assert sampler.repeat_y is True
assert sampler.filter == (moderngl.LINEAR, moderngl.LINEAR)
assert sampler.compare_func == '?'
# Change values
sampler.anisotropy = 2.0
sampler.repeat_x = False
sampler.repeat_y = False
sampler.filter = (moderngl.NEAREST_MIPMAP_NEAREST, moderngl.NEAREST)
sampler.compare_func = "<="
assert sampler.anisotropy == 2.0
assert sampler.repeat_x is False
assert sampler.repeat_y is False
assert sampler.filter == (moderngl.NEAREST_MIPMAP_NEAREST, moderngl.NEAREST)
assert sampler.compare_func == "<="
def test_0(self):
texture = self.ctx.texture_cube((16, 16), 4)
assert texture.size == (16, 16)
assert texture.components == 4
assert texture.filter == (moderngl.LINEAR, moderngl.LINEAR)
assert texture.anisotropy == 1.0
assert texture.swizzle == "RGBA"
assert texture.glo > 0
def test_1(self):
fbo = self.ctx.framebuffer(self.ctx.renderbuffer((16, 16)))
pixels = struct.pack('16B', 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 0, 0, 0, 255)
texture = self.ctx.texture((2, 2), 4, pixels)
fbo.use()
texture.use()
texture.filter = (moderngl.LINEAR, moderngl.LINEAR)
self.vao.render(moderngl.TRIANGLE_STRIP)
r, g, b = struct.unpack('3B', fbo.read((8, 8, 1, 1)))
self.assertTrue(abs(r - 49) < 8 and abs(g - 63) < 8 and abs(b - 63) < 8)
texture.filter = (moderngl.NEAREST, moderngl.NEAREST)
self.vao.render(moderngl.TRIANGLE_STRIP)
r, g, b = struct.unpack('3B', fbo.read((6, 6, 1, 1)))
self.assertTrue(abs(r - 255) < 8 and abs(g - 0) < 8 and abs(b - 0) < 8)
r, g, b = struct.unpack('3B', fbo.read((9, 6, 1, 1)))
self.assertTrue(abs(r - 0) < 8 and abs(g - 255) < 8 and abs(b - 0) < 8)
r, g, b = struct.unpack('3B', fbo.read((6, 9, 1, 1)))
self.assertTrue(abs(r - 0) < 8 and abs(g - 0) < 8 and abs(b - 255) < 8)
def assertProperties(self, texture):
"""Poke all the common properties"""
texture.use()
# FIXME: size for texture array bug in ModernGL
if not isinstance(texture, TextureArray):
assert texture.size == (256, 256)
assert texture.width == 256
assert texture.height == 256
assert texture.components > 0
texture.filter = moderngl.LINEAR, moderngl.LINEAR
assert isinstance(texture.dtype, str)
if not texture.depth:
assert texture.swizzle == 'RGBA'
assert isinstance(texture.ctx, moderngl.Context)
assert isinstance(texture.glo, int)
texture.repeat_x = False
texture.repeat_y = True
assert texture.repeat_x is False
assert texture.repeat_y is True
def load_samplers(self):
"""Load samplers referenced in gltf metadata"""
for sampler in self.gltf.samplers:
# Use a sane default sampler if the sampler data is empty
# Samplers can simply just be json data: "{}"
if sampler.minFilter is sampler.magFilter is None:
self.samplers.append(
self.ctx.sampler(
filter=(moderngl.LINEAR_MIPMAP_LINEAR, moderngl.LINEAR),
repeat_x=False,
repeat_y=False,
anisotropy=16.0,
)
)
else:
self.samplers.append(
self.ctx.sampler(
filter=(sampler.minFilter, sampler.magFilter),
repeat_x=sampler.wrapS in [REPEAT, MIRRORED_REPEAT],
repeat_y=sampler.wrapT in [REPEAT, MIRRORED_REPEAT],
anisotropy=16.0,
)
"uniform sampler2D texture0;",
"uniform float near;"
"uniform float far;"
"void main() {",
" float z = texture(texture0, uv).r;"
" float d = (2.0 * near) / (far + near - z * (far - near));"
" out_color = vec4(d);",
"}",
"#endif",
]
program = ShaderProgram(name="depth_shader")
program.set_source("\n".join(src))
program.prepare()
DepthTexture.sampler = context.ctx().sampler(
filter=(moderngl.LINEAR, moderngl.LINEAR),
compare_func='',
)
DepthTexture.shader = program
# Texture for storing depth values
self.offscreen_depth = self.ctx.depth_texture(self.wnd.buffer_size)
# Create a framebuffer we can render to
self.offscreen = self.ctx.framebuffer(
color_attachments=[
self.offscreen_diffuse,
self.offscreen_normals,
self.offscreen_viewpos,
],
depth_attachment=self.offscreen_depth,
)
# This is just for temp changing depth texture parameters
# temporary so we can use it as a normal texture
self.depth_sampler = self.ctx.sampler(
filter=(moderngl.LINEAR, moderngl.LINEAR),
compare_func='',
)
# A fullscreen quad just for rendering offscreen textures to the window
self.quad_fs = geometry.quad_fs()
# --- Shaders
# Simple program just rendering texture
self.texture_program = self.load_program('programs/fragment_picking/texture.glsl')
self.texture_program['texture0'].value = 0
# Geomtry shader writing to two offscreen layers (color, normal) + depth
self.geometry_program = self.load_program('programs/fragment_picking/geometry.glsl')
self.geometry_program['texture0'].value = 0 # use texture channel 0
# Shader for linearizing depth (debug visualization)
self.linearize_depth_program = self.load_program('programs/fragment_picking/linearize_depth.glsl')
"",
"#elif defined FRAGMENT_SHADER",
"out vec4 out_color;",
"in vec2 uv;",
"uniform sampler2D texture0;",
"void main() {",
" out_color = texture(texture0, uv);",
"}",
"#endif",
]
program = ShaderProgram(name="fbo_shader")
program.set_source("\n".join(src))
program.prepare()
Texture2D.sampler = context.ctx().sampler(
filter=(moderngl.LINEAR, moderngl.LINEAR),
)
Texture2D.shader = program
""",
fragment_shader="""
#version 330
out vec4 out_color;
in vec2 uv;
uniform sampler2D texture0;
void main() {
out_color = texture(texture0, uv);
}
"""
)
TextureHelper._texture2d_sampler = self.ctx.sampler(
filter=(moderngl.LINEAR, moderngl.LINEAR),
)