def video_load_frame(self, frame): """ Load frame (numpy array) in texture """ # return self.video_texture.update_ndarray(frame, 0) tex = self.video_texture opengles.glActiveTexture(GL_TEXTURE0) opengles.glBindTexture(GL_TEXTURE_2D, tex._tex) opengles.glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, tex.ix, tex.iy, self.frame_format, GL_UNSIGNED_BYTE, frame.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte))) opengles.glGenerateMipmap(GL_TEXTURE_2D)
def update_ndarray(self, new_array=None, texture_num=None): """to allow numpy arrays to be patched in to textures without regenerating new glTextureBuffers i.e. for movie textures *new_array* ndarray, if supplied this will be the pixel data for the new Texture2D *texture_num* int, if supplied this will make the update effective for a specific sampler number i.e. as held in the Buffer.textures array. This will be required where multiple textures are used on some of the Buffers being drawn in the scene""" if new_array is not None: self.image = new_array if texture_num is not None: opengles.glActiveTexture(GL_TEXTURE0 + texture_num) opengles.glBindTexture(GL_TEXTURE_2D, self._tex) # set filters according to mipmap and filter request for t in [GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER]: opengles.glTexParameteri(GL_TEXTURE_2D, t, self._get_filter(t)) opengles.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.m_repeat) opengles.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.m_repeat) iformat = self._get_format_from_array(self.image, self.i_format) opengles.glTexImage2D( GL_TEXTURE_2D, 0, iformat, self.ix, self.iy, 0, iformat, GL_UNSIGNED_BYTE, self.image.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte))) if opengles.glGetError() == GL_OUT_OF_MEMORY: LOGGER.critical('Out of GPU memory in Texture.update_ndarray') #opengles.glEnable(GL_TEXTURE_2D) # invalid in OpenGLES 2 if self.mipmap: opengles.glGenerateMipmap(GL_TEXTURE_2D) if self.free_after_load: self.image = None self.file_string = None self._loaded = False
def draw(self, shape=None, M=None, unif=None, shader=None, textures=None, ntl=None, shny=None, fullset=True): """Draw this Buffer, called by the parent Shape.draw() Keyword arguments: *shape* Shape object this Buffer belongs to, has to be passed at draw to avoid circular reference *shader* Shader object *textures* array of Texture objects *ntl* multiple for tiling normal map which can be less than or greater than 1.0. 0.0 disables the normal mapping, float *shiny* how strong to make the reflection 0.0 to 1.0, float """ self.load_opengl() shader = shader or self.shader or shape.shader or Shader.instance() shader.use() opengles.glUniformMatrix4fv(shader.unif_modelviewmatrix, GLsizei(3), GLboolean(0), M.ctypes.data) opengles.glUniform3fv(shader.unif_unif, GLsizei(20), unif) textures = textures or self.textures if ntl is not None: self.unib[0] = ntl if shny is not None: self.unib[1] = shny self._select() opengles.glVertexAttribPointer(shader.attr_vertex, GLint(3), GL_FLOAT, GLboolean(0), self.N_BYTES, 0) opengles.glEnableVertexAttribArray(shader.attr_vertex) if self.N_BYTES > 12: opengles.glVertexAttribPointer(shader.attr_normal, GLint(3), GL_FLOAT, GLboolean(0), self.N_BYTES, 12) opengles.glEnableVertexAttribArray(shader.attr_normal) if self.N_BYTES > 24: opengles.glVertexAttribPointer(shader.attr_texcoord, GLint(2), GL_FLOAT, GLboolean(0), self.N_BYTES, 24) opengles.glEnableVertexAttribArray(shader.attr_texcoord) opengles.glDisable(GL_BLEND) self.unib[2] = 0.6 for t, texture in enumerate(textures): if (self.disp.last_textures[t] != texture or self.disp.last_shader != shader or self.disp.offscreen_tex): # very slight speed increase for sprites opengles.glActiveTexture(GL_TEXTURE0 + t) assert texture.tex(), 'There was an empty texture in your Buffer.' opengles.glBindTexture(GL_TEXTURE_2D, texture.tex()) opengles.glUniform1i(shader.unif_tex[t], GLint(t)) self.disp.last_textures[t] = texture if texture.blend: # i.e. if any of the textures set to blend then all will for this shader. self.unib[2] = 0.05 if self.unib[2] != 0.6 or shape.unif[13] < 1.0 or shape.unif[14] < 1.0: #use unib[2] as flag to indicate if any Textures to be blended #needs to be done outside for..textures so materials can be transparent opengles.glEnable(GL_BLEND) self.unib[2] = 0.05 self.disp.last_shader = shader opengles.glUniform3fv(shader.unif_unib, GLsizei(5), self.unib) opengles.glEnable(GL_DEPTH_TEST) # TODO find somewhere more efficient to do this opengles.glDrawElements(self.draw_method, GLsizei(self.ntris * 3), GL_UNSIGNED_SHORT, 0)
def draw(self, shape=None, M=None, unif=None, shader=None, textures=None, ntl=None, shny=None, fullset=True): """Draw this Buffer, called by the parent Shape.draw() Keyword arguments: *shape* Shape object this Buffer belongs to, has to be passed at draw to avoid circular reference *shader* Shader object *textures* array of Texture objects *ntl* multiple for tiling normal map which can be less than or greater than 1.0. 0.0 disables the normal mapping, float *shiny* how strong to make the reflection 0.0 to 1.0, float """ self.load_opengl() shader = shader or self.shader or shape.shader or Shader.instance() shader.use() opengles.glUniformMatrix4fv(shader.unif_modelviewmatrix, 3, ctypes.c_ubyte(0), M.ctypes.data) opengles.glUniform3fv(shader.unif_unif, 20, unif) textures = textures or self.textures if ntl is not None: self.unib[0] = ntl if shny is not None: self.unib[1] = shny self._select() opengles.glVertexAttribPointer(shader.attr_vertex, 3, GL_FLOAT, 0, self.N_BYTES, 0) opengles.glEnableVertexAttribArray(shader.attr_vertex) if self.N_BYTES > 12: opengles.glVertexAttribPointer(shader.attr_normal, 3, GL_FLOAT, 0, self.N_BYTES, 12) opengles.glEnableVertexAttribArray(shader.attr_normal) if self.N_BYTES > 24: opengles.glVertexAttribPointer(shader.attr_texcoord, 2, GL_FLOAT, 0, self.N_BYTES, 24) opengles.glEnableVertexAttribArray(shader.attr_texcoord) opengles.glDisable(GL_BLEND) self.unib[2] = 0.6 for t, texture in enumerate(textures): if (self.disp.last_textures[t] != texture or self.disp.last_shader != shader or self.disp.offscreen_tex): # very slight speed increase for sprites opengles.glActiveTexture(GL_TEXTURE0 + t) assert texture.tex(), 'There was an empty texture in your Buffer.' opengles.glBindTexture(GL_TEXTURE_2D, texture.tex()) opengles.glUniform1i(shader.unif_tex[t], t) self.disp.last_textures[t] = texture if texture.blend: # i.e. if any of the textures set to blend then all will for this shader. self.unib[2] = 0.05 if self.unib[2] != 0.6 or shape.unif[13] < 1.0 or shape.unif[14] < 1.0: #use unib[2] as flag to indicate if any Textures to be blended #needs to be done outside for..textures so materials can be transparent opengles.glEnable(GL_BLEND) self.unib[2] = 0.05 self.disp.last_shader = shader opengles.glUniform3fv(shader.unif_unib, 5, self.unib) opengles.glEnable(GL_DEPTH_TEST) # TODO find somewhere more efficient to do this opengles.glDrawElements(self.draw_method, self.ntris * 3, GL_UNSIGNED_SHORT, 0)