Given an UV image, texturing from the texture map
(
ctx: RastContext,
uv: np.ndarray,
texture: np.ndarray,
interpolation: str= 'linear',
wrap: str = 'clamp'
)
| 304 | |
| 305 | |
| 306 | def texture( |
| 307 | ctx: RastContext, |
| 308 | uv: np.ndarray, |
| 309 | texture: np.ndarray, |
| 310 | interpolation: str= 'linear', |
| 311 | wrap: str = 'clamp' |
| 312 | ) -> np.ndarray: |
| 313 | """ |
| 314 | Given an UV image, texturing from the texture map |
| 315 | """ |
| 316 | assert len(texture.shape) == 3 and 1 <= texture.shape[2] <= 4 |
| 317 | assert uv.shape[2] == 2 |
| 318 | height, width = uv.shape[:2] |
| 319 | texture_dtype = map_np_dtype(texture.dtype) |
| 320 | |
| 321 | # Create VAO |
| 322 | screen_quad_vbo = ctx.mgl_ctx.buffer(np.array([[-1, -1], [1, -1], [1, 1], [-1, 1]], dtype='f4')) |
| 323 | screen_quad_ibo = ctx.mgl_ctx.buffer(np.array([0, 1, 2, 0, 2, 3], dtype=np.int32)) |
| 324 | screen_quad_vao = ctx.mgl_ctx.vertex_array(ctx.program_texture(texture.shape[2]), [(screen_quad_vbo, '2f4', 'in_vert')], index_buffer=screen_quad_ibo, index_element_size=4) |
| 325 | |
| 326 | # Create texture, set filter and bind. TODO: min mag filter, mipmap |
| 327 | texture_tex = ctx.mgl_ctx.texture((texture.shape[1], texture.shape[0]), texture.shape[2], dtype=texture_dtype, data=np.ascontiguousarray(texture)) |
| 328 | if interpolation == 'linear': |
| 329 | texture_tex.filter = (moderngl.LINEAR, moderngl.LINEAR) |
| 330 | elif interpolation == 'nearest': |
| 331 | texture_tex.filter = (moderngl.NEAREST, moderngl.NEAREST) |
| 332 | texture_tex.use(location=0) |
| 333 | texture_uv = ctx.mgl_ctx.texture((width, height), 2, dtype='f4', data=np.ascontiguousarray(uv.astype('f4', copy=False))) |
| 334 | texture_uv.filter = (moderngl.NEAREST, moderngl.NEAREST) |
| 335 | texture_uv.use(location=1) |
| 336 | |
| 337 | # Create render buffer and frame buffer |
| 338 | rb = ctx.mgl_ctx.renderbuffer((uv.shape[1], uv.shape[0]), texture.shape[2], dtype=texture_dtype) |
| 339 | fbo = ctx.mgl_ctx.framebuffer(color_attachments=[rb]) |
| 340 | |
| 341 | # Render |
| 342 | fbo.use() |
| 343 | fbo.viewport = (0, 0, width, height) |
| 344 | ctx.mgl_ctx.disable(ctx.mgl_ctx.BLEND) |
| 345 | screen_quad_vao.render() |
| 346 | |
| 347 | # Read buffer |
| 348 | image_buffer = np.frombuffer(fbo.read(components=texture.shape[2], attachment=0, dtype=texture_dtype), dtype=texture_dtype).reshape((height, width, texture.shape[2])) |
| 349 | |
| 350 | # Release |
| 351 | texture_tex.release() |
| 352 | rb.release() |
| 353 | fbo.release() |
| 354 | |
| 355 | return image_buffer |
| 356 | |
| 357 | |
| 358 | def warp_image_by_depth( |
nothing calls this directly
no test coverage detected