Esempio n. 1
0
    def test_procedural_examples(self):
        from pyrr import quaternion, matrix44, vector3
        import numpy as np

        point = vector3.create(1.,2.,3.)
        orientation = quaternion.create()
        translation = vector3.create()
        scale = vector3.create(1,1,1)

        # translate along X by 1
        translation += [1.0, 0.0, 0.0]

        # rotate about Y by pi/2
        rotation = quaternion.create_from_y_rotation(np.pi / 2.0)
        orientation = quaternion.cross(rotation, orientation)

        # create a matrix
        # start our matrix off using the scale
        matrix = matrix44.create_from_scale(scale)

        # apply our orientation
        orientation = matrix44.create_from_quaternion(orientation)
        matrix = matrix44.multiply(matrix, orientation)

        # apply our translation
        translation_matrix = matrix44.create_from_translation(translation)
        matrix = matrix44.multiply(matrix, translation_matrix)

        # transform our point by the matrix
        point = matrix44.apply_to_vector(matrix, point)
Esempio n. 2
0
    def matrix( self ):
        """A matrix representing the transform's translation,
        orientation and scale.

        The is an @property decorated method which allows
        retrieval and assignment of the scale value.
        """
        if self._matrix == None:
            # matrix transformations must be done in order
            # scaling
            # rotation
            # translation

            # apply our scale
            self._matrix = matrix44.create_from_scale( self.scale )

            # apply our quaternion
            self._matrix = matrix44.multiply(
                self._matrix,
                matrix44.create_from_quaternion( self.orientation )
                )

            # apply our translation
            # we MUST do this after the orientation
            self._matrix = matrix44.multiply(
                self._matrix,
                matrix44.create_from_translation( self.translation )
                )

        return self._matrix
Esempio n. 3
0
    def test_decompose(self):
        # define expectations
        expected_scale = vector3.create(*[1, 1, 2], dtype='f4')
        expected_rotation = quaternion.create_from_y_rotation(np.pi, dtype='f4')
        expected_translation = vector3.create(*[10, 0, -5], dtype='f4')
        expected_model = np.array([
            [-1, 0, 0, 0],
            [0, 1, 0, 0],
            [0, 0, -2, 0],
            [10, 0, -5, 1],
        ], dtype='f4')

        # compose matrix using Pyrr
        s = matrix44.create_from_scale(expected_scale, dtype='f4')
        r = matrix44.create_from_quaternion(expected_rotation, dtype='f4')
        t = matrix44.create_from_translation(expected_translation, dtype='f4')
        model = s.dot(r).dot(t)
        np.testing.assert_almost_equal(model, expected_model)
        self.assertTrue(model.dtype == expected_model.dtype)

        # decompose matrix
        scale, rotation, translation = matrix44.decompose(model)
        np.testing.assert_almost_equal(scale, expected_scale)
        self.assertTrue(scale.dtype == expected_scale.dtype)
        np.testing.assert_almost_equal(rotation, expected_rotation)
        self.assertTrue(rotation.dtype == expected_rotation.dtype)
        np.testing.assert_almost_equal(translation, expected_translation)
        self.assertTrue(translation.dtype == expected_translation.dtype)
Esempio n. 4
0
	def GetWorldMatrix(self):
		if self.worldMatrix is None or self.isDirty:
			self.isDirty = False
			translation = matrix44.create_from_translation(vector3.create(self.position[0], self.position[1], 0.0))
			rotation = matrix44.create_from_z_rotation(self.rotation)
			self.worldMatrix = matrix44.multiply(rotation, translation)

		return self.worldMatrix
Esempio n. 5
0
    def test_create_from_translation( self ):
        translation = numpy.array( [ 2.0, 3.0, 4.0 ] )
        mat = matrix44.create_from_translation( translation )
        result = mat[ 3, 0:3 ]

        expected = translation

        # translation goes down the last column in normal matrix
        self.assertTrue(
            numpy.array_equal( result, expected ),
            "Matrix44 translation not set properly"
            )
Esempio n. 6
0
def lookAtMatrix( camera, target, up ):
   forward = vector.normalise(target - camera)
   side = vector.normalise( vector.cross( forward, up ) )
   #shifts 'up' to the camera's up
   up = vector.cross( side, forward )

   matrix2 = array(
      [[ side[0], up[0], -forward[0], 0.0 ],
       [ side[1], up[1], -forward[1], 0.0 ], 
       [ side[2], up[2], -forward[2], 0.0 ], 
       [     0.0,   0.0,         0.0, 1.0 ]],
      dtype = float32)

   return array(mat4.multiply( mat4.create_from_translation( -camera ), matrix2 ), dtype=float32)
Esempio n. 7
0
   def regenViewMatrix(self):
      forward = array([ math.cos( self.verticalAngle ) * math.sin( self.horizontalAngle ),
                        math.sin( self.verticalAngle ),
                        math.cos( self.verticalAngle ) * math.cos( self.horizontalAngle ) ])
      up = array([0.,-1.,0.,])
      side = -vector.normalise( vector.cross( forward, up ) )
      #shifts 'up' to the camera's up
      up = vector.cross( side, forward )

      matrix2 = array(
         [[ side[0], up[0], forward[0], 0.0 ],
          [ side[1], up[1], forward[1], 0.0 ], 
          [ side[2], up[2], forward[2], 0.0 ], 
          [     0.0,   0.0,         0.0, 1.0 ]],
         dtype = float32)

      self.position += forward * self.xMovement
      self.position += side    * self.yMovement

      self.xMovement = self.yMovement = 0.

      self.viewMatrix =  array(mat4.multiply( mat4.create_from_translation( self.position ), matrix2 ), dtype=float32)
Esempio n. 8
0
	def render(self):
		t = clock()
		self.time_parameter.set_value(t)

		data = (
			((-0.5, -0.5, 0), (0.0, 0.0)),
			((0.5, -0.5, 0), (1.0, 0.0)),
			((0, 0.5, 0), (0.5, 1.0)),
		)

		proj = matrix44.create_perspective_projection_matrix(
			90, 4.0 / 3.0, 0.01, 100.0)

		mv = matrix44.multiply(
			matrix44.create_from_translation((sin(t * 10), 0.0, -3.0 * abs(sin(t * 30)))),
			matrix44.create_from_z_rotation(t * 50),
		)
 
		mvp = matrix44.multiply(mv, proj)

		self.model_view_proj.set_value(mvp)

		for pass_ in self.technique.passes:
			pass_.begin()

			gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

			gl.glBegin(gl.GL_TRIANGLES)

			for position, texcoord in data:
				gl.glMultiTexCoord2fv(gl.GL_TEXTURE0, texcoord)
				gl.glVertex3fv(position)

			gl.glEnd()

			pass_.end()
Esempio n. 9
0
 def test_create_from_translation( self ):
     result = matrix44.create_from_translation([2.,3.,4.])
     expected = np.eye(4)
     expected[3,:3] = [2.,3.,4.]
     np.testing.assert_almost_equal(result, expected, decimal=5)
Esempio n. 10
0
 def test_apply_to_vector_with_translation(self):
     mat = matrix44.create_from_translation([2.,3.,4.])
     result = matrix44.apply_to_vector(mat, [1.,1.,1.])
     np.testing.assert_almost_equal(result, [3.,4.,5.], decimal=5)
Esempio n. 11
0
	def _CreateMatrix(self):
		self.worldMatrix = matrix44.multiply(
								matrix44.create_from_translation(self.position), 
								matrix44.create_from_quaternion(self.rotation)
							)