Ejemplo n.º 1
0
 def draw_text(self, text, color=(1.0, 1.0, 1.0, 0.0),
               view_matrix=None, projection_matrix=None):
     gl.glUseProgram(self._program_id)
     gl.glActiveTexture(gl.GL_TEXTURE0+0)
     gl.glBindTexture(gl.GL_TEXTURE_2D, self._texture_id)
     gl.glBindSampler(0, self._sampler_id)
     gl.glUniform1i(self._uniform_locations['u_fonttex'], 0)
     gl.glUniform4f(self._uniform_locations['u_color'], *color)
     if view_matrix is not None:
         self._matrix.dot(view_matrix, out=self._modelview_matrix)
         gl.glUniformMatrix4fv(self._uniform_locations['u_modelviewMatrix'], 1, False, self._modelview_matrix)
     if projection_matrix is not None:
         gl.glUniformMatrix4fv(self._uniform_locations['u_projectionMatrix'], 1, False, projection_matrix)
     gl.glEnableVertexAttribArray(0)
     gl.glEnableVertexAttribArray(1)
     gl.glEnable(gl.GL_BLEND)
     gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
     x = 0.0
     text = [ord(c) - 32 for c in text]
     for i in text:
         if i >= 0 and i < 95:
             gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._buffer_ids[i])
             gl.glVertexAttribPointer(0, 2, gl.GL_FLOAT, False, 0, c_void_p(0))
             gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT, False, 0, c_void_p(8*4))
             gl.glUniform1f(self._uniform_locations['advance'], x)
             gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4)
             x += self._advance[i]
     gl.glDisableVertexAttribArray(0)
     gl.glDisableVertexAttribArray(1)
     gl.glDisable(gl.GL_BLEND)
Ejemplo n.º 2
0
    def use(self, location=0):
        """
        Use this sampler in the specified texture unit

        :param location: The texture unit (int)
        """
        GL.glBindSampler(location, self._id)
Ejemplo n.º 3
0
def set_material_state(material_name, gltf):
    if set_material_state.current_material == material_name:
        return
    set_material_state.current_material = material_name
    set_material_state.n_tex = 0
    material = gltf['materials'][material_name]
    set_technique_state(material['technique'], gltf)
    technique = gltf['techniques'][material['technique']]
    program = gltf['programs'][technique['program']]
    textures = gltf.get('textures', {})
    samplers = gltf.get('samplers', {})
    material_values = material.get('values', {})
    for uniform_name, parameter_name in technique['uniforms'].items():
        parameter = technique['parameters'][parameter_name]
        if 'semantic' in parameter:
            continue
        value = material_values.get(parameter_name,
                                    parameter.get('value',
                                                  _DEFAULT_MATERIAL_VALUES_BY_PARAM_TYPE.get(parameter['type'])))
        if value is None:
            raise Exception('''could not determine a value to use for material "%s", parameter "%s":
            %s %s''' % (material_name, parameter_name,
                        parameter['type'], uniform_name))
        if isinstance(value, (tuple, list)):
            value = np.array(value, dtype=np.float32)
        if uniform_name in program['uniform_locations']:
            location = program['uniform_locations'][uniform_name]
        else:
            location = gl.glGetUniformLocation(program['id'], uniform_name)
            program['uniform_locations'][uniform_name] = location
        if parameter['type'] == gl.GL_SAMPLER_2D:
            texture = textures[value]
            gl.glActiveTexture(gl.GL_TEXTURE0 + set_material_state.n_tex)
            gl.glBindTexture(texture['target'], texture['id'])
            gl.glBindSampler(gl.GL_TEXTURE0 + set_material_state.n_tex,
                             samplers[texture['sampler']]['id'])
            gl.glUniform1i(location, set_material_state.n_tex)
            set_material_state.n_tex += 1
        elif parameter['type'] == gl.GL_INT:
            gl.glUniform1i(location, value)
        elif parameter['type'] == gl.GL_FLOAT:
            gl.glUniform1f(location, value)
        elif parameter['type'] == gl.GL_FLOAT_VEC2:
            gl.glUniform2f(location, *value)
        elif parameter['type'] == gl.GL_FLOAT_VEC3:
            gl.glUniform3f(location, *value)
        elif parameter['type'] == gl.GL_FLOAT_VEC4:
            gl.glUniform4f(location, *value)
        elif parameter['type'] == gl.GL_FLOAT_MAT2:
            gl.glUniformMatrix2fv(location, 1, False, value)
        elif parameter['type'] == gl.GL_FLOAT_MAT3:
            gl.glUniformMatrix3fv(location, 1, False, value)
        elif parameter['type'] == gl.GL_FLOAT_MAT4:
            gl.glUniformMatrix4fv(location, 1, False, value)
        else:
            raise Exception('unhandled parameter type: %s' % parameter['type'])
    if CHECK_GL_ERRORS:
        if gl.glGetError() != gl.GL_NO_ERROR:
            raise Exception('error setting material state')
Ejemplo n.º 4
0
def set_material_state(material_name, gltf):
    if set_material_state.current_material == material_name:
        return
    set_material_state.current_material = material_name
    material = gltf['materials'][material_name]
    set_technique_state(material['technique'], gltf)
    technique = gltf['techniques'][material['technique']]
    program = gltf['programs'][technique['program']]
    textures = gltf.get('textures', {})
    samplers = gltf.get('samplers', {})
    material_values = material.get('values', {})
    for uniform_name, parameter_name in technique['uniforms'].items():
        parameter = technique['parameters'][parameter_name]
        if 'semantic' in parameter:
            continue
        value = material_values.get(parameter_name, parameter.get('value'))
        if value:
            if uniform_name in program['uniform_locations']:
                location = program['uniform_locations'][uniform_name]
            else:
                location = gl.glGetUniformLocation(program['id'], uniform_name)
                program['uniform_locations'][uniform_name] = location
            if parameter['type'] == gl.GL_SAMPLER_2D:
                texture = textures[value]
                gl.glActiveTexture(gl.GL_TEXTURE0 + 0)
                gl.glBindTexture(texture['target'], texture['id'])
                gl.glBindSampler(0, samplers[texture['sampler']]['id'])
                gl.glUniform1i(location, 0)
            elif parameter['type'] == gl.GL_FLOAT:
                gl.glUniform1f(location, value)
            elif parameter['type'] == gl.GL_FLOAT_VEC2:
                gl.glUniform2f(location, *value)
            elif parameter['type'] == gl.GL_FLOAT_VEC3:
                gl.glUniform3f(location, *value)
            elif parameter['type'] == gl.GL_FLOAT_VEC4:
                gl.glUniform4f(location, *value)
            else:
                raise Exception('unhandled parameter type: %s' %
                                parameter['type'])
        else:
            raise Exception('no value provided for parameter "%s"' %
                            parameter_name)
    if CHECK_GL_ERRORS:
        if gl.glGetError() != gl.GL_NO_ERROR:
            raise Exception('error setting material state')
Ejemplo n.º 5
0
def set_material_state(material_name, gltf):
    if set_material_state.current_material == material_name:
        return
    set_material_state.current_material = material_name
    material = gltf['materials'][material_name]
    set_technique_state(material['technique'], gltf)
    technique = gltf['techniques'][material['technique']]
    program = gltf['programs'][technique['program']]
    textures = gltf.get('textures', {})
    samplers = gltf.get('samplers', {})
    material_values = material.get('values', {})
    for uniform_name, parameter_name in technique['uniforms'].items():
        parameter = technique['parameters'][parameter_name]
        if 'semantic' in parameter:
            continue
        value = material_values.get(parameter_name, parameter.get('value'))
        if value:
            if uniform_name in program['uniform_locations']:
                location = program['uniform_locations'][uniform_name]
            else:
                location = gl.glGetUniformLocation(program['id'], uniform_name)
                program['uniform_locations'][uniform_name] = location
            if parameter['type'] == gl.GL_SAMPLER_2D:
                texture = textures[value]
                gl.glActiveTexture(gl.GL_TEXTURE0+0)
                gl.glBindTexture(texture['target'], texture['id'])
                gl.glBindSampler(0, samplers[texture['sampler']]['id'])
                gl.glUniform1i(location, 0)
            elif parameter['type'] == gl.GL_FLOAT:
                gl.glUniform1f(location, value)
            elif parameter['type'] == gl.GL_FLOAT_VEC2:
                gl.glUniform2f(location, *value)
            elif parameter['type'] == gl.GL_FLOAT_VEC3:
                gl.glUniform3f(location, *value)
            elif parameter['type'] == gl.GL_FLOAT_VEC4:
                gl.glUniform4f(location, *value)
            else:
                raise Exception('unhandled parameter type: %s' % parameter['type'])
        else:
            raise Exception('no value provided for parameter "%s"' % parameter_name)
    if CHECK_GL_ERRORS:
        if gl.glGetError() != gl.GL_NO_ERROR:
            raise Exception('error setting material state')
Ejemplo n.º 6
0
 def draw_text(self,
               text,
               color=(1.0, 1.0, 1.0, 0.0),
               view_matrix=None,
               projection_matrix=None):
     gl.glUseProgram(self._program_id)
     gl.glActiveTexture(gl.GL_TEXTURE0 + 0)
     gl.glBindTexture(gl.GL_TEXTURE_2D, self._texture_id)
     gl.glBindSampler(0, self._sampler_id)
     gl.glUniform1i(self._uniform_locations['u_fonttex'], 0)
     gl.glUniform4f(self._uniform_locations['u_color'], *color)
     if view_matrix is not None:
         self._matrix.dot(view_matrix, out=self._modelview_matrix)
         gl.glUniformMatrix4fv(self._uniform_locations['u_modelviewMatrix'],
                               1, False, self._modelview_matrix)
     if projection_matrix is not None:
         gl.glUniformMatrix4fv(
             self._uniform_locations['u_projectionMatrix'], 1, False,
             projection_matrix)
     gl.glEnableVertexAttribArray(0)
     gl.glEnableVertexAttribArray(1)
     gl.glEnable(gl.GL_BLEND)
     gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
     x = 0.0
     text = [ord(c) - 32 for c in text]
     for i in text:
         if i >= 0 and i < 95:
             gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._buffer_ids[i])
             gl.glVertexAttribPointer(0, 2, gl.GL_FLOAT, False, 0,
                                      c_void_p(0))
             gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT, False, 0,
                                      c_void_p(8 * 4))
             gl.glUniform1f(self._uniform_locations['advance'], x)
             gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4)
             x += self._advance[i]
     gl.glDisableVertexAttribArray(0)
     gl.glDisableVertexAttribArray(1)
     gl.glDisable(gl.GL_BLEND)
Ejemplo n.º 7
0
 def use(self, **frame_data):
     # if Material._current is self:
     #     return
     if not self._initialized:
         self.init_gl()
     self.technique.use()
     if self._on_use:
         self._on_use(self, **frame_data)
     tex_unit = 0
     for uniform_name, location in self.technique.uniform_locations.items():
         uniform = self.technique.uniforms[uniform_name]
         uniform_type = uniform['type']
         if uniform_type in (gl.GL_SAMPLER_2D, gl.GL_SAMPLER_CUBE):
             texture = self.textures[uniform_name]
             gl.glActiveTexture(gl.GL_TEXTURE0 + tex_unit)
             gl.glBindTexture(uniform_type, texture.texture_id)
             gl.glBindSampler(tex_unit, texture.sampler_id)
             gl.glUniform1i(location, tex_unit)
             tex_unit += 1
             continue
         elif uniform_name in self.values:
             value = self.values[uniform_name]
         elif uniform_name in frame_data:
             value = frame_data[uniform_name]
         else:
             continue
         if 'array_size' in uniform:
             ARRAY_TYPE_TO_UNIFORM_FN[uniform_type](location,
                                                    uniform['array_size'],
                                                    value)
         else:
             TYPE_TO_UNIFORM_FN[uniform_type](location, value)
     if CHECK_GL_ERRORS:
         err = gl.glGetError()
         if err != gl.GL_NO_ERROR:
             raise Exception('error setting material state: %d' % err)
     Material._current = self
Ejemplo n.º 8
0
 def use(self,
         u_view=None,
         u_modelview=None,
         u_projection=None,
         u_modelview_inverse_transpose=None,
         u_modelview_inverse=None,
         u_model=None,
         frame_data=None):
     # if Material._current is self:
     #     return
     if not self._initialized:
         self.init_gl()
     self.technique.use()
     if self._on_use:
         self._on_use(self, frame_data)
     tex_unit = 0
     for uniform_name, location in self.technique.uniform_locations.items():
         uniform = self.technique.uniforms[uniform_name]
         uniform_type = uniform['type']
         if uniform_type == gl.GL_SAMPLER_2D:
             texture = self.textures[uniform_name]
             gl.glActiveTexture(gl.GL_TEXTURE0 + tex_unit)
             gl.glBindTexture(gl.GL_TEXTURE_2D, texture.texture_id)
             gl.glBindSampler(tex_unit, texture.sampler_id)
             gl.glUniform1i(location, tex_unit)
             tex_unit += 1
         elif uniform_type == gl.GL_SAMPLER_CUBE:
             texture = self.textures[uniform_name]
             gl.glActiveTexture(gl.GL_TEXTURE0 + tex_unit)
             gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, texture.texture_id)
             gl.glBindSampler(tex_unit, texture.sampler_id)
             gl.glUniform1i(location, tex_unit)
             tex_unit += 1
         elif uniform_name in self.values:
             value = self.values[uniform_name]
             if uniform_type == gl.GL_FLOAT:
                 gl.glUniform1f(location, value)
             elif uniform_type == gl.GL_FLOAT_VEC2:
                 gl.glUniform2f(location, *value)
             elif uniform_type == gl.GL_FLOAT_VEC3:
                 gl.glUniform3f(location, *value)
             elif uniform_type == gl.GL_FLOAT_VEC4:
                 gl.glUniform4f(location, *value)
             else:
                 raise Exception('unhandled uniform type: %d' %
                                 uniform_type)
         else:
             if u_modelview is not None and uniform_name == 'u_modelview':
                 gl.glUniformMatrix4fv(location, 1, False, u_modelview)
             elif u_modelview_inverse is not None and uniform_name == 'u_modelview_inverse':
                 gl.glUniformMatrix4fv(location, 1, False,
                                       u_modelview_inverse)
             elif u_model is not None and uniform_name == 'u_model':
                 gl.glUniformMatrix4fv(location, 1, False, u_model)
             elif u_view is not None and uniform_name == 'u_view':
                 gl.glUniformMatrix4fv(location, 1, False, u_view)
             elif u_projection is not None and uniform_name == 'u_projection':
                 gl.glUniformMatrix4fv(location, 1, False, u_projection)
             elif u_modelview_inverse_transpose is not None and uniform_name == 'u_modelview_inverse_transpose':
                 gl.glUniformMatrix3fv(location, 1, False,
                                       u_modelview_inverse_transpose)
         if CHECK_GL_ERRORS:
             err = gl.glGetError()
             if err != gl.GL_NO_ERROR:
                 raise Exception('error setting material state: %d' % err)
     Material._current = self
Ejemplo n.º 9
0
 def init_gl(self):
     if self._gl_initialized:
         return
     import OpenGL.GL as gl
     self._texture_unit = 4
     vs_id = gl.glCreateShader(gl.GL_VERTEX_SHADER)
     gl.glShaderSource(vs_id, self._VERT_SHADER_SRC)
     gl.glCompileShader(vs_id)
     if not gl.glGetShaderiv(vs_id, gl.GL_COMPILE_STATUS):
         raise Exception('failed to compile %s vertex shader:\n%s' %
                         (self.__class__.__name__,
                          gl.glGetShaderInfoLog(vs_id).decode()))
     fs_id = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
     gl.glShaderSource(fs_id, self._FRAG_SHADER_SRC)
     gl.glCompileShader(fs_id)
     if not gl.glGetShaderiv(fs_id, gl.GL_COMPILE_STATUS):
         raise Exception('failed to compile %s fragment shader:\n%s' %
                         (self.__class__.__name__,
                          gl.glGetShaderInfoLog(fs_id).decode()))
     self._program_id = gl.glCreateProgram()
     gl.glAttachShader(self._program_id, vs_id)
     gl.glAttachShader(self._program_id, fs_id)
     gl.glLinkProgram(self._program_id)
     gl.glDetachShader(self._program_id, vs_id)
     gl.glDetachShader(self._program_id, fs_id)
     if not gl.glGetProgramiv(self._program_id, gl.GL_LINK_STATUS):
         raise Exception('failed to link program for %s' %
                         self.__class__.__name__)
     self._attribute_locations = {
         attribute: gl.glGetAttribLocation(self._program_id, attribute)
         for attribute in self._ATTRIBUTES
     }
     self._uniform_locations = {
         uniform: gl.glGetUniformLocation(self._program_id, uniform)
         for uniform in self._UNIFORMS
     }
     width, height = self._width, self._height
     self._image_width, self._image_height = image_width, image_height = width * 16, height * 6
     bitmap_buffer = np.zeros((image_height, image_width), dtype=np.ubyte)
     self._char_to_texcoords = {}
     for j in range(6):
         for i in range(16):
             i_char = j * 16 + i
             char = chr(32 + i_char)
             self._char_to_texcoords[char] = (i / 16.0, j / 6.0)
             self._face.load_char(char,
                                  FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT)
             glyph = self._face.glyph
             bitmap = glyph.bitmap
             x = i * width + glyph.bitmap_left
             y = j * height + self._max_asc - glyph.bitmap_top
             bitmap_buffer[y:y + bitmap.rows,
                           x:x + bitmap.width].flat = bitmap.buffer
     self._texture_id = gl.glGenTextures(1)
     gl.glBindTexture(gl.GL_TEXTURE_2D, self._texture_id)
     self._sampler_id = gl.glGenSamplers(1)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_MIN_FILTER,
                            gl.GL_LINEAR)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_MAG_FILTER,
                            gl.GL_LINEAR)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_WRAP_S,
                            gl.GL_CLAMP_TO_EDGE)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_WRAP_T,
                            gl.GL_CLAMP_TO_EDGE)
     gl.glBindSampler(self._texture_unit, self._sampler_id)
     gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
     gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RED, image_width,
                     image_height, 0, gl.GL_RED, gl.GL_UNSIGNED_BYTE,
                     bitmap_buffer)
     gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
     gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
     if gl.glGetError() != gl.GL_NO_ERROR:
         raise Exception('failed to create font texture')
     self._gl_initialized = True
     _logger.debug('%s.init_gl: OK', self.__class__.__name__)
Ejemplo n.º 10
0
 def use(self,
         u_view=None,
         u_modelview=None,
         u_projection=None,
         u_modelview_inverse_transpose=None,
         u_modelview_inverse=None,
         u_model=None,
         frame_data=None):
     # if Material._current is self:
     #     return
     if not self._initialized:
         self.init_gl()
     self.technique.use()
     if self._on_use:
         self._on_use(self, frame_data)
     tex_unit = 0
     for uniform_name, location in self.technique.uniform_locations.items():
         uniform = self.technique.uniforms[uniform_name]
         uniform_type = uniform['type']
         if uniform_type == gl.GL_SAMPLER_2D:
             texture = self.textures[uniform_name]
             gl.glActiveTexture(gl.GL_TEXTURE0+tex_unit)
             gl.glBindTexture(gl.GL_TEXTURE_2D, texture.texture_id)
             gl.glBindSampler(tex_unit, texture.sampler_id)
             gl.glUniform1i(location, tex_unit)
             tex_unit += 1
         elif uniform_type == gl.GL_SAMPLER_CUBE:
             texture = self.textures[uniform_name]
             gl.glActiveTexture(gl.GL_TEXTURE0+tex_unit)
             gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, texture.texture_id)
             gl.glBindSampler(tex_unit, texture.sampler_id)
             gl.glUniform1i(location, tex_unit)
             tex_unit += 1
         elif uniform_name in self.values:
             value = self.values[uniform_name]
             if uniform_type == gl.GL_FLOAT:
                 gl.glUniform1f(location, value)
             elif uniform_type == gl.GL_FLOAT_VEC2:
                 gl.glUniform2f(location, *value)
             elif uniform_type == gl.GL_FLOAT_VEC3:
                 gl.glUniform3f(location, *value)
             elif uniform_type == gl.GL_FLOAT_VEC4:
                 gl.glUniform4f(location, *value)
             else:
                 raise Exception('unhandled uniform type: %d' % uniform_type)
         else:
             if u_modelview is not None and uniform_name == 'u_modelview':
                 gl.glUniformMatrix4fv(location, 1, False, u_modelview)
             elif u_modelview_inverse is not None and uniform_name == 'u_modelview_inverse':
                 gl.glUniformMatrix4fv(location, 1, False, u_modelview_inverse)
             elif u_model is not None and uniform_name == 'u_model':
                 gl.glUniformMatrix4fv(location, 1, False, u_model)
             elif u_view is not None and uniform_name == 'u_view':
                 gl.glUniformMatrix4fv(location, 1, False, u_view)
             elif u_projection is not None and uniform_name == 'u_projection':
                 gl.glUniformMatrix4fv(location, 1, False, u_projection)
             elif u_modelview_inverse_transpose is not None and uniform_name == 'u_modelview_inverse_transpose':
                 gl.glUniformMatrix3fv(location, 1, False, u_modelview_inverse_transpose)
         if CHECK_GL_ERRORS:
             err = gl.glGetError()
             if err != gl.GL_NO_ERROR:
                 raise Exception('error setting material state: %d' % err)
     Material._current = self
Ejemplo n.º 11
0
 def init_gl(self):
     if self._gl_initialized:
         return
     import OpenGL.GL as gl
     self._texture_unit = 4
     vs_id = gl.glCreateShader(gl.GL_VERTEX_SHADER)
     gl.glShaderSource(vs_id, self._VERT_SHADER_SRC)
     gl.glCompileShader(vs_id)
     if not gl.glGetShaderiv(vs_id, gl.GL_COMPILE_STATUS):
         raise Exception('failed to compile %s vertex shader:\n%s' %
                         (self.__class__.__name__, gl.glGetShaderInfoLog(vs_id).decode()))
     fs_id = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
     gl.glShaderSource(fs_id, self._FRAG_SHADER_SRC)
     gl.glCompileShader(fs_id)
     if not gl.glGetShaderiv(fs_id, gl.GL_COMPILE_STATUS):
         raise Exception('failed to compile %s fragment shader:\n%s' %
                         (self.__class__.__name__, gl.glGetShaderInfoLog(fs_id).decode()))
     self._program_id = gl.glCreateProgram()
     gl.glAttachShader(self._program_id, vs_id)
     gl.glAttachShader(self._program_id, fs_id)
     gl.glLinkProgram(self._program_id)
     gl.glDetachShader(self._program_id, vs_id)
     gl.glDetachShader(self._program_id, fs_id)
     if not gl.glGetProgramiv(self._program_id, gl.GL_LINK_STATUS):
         raise Exception('failed to link program for %s' % self.__class__.__name__)
     self._attribute_locations = {attribute: gl.glGetAttribLocation(self._program_id, attribute)
                                  for attribute in self._ATTRIBUTES}
     self._uniform_locations = {uniform: gl.glGetUniformLocation(self._program_id, uniform)
                                for uniform in self._UNIFORMS}
     width, height = self._width, self._height
     self._image_width, self._image_height = image_width, image_height = width * 16, height * 6
     bitmap_buffer = np.zeros((image_height, image_width), dtype=np.ubyte)
     self._char_to_texcoords = {}
     for j in range(6):
         for i in range(16):
             i_char = j * 16 + i
             char = chr(32 + i_char)
             self._char_to_texcoords[char] = (i / 16.0, j / 6.0)
             self._face.load_char(char, FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT)
             glyph = self._face.glyph
             bitmap = glyph.bitmap
             x = i*width + glyph.bitmap_left
             y = j*height + self._max_asc - glyph.bitmap_top
             bitmap_buffer[y:y+bitmap.rows,x:x+bitmap.width].flat = bitmap.buffer
     self._texture_id = gl.glGenTextures(1)
     gl.glBindTexture(gl.GL_TEXTURE_2D, self._texture_id)
     self._sampler_id = gl.glGenSamplers(1)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
     gl.glSamplerParameteri(self._sampler_id, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)
     gl.glBindSampler(self._texture_unit, self._sampler_id)
     gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
     gl.glTexImage2D(gl.GL_TEXTURE_2D, 0,
                     gl.GL_RED,
                     image_width, image_height, 0,
                     gl.GL_RED, gl.GL_UNSIGNED_BYTE,
                     bitmap_buffer)
     gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
     gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
     if gl.glGetError() != gl.GL_NO_ERROR:
         raise Exception('failed to create font texture')
     self._gl_initialized = True
     _logger.debug('%s.init_gl: OK', self.__class__.__name__)
Ejemplo n.º 12
0
 def release(self, location=0):
     GL.glBindSampler(location, 0)
Ejemplo n.º 13
0
def set_material_state(material_name, gltf):
    if set_material_state.current_material == material_name:
        return
    set_material_state.current_material = material_name
    set_material_state.n_tex = 0
    material = gltf['materials'][material_name]
    set_technique_state(material['technique'], gltf)
    technique = gltf['techniques'][material['technique']]
    program = gltf['programs'][technique['program']]
    textures = gltf.get('textures', {})
    samplers = gltf.get('samplers', {})
    material_values = material.get('values', {})
    for uniform_name, parameter_name in technique['uniforms'].items():
        parameter = technique['parameters'][parameter_name]
        if 'semantic' in parameter:
            continue
        value = material_values.get(
            parameter_name,
            parameter.get(
                'value',
                _DEFAULT_MATERIAL_VALUES_BY_PARAM_TYPE.get(parameter['type'])))
        if value is None:
            raise Exception(
                '''could not determine a value to use for material "%s", parameter "%s":
            %s %s''' % (material_name, parameter_name, parameter['type'],
                        uniform_name))
        if isinstance(value, (tuple, list)):
            value = np.array(value, dtype=np.float32)
        if uniform_name in program['uniform_locations']:
            location = program['uniform_locations'][uniform_name]
        else:
            location = gl.glGetUniformLocation(program['id'], uniform_name)
            program['uniform_locations'][uniform_name] = location
        if parameter['type'] == gl.GL_SAMPLER_2D:
            texture = textures[value]
            gl.glActiveTexture(gl.GL_TEXTURE0 + set_material_state.n_tex)
            gl.glBindTexture(texture['target'], texture['id'])
            gl.glBindSampler(gl.GL_TEXTURE0 + set_material_state.n_tex,
                             samplers[texture['sampler']]['id'])
            gl.glUniform1i(location, set_material_state.n_tex)
            set_material_state.n_tex += 1
        elif parameter['type'] == gl.GL_INT:
            gl.glUniform1i(location, value)
        elif parameter['type'] == gl.GL_FLOAT:
            gl.glUniform1f(location, value)
        elif parameter['type'] == gl.GL_FLOAT_VEC2:
            gl.glUniform2f(location, *value)
        elif parameter['type'] == gl.GL_FLOAT_VEC3:
            gl.glUniform3f(location, *value)
        elif parameter['type'] == gl.GL_FLOAT_VEC4:
            gl.glUniform4f(location, *value)
        elif parameter['type'] == gl.GL_FLOAT_MAT2:
            gl.glUniformMatrix2fv(location, 1, False, value)
        elif parameter['type'] == gl.GL_FLOAT_MAT3:
            gl.glUniformMatrix3fv(location, 1, False, value)
        elif parameter['type'] == gl.GL_FLOAT_MAT4:
            gl.glUniformMatrix4fv(location, 1, False, value)
        else:
            raise Exception('unhandled parameter type: %s' % parameter['type'])
    if CHECK_GL_ERRORS:
        if gl.glGetError() != gl.GL_NO_ERROR:
            raise Exception('error setting material state')