Beispiel #1
0
 def __init__(self,
              camera=None,
              light=None,
              shader=None,
              texture=None,
              radius=0.0,
              x=0.0,
              y=0.0,
              z=1000,
              vx=0.0,
              vy=0.0,
              decay=0.001):
     super(ScissorBall, self).__init__(camera=camera,
                                       light=light,
                                       shader=shader,
                                       texture=texture,
                                       radius=radius,
                                       x=x,
                                       y=y,
                                       z=z,
                                       vx=vx,
                                       vy=vy,
                                       decay=decay)
     self.w = int(2.0 * radius)
     self.h = int(2.0 * radius)
     self.or_x = Display.INSTANCE.width / 2.0  # coord origin
     self.or_y = Display.INSTANCE.height / 2.0
     opengles.glEnable(GL_SCISSOR_TEST)
Beispiel #2
0
 def start_capture(self, clear=True):
     """ after calling this method all object.draw()s will rendered
 to this texture and not appear on the display. Large objects
 will obviously take a while to draw and re-draw
 """
     super(PostProcess, self)._start(clear=clear)
     from pi3d.Display import Display
     xx = int(Display.INSTANCE.width / 2.0 * (1.0 - self.scale))
     yy = int(Display.INSTANCE.height / 2.0 * (1.0 - self.scale))
     ww = int(Display.INSTANCE.width * self.scale)
     hh = int(Display.INSTANCE.height * self.scale)
     opengles.glEnable(GL_SCISSOR_TEST)
     opengles.glScissor(GLint(xx), GLint(yy), GLsizei(ww), GLsizei(hh))
Beispiel #3
0
    def draw(self, shape):
        """ draw the shape using the clashtest Shader

    Arguments:
      *shape*
        Shape object that will be drawn
    """
        if not self.s_flg:
            opengles.glEnable(GL_SCISSOR_TEST)
            opengles.glScissor(GLint(0), GLint(self.y0), GLsizei(self.ix),
                               GLsizei(1))
            self.s_flg = True
        shape.draw(shader=self.shader)
Beispiel #4
0
    def create_display(self,
                       x=0,
                       y=0,
                       w=0,
                       h=0,
                       depth=24,
                       samples=4,
                       layer=0,
                       display_config=DISPLAY_CONFIG_DEFAULT,
                       window_title='',
                       use_glx=False):
        self.use_glx = use_glx and (
            X_WINDOW and hasattr(glx, 'glXChooseFBConfig')
        )  # only use glx if x11 window and glx available
        self.display_config = display_config
        self.window_title = window_title.encode()
        if not self.use_glx:
            self.display = openegl.eglGetDisplay(EGL_DEFAULT_DISPLAY)
            assert self.display != EGL_NO_DISPLAY and self.display is not None
            for smpl in [
                    samples, 0
            ]:  # try with samples first but ANGLE dll can't cope so drop to 0 for windows
                r = openegl.eglInitialize(self.display, None, None)
                attribute_list = (EGLint * 19)(
                    EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
                    EGL_DEPTH_SIZE, depth, EGL_ALPHA_SIZE, 8, EGL_BUFFER_SIZE,
                    32, EGL_SAMPLES, smpl, EGL_STENCIL_SIZE, 8,
                    EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE)
                numconfig = EGLint(0)
                poss_configs = (EGLConfig * 5)(*(EGLConfig()
                                                 for _ in range(5)))

                r = openegl.eglChooseConfig(self.display, attribute_list,
                                            poss_configs,
                                            EGLint(len(poss_configs)),
                                            byref(numconfig))

                context_attribs = (EGLint * 3)(EGL_CONTEXT_CLIENT_VERSION, 2,
                                               EGL_NONE)
                if numconfig.value > 0:
                    self.config = poss_configs[0]
                    self.context = openegl.eglCreateContext(
                        self.display, self.config, EGL_NO_CONTEXT,
                        context_attribs)
                    if self.context != EGL_NO_CONTEXT:
                        break
            assert self.context != EGL_NO_CONTEXT and self.context is not None

        self.create_surface(x, y, w, h, layer)
        opengles.glDepthRangef(GLfloat(0.0), GLfloat(1.0))
        opengles.glClearColor(GLfloat(0.3), GLfloat(0.3), GLfloat(0.7),
                              GLfloat(1.0))
        opengles.glBindFramebuffer(GL_FRAMEBUFFER, GLuint(0))

        # get GL v GLES and version num for shader translation
        version = opengles.glGetString(GL_VERSION)
        version = ctypes.cast(version, c_char_p).value
        if b"ES" in version:
            for s in version.split():
                if b'.' in s:
                    self.gl_id = b"GLES" + s.split(b'.')[0]
                    break

        #Setup default hints
        opengles.glEnable(GL_CULL_FACE)
        opengles.glCullFace(GL_BACK)
        opengles.glFrontFace(GL_CW)
        opengles.glEnable(GL_DEPTH_TEST)
        if b"GLES" not in self.gl_id:
            if b"2" in self.gl_id:
                opengles.glEnable(GL_VERTEX_PROGRAM_POINT_SIZE)
            else:
                opengles.glEnable(GL_PROGRAM_POINT_SIZE)  # only in > GL3
            opengles.glEnable(GL_POINT_SPRITE)
        opengles.glDepthFunc(GL_LESS)
        opengles.glDepthMask(GLboolean(True))
        opengles.glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST)
        opengles.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1,
                                     GL_ONE_MINUS_SRC_ALPHA)
        opengles.glColorMask(GLboolean(True), GLboolean(True), GLboolean(True),
                             GLboolean(False))
        #opengles.glEnableClientState(GL_VERTEX_ARRAY)
        #opengles.glEnableClientState(GL_NORMAL_ARRAY)
        self.max_texture_size = GLint(0)
        opengles.glGetIntegerv(GL_MAX_TEXTURE_SIZE,
                               byref(self.max_texture_size))

        self.active = True
Beispiel #5
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, 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)
Beispiel #6
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)