예제 #1
0
 def material_matrix(self):
     """
     Creates a transformation matrix scaled to the size of the torus
     :return: the transformation matrix
     :return: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.5, .5, .5]))
     out.scale(Vector([self.radius, self.radius, self.radius]) *
               (.5 / (self.radius + self.thickness)))
     return out
예제 #2
0
 def material_matrix(self):
     """
     Creates a transformation matrix for spherical objects
     :return: the transformation matrix
     :rtype: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.5, .5, .5]))
     scale = self.scale
     out.scale(scale * (.5 / max(scale.x_component, max(scale.y_component,
                                                        scale.z_component))))
     return out
예제 #3
0
 def material_matrix(self):
     """
     Creates a transformation matrix for spherical objects
     :return: the transformation matrix
     :rtype: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.5, .5, .5]))
     scale = self.scale
     out.scale(scale * (.5 / max(
         scale.x_component, max(scale.y_component, scale.z_component))))
     return out
예제 #4
0
def test_tmatrix_scale():
    from pyglet_helper.util import Tmatrix, Vertex
    _tmatrix1 = Tmatrix()
    _tmatrix1.matrix[0, 0] = 0.0
    _tmatrix1.matrix[1, 1] = 0.0
    _tmatrix1.matrix[1, 0] = 1.0
    _tmatrix1.matrix[0, 1] = 1.0
    _vert = Vertex([0, 1.0, 0, 0.5])
    _tmatrix1.scale(_vert)
    assert _tmatrix1.matrix[0, 0] == 0.0
    assert _tmatrix1.matrix[1, 1] == 0.0
    assert _tmatrix1.matrix[0, 1] == 0.0
    assert _tmatrix1.matrix[3, 3] == 0.5
예제 #5
0
 def material_matrix(self):
     """
     Creates a transformation matrix for pyramid objects
     :return: the transformation matrix
     :rtype: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([0, .5, .5]))
     scale = Vector([self.axis.mag(), self.height, self.width])
     out.scale(Vector([self.scale,self.scale,self.scale]) *
               (1.0 /max(scale.x_component, max(scale.y_component,
                                           scale.z_component))))
     return out
예제 #6
0
def test_tmatrix_scale():
    from pyglet_helper.util import Tmatrix, Vertex
    _tmatrix1 = Tmatrix()
    _tmatrix1.matrix[0, 0] = 0.0
    _tmatrix1.matrix[1, 1] = 0.0
    _tmatrix1.matrix[1, 0] = 1.0
    _tmatrix1.matrix[0, 1] = 1.0
    _vert = Vertex([0, 1.0, 0, 0.5])
    _tmatrix1.scale(_vert)
    assert _tmatrix1.matrix[0, 0] == 0.0
    assert _tmatrix1.matrix[1, 1] == 0.0
    assert _tmatrix1.matrix[0, 1] == 0.0
    assert _tmatrix1.matrix[3, 3] == 0.5
예제 #7
0
 def material_matrix(self):
     """
     Creates a transformation matrix scaled to the size of the axial object
     :return: the transformation matrix
     :return: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.0005, .5, .5]))
     out.scale(self.scale * (.999 / max(self.scale.x_component,
                                        self.scale.y_component * 2)))
     out_vector = Vector([0, 1, 0])
     out_vector = out_vector.rotate(angle=.5*pi)
     out = out * out_vector
     return out
예제 #8
0
 def material_matrix(self):
     """
     Creates a transformation matrix scaled to the size of the axial object
     :return: the transformation matrix
     :return: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.0005, .5, .5]))
     out.scale(
         self.scale *
         (.999 / max(self.scale.x_component, self.scale.y_component * 2)))
     out_vector = Vector([0, 1, 0])
     out_vector = out_vector.rotate(angle=.5 * pi)
     out = out * out_vector
     return out
예제 #9
0
    def model_world_transform(self,
                              world_scale=0.0,
                              object_scale=Vector([1, 1, 1])):
        """Performs scale, rotation, translation, and world scale (gcf)
        transforms in that order.

        :param world_scale: The global scaling factor.
        :type world_scale: float
        :param object_scale: The scaling to applied to this specific object
        :type object_scale: pyglet_helper.util.Vector
        :rtype: pyglet_helper.util.Tmatrix
        :returns:  Returns a tmatrix that performs reorientation of the object
        from model orientation to world
         (and view) orientation.
        """
        ret = Tmatrix()
        # A unit vector along the z_axis.
        z_axis = Vector([0, 0, 1])
        if abs(self.axis.dot(self.up_vector) / self.up_vector.mag()**2.0) \
                > 0.98:
            # Then axis and up are in (nearly) the same direction: therefore,
            # try two other possible directions for the up vector.
            if abs(self.axis.norm().dot(Vector([-1, 0, 0]))) > 0.98:
                z_axis = self.axis.cross(Vector([0, 0, 1])).norm()
            else:
                z_axis = self.axis.cross(Vector([-1, 0, 0])).norm()
        else:
            z_axis = self.axis.cross(self.up_vector).norm()

        y_axis = z_axis.cross(self.axis).norm()
        x_axis = self.axis.norm()
        ret.x_column(x_axis)
        ret.y_column(y_axis)
        ret.z_column(z_axis)
        w_column = world_scale * self.pos
        ret.w_column(w_column)
        ret.w_row()

        ret.scale(object_scale * world_scale, 1)

        return ret
예제 #10
0
    def model_world_transform(self, world_scale=0.0,
                              object_scale=Vector([1, 1, 1])):
        """Performs scale, rotation, translation, and world scale (gcf)
        transforms in that order.

        :param world_scale: The global scaling factor.
        :type world_scale: float
        :param object_scale: The scaling to applied to this specific object
        :type object_scale: pyglet_helper.util.Vector
        :rtype: pyglet_helper.util.Tmatrix
        :returns:  Returns a tmatrix that performs reorientation of the object
        from model orientation to world
         (and view) orientation.
        """
        ret = Tmatrix()
        # A unit vector along the z_axis.
        z_axis = Vector([0, 0, 1])
        if abs(self.axis.dot(self.up_vector) / self.up_vector.mag()**2.0) \
                > 0.98:
            # Then axis and up are in (nearly) the same direction: therefore,
            # try two other possible directions for the up vector.
            if abs(self.axis.norm().dot(Vector([-1, 0, 0]))) > 0.98:
                z_axis = self.axis.cross(Vector([0, 0, 1])).norm()
            else:
                z_axis = self.axis.cross(Vector([-1, 0, 0])).norm()
        else:
            z_axis = self.axis.cross(self.up_vector).norm()

        y_axis = z_axis.cross(self.axis).norm()
        x_axis = self.axis.norm()
        ret.x_column(x_axis)
        ret.y_column(y_axis)
        ret.z_column(z_axis)
        w_column = world_scale*self.pos
        ret.w_column(w_column)
        ret.w_row()

        ret.scale(object_scale * world_scale, 1)

        return ret
예제 #11
0
    def render(self, scene):
        """ Render the arrow on the current view.

        :param scene: The view to render the model into.
        :type scene: pyglet_helper.objects.View
        """
        mat = Material()
        if self.degenerate:
            return
        self.init_model(scene)
        self.color.gl_set(self.opacity)
        _head_width, _shaft_width, _len, _head_length = \
            self.effective_geometry(1.0)
        if self.mat and self.mat.get_shader_program():
            model_material_loc = self.mat.get_shader_program().\
                get_uniform_location(scene, "model_material")
        else:
            model_material_loc = -1
        # Render the shaft and the head in back to front order (the shaft is in
        # front of the head if axis points away from the camera)
        shaft = self.axis.dot(scene.camera - (self.pos + self.axis *
                                              (1 - _head_length / _len))) < 0
        gl.glPushMatrix()
        self.model_world_transform(scene.gcf).gl_mult()

        for part in range(0, 2):
            if part == shaft:
                gl.glScaled(_len - _head_length, _shaft_width, _shaft_width)
                gl.glTranslated(0.5, 0, 0)
                if model_material_loc >= 0:
                    model_mat = Tmatrix()
                    scale = 1.0 / max(_len, _head_width)
                    _translation_magnitude = (_len - _head_length) * scale *\
                                             0.5
                    model_mat.translate(
                        Vector([_translation_magnitude, 0.5, 0.5]))
                    model_mat.scale(
                        Vector([(_len - _head_length), _shaft_width,
                                _shaft_width]) * scale)
                    mat.get_shader_program().\
                        set_uniform_matrix(scene, model_material_loc,
                                           model_mat)
                scene.box_model.gl_render()
                gl.glTranslated(-0.5, 0, 0)
                gl.glScaled(1 / (_len - _head_length), 1 / _shaft_width,
                            1 / _shaft_width)
            else:
                gl.glTranslated(_len - _head_length, 0, 0)
                gl.glScaled(_head_length, _head_width, _head_width)
                if model_material_loc >= 0:
                    model_mat = Tmatrix()
                    _scale = 1.0 / max(_len, _head_width)
                    model_mat.translate(
                        Vector([(_len - _head_length) * _scale, 0.5, 0.5]))
                    model_mat.scale(
                        Vector([_head_length, _head_width, _head_width]) *
                        _scale)
                    mat.get_shader_program().\
                        set_uniform_matrix(scene, model_material_loc,
                                           model_mat)
                scene.pyramid_model.gl_render()
                gl.glScaled(1 / _head_length, 1 / _head_width, 1 / _head_width)
                gl.glTranslated(-_len + _head_length, 0, 0)
        gl.glPopMatrix()