|
| 1 | +# Copyright 2018 The dm_control Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================ |
| 15 | +# Lint as: python3 |
| 16 | +"""OpenGL utility for rendering numpy arrays as images on a quad surface.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import ctypes |
| 23 | + |
| 24 | +import numpy as np |
| 25 | +from OpenGL import GL |
| 26 | +from OpenGL.GL import shaders |
| 27 | + |
| 28 | +# This array contains packed position and texture cooridnates of a fullscreen |
| 29 | +# quad. |
| 30 | +# It contains definition of 4 vertices that will be rendered as a triangle |
| 31 | +# strip. Each vertex is described by a tuple: |
| 32 | +# (VertexPosition.X, VertexPosition.Y, TextureCoord.U, TextureCoord.V) |
| 33 | +_FULLSCREEN_QUAD_VERTEX_POSITONS_AND_TEXTURE_COORDS = np.array([ |
| 34 | + -1, -1, 0, 1, |
| 35 | + -1, 1, 0, 0, |
| 36 | + 1, -1, 1, 1, |
| 37 | + 1, 1, 1, 0], dtype=np.float32) |
| 38 | +_FLOATS_PER_XY = 2 |
| 39 | +_FLOATS_PER_VERTEX = 4 |
| 40 | +_SIZE_OF_FLOAT = ctypes.sizeof(ctypes.c_float) |
| 41 | + |
| 42 | +_VERTEX_SHADER = """ |
| 43 | +#version 120 |
| 44 | +attribute vec2 position; |
| 45 | +attribute vec2 uv; |
| 46 | +void main() { |
| 47 | + gl_Position = vec4(position, 0, 1); |
| 48 | + gl_TexCoord[0].st = uv; |
| 49 | +} |
| 50 | +""" |
| 51 | +_FRAGMENT_SHADER = """ |
| 52 | +#version 120 |
| 53 | +uniform sampler2D tex; |
| 54 | +void main() { |
| 55 | + gl_FragColor = texture2D(tex, gl_TexCoord[0].st); |
| 56 | +} |
| 57 | +""" |
| 58 | +_VAR_POSITION = 'position' |
| 59 | +_VAR_UV = 'uv' |
| 60 | +_VAR_TEXTURE_SAMPLER = 'tex' |
| 61 | + |
| 62 | + |
| 63 | +class FullscreenQuadRenderer(object): |
| 64 | + """Renders pixmaps on a fullscreen quad using OpenGL.""" |
| 65 | + |
| 66 | + def __init__(self): |
| 67 | + """Initializes the fullscreen quad renderer.""" |
| 68 | + GL.glClearColor(0, 0, 0, 0) |
| 69 | + self._init_geometry() |
| 70 | + self._init_texture() |
| 71 | + self._init_shaders() |
| 72 | + |
| 73 | + def _init_geometry(self): |
| 74 | + """Initializes the fullscreen quad geometry.""" |
| 75 | + vertex_buffer = GL.glGenBuffers(1) |
| 76 | + GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer) |
| 77 | + GL.glBufferData( |
| 78 | + GL.GL_ARRAY_BUFFER, |
| 79 | + _FULLSCREEN_QUAD_VERTEX_POSITONS_AND_TEXTURE_COORDS.nbytes, |
| 80 | + _FULLSCREEN_QUAD_VERTEX_POSITONS_AND_TEXTURE_COORDS, GL.GL_STATIC_DRAW) |
| 81 | + |
| 82 | + def _init_texture(self): |
| 83 | + """Initializes the texture storage.""" |
| 84 | + self._texture = GL.glGenTextures(1) |
| 85 | + GL.glBindTexture(GL.GL_TEXTURE_2D, self._texture) |
| 86 | + GL.glTexParameteri( |
| 87 | + GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST) |
| 88 | + GL.glTexParameteri( |
| 89 | + GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST) |
| 90 | + |
| 91 | + def _init_shaders(self): |
| 92 | + """Initializes the shaders used to render the textures fullscreen quad.""" |
| 93 | + vs = shaders.compileShader(_VERTEX_SHADER, GL.GL_VERTEX_SHADER) |
| 94 | + fs = shaders.compileShader(_FRAGMENT_SHADER, GL.GL_FRAGMENT_SHADER) |
| 95 | + self._shader = shaders.compileProgram(vs, fs) |
| 96 | + |
| 97 | + stride = _FLOATS_PER_VERTEX * _SIZE_OF_FLOAT |
| 98 | + var_position = GL.glGetAttribLocation(self._shader, _VAR_POSITION) |
| 99 | + GL.glVertexAttribPointer( |
| 100 | + var_position, 2, GL.GL_FLOAT, GL.GL_FALSE, stride, None) |
| 101 | + GL.glEnableVertexAttribArray(var_position) |
| 102 | + |
| 103 | + var_uv = GL.glGetAttribLocation(self._shader, _VAR_UV) |
| 104 | + uv_offset = ctypes.c_void_p(_FLOATS_PER_XY * _SIZE_OF_FLOAT) |
| 105 | + GL.glVertexAttribPointer( |
| 106 | + var_uv, 2, GL.GL_FLOAT, GL.GL_FALSE, stride, uv_offset) |
| 107 | + GL.glEnableVertexAttribArray(var_uv) |
| 108 | + |
| 109 | + self._var_texture_sampler = GL.glGetUniformLocation( |
| 110 | + self._shader, _VAR_TEXTURE_SAMPLER) |
| 111 | + |
| 112 | + def render(self, pixmap, viewport_shape): |
| 113 | + """Renders the pixmap on a fullscreen quad. |
| 114 | +
|
| 115 | + Args: |
| 116 | + pixmap: A 3D numpy array of bytes (np.uint8), with dimensions |
| 117 | + (width, height, 3). |
| 118 | + viewport_shape: A tuple of two elements, (width, height). |
| 119 | + """ |
| 120 | + GL.glClear(GL.GL_COLOR_BUFFER_BIT) |
| 121 | + GL.glViewport(0, 0, *viewport_shape) |
| 122 | + GL.glUseProgram(self._shader) |
| 123 | + GL.glActiveTexture(GL.GL_TEXTURE0) |
| 124 | + GL.glBindTexture(GL.GL_TEXTURE_2D, self._texture) |
| 125 | + GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1) |
| 126 | + GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, pixmap.shape[1], |
| 127 | + pixmap.shape[0], 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, |
| 128 | + pixmap) |
| 129 | + GL.glUniform1i(self._var_texture_sampler, 0) |
| 130 | + GL.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4) |
0 commit comments