def init_values(N_particles, N_steps, Ze, Zo, energy, alpha_mass): pos_array = np.full((N_particles, N_steps), fill_value=v3d.vec3d(0, 0, 0)) vel_array = np.full((N_particles, N_steps), fill_value=v3d.vec3d(0, 0, 0)) inter_distance = Ze * Zo * spc.e**2 / (4 * np.pi * spc.epsilon_0) / energy angles_list = [] emittent_radius = 10 * inter_distance pos_x = -100 * inter_distance vel_x = np.sqrt(2 * energy / alpha_mass) tau = inter_distance / vel_x k = 0 #inizializo l'indice del loop while per riampire le posizioni delle particelle np.random.seed( ) #diverso seed per la funzione random.rand() ad ogni run del programma while k < N_particles: pos_y = emittent_radius * (2 * np.random.rand() - 1) pos_z = emittent_radius * (2 * np.random.rand() - 1) if np.sqrt(pos_y**2 + pos_z**2) <= ( emittent_radius): #condizione di raggio cilindrico- pos_array[k, 0] = v3d.vec3d(pos_x, pos_y, pos_z) vel_array[k, 0] = v3d.vec3d(vel_x, 0, 0) k += 1 #incrementa SOLO se la posizione generata random sta dentro il cerchio return pos_array, vel_array, angles_list, inter_distance, tau
def increaseSubdivision(self): if self.totalPolygonCount < 5000: tempPolygons = [] for i in range(len(self.polygons)): polygon = self.polygons[i] (centerX, centerY, centerZ) = self.getCentroidOfPolygon(polygon) for j in range(len(polygon)): tempPolygon = [] pointTwo = polygon[0] if j != len(polygon) - 1: pointTwo = polygon[j + 1] pointZer = polygon[len(polygon) - 1] if j != 0: pointZer = polygon[j - 1] pointOne = polygon[j] midPointPrev = self.getCentroidOfTwoPoints(pointOne, pointZer) midPointNext = self.getCentroidOfTwoPoints(pointOne, pointTwo) verticeOne = vec3d(pointOne.x, pointOne.y, pointOne.z, 1) verticeTwo = vec3d(midPointNext.x, midPointNext.y, midPointNext.z, 1) verticeThr = vec3d(centerX, centerY, centerZ, 1) verticeFou = vec3d(midPointPrev.x, midPointPrev.y, midPointPrev.z, 1) tempPolygon.append(verticeOne) tempPolygon.append(verticeTwo) tempPolygon.append(verticeThr) tempPolygon.append(verticeFou) tempPolygons.append(tempPolygon) self.polygons = tempPolygons self.totalPolygonCount = len(self.polygons) print "Total Polygon: ", self.totalPolygonCount
def calculateVertices(self): self.vertices = [] for i in range(0, self.latitudeCount + 1): latitudeOne = pi * (-0.5 + float(float(i - 1) / float(self.latitudeCount))) z0 = sin(latitudeOne) zr0 = cos(latitudeOne) latitudeTwo = pi * (-0.5 + float(float(i) / float(self.latitudeCount))) z1 = sin(latitudeTwo) zr1 = cos(latitudeTwo) longitudeGroup = [] for j in range(0, self.longitudeCount + 1): longitude = 2 * pi * float(float(j - 1) / float(self.longitudeCount)) x = cos(longitude) y = sin(longitude) vecZ0 = vec3d(x * zr0, y * zr0, z0, 1) vecZ1 = vec3d(x * zr1, y * zr1, z1, 1) verticeGroup = [] verticeGroup.append(vecZ0) verticeGroup.append(vecZ0) verticeGroup.append(vecZ1) verticeGroup.append(vecZ1) longitudeGroup.append(verticeGroup) self.vertices.append(longitudeGroup) return self.vertices
def calculate_right_vec(self): lookvec = vec3d(self.center_position.x-self.look_vec.x,self.center_position.y-self.look_vec.y,self.center_position.z-self.look_vec.z,1) lookvec_mag = lookvec.calculate_magnitute() lookvec = vec3d(lookvec.x/lookvec_mag,lookvec.y/lookvec_mag,lookvec.z/lookvec_mag,1) rightvec = vec3d(self.up_vec.x,self.up_vec.y,self.up_vec.z,1).cross_product(lookvec) rightvec_mag = rightvec.calculate_magnitute() rightvec = vec3d(rightvec.x/rightvec_mag,rightvec.y/rightvec_mag,rightvec.z/rightvec_mag,1) return rightvec
def parse_file(self): tori_file = open(self.file_name, "r") tori_file_lines = tori_file.readlines() tori_face_vertex_indes = [] tori_vertecies = [] shapes = [] for line in tori_file_lines: if (line[0] == 'f'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[3] = line_array[3].rstrip("\n") line_array[0] = int(line_array[0]) line_array[1] = int(line_array[1]) line_array[2] = int(line_array[2]) line_array[3] = int(line_array[3]) tori_face_vertex_indes.append(line_array) xtori_face_vertex_indes = [] xtori_vertecies = [] xshapes = [] for line in tori_file_lines: if (line[0] == 'f'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[3] = line_array[3].rstrip("\n") line_array[0] = int(line_array[0]) - 1 line_array[1] = int(line_array[1]) - 1 line_array[2] = int(line_array[2]) - 1 line_array[3] = int(line_array[3]) - 1 xtori_face_vertex_indes.append(line_array) for line in tori_file_lines: if (line[0] == 'v'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[2] = line_array[2].rstrip("\n") line_array[0] = float(line_array[0]) line_array[1] = float(line_array[1]) line_array[2] = float(line_array[2]) tori_vertecies.append(line_array) tori_file.close() for face_indexes in tori_face_vertex_indes: shape = objct(vec3d(0.0, 0.0, 0.0, 1.0), [], []) for face_index in face_indexes: x = tori_vertecies[face_index - 1][0] y = tori_vertecies[face_index - 1][1] z = tori_vertecies[face_index - 1][2] vec = vec3d(x, y, z, 1) shape.vertices.append(vec) shapes.append(shape) return [shapes, tori_face_vertex_indes, tori_vertecies]
def sub_division_v2(self, numDivision): newShapes = [] for shape in self.shapes: newShape = objct(shape.position, [], shape.matrix_stack) vertices = shape.vertices betweenlen_x = ((vertices[1].x - vertices[0].x)) / numDivision betweenlen_y = ((vertices[1].y - vertices[0].y)) / numDivision betweenlen_z = ((vertices[1].z - vertices[0].z)) / numDivision vertices_edge_1 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[0], vertices[1], numDivision) betweenlen_x = ((vertices[2].x - vertices[1].x)) / numDivision betweenlen_y = ((vertices[2].y - vertices[1].y)) / numDivision betweenlen_z = ((vertices[2].z - vertices[1].z)) / numDivision vertices_edge_2 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[1], vertices[2], numDivision) betweenlen_x = ((vertices[3].x - vertices[2].x)) / numDivision betweenlen_y = ((vertices[3].y - vertices[2].y)) / numDivision betweenlen_z = ((vertices[3].z - vertices[2].z)) / numDivision vertices_edge_3 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[2], vertices[3], numDivision) betweenlen_x = ((vertices[0].x - vertices[3].x)) / numDivision betweenlen_y = ((vertices[0].y - vertices[3].y)) / numDivision betweenlen_z = ((vertices[0].z - vertices[3].z)) / numDivision vertices_edge_4 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[3], vertices[0], numDivision) vertices_between = [] for i in range(len(vertices_edge_2)): betweenlen_x = ((vertices_edge_2[i].x - vertices_edge_4[len(vertices_edge_2) - 1 - i].x)) / numDivision betweenlen_y = ((vertices_edge_2[i].y - vertices_edge_4[len(vertices_edge_2) - 1 - i].y)) / numDivision betweenlen_z = ((vertices_edge_2[i].z - vertices_edge_4[len(vertices_edge_2) - 1 - i].z)) / numDivision vertices_edge_middle = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices_edge_4[i], vertices_edge_2[len(vertices_edge_2) - 1 - i], numDivision) vertices_between.append(vertices_edge_middle) newShapes += self.edges_to_shapes(vertices_between, shape) self.shapes = newShapes
def multiply(self, vec3dd): return vec3d( vec3d(self.matrix[0][0], self.matrix[0][1], self.matrix[0][2], self.matrix[0][3]).dot_product(vec3dd), vec3d(self.matrix[1][0], self.matrix[1][1], self.matrix[1][2], self.matrix[1][3]).dot_product(vec3dd), vec3d(self.matrix[2][0], self.matrix[2][1], self.matrix[2][2], self.matrix[2][3]).dot_product(vec3dd), vec3d(self.matrix[3][0], self.matrix[3][1], self.matrix[3][2], self.matrix[3][3]).dot_product(vec3dd))
def calculate_look_vector_depth(self, distance): look_unit = self.look_vec.calculate_basis_vector() x = self.look_vec.x + look_unit.x * distance y = self.look_vec.y + look_unit.y * distance z = self.look_vec.z + look_unit.z * distance new_look_vec = vec3d(x, y, z, 1) self.location_vec = new_look_vec return new_look_vec
def face_and_vertices_to_shapes(self,tori_face_vertex_indes, vertices): shapes = [] faces = [] for face_indexes in tori_face_vertex_indes: face = [face_indexes[0]+1,face_indexes[1]+1,face_indexes[2]+1,face_indexes[3]+1] faces.append(face) for face_indexes in faces: shape = objct(vec3d(0.0,0.0,0.0, 1.0),[],[]) for face_index in face_indexes: x = vertices[face_index-1][0] y = vertices[face_index-1][1] z = vertices[face_index-1][2] vec = vec3d(x,y,z,1) shape.vertices.append(vec) shapes.append(shape) return shapes
def find_middle_vertices(self,vec_len,vec_vert1,vec_vert2,numD): vertices = [] vertices.append(vec_vert1) for i in range(numD-1): newX=vec_vert1.x+vec_len.x newY=vec_vert1.y+vec_len.y newZ=vec_vert1.z+vec_len.z vec_vert1= vec3d(round(newX,3),round(newY,3),round(newZ,3),1) vertices.append(vec_vert1) vertices.append(vec_vert2) return vertices
def divideXY(self,divideNum,len_x,len_y,initial_point): vertices = [[0 for x in range(divideNum+1)] for y in range(divideNum+1)] initial_x = initial_point.x initial_y = initial_point.y for i in range(divideNum+1): y = initial_y + (len_y*i) for j in range(divideNum+1): x = initial_x + (len_x*j) vec = vec3d(x, y, initial_point.z, 1.0) vertices[i][j] = vec return vertices;
def calculateVertices(self): self.vertices = [] n = float(self.subdivision) points = [] for i in range(int(n) + 1): angle = 2 * pi * (i / n) x = self.radius * cos(angle) y = self.radius * sin(angle) point = (x, y) points.append(point) groupCoverFirst = [] groupCoverFirst.append(vec3d(0, 0, self.height / 2.0, 1)) for (x, y) in points: z = self.height / 2.0 groupCoverFirst.append(vec3d(x, y, z, 1)) self.vertices.append(groupCoverFirst) groupCoverSecond = [] groupCoverSecond.append(vec3d(0, 0, self.height / 2.0, 1)) for (x, y) in points: z = -self.height / 2.0 groupCoverSecond.append(vec3d(x, y, z, 1)) self.vertices.append(groupCoverSecond) groupSide = [] for (x, y) in points: z = self.height / 2.0 groupSide.append(vec3d(x, y, z, 1)) groupSide.append(vec3d(x, y, -z, 1)) self.vertices.append(groupSide) return self.vertices
def multiplication(self, vertex): vec = vec3d(vertex.x, vertex.y, vertex.z, vertex.w) vecRow01 = vec3d(self.matrix[0][0], self.matrix[0][1], self.matrix[0][2], self.matrix[0][3]) vecRow02 = vec3d(self.matrix[1][0], self.matrix[1][1], self.matrix[1][2], self.matrix[1][3]) vecRow03 = vec3d(self.matrix[2][0], self.matrix[2][1], self.matrix[2][2], self.matrix[2][3]) vecRow04 = vec3d(self.matrix[3][0], self.matrix[3][1], self.matrix[3][2], self.matrix[3][3]) return vec3d(vecRow01.dotProduct(vec), vecRow02.dotProduct(vec), vecRow03.dotProduct(vec), vecRow04.dotProduct(vec))
def divideXZ(self,divideNum,len_x,len_z,initial_point): vertices = [[0 for x in range(divideNum+1)] for y in range(divideNum+1)] initial_x = initial_point.x initial_z = initial_point.z for i in range(divideNum+1): z = initial_z + (len_z*i) for j in range(divideNum+1): x = initial_x + (len_x*j) vec = vec3d(x, initial_point.y, z, 1.0) vertices[i][j] = vec return vertices;
def calculate_look_vector_rotate(self,axis, theta): axis = np.asarray(axis) axis = axis / math.sqrt(np.dot(axis, axis)) a = math.cos(math.radians(theta / 2.0)) b, c, d = -axis * math.sin(math.radians(theta / 2.0)) aa, bb, cc, dd = a * a, b * b, c * c, d * d bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d x = np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)], [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)], [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]]) location = np.dot(x, [self.look_vec.x,self.look_vec.y,self.look_vec.z]) location_vec = vec3d(location[0],location[1],location[2],1) self.location_vec = location_vec return self.location_vec
def divideYZ(self,divideNum,len_y,len_z,initial_point): vertices = [[0 for x in range(divideNum+1)] for y in range(divideNum+1)] initial_y = initial_point.y initial_z = initial_point.z for i in range(divideNum+1): z = initial_z + (len_z*i) for j in range(divideNum+1): y = initial_y + (len_y*j) vec = vec3d(initial_point.x, y, z, 1.0) vertices[i][j] = vec return vertices;
def findQuad(self, lat_angle, long_angle, lat, long, r): vertices = [] lat_step = math.pi / lat long_step = 2 * math.pi / long x = (r * math.cos(lat_angle)) * math.cos(long_angle) y = (r * math.cos(lat_angle)) * math.sin(long_angle) z = r * math.sin(lat_angle) vertx1 = vec3d(x, y, z, 1) x = (r * math.cos(lat_angle + lat_step)) * math.cos(long_angle) y = (r * math.cos(lat_angle + lat_step)) * math.sin(long_angle) z = r * math.sin(lat_angle + lat_step) vertx2 = vec3d(x, y, z, 1) x = (r * math.cos(lat_angle)) * math.cos(long_angle + long_step) y = (r * math.cos(lat_angle)) * math.sin(long_angle + long_step) z = r * math.sin(lat_angle) vertx3 = vec3d(x, y, z, 1) x = (r * math.cos(lat_angle + lat_step)) * math.cos(long_angle + long_step) y = (r * math.cos(lat_angle + lat_step)) * math.sin(long_angle + long_step) z = r * math.sin(lat_angle + lat_step) vertx4 = vec3d(x, y, z, 1) vertices.append(vertx4) vertices.append(vertx3) vertices.append(vertx1) vertices.append(vertx2) initial = mat3d([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) quad = objct(initial, vertices, []) return quad
def load(self, fileName): faces = [] for line in open(fileName, 'r'): if line.startswith('#'): continue data = line.split() if not data: continue if data[0] == 'v': x = float(data[1]) y = float(data[2]) z = float(data[3]) if x > self.biggest: self.biggest = x if y > self.biggest: self.biggest = y if z > self.biggest: self.biggest = z vertice = vec3d(x, y, z, 1) self.vertices.append(vertice) if data[0] == 'f': face = [] for v in data[1:5]: w = v.split('/') face.append(int(w[0])-1) faces.append(face) self.polygons = [] for i in range(len(faces)): face = faces[i] polygon = [] for j in range(len(face)): point = face[j] polygon.append(self.vertices[point]) self.polygons.append(polygon) self.totalPolygonCount = len(self.polygons)
def load(self, fileName): name = "" faces = [] for line in open(fileName, 'r'): if line.startswith('#'): continue data = line.split() if not data: continue if (data[0] == "g" and data[1] == "default"): # print("Group") self.createObject(name, faces) # self.vertices = [] self.polygons = [] faces = [] continue elif (data[0] == "g"): name = data[1] continue if data[0] == 'v': x = float(data[1]) y = float(data[2]) z = float(data[3]) vertice = vec3d(x, y, z, 1) self.vertices.append(vertice) if data[0] == 'f': face = [] for v in data[1:5]: w = v.split('/') face.append(int(w[0]) - 1) faces.append(face) self.createObject(name, faces)
def drawMultiplePrim(shapes_List, x, y, z, angle_h, angle_v, distanca, drawType): global tempx, tempz, tempy, eyeX, eyeY, eyeZ, upX, upY, upZ cam = Camera(vec3d(0.0, 0.0, 45.0, 1.0), vec3d(0.0, 1.0, 0.0, 1.0), vec3d(0.0, 0.0, 0.0, 1.0)) c1 = vec3d(1.0, 0.0, 0.0, 0.0) # Red c2 = vec3d(0.0, 1.0, 0.0, 0.0) # Green c3 = vec3d(0.0, 0.0, 1.0, 0.0) # Blue #for shape in box.primitive3d.shapes: # shape.applyMatrixStack() upvec = cam.calculate_up_vec() axis = [upvec.x, upvec.y, upvec.z] r = cam.calculate_right_vec() axis2 = [r.x, r.y, r.z] lool_vec = cam.calculate_look_vector_rotate(axis, angle_h) eyeX = lool_vec.x eyeY = lool_vec.y eyeZ = lool_vec.z tempx = angle_h cam = Camera(vec3d(eyeX, eyeY, eyeZ, 1), vec3d(upvec.x, upvec.y, upvec.z, 1), vec3d(0.0, 0.0, 0.0, 1.0)) r = cam.calculate_right_vec() axis2 = [r.x, r.y, r.z] lool_vec2 = cam.calculate_look_vector_rotate(axis2, angle_v) eyeX = lool_vec2.x eyeY = lool_vec2.y eyeZ = lool_vec2.z tempz = angle_v cam = Camera(vec3d(eyeX, eyeY, eyeZ, 1), vec3d(upvec.x, upvec.y, upvec.z, 1), vec3d(0.0, 0.0, 0.0, 1.0)) look_vec = cam.calculate_look_vector_depth(distanca) eyeX = look_vec.x eyeY = look_vec.y eyeZ = look_vec.z tempy = distanca upvec = cam.calculate_up_vec() gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, upvec.x, upvec.y, upvec.z) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glDepthRange(0, 10000) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) s_box_color = [0, 0, 1] t_box_color = [0, 0, 1] floor_color = [0.8, 0.3, 0.8] ceeling_color = [0.8, 0.3, 0.8] l_wall_color = [1.0, 0, 0] r_wall_color = [0, 1.0, 0] back_wall_color = [0.8, 0.3, 0.8] shape_num = 0 glTranslatef(0, -12, 0) for shape in shapes_List[0]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.1, 0.1, 0.1, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [0.5, 0.5, 0.5, 1.0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() for shape in shapes_List[1]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.1, 0.1, 0.1, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [0.5, 0.5, 0.5, 1.0]) glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() for shape in shapes_List[2]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.1, 0.1, 0.1, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 1, 1, 1.0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() for shape in shapes_List[3]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 1, 1, 1.0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() for shape in shapes_List[4]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.1, 0.1, 0.1, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() for shape in shapes_List[5]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.0, 0.1, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [0.0, 1, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() for shape in shapes_List[6]: glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.1, 0.1, 0.1, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 1, 1, 1.0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128.0) glMaterialfv(GL_FRONT, GL_EMISSION, [0.0, 0.0, 0.0, 1.0]) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glEnd() glTranslatef(0, 12, 0)
def drawBox(box,x,y,z,angle_h,angle_v,distanca): global tempx,tempz,tempy,eyeX,eyeY,eyeZ,upX,upY,upZ cam = Camera(vec3d(15,0,0,1),vec3d(0,1,0,1),vec3d(0,0,0,1)) c1 = vec3d(1.0, 0.0, 0.0, 0.0) # Red c2 = vec3d(0.0, 1.0, 0.0, 0.0) # Green c3 = vec3d(0.0, 0.0, 1.0, 0.0) # Blue #for shape in box.primitive3d.shapes: # shape.applyMatrixStack() upvec = cam.calculate_up_vec() axis = [upvec.x, upvec.y, upvec.z] r = cam.calculate_right_vec() lool_vec = cam.calculate_look_vector_rotate(axis,angle_h) eyeX = lool_vec.x eyeY = lool_vec.y eyeZ = lool_vec.z cam = Camera(vec3d(eyeX,eyeY,eyeZ,1),vec3d(upvec.x,upvec.y,upvec.z,1),vec3d(0,0,0,1)) r = cam.calculate_right_vec() axis2 = [r.x, r.y, r.z] lool_vec2 = cam.calculate_look_vector_rotate(axis2,angle_v) eyeX = lool_vec2.x eyeY = lool_vec2.y eyeZ = lool_vec2.z cam = Camera(vec3d(eyeX,eyeY,eyeZ,1),vec3d(upvec.x,upvec.y,upvec.z,1),vec3d(0,0,0,1)) look_vec = cam.calculate_look_vector_depth(distanca) eyeX = look_vec.x eyeY = look_vec.y eyeZ = look_vec.z upvec = cam.calculate_up_vec() gluLookAt(eyeX,eyeY,eyeZ,0,0,0,upvec.x,upvec.y,upvec.z) s = Sphere(3,10,10) ver = s.create_primitive_sphere() glBegin(GL_QUADS) for v in ver: glColor3f(c1.x, c1.y, c1.z) glVertex3f(v[0].x, v[0].y, v[0].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(v[1].x, v[1].y, v[1].z) glColor3f(c3.x, c3.y, c3.z) glVertex3f(v[2].x, v[2].y, v[2].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(v[3].x, v[3].y, v[3].z) glEnd() glBegin(GL_QUADS) for shape in box.primitive3d.shapes: glColor3f(c1.x, c1.y, c1.z) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glColor3f(c3.x, c3.y, c3.z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(shape.vertices[3].x, shape.vertices[3].y, shape.vertices[3].z) glEnd()
def __init__(self, position, vertices, matrix_stack): self.position = position self.vertices = vertices self.matrix_stack = matrix_stack self.color = vec3d(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), 1)
def getCentroidOfTwoPoints(self, pointOne, pointTwo): return vec3d((pointOne.x + pointTwo.x) / 2, (pointOne.y + pointTwo.y) / 2, (pointOne.z + pointTwo.z) / 2, 1)
def drawPrim(primitive,x,y,z,angle_h,angle_v,distanca,drawType): global tempx,tempz,tempy,eyeX,eyeY,eyeZ,upX,upY,upZ cam = Camera(vec3d(15.0,0.0,0.0,1.0),vec3d(0.0,1.0,0.0,1.0),vec3d(0.0,0.0,0.0,1.0)) c1 = vec3d(1.0, 0.0, 0.0, 0.0) # Red c2 = vec3d(0.0, 1.0, 0.0, 0.0) # Green c3 = vec3d(0.0, 0.0, 1.0, 0.0) # Blue #for shape in box.primitive3d.shapes: # shape.applyMatrixStack() upvec = cam.calculate_up_vec() axis = [upvec.x, upvec.y, upvec.z] r = cam.calculate_right_vec() axis2 = [r.x, r.y, r.z] lool_vec = cam.calculate_look_vector_rotate(axis,angle_h) eyeX = lool_vec.x eyeY = lool_vec.y eyeZ = lool_vec.z tempx = angle_h cam = Camera(vec3d(eyeX,eyeY,eyeZ,1),vec3d(upvec.x,upvec.y,upvec.z,1),vec3d(0.0,0.0,0.0,1.0)) r = cam.calculate_right_vec() axis2 = [r.x, r.y, r.z] lool_vec2 = cam.calculate_look_vector_rotate(axis2,angle_v) eyeX = lool_vec2.x eyeY = lool_vec2.y eyeZ = lool_vec2.z tempz=angle_v cam = Camera(vec3d(eyeX,eyeY,eyeZ,1),vec3d(upvec.x,upvec.y,upvec.z,1),vec3d(0.0,0.0,0.0,1.0)) look_vec = cam.calculate_look_vector_depth(distanca) eyeX = look_vec.x eyeY = look_vec.y eyeZ = look_vec.z tempy=distanca upvec = cam.calculate_up_vec() gluLookAt(eyeX,eyeY,eyeZ,0.0,0.0,0.0,upvec.x,upvec.y,upvec.z) glPolygonMode(GL_FRONT_AND_BACK,GL_FILL) if(drawType=="lines"): glPolygonMode(GL_FRONT_AND_BACK,GL_LINE) glBegin(GL_QUADS) glColor3f(1,1,1) for shape in primitive.shapes: glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glVertex3f(shape.vertices[3].x, shape.vertices[3].y, shape.vertices[3].z) glEnd() else: glPolygonMode(GL_FRONT_AND_BACK,GL_FILL) glBegin(GL_QUADS) for shape in primitive.shapes: glColor3f(shape.color.x,shape.color.y,shape.color.z) glVertex3f(shape.vertices[0].x, shape.vertices[0].y, shape.vertices[0].z) glVertex3f(shape.vertices[1].x, shape.vertices[1].y, shape.vertices[1].z) glVertex3f(shape.vertices[2].x, shape.vertices[2].y, shape.vertices[2].z) glVertex3f(shape.vertices[3].x, shape.vertices[3].y, shape.vertices[3].z) glEnd()
def parse_file(self): tori_file = open(self.file_name, "r") tori_file_lines = tori_file.readlines() tori_face_vertex_indes = [] tori_vertecies = [] shapes = [] prims_shapes = [] primitivesLines = [] primitiveLines = [] primindex = 1 g_default_index = 0 for line in tori_file_lines: whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) if line_array[0] == "g" and line_array[1] == "default\n": g_default_index += 1 if primindex == g_default_index: primitiveLines.append(line) elif g_default_index > 0: primitivesLines.append(primitiveLines) primitiveLines = [] primindex += 1 primitivesLines.append(primitiveLines) for primitiveLines in primitivesLines: for line in primitiveLines: if (line[0] == 'f'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[2] = line_array[2].rstrip("\n") line_array[0] = int(line_array[0]) line_array[1] = int(line_array[1]) line_array[2] = int(line_array[2]) tori_face_vertex_indes.append(line_array) for line in tori_file_lines: if (line[0] == 'v'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[2] = line_array[2].rstrip("\n") line_array[0] = float(line_array[0]) / 2 line_array[1] = float(line_array[1]) / 2 line_array[2] = float(line_array[2]) / 2 tori_vertecies.append(line_array) tori_file.close() for face_indexes in tori_face_vertex_indes: shape = objct(vec3d(0.0, 0.0, 0.0, 1.0), [], []) for face_index in face_indexes: x = tori_vertecies[face_index - 1][0] y = tori_vertecies[face_index - 1][1] z = tori_vertecies[face_index - 1][2] vec = vec3d(x, y, z, 1) shape.vertices.append(vec) shapes.append(shape) prims_shapes.append(shapes) tori_face_vertex_indes = [] shapes = [] return prims_shapes
from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * from vec3d import vec3d from mat3d import mat3d from shape import objct import time import sys # Number of the glut window. window = 0 initial = mat3d([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) triangle = objct( vec3d(-2.7, 1, -8.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.translating_matrix(initial, -1.0, 1.0, 0.0), mat3d.rotation_xy_matrix(initial, 0.02), mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square = objct( vec3d(1.1, 1.0, -8.0, 1.0), # Position [ # Vertices vec3d(-1.0, 1.0, 0.0, 1.0), # Top Left
def DrawGLScene(): initial_time = time.time() global triangle, square, time_between_frames, refresh_time # update shape positions triangle.applyMatrixStack() square.applyMatrixStack() # update shape positions # Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # Reset The View glTranslatef(triangle.position.x, triangle.position.y, triangle.position.z) c1 = vec3d(1.0, 0.0, 0.0, 0.0) # Red c2 = vec3d(0.0, 1.0, 0.0, 0.0) # Green c3 = vec3d(0.0, 0.0, 1.0, 0.0) # Blue # Draw a triangle glBegin(GL_POLYGON) glColor3f(c1.x, c1.y, c1.z) glVertex3f(triangle.vertices[0].x, triangle.vertices[0].y, triangle.vertices[0].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(triangle.vertices[1].x, triangle.vertices[1].y, triangle.vertices[1].z) glColor3f(c3.x, c3.y, c3.z) glVertex3f(triangle.vertices[2].x, triangle.vertices[2].y, triangle.vertices[2].z) glEnd() glTranslatef(-triangle.position.x, -triangle.position.y, -triangle.position.z) # End of triangle glTranslatef(square.position.x, square.position.y, square.position.z) #Draw a triangle glBegin(GL_POLYGON) glColor3f(c1.x, c1.y, c1.z) glVertex3f(square.vertices[0].x, square.vertices[0].y, square.vertices[0].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(square.vertices[1].x, square.vertices[1].y, square.vertices[1].z) glColor3f(c3.x, c3.y, c3.z) glVertex3f(square.vertices[2].x, square.vertices[2].y, square.vertices[2].z) glColor3f(c2.x, c2.y, c2.z) glVertex3f(square.vertices[3].x, square.vertices[3].y, square.vertices[3].z) glEnd() glTranslatef(-square.position.x, -square.position.y, -square.position.z) # End of square time_after_drawing = time.time() if ((time_after_drawing - initial_time) < refresh_time): time.sleep(refresh_time - (time_after_drawing - initial_time)) # since this is double buffered, swap the buffers to display what just got drawn. glutSwapBuffers()
face_list = [] for face in self.faces: index_list = [face[0]-1,face[1]-1,face[2]-1,face[3]-1] face_list.append(index_list) output_points, output_faces = self.vertices,face_list for i in range(numdivision-1): output_points, output_faces = cmc_subdiv(output_points, output_faces) self.shapes = self.face_and_vertices_to_shapes(output_faces,output_points) initial = mat3d([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]) square1 = objct(vec3d(1.1, 1.0, -8.0, 1.0), # Position [ # Vertices vec3d(-1.4, 1.0, -0.4, 1.0), vec3d(1.0, 1.0, -1.0, 1.0), vec3d(1.0, -1.0, -1.0, 1.0), vec3d(-1.4, -1.0, -0.4, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial,-1, 1.0, 0.0), mat3d.rotation_zx_matrix(initial,0.01), mat3d.translating_matrix(initial,1.0, -1.0, 0.0) ])
face_list = [] for face in self.faces: index_list = [face[0] - 1, face[1] - 1, face[2] - 1, face[3] - 1] face_list.append(index_list) output_points, output_faces = self.vertices, face_list for i in range(numdivision - 1): output_points, output_faces = cmc_subdiv(output_points, output_faces) self.shapes = self.face_and_vertices_to_shapes(output_faces, output_points) initial = mat3d([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) square1 = objct( vec3d(1.1, 1.0, -8.0, 1.0), # Position [ # Vertices vec3d(-1.4, 1.0, -0.4, 1.0), vec3d(1.0, 1.0, -1.0, 1.0), vec3d(1.0, -1.0, -1.0, 1.0), vec3d(-1.4, -1.0, -0.4, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), mat3d.rotation_zx_matrix(initial, 0.01), mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ])
def create_primitive_box(self, quadlength): initial = mat3d([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) rotation_mat3d = mat3d.rotation_yz_matrix(initial, 0.01) square1 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(quadlength, quadlength, quadlength, 1.0), vec3d(quadlength, quadlength, -1 * quadlength, 1.0), vec3d(quadlength, -quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square2 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, -quadlength, quadlength, 1.0), vec3d(quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square3 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, -quadlength, 1.0), vec3d(quadlength, quadlength, -quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square4 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(-quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, -quadlength, 1.0), vec3d(-quadlength, -quadlength, -quadlength, 1.0), vec3d(-quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square5 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(-quadlength, -quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, quadlength, 1.0), vec3d(-quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square6 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(-quadlength, quadlength, -quadlength, 1.0), vec3d(quadlength, quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, -quadlength, 1.0), vec3d(-quadlength, -quadlength, -quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) prim = Primitive3D( [square1, square2, square3, square4, square5, square6]) return prim