Ejemplo n.º 1
0
 def __init__(self, **kwargs):
     self.this_op = None
     self.units_per_meter = 16.0
     self.gravity = 9.8
     self.fps = 60.0
     self.vertex_format = [
         (b'vPosition', 3, 'float'),
         (b'vSize', 1, 'float'),
         (b'vRotation', 1, 'float'),
         (b'vVelocity', 3, 'float'),
     ]
     self.ROTATION_I = 4  # must match index of vRotation above
     self.VELOCITY_I = 5  # must match index of vVelocity above
     self.vertex_depth = 0
     for element in self.vertex_format:
         self.vertex_depth += element[1]
     self.star_list = []
     self.canvas = RenderContext(use_parent_projection=True)
     self.canvas.shader.source = 'pointshader3d.glsl'
     glEnable(0x8642)  #GL_VERTEX_PROGRAM_POINT_SIZE
     glEnable(0x8861)  #GL_POINT_SPRITE
     self.mesh = None
     super(PointRenderer, self).__init__(**kwargs)
     self.canvas["camera_eyespace_z"] = self.width / 2.0
     self.canvas["max_particle_size"] = self.width
     self.draw_mesh_points(60)
     #Clock.schedule_interval(self.test_mesh_remove, 1./60.)
     Clock.schedule_interval(self.test_mesh_move, 1. / self.fps)
Ejemplo n.º 2
0
    def __init__(self, **kwargs):
        shaders = glShader()
        glEnable(0x8642)  #GL_VERTEX_PROGRAM_POINT_SIZE
        glEnable(0x8861)  #GL_POINT_SPRITE

        self.instructions = InstructionGroup()
        self.canvas = RenderContext(compute_normal_mat=True)
        self.canvas.shader.source = resource_find(shaders.line_shader)
        self.point_texture = resource_find(settings.path_root +
                                           '/images/textures/png/point2.png')

        self.draw_points = RenderContext(compute_normal_mat=True)
        self.draw_points.shader.source = resource_find(shaders.point_shader)

        self.draw_lines = RenderContext(compute_normal_mat=True)
        self.draw_lines.shader.source = resource_find(shaders.line_shader)

        self.instructions.add(self.draw_points)
        self.instructions.add(self.draw_lines)

        self.w = workspace({})
        self.w.load(settings.path_root + '/models/xml/fabricad.xml')

        super(Renderer, self).__init__(**kwargs)
        self.canvas.add(self.instructions)
        with self.canvas:
            self.cb = Callback(self.setup_gl_context)
            PushMatrix()
            self.setup_scene()
            PopMatrix()
            self.cb = Callback(self.reset_gl_context)
Ejemplo n.º 3
0
    def draw_panda(self, instr):
        self.fbo.clear_buffer()
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        self.msb.taskMgr.step()

        self.msb.camera.setPos(Point3(*self.cam_pos))
        self.msb.camera.lookAt(Point3(*self.cam_lookat), Vec3(0, 0, 1))
Ejemplo n.º 4
0
    def draw_panda(self, instr):
        self.fbo.clear_buffer()
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        self.msb.taskMgr.step()

        self.msb.camera.setPos(Point3(*self.cam_pos))
        self.msb.camera.lookAt(Point3(*self.cam_lookat), Vec3(0, 0, 1))
Ejemplo n.º 5
0
 def __init__(self, **kwargs):
     self.canvas = RenderContext(use_parent_projection=True)
     self.canvas.shader.source = 'pointshader.glsl'
     glEnable(0x8642) #GL_VERTEX_PROGRAM_POINT_SIZE
     glEnable(0x8861) #GL_POINT_SPRITE
     self.mesh = None
     super(PointRenderer, self).__init__(**kwargs) 
     self.draw_mesh_points(60)
     Clock.schedule_interval(self.test_mesh_remove, 1./60.)
Ejemplo n.º 6
0
 def __init__(self, **kwargs):
     self.canvas = RenderContext(use_parent_projection=True)
     self.canvas.shader.source = 'pointshader.glsl'
     glEnable(0x8642)  #GL_VERTEX_PROGRAM_POINT_SIZE
     glEnable(0x8861)  #GL_POINT_SPRITE
     self.mesh = None
     super(PointRenderer, self).__init__(**kwargs)
     self.draw_mesh_points(60)
     Clock.schedule_interval(self.test_mesh_remove, 1. / 60.)
Ejemplo n.º 7
0
 def build(self):
     root = FloatLayout()
     mq = PointRenderer(size=(800, 800))
     root.add_widget(mq)
     gl_major, gl_minor = gl_get_version()
     if gl_major > 2 or (gl_major == 2 and gl_minor >= 1):
         glEnable(0x8642) #GL_VERTEX_PROGRAM_POINT_SIZE
         glEnable(0x8861) #GL_POINT_SPRITE
         self.shader_path = 'assets/shaders/GL/'
     return root
Ejemplo n.º 8
0
 def reset_gl_context(self):
     gl.glEnable(gl.GL_BLEND)
     gl.glDisable(gl.GL_DEPTH_TEST)
     gl.glDisable(gl.GL_CULL_FACE)
     gl.glEnable(gl.GL_STENCIL_TEST)
     gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
     gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA,
                            gl.GL_ONE, gl.GL_ONE)
     gl.glActiveTexture(gl.GL_TEXTURE0)
     gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
Ejemplo n.º 9
0
 def render(self, obj_file):
     self.canvas.clear()
     self.scene = ObjFile(obj_file)
     with self.canvas:
         self.cb = Callback(lambda args: glEnable(GL_DEPTH_TEST))
         PushMatrix()
         self._setup_scene()
         PopMatrix()
         self.cb = Callback(lambda args: glDisable(GL_DEPTH_TEST))
     Clock.schedule_interval(self._update_glsl, 1 / 60.)
Ejemplo n.º 10
0
    def iterate(self, dt):
        glDisable(GL_BLEND)

        # pass 1
        fbo = self.fbo_pos2
        fbo['texture_vel'] = 1
        fbo['dt'] = dt
        fbo.ask_update()
        fbo.draw()

        # pass 2
        fbo = self.fbo_vel2
        fbo['dt'] = dt
        fbo.ask_update()
        fbo.draw()

        glEnable(GL_BLEND)

        # switch the texture for the next run
        self.fbo_pos, self.fbo_pos2 = self.fbo_pos2, self.fbo_pos
        self.fbo_vel, self.fbo_vel2 = self.fbo_vel2, self.fbo_vel
Ejemplo n.º 11
0
    def flush(self):
        if self.vertexIndex == 0:
            return

        self.renderCalls += 1
        self.totalRenderCalls += 1
        trianglesInBatch = self.triangleIndex
        if trianglesInBatch > self.maxTrianglesInBatch:
            self.maxTrianglesInBatch = trianglesInBatch

        self.lastTexture.bind()
        self.mesh.setVertices(self.vertices, 0, self.vertexIndex)
        self.mesh.setIndices(self.triangles, 0, trianglesInBatch)
        if self.blendingDisabled:
            glDisable(GL_BLEND)
        else:
            glEnable(GL_BLEND)
            if self.blendSrcFunc != -1:
                glBlendFuncSeparate(self.blendSrcFunc, self.blendDstFunc, self.blendSrcFuncAlpha, self.blendDstFuncAlpha)

        self.mesh.render(self.customShader if self.customShader is not None else self.shader, GL_TRIANGLES, 0, trianglesInBatch)

        self.vertexIndex = 0
        self.triangleIndex = 0
Ejemplo n.º 12
0
 def setup_gl_context(self, *args):
     glEnable(GL_DEPTH_TEST)
Ejemplo n.º 13
0
 def activate_depthtest(self, instr):
     glEnable(GL_DEPTH_TEST)
Ejemplo n.º 14
0
 def alpha_sample_callback(self, *args):
     gl.glEnable(gl.GL_SAMPLE_ALPHA_TO_COVERAGE)
Ejemplo n.º 15
0
 def setup_gl_context(self, *args):
     glEnable(GL_DEPTH_TEST)
Ejemplo n.º 16
0
 def _setup_gl_context(self, *args):
     glEnable(GL_DEPTH_TEST)
     self.fbo.clear_buffer()
Ejemplo n.º 17
0
 def alpha_sample_callback(self, *args):
     gl.glEnable(gl.GL_SAMPLE_ALPHA_TO_COVERAGE)
Ejemplo n.º 18
0
 def setup_gl_context(self, *args):
     glEnable(GL_DEPTH_TEST)
     self.fbo.clear_buffer()
Ejemplo n.º 19
0
 def activate_depthtest(self, instr):
     glEnable(GL_DEPTH_TEST)
    def draw(self, skeleton):
        self.shapes.clear()
        skeletonX = skeleton.getX()
        skeletonY = skeleton.getY()

        glEnable(GL_BLEND)
        srcFunc = GL_ONE if self.preMultipliedAlpha else GL_SRC_ALPHA
        glBlendFunc(srcFunc, GL_ONE_MINUS_SRC_ALPHA)

        bones = skeleton.getBones()
        if self.drawBones:
            self.shapes.add(self.boneLineColor)
            for i in range(len(bones)):
                bone = bones[i]
                if bone.parent is None:
                    continue
                x = skeletonX + bone.data.length * bone.m00 + bone.worldX
                y = skeletonY + bone.data.length * bone.m10 + bone.worldY
                self.shapes.add(
                    Line(points=[
                        skeletonX + bone.worldX, skeletonY + bone.worldY, x, y
                    ],
                         width=self.boneWidth * self.scale))
            self.shapes.add(Line(circle=(skeletonX, skeletonY,
                                         4 * self.scale)))

        if self.drawRegionAttachments:
            self.shapes.add(self.attachmentLineColor)
            slots = skeleton.getSlots()
            for i in range(len(slots)):
                slot = slots[i]
                attachment = slot.attachment
                if isinstance(attachment, RegionAttachment):
                    attachment.updateWorldVertices(slot, False)
                    vertices = attachment.getWorldVertices()
                    self.shapes.add(
                        Line(points=([
                            vertices[X1], vertices[Y1], vertices[X2],
                            vertices[Y2]
                        ])))
                    self.shapes.add(
                        Line(points=([
                            vertices[X2], vertices[Y2], vertices[X3],
                            vertices[Y3]
                        ])))
                    self.shapes.add(
                        Line(points=([
                            vertices[X3], vertices[Y3], vertices[X4],
                            vertices[Y4]
                        ])))
                    self.shapes.add(
                        Line(points=([
                            vertices[X4], vertices[Y4], vertices[X1],
                            vertices[Y1]
                        ])))

        if self.drawMeshHull or self.drawMeshTriangles:
            slots = skeleton.getSlots()
            for i in range(len(slots)):
                slot = slots[i]
                attachment = slot.attachment
                vertices = None
                triangles = None
                hullLength = 0
                if isinstance(attachment, MeshAttachment):
                    attachment.updateWorldVertices(slot, False)
                    vertices = attachment.getWorldVertices()
                    triangles = attachment.getTriangles()
                    hullLength = attachment.getHullLength()
                elif isinstance(attachment, SkinnedMeshAttachment):
                    attachment.updateWorldVertices(slot, False)
                    vertices = attachment.getWorldVertices()
                    triangles = attachment.getTriangles()
                    hullLength = attachment.getHullLength()
                if vertices is None or triangles is None:
                    continue
                if self.drawMeshTriangles:
                    self.shapes.add(self.triangleLineColor)
                    for ii in range(0, len(triangles), 3):
                        v1, v2, v3 = triangles[ii] * 5, triangles[
                            ii + 1] * 5, triangles[ii + 3] * 5
                        self.shapes.add(
                            Line(points=[
                                vertices[v1], vertices[v1 + 1], vertices[v2],
                                vertices[v2 + 1], vertices[v3], vertices[v3 +
                                                                         1]
                            ]))
                if self.drawMeshHull and hullLength > 0:
                    self.shapes.add(self.attachmentLineColor)
                    hullLength = hullLength / 2 * 5
                    lastX, lastY = vertices[hullLength -
                                            5], vertices[hullLength - 4]
                    for ii in range(0, hullLength, 5):
                        x, y = vertices[ii], vertices[ii + 1]
                        self.shapes.add(Line(points=[x, y, lastX, lastY]))
                        lastX = x
                        lastY = y

        if self.drawBoundingBoxes:
            bounds = self.bounds
            bounds.update(skeleton, True)
            self.shapes.add(self.aabbColor)
            self.shapes.add(
                Rectangle(x=bounds.getMinX(),
                          y=bounds.getMinY(),
                          width=bounds.getWidth(),
                          height=bounds.getHeight()))
            self.shapes.add(self.boundingBoxColor)
            polygons = bounds.getPolygons()
            for polygon in polygons:
                self.shapes.add(Line(points=polygon))

        if self.drawBones:
            self.shapes.add(self.boneOriginColor)
            for bone in bones:
                self.shapes.add(Color(0, 1, 0, 1))
                self.shapes.add(
                    Line(circle=(skeletonX + bone.worldX,
                                 skeletonY + bone.worldY, 3 * self.scale)))
        del bones
Ejemplo n.º 21
0
 def glSetup(self):
     gl.glEnable(gl.GL_CULL_FACE)
     gl.glCullFace(gl.GL_BACK)
Ejemplo n.º 22
0
 def _enter_3d(self, *args):
     glEnable(GL_DEPTH_TEST)
     glEnable(GL_CULL_FACE)
Ejemplo n.º 23
0
    def init_smaa(self):
        curdir = dirname(__file__)

        # load shaders sources
        with open(join(curdir, "SMAA.h"), "r") as fd:
            smaa_h = fd.read()

        config = """
            #version 410 compatibility
            #define SMAA_PIXEL_SIZE vec2(1.0 / {width}, 1.0 / {height})
            #define SMAA_PRESET_{quality} 1
            #define SMAA_GLSL_4 1
        """.format(
            width=self.width, height=self.height, quality=self.quality.upper()
        )

        header_vs = (
            config
            + """
            #define SMAA_ONLY_COMPILE_VS 1

            in vec2 vPosition;
            in vec2 vTexCoords0;
            uniform mat4 modelview_mat;
            uniform mat4 projection_mat;
        """
            + smaa_h
        )

        header_fs = (
            config
            + """
            #define SMAA_ONLY_COMPILE_PS 1
        """
            + smaa_h
        )

        edge_vs = (
            header_vs
            + """
            out vec2 texcoord;
            out vec4 offset[3];
            out vec4 dummy2;
            void main()
            {
                texcoord = vTexCoords0;
                vec4 dummy1 = vec4(0);
                SMAAEdgeDetectionVS(dummy1, dummy2, texcoord, offset);
                gl_Position = projection_mat * modelview_mat * vec4(vPosition.xy, 0.0, 1.0);
            }
        """
        )

        edge_fs = (
            header_fs
            + """
            uniform sampler2D albedo_tex;
            in vec2 texcoord;
            in vec4 offset[3];
            in vec4 dummy2;
            void main()
            {
                #if SMAA_PREDICATION == 1
                    gl_FragColor = SMAAColorEdgeDetectionPS(texcoord, offset, albedo_tex, depthTex);
                #else
                    gl_FragColor = SMAAColorEdgeDetectionPS(texcoord, offset, albedo_tex);
                #endif
            }
        """
        )

        blend_vs = (
            header_vs
            + """
            out vec2 texcoord;
            out vec2 pixcoord;
            out vec4 offset[3];
            out vec4 dummy2;
            void main()
            {
                texcoord = vTexCoords0;
                vec4 dummy1 = vec4(0);
                SMAABlendingWeightCalculationVS(dummy1, dummy2, texcoord, pixcoord, offset);
                gl_Position = projection_mat * modelview_mat * vec4(vPosition.xy, 0.0, 1.0);
            }
        """
        )

        blend_fs = (
            header_fs
            + """
            uniform sampler2D edge_tex;
            uniform sampler2D area_tex;
            uniform sampler2D search_tex;
            in vec2 texcoord;
            in vec2 pixcoord;
            in vec4 offset[3];
            in vec4 dummy2;
            void main()
            {
                gl_FragColor = SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edge_tex, area_tex, search_tex, ivec4(0));
            }
        """
        )

        neighborhood_vs = (
            header_vs
            + """
            out vec2 texcoord;
            out vec4 offset[2];
            out vec4 dummy2;
            void main()
            {
                texcoord = vTexCoords0;
                vec4 dummy1 = vec4(0);
                SMAANeighborhoodBlendingVS(dummy1, dummy2, texcoord, offset);
                gl_Position = projection_mat * modelview_mat * vec4(vPosition.xy, 0.0, 1.0);
            }
        """
        )

        neighborhood_fs = (
            header_fs
            + """
            uniform sampler2D albedo_tex;
            uniform sampler2D blend_tex;
            in vec2 texcoord;
            in vec4 offset[2];
            in vec4 dummy2;
            void main()
            {
                gl_FragColor = SMAANeighborhoodBlendingPS(texcoord, offset, albedo_tex, blend_tex);
            }
        """
        )

        size = self.size
        self.albedo_tex = Texture.create(size=size, bufferfmt="float")
        self.albedo_fbo = Fbo(size=size, texture=self.albedo_tex)

        self.edge_tex = Texture.create(size=size, bufferfmt="float")
        self.edge_fbo = Fbo(size=size, vs=edge_vs, fs=edge_fs, texture=self.edge_tex)
        self.edge_fbo.bind()
        self.edge_fbo["albedo_tex"] = 0
        self.edge_fbo.release()

        self.blend_tex = Texture.create(size=size, bufferfmt="float")
        self.blend_fbo = Fbo(size=size, vs=blend_vs, fs=blend_fs, texture=self.blend_tex)
        self.blend_fbo.bind()
        self.blend_fbo["edge_tex"] = 0
        self.blend_fbo["area_tex"] = 1
        self.blend_fbo["search_tex"] = 2
        self.blend_fbo.release()

        self.neighborhood = RenderContext(
            use_parent_modelview=True, use_parent_projection=True, vs=neighborhood_vs, fs=neighborhood_fs
        )
        with self.neighborhood:
            self.neighborhood["albedo_tex"] = 0
            self.neighborhood["blend_tex"] = 1

        self.area_tex = Texture.create(size=(AREATEX_WIDTH, AREATEX_HEIGHT), colorfmt="rg", icolorfmt="rg8")

        with open(join(curdir, "smaa_area.raw"), "rb") as fd:
            self.area_tex.blit_buffer(fd.read(), colorfmt="rg")

        self.search_tex = Texture.create(size=(SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT), colorfmt="red", icolorfmt="r8")

        self.search_tex.min_filter = "nearest"
        self.search_tex.mag_filter = "nearest"

        with open(join(curdir, "smaa_search.raw"), "rb") as fd:
            self.search_tex.blit_buffer(fd.read(), colorfmt="red")

        with self.albedo_fbo:
            ClearColor(0, 0, 0, 0)
            ClearBuffers()

        with self.edge_fbo:
            Rectangle(size=self.size, texture=self.albedo_tex)

        with self.blend_fbo:
            BindTexture(index=1, texture=self.area_tex)
            BindTexture(index=2, texture=self.search_tex)
            Rectangle(size=self.size, texture=self.edge_tex)

        self.neighborhood.add(self.albedo_fbo)
        self.neighborhood.add(Callback(lambda *x: glDisable(GL_BLEND)))
        self.neighborhood.add(self.edge_fbo)
        self.neighborhood.add(self.blend_fbo)
        self.neighborhood.add(Callback(lambda *x: glEnable(GL_BLEND)))
        with self.neighborhood:
            BindTexture(index=1, texture=self.blend_tex)
            Rectangle(size=self.size, texture=self.albedo_tex)

        self.smaa_canvas = Canvas()
        with self.smaa_canvas.before:

            def do_stuff(*args):
                self.albedo_fbo.bind()
                self.albedo_fbo.clear_buffer()
                self.albedo_fbo.release()
                self.edge_fbo.bind()
                self.edge_fbo.clear_buffer()
                self.edge_fbo.release()
                self.blend_fbo.bind()
                self.blend_fbo.clear_buffer()
                self.blend_fbo.release()
                self.albedo_fbo.ask_update()
                self.edge_fbo.ask_update()
                self.blend_fbo.ask_update()
                self.neighborhood.ask_update()

            Callback(do_stuff)
        self.smaa_canvas.add(self.neighborhood)

        self._g_debug_added = False
        self._g_debug = [
            Callback(lambda *x: glDisable(GL_BLEND)),
            Color(0, 0, 0, 1),
            Rectangle(size=self.size),
            Color(1, 1, 1, 1),
            Rectangle(size=self.size),
            Callback(lambda *x: glEnable(GL_BLEND)),
        ]