Exemplo n.º 1
0
  def __init__(self, shfile=None, vshader_source=None, fshader_source=None):
    """
    Arguments:
      *shfile*
        Pathname without vs or fs ending i.e. "shaders/uv_light"
      *vshader_source*
        String with the code for the vertex shader.
      *vshader_source*
        String with the code for the fragment shader.
    """
    assert Loadable.is_display_thread()

    # TODO: the rest of the constructor should be split into load_disk
    # and load_opengl so that we can delete that assert.

    self.program = opengles.glCreateProgram()
    self.shfile = shfile

    def make_shader(src, suffix, shader_type):
      src = src or self.loadShader(shfile + suffix)
      characters = ctypes.c_char_p(src)
      shader = opengles.glCreateShader(shader_type)
      opengles.glShaderSource(shader, 1, ctypes.byref(characters), 0)
      opengles.glCompileShader(shader)
      self.showshaderlog(shader)
      opengles.glAttachShader(self.program, shader)
      return shader, src

    self.vshader_source, self.vshader = make_shader(
      vshader_source, '.vs', GL_VERTEX_SHADER)

    self.fshader_source, self.fshader = make_shader(
      fshader_source, '.fs', GL_FRAGMENT_SHADER)

    opengles.glLinkProgram(self.program)
    self.showprogramlog(self.program)

    self.attr_vertex = opengles.glGetAttribLocation(self.program, "vertex")
    self.attr_normal = opengles.glGetAttribLocation(self.program, "normal")

    self.unif_modelviewmatrix = opengles.glGetUniformLocation(self.program, "modelviewmatrix")
    self.unif_cameraviewmatrix = opengles.glGetUniformLocation(self.program, "cameraviewmatrix")

    self.unif_unif = opengles.glGetUniformLocation(self.program, "unif")
    self.unif_unib = opengles.glGetUniformLocation(self.program, "unib")

    self.attr_texcoord = opengles.glGetAttribLocation(self.program, "texcoord")
    opengles.glEnableVertexAttribArray(self.attr_texcoord)
    self.unif_tex = []
    self.texture = []
    for t in range(3):
      self.unif_tex.append(opengles.glGetUniformLocation(self.program, "tex"+str(t)))
      """
      *NB*
        for *uv* shaders tex0=texture tex1=normal map tex2=reflection

        for *mat* shaders tex0=normal map tex1=reflection
      """
    self.use()
Exemplo n.º 2
0
    def __init__(self, shfile=None, vshader_source=None, fshader_source=None):
        """
    Arguments:
      *shfile*
        Pathname without vs or fs ending i.e. "shaders/uv_light"

      *vshader_source*
        String with the code for the vertex shader.

      *fshader_source*
        String with the code for the fragment shader.
    """
        try:
            assert Loadable.is_display_thread()
        except AssertionError as err:
            LOGGER.error('load_opengl must be called on main thread for %s',
                         self)
            raise err

        # TODO: the rest of the constructor should be split into load_disk
        # and load_opengl so that we can delete that assert.

        self.program = opengles.glCreateProgram()
        self.shfile = shfile

        def make_shader(src, suffix, shader_type):
            src = src or self._load_shader(shfile + suffix)
            if type(
                    src
            ) != bytes:  #if ..shader_source passed as a string to __init__
                src = src.encode()
            characters = ctypes.c_char_p(src)
            shader = opengles.glCreateShader(shader_type)
            src_len = ctypes.c_int(len(src))
            opengles.glShaderSource(shader, 1, characters,
                                    ctypes.byref(src_len))
            opengles.glCompileShader(shader)
            self.showshaderlog(shader, src)
            opengles.glAttachShader(self.program, shader)
            return shader, src

        self.vshader, self.vshader_source = make_shader(
            vshader_source, '.vs', GL_VERTEX_SHADER)

        self.fshader, self.fshader_source = make_shader(
            fshader_source, '.fs', GL_FRAGMENT_SHADER)

        opengles.glLinkProgram(self.program)
        self.showprogramlog(self.program)

        self.attr_vertex = opengles.glGetAttribLocation(
            self.program, b'vertex')
        self.attr_normal = opengles.glGetAttribLocation(
            self.program, b'normal')

        self.unif_modelviewmatrix = opengles.glGetUniformLocation(
            self.program, b'modelviewmatrix')

        self.unif_unif = opengles.glGetUniformLocation(self.program, b'unif')
        self.unif_unib = opengles.glGetUniformLocation(self.program, b'unib')

        self.attr_texcoord = opengles.glGetAttribLocation(
            self.program, b'texcoord')
        opengles.glEnableVertexAttribArray(self.attr_texcoord)
        self.unif_tex = []
        self.textures = []
        for i in range(8):
            s = 'tex{}'.format(i).encode()
            self.unif_tex.append(opengles.glGetUniformLocation(
                self.program, s))
            self.textures.append(None)
            """
      *NB*
        for *uv* shaders tex0=texture tex1=normal map tex2=reflection

        for *mat* shaders tex0=normal map tex1=reflection
      """
        self.use()
Exemplo n.º 3
0
    def __init__(self, shfile=None, vshader_source=None, fshader_source=None):
        """
    Arguments:
      *shfile*
        Pathname without vs or fs ending i.e. "shaders/uv_light"

      *vshader_source*
        String with the code for the vertex shader.

      *vshader_source*
        String with the code for the fragment shader.
    """
        assert Loadable.is_display_thread()

        # TODO: the rest of the constructor should be split into load_disk
        # and load_opengl so that we can delete that assert.

        self.program = opengles.glCreateProgram()
        self.shfile = shfile

        def make_shader(src, suffix, shader_type):
            src = src or self._load_shader(shfile + suffix)
            characters = ctypes.c_char_p(src.encode())
            shader = opengles.glCreateShader(shader_type)
            src_len = (ctypes.c_int * 1)(len(src))  # array of just one c_int
            opengles.glShaderSource(shader, 1, ctypes.byref(characters),
                                    ctypes.byref(src_len))
            opengles.glCompileShader(shader)
            self.showshaderlog(shader, src)
            opengles.glAttachShader(self.program, shader)
            return shader, src

        self.vshader, self.vshader_source = make_shader(
            vshader_source, '.vs', GL_VERTEX_SHADER)

        self.fshader, self.fshader_source = make_shader(
            fshader_source, '.fs', GL_FRAGMENT_SHADER)

        opengles.glLinkProgram(self.program)
        self.showprogramlog(self.program)

        self.attr_vertex = opengles.glGetAttribLocation(
            self.program, b'vertex')
        self.attr_normal = opengles.glGetAttribLocation(
            self.program, b'normal')

        self.unif_modelviewmatrix = opengles.glGetUniformLocation(
            self.program, b'modelviewmatrix')
        self.unif_cameraviewmatrix = opengles.glGetUniformLocation(
            self.program, b'cameraviewmatrix')

        self.unif_unif = opengles.glGetUniformLocation(self.program, b'unif')
        self.unif_unib = opengles.glGetUniformLocation(self.program, b'unib')

        self.attr_texcoord = opengles.glGetAttribLocation(
            self.program, b'texcoord')
        opengles.glEnableVertexAttribArray(self.attr_texcoord)
        self.unif_tex = []
        self.textures = []
        for s in [b'tex0', b'tex1', b'tex2']:
            self.unif_tex.append(opengles.glGetUniformLocation(
                self.program, s))
            self.textures.append(None)
            """
      *NB*
        for *uv* shaders tex0=texture tex1=normal map tex2=reflection

        for *mat* shaders tex0=normal map tex1=reflection
      """
        self.use()
Exemplo n.º 4
0
Arquivo: Shader.py Projeto: tipam/pi3d
  def __init__(self, shfile=None, vshader_source=None, fshader_source=None):
    """
    Arguments:
      *shfile*
        Pathname without vs or fs ending i.e. "shaders/uv_light"

      *vshader_source*
        String with the code for the vertex shader.

      *fshader_source*
        String with the code for the fragment shader.
    """
    try:
      assert Loadable.is_display_thread()
    except AssertionError as err:
      LOGGER.error('load_opengl must be called on main thread for %s', self)
      raise err

    # TODO: the rest of the constructor should be split into load_disk
    # and load_opengl so that we can delete that assert.

    self.program = opengles.glCreateProgram()
    self.shfile = shfile

    def make_shader(src, suffix, shader_type):
      src = src or self._load_shader(shfile + suffix)
      if type(src) != bytes: #if ..shader_source passed as a string to __init__
        src = src.encode()
      characters = ctypes.c_char_p(src)
      shader = opengles.glCreateShader(shader_type)
      src_len = ctypes.c_int(len(src))
      opengles.glShaderSource(shader, 1, characters, ctypes.byref(src_len))
      opengles.glCompileShader(shader)
      self.showshaderlog(shader, src)
      opengles.glAttachShader(self.program, shader)
      return shader, src

    self.vshader, self.vshader_source = make_shader(
      vshader_source, '.vs', GL_VERTEX_SHADER)

    self.fshader, self.fshader_source = make_shader(
      fshader_source, '.fs', GL_FRAGMENT_SHADER)

    opengles.glLinkProgram(self.program)
    self.showprogramlog(self.program)

    self.attr_vertex = opengles.glGetAttribLocation(self.program, b'vertex')
    self.attr_normal = opengles.glGetAttribLocation(self.program, b'normal')

    self.unif_modelviewmatrix = opengles.glGetUniformLocation(
      self.program, b'modelviewmatrix')

    self.unif_unif = opengles.glGetUniformLocation(self.program, b'unif')
    self.unif_unib = opengles.glGetUniformLocation(self.program, b'unib')

    self.attr_texcoord = opengles.glGetAttribLocation(self.program, b'texcoord')
    opengles.glEnableVertexAttribArray(self.attr_texcoord)
    self.unif_tex = []
    self.textures = []
    for i in range(8):
      s = 'tex{}'.format(i).encode() 
      self.unif_tex.append(opengles.glGetUniformLocation(self.program, s))
      self.textures.append(None)
      """
      *NB*
        for *uv* shaders tex0=texture tex1=normal map tex2=reflection

        for *mat* shaders tex0=normal map tex1=reflection
      """
    self.use()