コード例 #1
0
    def loadObj(self, objs):
        print("load objects ", len(objs))

        for io, o in enumerate(objs):
            tao = {}
            tao['name'] = o[0]
            tao['filePath'] = o[1]
            tao['texturePath'] = o[2]
            m = ObjFile(tao['filePath'])
            tao['ol'] = m
            tao['mesh'] = None
            tao['cit'] = ObjectProperty(None)

            #print("doing normalization")

            m = list(m.objects.values())[0]
            res = []
            count = 0
            for i, o in enumerate(m.vertices):
                res.append(o)
                if (i + 1) % 8 == 0:
                    count += 1
                    res.append(0.0)
                    res.append(0.0)
                    res.append(0.0)

                    res.append(i // 8)
                    res.append(i // 8)

                    if count >= 3:
                        l = len(res)
                        v0 = [
                            res[l - 13 * 3], res[l - 13 * 3 + 1],
                            res[l - 13 * 3 + 2]
                        ]
                        v1 = [
                            res[l - 13 * 2], res[l - 13 * 2 + 1],
                            res[l - 13 * 2 + 2]
                        ]
                        v2 = [
                            res[l - 13 * 1], res[l - 13 * 1 + 1],
                            res[l - 13 * 1 + 2]
                        ]

                        t0xy = [res[l - 13 * 3 + 6], res[l - 13 * 3 + 7]]
                        t1xy = [res[l - 13 * 2 + 6], res[l - 13 * 2 + 7]]
                        t2xy = [res[l - 13 + 6], res[l - 13 + 7]]

                        edge1 = min_vector(v1, v0)
                        edge2 = min_vector(v2, v0)

                        delta_u1 = t1xy[0] - t0xy[0]
                        delta_v1 = t1xy[1] - t0xy[1]

                        delta_u2 = t2xy[0] - t0xy[0]
                        delta_v2 = t2xy[1] - t0xy[1]

                        d = (delta_u1 * delta_v2 - delta_u2 * delta_v1)
                        if d == 0:
                            d = 0.01
                        f = 1.0 / d

                        tangent_x = f * (delta_v2 * edge1[0] -
                                         delta_v1 * edge2[0])
                        tangent_y = f * (delta_v2 * edge1[1] -
                                         delta_v1 * edge2[1])
                        tangent_z = f * (delta_v2 * edge1[2] -
                                         delta_v1 * edge2[2])

                        for _i in range(1, 4):
                            res[l - 13 * _i + 8] += tangent_x
                            res[l - 13 * _i + 9] += tangent_y
                            res[l - 13 * _i + 10] += tangent_z

                        count = 0

            for i in range(len(res)):
                if (i + 1) % 13 == 0:
                    vec = [res[i - 12 + 8], res[i - 12 + 9], res[i - 12 + 10]]
                    n_vec = normalize(vec)
                    res[i - 12 + 8] = n_vec[0]
                    res[i - 12 + 9] = n_vec[1]
                    res[i - 12 + 10] = n_vec[2]

            m.vertices = res
            _vertices = m.vertices
            tao['ver'] = _vertices
            _indices = m.indices
            tao['ind'] = _indices

            #print("normalization done")

            self.objs.append(tao)
            print(("    %s / %s    [" % (io + 1, len(objs))), tao['name'],
                  '] DONE')
コード例 #2
0
    def start(self, current_callback):
        if self.init == 0:
            self.current_callback = current_callback
            Color(1, 1, 1, 1)

            normal_map_value = 0
            if len(self._normal_map) > 0:
                normal_map_value = 1
                image = Image(self._normal_map)
                bind_texture = BindTexture(texture=image.texture, index=2)

            self._instruction_group = InstructionGroup()

            self._instruction_group.add(Callback(self.update_params))

            self.state = ChangeState(
                enabled_shadow=(float(self.receive_shadows)),
                lighting=(float(self.lighting)),
                light_intensity=self.light_intensity,
                flip_coords=(float(self.flip_coords)),
                alpha=(float(self.alpha)),
                #pitch=(float(self.pitch)),
                #yaw=self.yaw,
                #roll=(float(self.roll)),
                shadows_bias=(float(self.shadows_bias)),
                normal_map_enabled=(float(normal_map_value)),
                specular_intensity=(float(self.specular_intensity)),
                specular_power=(float(self.specular_power)),
                min_light_intensity=(float(self.min_light_intensity)))

            self._instruction_group.add(self.state)
            self._translate = Translate(*self.translate)

            self._rotate = Rotate(*self.rotate)
            self._scale = Scale(*self.scale)

            self._instructions.append(self.state)
            self._instructions.append(self._translate)
            self._instructions.append(self._rotate)
            self._instructions.append(self._scale)
            self._instructions.append(self._instruction_group)
            self._instructions.append(Callback(self.before_render))

        elif self.init == 1:
            self.current_callback_fbo = current_callback
            self._shadow_instructions.append(Callback(self.update_params_fbo))
            state = ChangeState(cast_shadows=(float(self.cast_shadows)))

            self._shadow_translate = Translate(*self.translate)
            self._shadow_rotate = Rotate(*self.rotate)
            #self._scale = Scale(*self.scale)

            self._shadow_scale = Scale(*self.scale)
            self._shadow_instructions.append(state)
            self._shadow_instructions.append(self._shadow_translate)
            self._shadow_instructions.append(self._shadow_rotate)

            self._shadow_instructions.append(self._shadow_scale)
            #self._instructions.append(Callback(self.before_render))

        elif self.init == 2:
            self.current_callback_picking_fbo = current_callback
            mrange = 0
            if self.effect:
                mrange = 0.50

            self._picking_instructions.append(
                Callback(self.update_params_picking_fbo))
            state = ChangeState(id_color=(round(self.pick_id + mrange, 2),
                                          float(self.effect), 0.0))
            self._picking_translate = Translate(*self.translate)
            self._picking_rotate = Rotate(*self.rotate)

            self._picking_scale = Scale(*self.scale)
            self._picking_instructions.append(state)
            self._picking_instructions.append(self._picking_translate)
            self._picking_instructions.append(self._picking_rotate)

            self._picking_instructions.append(self._picking_scale)
            self._instructions.append(Callback(self.before_render))

        elif self.init == 3:
            state = ChangeState(id=(self.motion_id))
            self._motion_blur_translate = Translate(*self.translate)
            self._motion_blur_rotate = Rotate(*self.rotate)
            self._motion_blur_scale = Scale(*self.scale)
            self._blur_instructions.append(state)
            self._blur_instructions.append(self._motion_blur_translate)
            self._blur_instructions.append(self._motion_blur_rotate)
            self._blur_instructions.append(self._motion_blur_scale)
            self._instructions.append(Callback(self.before_render))

        UpdateNormalMatrix()
        for e in self._objs:
            _vertices = []
            _indices = []
            if ".obj" in e:
                filename = resource_find(e)
                if not filename:
                    raise IOError("File: '{0}' not found".format(e))
                m = ObjFile(resource_find(e))
                m = list(m.objects.values())[0]
                res = []
                count = 0
                for i, o in enumerate(m.vertices):
                    res.append(o)
                    if (i + 1) % 8 == 0:
                        count += 1
                        res.append(0.0)
                        res.append(0.0)
                        res.append(0.0)

                        res.append(i // 8)
                        res.append(i // 8)

                        if count >= 3:
                            l = len(res)
                            v0 = [
                                res[l - 13 * 3], res[l - 13 * 3 + 1],
                                res[l - 13 * 3 + 2]
                            ]
                            v1 = [
                                res[l - 13 * 2], res[l - 13 * 2 + 1],
                                res[l - 13 * 2 + 2]
                            ]
                            v2 = [
                                res[l - 13 * 1], res[l - 13 * 1 + 1],
                                res[l - 13 * 1 + 2]
                            ]

                            t0xy = [res[l - 13 * 3 + 6], res[l - 13 * 3 + 7]]
                            t1xy = [res[l - 13 * 2 + 6], res[l - 13 * 2 + 7]]
                            t2xy = [res[l - 13 + 6], res[l - 13 + 7]]

                            edge1 = min_vector(v1, v0)
                            edge2 = min_vector(v2, v0)

                            delta_u1 = t1xy[0] - t0xy[0]
                            delta_v1 = t1xy[1] - t0xy[1]

                            delta_u2 = t2xy[0] - t0xy[0]
                            delta_v2 = t2xy[1] - t0xy[1]

                            d = (delta_u1 * delta_v2 - delta_u2 * delta_v1)
                            if d == 0:
                                d = 0.01
                            f = 1.0 / d

                            tangent_x = f * (delta_v2 * edge1[0] -
                                             delta_v1 * edge2[0])
                            tangent_y = f * (delta_v2 * edge1[1] -
                                             delta_v1 * edge2[1])
                            tangent_z = f * (delta_v2 * edge1[2] -
                                             delta_v1 * edge2[2])

                            for _i in range(1, 4):
                                res[l - 13 * _i + 8] += tangent_x
                                res[l - 13 * _i + 9] += tangent_y
                                res[l - 13 * _i + 10] += tangent_z

                            count = 0

                for i in range(len(res)):
                    if (i + 1) % 13 == 0:
                        vec = [
                            res[i - 12 + 8], res[i - 12 + 9], res[i - 12 + 10]
                        ]
                        n_vec = normalize(vec)
                        res[i - 12 + 8] = n_vec[0]
                        res[i - 12 + 9] = n_vec[1]
                        res[i - 12 + 10] = n_vec[2]

                m.vertices = res
                _vertices = m.vertices
                _indices = m.indices
                if self.init != 3:
                    mesh = Mesh(
                        vertices=_vertices,
                        indices=_indices,
                        fmt=[(b'v_pos', 3, 'float'), (b'v_normal', 3, 'float'),
                             (b'v_tc0', 2, 'float'), (b'tangent', 3, 'float'),
                             (b'vert_pos', 2, 'float')],
                        mode='triangles',
                        source=e + ".png",
                    )
                    self.objs.append(mesh)
                    if self.init == 0:
                        self._instructions.append(mesh)
                    if self.init == 1:
                        self._shadow_instructions.append(mesh)
                    if self.init == 2:
                        self._picking_instructions.append(mesh)
                    if self.init == 3:
                        self._blur_instructions.append(mesh)

            if (".dae" in e) or ('.xml' in e and not ".mesh.xml" in e):
                raise Exception("Collada not yet implemented")
                for o in load_dae_scene(e):
                    self.objs.append(o)
                    if self.init == 0:
                        self.mesh = o

            if ".mesh.xml" in e:
                for o, ba, skel, vert in load_ogre(e):
                    self.skeletons = skel
                    self.objs.append(o)
                    self.bones_data.append(ba)
                    self.vertices = vert[:]
                    if self.init == 0:
                        self.mesh = o

            if ".md5mesh" in e:
                raise Exception("MD5 not implemented")
                for o in load_md5(e, self._anims):
                    self.objs.append(o)
                    if self.init == 0:
                        self.mesh = o

        self._instructions.append(Callback(self.after_render))
        self.init += 1
コード例 #3
0
ファイル: node.py プロジェクト: hzgcoding/kivy3dgui
    def start(self):
        if self.init == 0:
            Color(1, 1, 1, 1)
            s = ChangeState(enabled_shadow=(float(self.receive_shadows)),
                        lighting=(float(self.lighting)),
                        light_intensity=self.light_intensity,
                        flip_coords=(float(self.flip_coords)))
            self._translate = Translate(*self.translate)
            self._rotate = Rotate(*self.rotate)
            self._scale = Scale(*self.scale)
        elif self.init == 1:
            ChangeState(cast_shadows=(float(self.cast_shadows)))
            self._shadow_translate = Translate(*self.translate)
            self._shadow_rotate = Rotate(*self.rotate)
            self._shadow_scale = Scale(*self.scale)
        elif self.init == 2:
            range = 0
            if self.effect:
                range = 0.50
            ChangeState(id_color=(round(self.pick_id + range, 2), float(self.effect), 0.0))
            self._picking_translate = Translate(*self.translate)
            self._picking_rotate = Rotate(*self.rotate)
            self._picking_scale = Scale(*self.scale)
        elif self.init == 3:
            ChangeState(id=(self.motion_id))
            self._motion_blur_translate = Translate(*self.translate)
            self._motion_blur_rotate = Rotate(*self.rotate)
            self._motion_blur_scale = Scale(*self.scale)

        UpdateNormalMatrix()
        for e in self._objs:
            _vertices = []
            _indices = []
            if ".obj" in e:
                m = ObjFile(resource_find(e))
                m = list(m.objects.values())[0]
                res = []
                for i, o in enumerate(m.vertices):
                    res.append(o)
                    if (i + 1) % 8 == 0:
                        res.append(i // 8)
                        res.append(i // 8)
                m.vertices = res
                _vertices = m.vertices
                _indices = m.indices
                mesh = Mesh(
                    vertices=_vertices,
                    indices=_indices,
                    fmt=[(b'v_pos', 3, 'float'), (b'v_normal', 3, 'float'), (b'v_tc0', 2, 'float'),
                         (b'vert_pos', 2, 'float')],
                    mode='triangles',
                    source=e+".png",
                )
                self.objs.append(mesh)

            if (".dae" in e) or ('.xml' in e and not ".mesh.xml" in e):
                raise Exception("Collada not yet implemented")
                for o in load_dae_scene(e):
                    self.objs.append(o)
                    if self.init == 0:
                        self.mesh = o

            if ".mesh.xml" in e:
                for o, ba, skel, vert in load_ogre(e):
                    self.skeletons = skel
                    self.objs.append(o)
                    self.bones_data.append(ba)
                    self.vertices = vert[:]
                    if self.init == 0:
                        self.mesh = o

            if ".md5mesh" in e:
                raise Exception("MD5 not implemented")
                for o in load_md5(e, self._anims):
                    self.objs.append(o)
                    if self.init == 0:
                        self.mesh = o

        self.init += 1