Exemplo n.º 1
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
Exemplo n.º 2
0
 def test_create_from_z_rotation(self):
     mat = matrix44.create_from_z_rotation(np.pi / 2.)
     self.assertTrue(
         np.allclose(np.dot([1., 0., 0., 1.], mat), [0., -1., 0., 1.]))
     self.assertTrue(
         np.allclose(np.dot([0., 1., 0., 1.], mat), [1., 0., 0., 1.]))
     self.assertTrue(
         np.allclose(np.dot([0., 0., 1., 1.], mat), [0., 0., 1., 1.]))
Exemplo n.º 3
0
        def rotated_z():
            quat = quaternion.create_from_z_rotation( math.pi )
            result = matrix44.create_from_quaternion( quat )

            expected = matrix44.create_from_z_rotation( math.pi )

            self.assertTrue(
                numpy.allclose( result, expected ),
                "Matrix44 from quaternion incorrect with PI rotation about Z"
                )
Exemplo n.º 4
0
        def rotated_z():
            mat = matrix44.create_from_z_rotation( math.pi )
            vec = vector3.unit.x

            result = matrix44.apply_to_vector( mat, vec )

            expected = -vec

            self.assertTrue(
                numpy.allclose( result, expected ),
                "Matrix44 apply_to_vector incorrect with rotation about Y"
                )
Exemplo n.º 5
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
Exemplo n.º 6
0
    def view_matrix(camera):
        translation_matrix = np.matrix([[1, 0, 0, -camera.position[0]],
                                        [0, 1, 0, -camera.position[1]],
                                        [0, 0, 1, -camera.position[2]],
                                        [0, 0, 0, 1]])
        rotation_matrix_x = matrix44.create_from_x_rotation(
            np.radians(camera.pitch))
        rotation_matrix_y = matrix44.create_from_y_rotation(
            np.radians(camera.yaw))
        rotation_matrix_z = matrix44.create_from_z_rotation(
            np.radians(camera.roll))

        rot = np.dot(rotation_matrix_x,
                     np.dot(rotation_matrix_y, rotation_matrix_z))
        txy = matrix44.multiply(rot, translation_matrix)
        view_matrix = matrix44.multiply(txy, rotation_matrix_z)

        return view_matrix
Exemplo n.º 7
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
Exemplo 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()
Exemplo n.º 9
0
import os

import numpy as np
from renderer import MGLRenderer
from pyrr import matrix44

datdir = os.path.expanduser('~/data/ax3d/tmp/lowpoly_models_final')
objdir = os.path.join(datdir, 'schoolbus/schoolbus01_49')
objfn = os.path.join(objdir, 'schoolbus01_49.obj')
texfn = os.path.join(objdir, 'SchoolBus01.png')

rdr = MGLRenderer()
rdr.loadobj(objfn, texfn)
# mat = matrix44.create_from_y_rotation(np.pi / 6)
mat = matrix44.create_from_z_rotation(np.pi / 4)
# mat = matrix44.create_identity()
img = rdr.render(mat)
os.makedirs('tmp/get', mode=0o755, exist_ok=True)
img.save('tmp/get/hello.png')
Exemplo n.º 10
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1280, 720
    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)

    #        positions        texture_coords
    cube = [-0.5, -0.5, 0.5, 0.0, 0.0,
            0.5, -0.5, 0.5, 1.0, 0.0,
            0.5, 0.5, 0.5, 1.0, 1.0,
            -0.5, 0.5, 0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, -0.5, -0.5, 1.0, 0.0,
            0.5, 0.5, -0.5, 1.0, 1.0,
            -0.5, 0.5, -0.5, 0.0, 1.0,

            0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, 0.5, -0.5, 1.0, 0.0,
            0.5, 0.5, 0.5, 1.0, 1.0,
            0.5, -0.5, 0.5, 0.0, 1.0,

            -0.5, 0.5, -0.5, 0.0, 0.0,
            -0.5, -0.5, -0.5, 1.0, 0.0,
            -0.5, -0.5, 0.5, 1.0, 1.0,
            -0.5, 0.5, 0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, -0.5, -0.5, 1.0, 0.0,
            0.5, -0.5, 0.5, 1.0, 1.0,
            -0.5, -0.5, 0.5, 0.0, 1.0,

            0.5, 0.5, -0.5, 0.0, 0.0,
            -0.5, 0.5, -0.5, 1.0, 0.0,
            -0.5, 0.5, 0.5, 1.0, 1.0,
            0.5, 0.5, 0.5, 0.0, 1.0]

    cube = numpy.array(cube, dtype=numpy.float32)

    indices = [0, 1, 2, 2, 3, 0,
               4, 5, 6, 6, 7, 4,
               8, 9, 10, 10, 11, 8,
               12, 13, 14, 14, 15, 12,
               16, 17, 18, 18, 19, 16,
               20, 21, 22, 22, 23, 20]

    indices = numpy.array(indices, dtype=numpy.uint32)

    vertex_shader = """
    #version 330
    in layout(location = 0) vec3 position;
    in layout(location = 1) vec2 texture_cords;
    uniform mat4 vp;
    uniform mat4 model;
    out vec2 textures;
    void main()
    {
        gl_Position =  vp * model * vec4(position, 1.0f);
        textures = texture_cords;
    }
    """

    fragment_shader = """
    #version 330
    in vec2 textures;
    out vec4 color;
    uniform sampler2D tex_sampler;
    void main()
    {
        color = texture(tex_sampler, textures);
    }
    """
    shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
                                              OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))

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

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW)

    # position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    crate = TextureLoader.load_texture("resources/images/planks_brown_10_diff_1k.jpg")
    metal = TextureLoader.load_texture("resources/images/green_metal_rust_diff_1k.jpg")
    brick = TextureLoader.load_texture("resources/images/castle_brick_07_diff_1k.jpg")

    glUseProgram(shader)

    glClearColor(0.5, 0.1, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    view = matrix44.create_from_translation(Vector3([0.0, 0.0, -4.0]))
    projection = matrix44.create_perspective_projection_matrix(45.0, aspect_ratio, 0.1, 100.0)

    vp = matrix44.multiply(view, projection)

    vp_loc = glGetUniformLocation(shader, "vp")
    model_loc = glGetUniformLocation(shader, "model")

    # cube_positions = [(1.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -1.2, -2.5), (-8.8, -2.0, -12.3)]
    cube_positions = [(1.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -1.2, -2.5), (-8.8, -2.0, -12.3), (-2.0, 2.0, -5.5),
                      (-4.0, 2.0, -3.0)]
    # cube_positions = [(-1.5, 1.0, -0.5), (0.0, 1.0, -0.5), (1.5, 1.0, -0.5), (-1.5, -1.0, -0.5), (0.0, -1.0, -0.5), (1.5, -1.0, -0.5)]

    glUniformMatrix4fv(vp_loc, 1, GL_FALSE, vp)

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

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        time = glfw.get_time()

        rot_x = matrix44.create_from_x_rotation(sin(time) * 2)
        rot_y = matrix44.create_from_y_rotation(time * 0.5)
        rot_z = matrix44.create_from_z_rotation(time)

        for i in range(len(cube_positions)):
            model = matrix44.create_from_translation(cube_positions[i])
            if i < 2:
                glBindTexture(GL_TEXTURE_2D, crate)
                rotX = matrix44.multiply(rot_x, model)
                glUniformMatrix4fv(model_loc, 1, GL_FALSE, rotX)
            elif i == 2 or i == 3:
                glBindTexture(GL_TEXTURE_2D, metal)
                rotY = matrix44.multiply(rot_y, model)
                glUniformMatrix4fv(model_loc, 1, GL_FALSE, rotY)
            else:
                glBindTexture(GL_TEXTURE_2D, brick)
                rotZ = matrix44.multiply(rot_z, model)
                glUniformMatrix4fv(model_loc, 1, GL_FALSE, rotZ)

            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        glfw.swap_buffers(window)

    glfw.terminate()
Exemplo n.º 11
0
 def test_create_from_z_rotation(self):
     mat = matrix44.create_from_z_rotation(np.pi / 2.)
     self.assertTrue(np.allclose(np.dot([1.,0.,0.,1.], mat), [0.,-1.,0.,1.]))
     self.assertTrue(np.allclose(np.dot([0.,1.,0.,1.], mat), [1.,0.,0.,1.]))
     self.assertTrue(np.allclose(np.dot([0.,0.,1.,1.], mat), [0.,0.,1.,1.]))
Exemplo n.º 12
0
 def test_apply_to_vector_z_rotation(self):
     mat = matrix44.create_from_z_rotation(np.pi)
     result = matrix44.apply_to_vector(mat, [1.,0.,0.])
     np.testing.assert_almost_equal(result, [-1.,0.,0.], decimal=5)
Exemplo n.º 13
0
 def test_apply_to_vector_z_rotation(self):
     mat = matrix44.create_from_z_rotation(np.pi)
     result = matrix44.apply_to_vector(mat, [1., 0., 0.])
     np.testing.assert_almost_equal(result, [-1., 0., 0.], decimal=5)
Exemplo n.º 14
0
 def rotate(self, xrot, yrot, zrot):
     xrot_mat = matrix44.create_from_x_rotation(radians(xrot))
     yrot_mat = matrix44.create_from_y_rotation(radians(yrot))
     zrot_mat = matrix44.create_from_z_rotation(radians(zrot))
     self.rotation_matrix = xrot_mat * yrot_mat * zrot_mat