示例#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)
示例#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
示例#3
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
示例#4
0
    def __init__(self, data):
        self.children = data.get('children')
        self.matrix = data.get('matrix')
        self.mesh = data.get('mesh')
        self.camera = data.get('camera')

        self.translation = data.get('translation')
        self.rotation = data.get('rotation')
        self.scale = data.get('scale')

        if self.matrix is None:
            self.matrix = matrix44.create_identity()

        if self.translation is not None:
            self.matrix = matrix44.create_from_translation(self.translation)

        if self.rotation is not None:
            quat = quaternion.create(self.rotation[0], self.rotation[1],
                                     self.rotation[2], self.rotation[3])
            mat = matrix44.create_from_quaternion(quat)
            self.matrix = matrix44.multiply(mat, self.matrix)

        if self.scale is not None:
            self.matrix = matrix44.multiply(
                matrix44.create_from_scale(self.scale), self.matrix)
示例#5
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)
示例#6
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)
示例#7
0
    def test_create_from_scale( self ):
        scale = numpy.array( [ 2.0, 3.0, 4.0 ] )

        mat = matrix44.create_from_scale( scale )

        result = mat.diagonal()[ :-1 ]

        expected = scale

        # extract the diagonal scale and ignore the last value
        self.assertTrue(
            numpy.array_equal( result, expected ),
            "Matrix44 scale not set properly"
            )
示例#8
0
    def model_matrix(terrain):
        translation_matrix = np.matrix([[1, 0, 0, terrain.x], [0, 1, 0, 0],
                                        [0, 0, 1, terrain.z], [0, 0, 0, 1]])
        rotation_matrix_x = matrix44.create_from_x_rotation(np.radians(0))
        rotation_matrix_y = matrix44.create_from_y_rotation(np.radians(0))
        rotation_matrix_z = matrix44.create_from_z_rotation(np.radians(0))
        scale_matrix = matrix44.create_from_scale([1, 1, 1])

        tx = matrix44.multiply(translation_matrix, rotation_matrix_x)
        txy = matrix44.multiply(tx, rotation_matrix_y)
        tr = matrix44.multiply(txy, rotation_matrix_z)
        model_matrix = matrix44.multiply(tr, scale_matrix)

        return model_matrix
示例#9
0
    def model_matrix(thing):
        translation_matrix = np.matrix([[1, 0, 0, thing.position[0]],
                                        [0, 1, 0, thing.position[1]],
                                        [0, 0, 1, thing.position[2]],
                                        [0, 0, 0, 1]])
        rotation_matrix_x = matrix44.create_from_x_rotation(
            np.radians(thing.rotX))
        rotation_matrix_y = matrix44.create_from_y_rotation(
            np.radians(thing.rotY))
        rotation_matrix_z = matrix44.create_from_z_rotation(
            np.radians(thing.rotZ))
        scale_matrix = matrix44.create_from_scale(thing.scale)

        tx = matrix44.multiply(translation_matrix, rotation_matrix_x)
        txy = matrix44.multiply(tx, rotation_matrix_y)
        tr = matrix44.multiply(txy, rotation_matrix_z)
        model_matrix = matrix44.multiply(tr, scale_matrix)

        return model_matrix
示例#10
0
 def getModelMatrix(self):
     return matrix44.create_from_translation(self.position) * matrix44.create_from_scale(np.array(self.radius))
示例#11
0
 def test_create_from_scale( self ):
     result = matrix44.create_from_scale([2.,3.,4.])
     np.testing.assert_almost_equal(result.diagonal()[:-1], [2.,3.,4.], decimal=5)
示例#12
0
 def test_create_from_scale(self):
     result = matrix44.create_from_scale([2., 3., 4.])
     np.testing.assert_almost_equal(result.diagonal()[:-1], [2., 3., 4.],
                                    decimal=5)
示例#13
0
def main():

    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 800, 600
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)

    obj = ObjLoader()
    obj.load_model("objects/sphere.obj")

    texture_offset = len(obj.vertex_index) * 12
    normal_offset = (texture_offset + len(obj.texture_index) * 8)

    shader = ShaderLoader.compile_shader("shaders/main_vert.vs",
                                         "shaders/main_frag.fs")

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model),
                 obj.model, GL_STATIC_DRAW)

    #positions
    position = glGetAttribLocation(shader, "position")
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE,
                          obj.model.itemsize * 3, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)
    #textures
    texCoords = glGetAttribLocation(shader, "inTexCoords")
    glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE,
                          obj.model.itemsize * 2,
                          ctypes.c_void_p(texture_offset))
    glEnableVertexAttribArray(texCoords)
    #normals
    normals = glGetAttribLocation(shader, "vertNormal")
    glVertexAttribPointer(normals, 3, GL_FLOAT,
                          GL_FALSE, obj.model.itemsize * 3,
                          ctypes.c_void_p(normal_offset))
    glEnableVertexAttribArray(normals)

    for planet in planets:
        texture = TextureLoader.load_texture(planets[planet]['image_path'])
        planets[planet]['texture'] = texture

        #initial position
        if planet == 'sun' or planet == 'stars':
            distance_from_sun = planets[planet]['distance_from_sun']
            planets[planet]['initial_position'] = [
                distance_from_sun, 0.0, distance_from_sun
            ]
        else:
            distance_from_sun = 30.0 + planets[planet]['distance_from_sun']
            x = round(random.uniform(-distance_from_sun, distance_from_sun), 3)
            z = round(math.sqrt((distance_from_sun**2) - (x**2)), 3)
            planets[planet]['initial_position'] = [x, 0.0, z]

    glEnable(GL_TEXTURE_2D)

    glUseProgram(shader)

    glClearColor(0.0, 0.2, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    projection = pyrr.matrix44.create_perspective_projection_matrix(
        65.0, w_width / w_height, 0.1, 10000.0)
    scale = pyrr.matrix44.create_from_scale(pyrr.Vector3([0.1, 0.1, 0.1]))

    view_loc = glGetUniformLocation(shader, "view")
    proj_loc = glGetUniformLocation(shader, "projection")
    model_loc = glGetUniformLocation(shader, "model")
    normal_loc = glGetUniformLocation(shader, "normalMatrix")
    scale_loc = glGetUniformLocation(shader, "scale")
    scale_planet_loc = glGetUniformLocation(shader, "scale_planet")

    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
    glUniformMatrix4fv(scale_loc, 1, GL_FALSE, scale)

    Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient")
    Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient")
    Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse")
    Light_specular_loc = glGetUniformLocation(shader, "Light_specular")
    Light_location_loc = glGetUniformLocation(shader, "Light_location")
    Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient")
    Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse")
    Material_specular_loc = glGetUniformLocation(shader, "Material_specular")
    Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess")

    glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1)
    glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0)
    glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0)
    glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0)
    glUniform3f(Light_location_loc, 0, 0, 0)

    # *******************************************************************************************

    # obj2 = ObjLoader()
    # obj2.load_model("objects/cruiser.obj")
    #
    # shader2 = ShaderLoader.compile_shader("shaders/vert.vs", "shaders/frag.fs")
    #
    # VBO2 = glGenBuffers(1)
    # glBindBuffer(GL_ARRAY_BUFFER, VBO2)
    # glBufferData(GL_ARRAY_BUFFER, obj2.model.itemsize * len(obj2.model), obj2.model, GL_STATIC_DRAW)
    #
    # # positions
    # position2 = glGetAttribLocation(shader, "position")
    # glVertexAttribPointer(position2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(0))
    # glEnableVertexAttribArray(position2)
    # # textures
    # texCoords2 = glGetAttribLocation(shader2, "inTexCoords")
    # glVertexAttribPointer(texCoords2, 2, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 2, ctypes.c_void_p(texture_offset))
    # glEnableVertexAttribArray(texCoords2)
    # # normals
    # normals2 = glGetAttribLocation(shader2, "vertNormal")
    # glVertexAttribPointer(normals2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(normal_offset))
    # glEnableVertexAttribArray(normals2)
    #
    #
    # texture2 = TextureLoader.load_texture(planets['cruiser']['image_path'])
    # distance_from_sun = planets['cruiser']['distance_from_sun']
    # planets['cruiser']['initial_position'] = [distance_from_sun, 0.0, distance_from_sun]
    #
    # glUseProgram(shader2)
    #
    #
    # Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient")
    # Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient")
    # Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse")
    # Light_specular_loc = glGetUniformLocation(shader, "Light_specular")
    # Light_location_loc = glGetUniformLocation(shader, "Light_location")
    # Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient")
    # Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse")
    # Material_specular_loc = glGetUniformLocation(shader, "Material_specular")
    # Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess")
    #
    # glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1)
    # glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0)
    # glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0)
    # glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0)
    # glUniform3f(Light_location_loc, 0, 0, 0)
    # glUniform4f(Material_ambient_loc, 0.4, 0.4, 0.4, 1.0)
    # glUniform4f(Material_diffuse_loc, 0.15, 0.15, 0.15, 1.0)
    # glUniform4f(Material_specular_loc, 1.0, 1.0, 1.0, 1.0)
    # glUniform1f(Material_shininess_loc, .95)

    # ******************************************************************************************

    while not glfw.window_should_close(window):
        glfw.poll_events()

        do_movement()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        time = glfw.get_time()

        view = cam.get_view_matrix()
        glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)

        # **********************************planets****************************************
        for planet in planets:
            if planet == 'cruiser':
                glBindTexture(GL_TEXTURE_2D, texture2)
            else:
                glBindTexture(GL_TEXTURE_2D, planets[planet]['texture'])

            revolution_speed = time * planets[planet][
                'revolution_ratio_relative_to_earth']
            rotation_speed = time * planets[planet][
                'rotation_ratio_relative_to_earth']
            # scale planet
            scale_factor = planets[planet]['size_ratio_relative_to_earth']
            scale_planet = matrix44.create_from_scale(
                pyrr.Vector3([scale_factor, scale_factor, scale_factor]))
            glUniformMatrix4fv(scale_planet_loc, 1, GL_FALSE, scale_planet)

            # translation
            model = matrix44.create_from_translation(
                pyrr.Vector3(planets[planet]['initial_position']))
            revolution = matrix44.create_from_y_rotation(revolution_speed)
            rotation = matrix44.create_from_y_rotation(rotation_speed)
            # revolution about z axis
            model = matrix44.multiply(model, revolution)
            # rotation about own axis
            model = matrix44.multiply(rotation, model)

            glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)

            # ----create normalMatrix--
            modelView = numpy.matmul(view, model)
            # modelView = numpy.matmul(modelView, scale_planet)
            # modelView = numpy.matmul(modelView, scale)
            modelView33 = modelView[0:-1, 0:-1]
            normalMatrix = numpy.transpose(numpy.linalg.inv(modelView33))
            # -----------------
            glUniformMatrix3fv(normal_loc, 1, GL_FALSE, normalMatrix)

            a, b, c, d = planets[planet]['Material_ambient']
            glUniform4f(Material_ambient_loc, a, b, c, d)
            a, b, c, d = planets[planet]['Material_diffuse']
            glUniform4f(Material_diffuse_loc, a, b, c, d)
            a, b, c, d = planets[planet]['Material_specular']
            glUniform4f(Material_specular_loc, a, b, c, d)
            s = planets[planet]['Material_shininess'][0]
            glUniform1f(Material_shininess_loc, s)

            if planet == 'cruiser':
                glDrawArrays(GL_TRIANGLES, 0, len(obj2.vertex_index))
            else:
                glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index))

        # *******************************************************************************

        glfw.swap_buffers(window)

    glfw.terminate()
示例#14
0
 def scale(self, scale):
     if type(scale) == Matrix44:
         self.scale_matrix = scale
     else:
         self.scale_matrix = matrix44.create_from_scale(Vector3(scale))
示例#15
0
 def scale(self, scale):
     scale = mat4.create_from_scale(scale, dtype='f')
     self.model = mat4.multiply(self.model, scale)
示例#16
0
atari_scale = pyrr.matrix44.create_from_scale(
    pyrr.Vector3([float(atari_width),
                  float(atari_height), 1.0]))
atari_screen1_model = pyrr.matrix44.multiply(atari_scale, translation)

zoom = 2.2
atari_screen2_scale = pyrr.matrix44.create_from_scale(
    pyrr.Vector3([float(atari_width) * zoom,
                  float(atari_height) * zoom, 1.0]))
atari_screen2_translation = pyrr.matrix44.create_from_translation(
    pyrr.Vector3([0, 0, -3]))
atari_screen_2 = pyrr.matrix44.multiply(atari_screen2_scale,
                                        atari_screen2_translation)

translation_cut = matrix44.create_from_translation(Vector3([0.0, -34.0, 0.0]))
scale_cut = matrix44.create_from_scale(Vector3([160, 210, 1.0]))
cutout_model = pyrr.matrix44.multiply(scale_cut, translation_cut)

minime_translate = matrix44.create_from_translation(
    Vector3([0.0, -34.0 / 210.0, 0.0]))
minime_scale = matrix44.create_from_scale(
    Vector3([32.0, (210.0 / (168.0 - 34.0)) * 32.0, 1.0]))
minime_model = matrix44.multiply(minime_translate, minime_scale)
minime_inv_model = matrix44.inverse(minime_model)
transporter_scale = matrix44.create_from_scale(Vector3([32.0, 32.0, 1.0]))

bbox_scale_factor = 16.0 / 0.9
bbox1_model = pyrr.matrix44.create_from_scale(
    pyrr.Vector3([bbox_scale_factor, bbox_scale_factor, 1.0]))

model_loc = glGetUniformLocation(shader, "model")