Esempio n. 1
0
    def construct_transform_matrix(self):
        """Constructs transformation matrix

            Arguments:
                shape {Shape} -- Shape to change
                mat {Mat3d} -- Matrix class related to given Shape
        """
        self.transform_matrix = Mat3d().reset_transform_matrix()
        for i in range(len(self.transform_operations_stack)):
            self.transform_matrix = Mat3d.multiply_matrices_for_transform(
                self.transform_matrix, self.pop_from_stack())
Esempio n. 2
0
    def rotate(self, yaw_angle, pitch_angle):
        cam_focus_vector = Vec3d(self.eye.x - self.center.x,
                                 self.eye.y - self.center.y,
                                 self.eye.z - self.center.z, 1.0)
        ymat = Mat3d()
        pmat = Mat3d()
        ymat.define_rotation_matrix(yaw_angle, "y")
        pmat.define_rotation_matrix(pitch_angle, "x")
        ymat.matmul(pmat)
        cam_focus_vector = ymat.vecmul(cam_focus_vector)
        cam_focus_vector += self.center

        self.eye = cam_focus_vector.copy()
        self.compute_camera_space()
Esempio n. 3
0
    def get_matrix(self, points):
        m = []
        for i in points:
            k = [i.x, i.y, i.z, i.w]

            m.append(k)
        return Mat3d(m).transpose()
Esempio n. 4
0
 def __init__(self):
     self.vertices = []
     self.faces = []
     self.subdivision_level = 0
     self.original_vertices = []
     self.transformation_matrix_stack = []
     self.final_transformation_matrix = Mat3d()
Esempio n. 5
0
 def __init__(self):
     """Constructor of class
     """
     self.vertices_list = Set([])
     self.faces = []
     self.subdivision_level = 0
     self.transform_operations_stack = []
     self.transform_matrix = Mat3d().transform_matrix
Esempio n. 6
0
    def translate(self, x, y, z):
        m = Mat3d([])
        new_matrix = m.get_translation_matrix(x, y, z).matrixMul(self.matrix)

        self.set_points(new_matrix)

        self.transformations.append(Transformations.Translate)
        self.transformation_matrix = m.get_translation_matrix(
            x, y, z).matrixMul(self.transformation_matrix)
Esempio n. 7
0
    def rotate_y(self, Q):
        m = Mat3d([])
        new_matrix = m.get_rotation_matrix_y_axis(Q).matrixMul(self.matrix)

        self.set_points(new_matrix)

        self.transformations.append(Transformations.Rotate_Y)
        self.transformation_matrix = m.get_rotation_matrix_y_axis(Q).matrixMul(
            self.transformation_matrix)
Esempio n. 8
0
    def scale(self, scalar):
        m = Mat3d([])
        new_matrix = m.get_scale_matrix(scalar).matrixMul(self.matrix)

        self.set_points(new_matrix)

        self.transformations.append(Transformations.Scale)

        self.transformation_matrix = m.get_scale_matrix(scalar).matrixMul(
            self.transformation_matrix)
Esempio n. 9
0
def transform(shape, matrix):
    """Applies transformation to the vertices
        
        Arguments:
            Shape {Shape} -- Shape to change
            matrix {List} -- Transform matrix
    """
    for j in range(len(shape.vertices_list)):
        temp = Mat3d.multipy_matrices(matrix, shape.vertices_list[j].homo_vector)
        shape.vertices_list[j] = Vec3d(temp[0][0], temp[1][0], temp[2][0], temp[3][0])
Esempio n. 10
0
def keyPressed(*args):
    # If escape is pressed, kill everything.
    if args[0] == ESCAPE:
        sys.exit()
    # If plus is pressed, adds subdivison
    elif args[0] == ADD:
        shape.add_subdivison()
    # If minus is pressed, removes subdivison
    elif args[0] == SUBS:
        shape.remove_subdivison()
    # If left arrow is pressed, rotate left
    elif args[0] == GLUT_KEY_LEFT:
        shape.push_to_stack(Mat3d.rotation(0, -10, 0))
        shape.construct_transform_matrix()
        shape.transform()
    # If right arrow is pressed, rotate right
    elif args[0] == GLUT_KEY_RIGHT:
        shape.push_to_stack(Mat3d.rotation(0, 10, 0))
        shape.construct_transform_matrix()
        shape.transform()
Esempio n. 11
0
    def __init__(self, type, vertices, faces):
        self.type = type
        self.vertices = vertices
        self.faces = faces
        self.colors = []
        self.create_colors()

        self.operation = Mat3d()
        self.wireOnShaded = False
        self.wireWidth = 2
        self.wireOnShadedColor = HCoordinates(1.0, 1.0, 1.0, 1.0)
Esempio n. 12
0
    def transform(self):
        """Applies transformation to the vertices

            Arguments:
                Shape {Shape} -- Shape to change
                matrix {List} -- Transform matrix
        """
        for vertex in self.vertices_list:
            temp = Mat3d.multiply_matrices(self.transform_matrix,
                                           vertex.homo_vector)
            vertex.x = temp[0][0]
            vertex.y = temp[1][0]
            vertex.z = temp[2][0]
            vertex.w = temp[3][0]
Esempio n. 13
0
    def __init__(self, vertices, faces):
        self._vertices = vertices
        self._faces = faces
        self._edges = []
        self.create_edge_list()

        self._subdivision_history = []
        self._subdivision_level = 0
        self._size = 0

        self._subdivider = None

        self.transformation_matrix_stack = []
        self.final_transformation_matrix = Mat3d()
Esempio n. 14
0
    def rotate_point_y(
            self, Q,
            point):  #rotates according to a point, and according to z axis.
        m = Mat3d([])
        new_matrix = m.get_translation_matrix(-point.x, -point.y,
                                              -point.z).matrixMul(self.matrix)
        new_matrix = m.get_rotation_matrix_y_axis(Q).matrixMul(new_matrix)
        new_matrix = m.get_translation_matrix(point.x, point.y,
                                              point.z).matrixMul(new_matrix)

        self.set_points(new_matrix)

        self.transformations.append(Transformations.Translate)
        self.transformations.append(Transformations.Rotate_Z)
        self.transformations.append(Transformations.Translate)

        self.transformation_matrix = m.get_translation_matrix(
            -point.x, -point.y, -point.z).matrixMul(self.transformation_matrix)
        self.transformation_matrix = m.get_rotation_matrix_y_axis(Q).matrixMul(
            self.transformation_matrix)
        self.transformation_matrix = m.get_translation_matrix(
            point.x, point.y, point.z).matrixMul(self.transformation_matrix)
Esempio n. 15
0
    def __init__(
        self, points
    ):  #points are vec3d(can be in homogeneus coordinates) objects in a list
        self.points = points

        self.matrix = self.get_matrix(points)

        self.center = self.get_center(points)

        mat = Mat3d([])
        self.transformation_matrix = mat.get_scale_matrix(1)

        self.subdivision_level = 0
        self.subdivisionMemory = [
        ]  #for going back and forth in subdivision level (caching the data)
        self.subdivisionMemory.append(points)

        self.meshMemory = []
        #note that we are not forgeting the structure of original object, it stays in our array(the cache)
        self.vertexTable = []
        self.faceTable = []
        self.edgeTable = []
Esempio n. 16
0
def InitGL(Width, Height):  # We call this right after our OpenGL window is created.
    glClearColor(0.0, 0.0, 0.0, 0.0)  # This Will Clear The Background Color To Black
    glClearDepth(1.0)  # Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS)  # The Type Of Depth Test To Do
    glEnable(GL_DEPTH_TEST)  # Enables Depth Testing
    glShadeModel(GL_SMOOTH)  # Enables Smooth Color Shading

    # Add vertices to the object
    triangle.add_vertex(Vec3d(0.0, 1.0, 0.0, 1.0))
    triangle.add_vertex(Vec3d(1.0, -1.0, 0.0, 1.0))
    triangle.add_vertex(Vec3d(-1.0, -1.0, 0.0, 1.0))

    square.add_vertex(Vec3d(-1.0, 1.0, 0.0, 1.0))
    square.add_vertex(Vec3d(1.0, 1.0, 0.0, 1.0))
    square.add_vertex(Vec3d(1.0, -1.0, 0.0, 1.0))
    square.add_vertex(Vec3d(-1.0, -1.0, 0.0, 1.0))

    # Add transformation matrices to the object
    first_matrix = Mat3d()
    second_matrix = Mat3d()
    third_matrix = Mat3d()
    square.add_transformation(first_matrix.define_translation_matrix(-1, -1, 0)) # To rotate around one vertex, we perform transformation as TRT^-1
    square.add_transformation(second_matrix.define_rotation_matrix(5, "z"))
    square.add_transformation(third_matrix.define_translation_matrix(1, 1, 0))

    fourth_matrix = Mat3d()
    fifth_matrix = Mat3d()
    sixth_matrix = Mat3d()
    triangle.add_transformation(fourth_matrix.define_translation_matrix(1, 1, 0))
    triangle.add_transformation(fifth_matrix.define_rotation_matrix(5, "z"))
    triangle.add_transformation(sixth_matrix.define_translation_matrix(-1, -1, 0))

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()  # Reset The Projection Matrix
    # Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0, float(Width) / float(Height), 0.1, 100.0)

    glMatrixMode(GL_MODELVIEW)
Esempio n. 17
0
from vec3d import Vec3d

ESCAPE = '\033'

# Number of the glut window.
window = 0

# Objects
tri_positions = Vec3d(-1.5, 0.0, -6.0, 0.0)
tri_vertices = [
    Vec3d(0.0, 1.0, 0.0, 1.0),
    Vec3d(1.0, -1.0, 0.0, 1.0),
    Vec3d(-1.0, -1.0, 0.0, 1.0)
]
tri_operations = [
    Mat3d.transformation_matrix(Vec3d(-1.0, 0.0, 0.0, 0.0)),
    Mat3d.rotate_y(0.05),
    Mat3d.transformation_matrix(Vec3d(1.0, 0.0, 0.0, 0.0))
]
triangle = Object(tri_vertices, tri_positions, tri_operations)

squ_positions = Vec3d(3.0, 0.0, 0.0, 0.0)
squ_vertices = [
    Vec3d(-1.0, 1.0, 0.0, 0.0),
    Vec3d(1.0, 1.0, 0.0, 0.0),
    Vec3d(1.0, -1.0, 0.0, 0.0),
    Vec3d(-1.0, -1.0, 0.0, 0.0)
]
squ_operation_matrices = [Mat3d.rotate_x(0.1)]
square = Object(squ_vertices, squ_positions, squ_operation_matrices)
Esempio n. 18
0
 def __init__(self):
     self.vertices = []
     self.original_vertices = []
     self.transformation_matrix_stack = []
     self.final_transformation_matrix = Mat3d()
Esempio n. 19
0
# Some api in the chain is translating the keystrokes to this octal string
# so instead of saying: ESCAPE = 27, we use the following.
ESCAPE = '\033'

# Number of the glut window.
window = 0

# Shapes
triangle = Shape(Vec3d(-1.5, 0.0, -6.0, 1.0), # Position
[ # Vertices
	Vec3d(0.0, 1.0, 0.0, 1.0), 			# Top
	Vec3d(1.0, -1.0, 0.0, 1.0),			# Bottom Right
	Vec3d(-1.0, -1.0, 0.0, 1.0),		# Bottom Left
], [ # matrix stack
	Mat3d.translationMatrix(0.0, -1.0, 0.0),
	Mat3d.rotationZMatrix(0.02),
	Mat3d.translationMatrix(0.0, 1.0, 0.0)
]) 

square = Shape(Vec3d(1.5, 0.0, -6.0, 1.0), # Position
[ # Vertices
	Vec3d(-1.0, 1.0, 0.0, 1.0),			# Top Left
	Vec3d(1.0, 1.0, 0.0, 1.0),			# Top Right
	Vec3d(1.0, -1.0, 0.0, 1.0),			# Bottom Right
	Vec3d(-1.0, -1.0, 0.0, 1.0)			# Bottom Left
], [ # matrix stack
	Mat3d.rotationZMatrix(0.1)
])

# A general OpenGL initialization function.  Sets all of the initial parameters. 
Esempio n. 20
0
 def __init__(self):
     """Consructor of class
     """
     self.vertices_list = []
     self.transform_operations_stack = []
     self.transform_matrix = Mat3d().transform_matrix
Esempio n. 21
0
def main():
    # Defines global elements to use 
    global window
    global triangle_shape
    global square_shape
    global mat3d_triangle
    global mat3d_square
    global transform_matrix_triangle
    global transform_matrix_square

    triangle_shape = Shape()
    square_shape = Shape()
    mat3d_triangle = Mat3d()
    mat3d_square = Mat3d()

    
    # For now we just pass glutInit one empty argument. I wasn't sure what should or could be passed in (tuple, list, ...)
    # Once I find out the right stuff based on reading the PyOpenGL source, I'll address this.
    glutInit(sys.argv)

    # Select type of Display mode:
    #  Double buffer
    #  RGBA color
    # Alpha components supported
    # Depth buffer
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)

    # get a 640 x 480 window
    glutInitWindowSize(640, 480)

    # the window starts at the upper left corner of the screen
    glutInitWindowPosition(0, 0)

    # Okay, like the C version we retain the window id to use when closing, but for those of you new
    # to Python (like myself), remember this assignment would make the variable local and not global
    # if it weren't for the global declaration at the start of main.
    window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")

    # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
    # set the function pointer and invoke a function to actually register the callback, otherwise it
    # would be very much like the C version of the code.
    glutDisplayFunc(DrawGLScene)

    # Uncomment this line to get full screen.
    # glutFullScreen()

    # When we are doing nothing, redraw the scene.
    glutIdleFunc(DrawGLScene)

    # Register the function called when our window is resized.
    glutReshapeFunc(ReSizeGLScene)

    # Register the function called when the keyboard is pressed.
    glutKeyboardFunc(keyPressed)

    # Initialize our window.
    InitGL(640, 480)

    # Below, we construct transformation matrices for each Shape
    transform_matrix_triangle = construct_transform_matrix(triangle_shape, mat3d_triangle)
    transform_matrix_square = construct_transform_matrix(square_shape, mat3d_square)

    # Start Event Processing Engine
    glutMainLoop()
Esempio n. 22
0
    def apply_transformation_matrix(self):
        m = Mat3d([])
        new_matrix = self.transformation_matrix.matrixMul(self.matrix)

        self.set_points(new_matrix)