Beispiel #1
0
    def test_create_orthogonal_projection_matrix_vector4(self):
        def apply_test(m, point, inside):
            p = matrix44.apply_to_vector(m, point)
            if p[3] == 0.:
                self.assertFalse(inside)

            # the values are now in clip space from (-1.,-1.,-1.) -> (1.,1.,1.)
            # to be inside = all(-1. < value < 1.)
            self.assertTrue(inside == (np.amax(np.absolute(p[:3])) <= 1.),
                            (inside, point, p))

        m = matrix44.create_orthogonal_projection_matrix(
            -1., 1., -1., 1., 1., 10.)

        # +Z
        apply_test(m, np.array((0., 0., 0., 1.)), False)
        apply_test(m, np.array((0., 0., 1., 1.)), False)
        # -Z but outside near, far
        apply_test(m, np.array((0., 0., -.5, 1.)), False)
        apply_test(m, np.array((0., 0., -11., 1.)), False)
        apply_test(m, np.array((0., 0., 1., 1.)), False)
        # Valid
        apply_test(m, np.array((0., 0., -10., 1.)), True)
        apply_test(m, np.array((0., 0., -1., 1.)), True)
        apply_test(m, np.array((0., 0., -2., 1.)), True)
        apply_test(m, np.array((0., 0., -9., 1.)), True)
        apply_test(m, np.array((-1., -1., -1., 1.)), True)
        apply_test(m, np.array((-1., -1., -10., 1.)), True)
        apply_test(m, np.array((1., 1., -1., 1.)), True)
        apply_test(m, np.array((1., 1., -10., 1.)), True)
        # Outside left, right, top, bottom
        apply_test(m, np.array((1.1, 1.1, -1., 1.)), False)
        apply_test(m, np.array((-1.1, -1.1, -1., 1.)), False)
        apply_test(m, np.array((1.1, 1.1, -10., 1.)), False)
        apply_test(m, np.array((-1.1, -1.1, -10., 1.)), False)
Beispiel #2
0
    def test_create_orthogonal_projection_matrix_vector4(self):
        def apply_test(m, point, inside):
            p = matrix44.apply_to_vector(m, point)
            if p[3] == 0.:
                self.assertFalse(inside)

            # the values are now in clip space from (-1.,-1.,-1.) -> (1.,1.,1.)
            # to be inside = all(-1. < value < 1.)
            self.assertTrue(inside == (np.amax(np.absolute(p[:3])) <= 1.), (inside, point, p))

        m = matrix44.create_orthogonal_projection_matrix(-1., 1., -1., 1., 1., 10.)

        # +Z
        apply_test(m, np.array((0.,0.,0.,1.)), False)
        apply_test(m, np.array((0.,0.,1.,1.)), False)
        # -Z but outside near, far
        apply_test(m, np.array((0.,0.,-.5,1.)), False)
        apply_test(m, np.array((0.,0.,-11.,1.)), False)
        apply_test(m, np.array((0.,0.,1.,1.)), False)
        # Valid
        apply_test(m, np.array((0.,0.,-10.,1.)), True)
        apply_test(m, np.array((0.,0.,-1.,1.)), True)
        apply_test(m, np.array((0.,0.,-2.,1.)), True)
        apply_test(m, np.array((0.,0.,-9.,1.)), True)
        apply_test(m, np.array((-1.,-1.,-1.,1.)), True)
        apply_test(m, np.array((-1.,-1.,-10.,1.)), True)
        apply_test(m, np.array((1.,1.,-1.,1.)), True)
        apply_test(m, np.array((1.,1.,-10.,1.)), True)
        # Outside left, right, top, bottom
        apply_test(m, np.array((1.1,1.1,-1.,1.)), False)
        apply_test(m, np.array((-1.1,-1.1,-1.,1.)), False)
        apply_test(m, np.array((1.1,1.1,-10.,1.)), False)
        apply_test(m, np.array((-1.1,-1.1,-10.,1.)), False)
Beispiel #3
0
 def projection_matrix(self, ptype=ProjectionType.perspective):
     if (ptype == ProjectionType.perspective):
         # Return the current perspective projection Matrix
         return matrix44.create_perspective_projection_matrix(
             self._fov, self._aspect, self._zNear, self._zFar)
     else:
         # Return the current orthogonal projection Matrix
         return matrix44.create_orthogonal_projection_matrix(
             self._rect[0], self._rect[1], self._rect[2], self._rect[3],
             self._zNear, self._zFar)
Beispiel #4
0
 def aspect_ratio(self, value):
     self._aspect_ratio = value
     self._projection_bytes = matrix44.create_orthogonal_projection_matrix(
         -self.aspect_ratio,  # left
         self.aspect_ratio,  # right
         -1.0,  # bottom
         1.0,  # top
         -100.0,  # near
         100.0,  # far
         dtype=numpy.float32,
     ).tobytes()
Beispiel #5
0
    def draw(self, pos, length=-1, size=24.0):
        # Calculate ortho projection based on viewport
        vp = self.ctx.fbo.viewport
        w, h = vp[2] - vp[0], vp[3] - vp[1]
        projection = matrix44.create_orthogonal_projection_matrix(
            0,  # left
            w,  # right
            0,  # bottom
            h,  # top
            1.0,  # near
            -1.0,  # far
            dtype=numpy.float32,
        )

        self._texture.use(location=0)
        self._program["m_proj"].write(projection)
        self._program["text_pos"].value = pos
        self._program["font_texture"].value = 0
        self._program[
            "char_size"].value = self._meta.char_aspect_wh * size, size

        self._vao.render(self._program, instances=len(self._text))