def createCar(): # Cheating a single wheel wheel = sg.SceneGraphNode("wheel") wheel.transform = tr.uniformScale(0.2) wheel.childs += [createQuad(0, 0, 0)] wheelRotation = sg.SceneGraphNode("wheelRotation") wheelRotation.childs += [wheel] # Instanciating 2 wheels, for the front and back parts frontWheel = sg.SceneGraphNode("frontWheel") frontWheel.transform = tr.translate(0.3, -0.3, 0) frontWheel.childs += [wheelRotation] backWheel = sg.SceneGraphNode("backWheel") backWheel.transform = tr.translate(-0.3, -0.3, 0) backWheel.childs += [wheelRotation] # Creating the chasis of the car chasis = sg.SceneGraphNode("chasis") chasis.transform = tr.scale(1, 0.5, 1) chasis.childs += [createQuad(1, 0, 0)] car = sg.SceneGraphNode("car") car.childs += [chasis] car.childs += [frontWheel] car.childs += [backWheel] traslatedCar = sg.SceneGraphNode("traslatedCar") traslatedCar.transform = tr.translate(0, 0.3, 0) traslatedCar.childs += [car] return traslatedCar
def crearArbol(): #Se crean las figuras que se utilizaran gpuGreenTriangle = es.toGPUShape(bs.create2ColorTriangle(0.0549, 0.4549, 0.41, 0, 0.8, 0)) gpuBrownQuad = es.toGPUShape(bs.createColorQuad(0.502, 0.251, 0)) #Aqui se crean las partes de la copa del pino copa1 = sg.SceneGraphNode("copa1") copa1.transform = tr.uniformScale(0.5) copa1.childs += [gpuGreenTriangle] copa2 = sg.SceneGraphNode("copa2") copa2.transform = tr.matmul([tr.uniformScale(0.4), tr.translate(0,0.3,0)]) copa2.childs += [gpuGreenTriangle] copa3 = sg.SceneGraphNode("copa3") copa3.transform = tr.matmul([tr.uniformScale(0.3), tr.translate(0,0.8,0)]) copa3.childs += [gpuGreenTriangle] #Aqui se crea el tronco del arbol tronco = sg.SceneGraphNode("tronco") tronco.transform = tr.matmul ([tr.scale(0.1,0.5,0), tr.translate(0, -0.4, 0)]) tronco.childs += [gpuBrownQuad] #Se juntan las partes para crear el arbol arbol = sg.SceneGraphNode("car") arbol.childs += [tronco, copa1, copa2, copa3] return arbol
def crearBosque(N): # Primero escalamos el arbol escalarArbol = sg.SceneGraphNode("escalarArbol") escalarArbol.transform = tr.uniformScale(0.5) escalarArbol.childs += [crearArbol()] bosque = sg.SceneGraphNode("bosque") baseName = "arbol" for i in range(-N, N+1): newNode = sg.SceneGraphNode(baseName + str(i)) newNode.transform = tr.translate(0.3 * i, 0, 0) newNode.childs += [escalarArbol] bosque.childs += [newNode] #Se añade la nueva fila de arboles bosqueComp = sg.SceneGraphNode("bosqueComp") #Creamos un bosque con las filas de arboles antes creadas baseName = "fila" for i in range(-5,3): #el objetivo de este for es multiplicar las filas de arboles para crear un bosque newNode = sg.SceneGraphNode(baseName + str(i)) newNode.transform = tr.translate(0.23*(-1)**i,-0.1*i , 0) newNode.childs += [bosque] bosqueComp.childs += [newNode] return bosqueComp
def createMotor(): gpuGrayTrapeze = es.toGPUShape(ve.createColorTrapeze(0.6, 0.6, 0.6)) gpuBlueTriangle = es.toGPUShape(ve.createColorTriangle(0.2, 0.5, 1)) gpuWhiteTriangle = es.toGPUShape(ve.createColorTriangle(1, 1, 1)) # A motor is made of a body and an animated flame blueFlame = sg.SceneGraphNode("blueFlame") blueFlame.transform = tr.matmul([tr.translate(0, 0.18, 1), tr.uniformScale(0.5)]) blueFlame.childs += [gpuBlueTriangle] whiteFlame = sg.SceneGraphNode("whiteFlame") whiteFlame.transform = tr.matmul([tr.translate(0, 0.05, 1), tr.uniformScale(0.3)]) whiteFlame.childs += [gpuWhiteTriangle] # This flame is animated in the main script animatedFlame = sg.SceneGraphNode("animatedFlame") animatedFlame.childs += [blueFlame] animatedFlame.childs += [whiteFlame] body = sg.SceneGraphNode("body") body.transform = tr.scale(1, 0.25, 1) body.childs += [gpuGrayTrapeze] # Joining both parts motor = sg.SceneGraphNode("simpleMotor") motor.childs += [animatedFlame] motor.childs += [body] return motor
def update(self, t): self.update_parent_x() self.update_parent_y() parent_x = self.get_parent_x() parent_y = self.get_parent_y() d = self.get_distance() w = self.get_velocity() new_x = d * sin(w * t) new_y = d * cos(w * t) self.set_x(new_x + parent_x) self.set_y(new_y + parent_y) satellites = self.get_satellites_objects() model = self.get_model() body = sg.findNode(model, 'body') body.transform = tr.translate(self.get_x(), self.get_y(), 0) orbit = sg.findNode(model, 'orbit') orbit.transform = tr.matmul([ tr.uniformScale(self.get_zoom()), tr.translate(self.get_parent_x(), self.get_parent_y(), 0) ]) for planeta in satellites: planeta.update(t) selection = sg.findNode(model, 'selection_circle') if self.selected: selection.transform = tr.uniformScale(self.radius + 0.009) else: selection.transform = tr.uniformScale(0)
def createCar(r,g,b): gpuBlackQuad = es.toGPUShape(bs.createColorCube(0,0,0)) gpuChasisQuad = es.toGPUShape(bs.createColorCube(r,g,b)) # Cheating a single wheel wheel = sg.SceneGraphNode("wheel") wheel.transform = tr.scale(0.2, 0.8, 0.2) wheel.childs += [gpuBlackQuad] wheelRotation = sg.SceneGraphNode("wheelRotation") wheelRotation.childs += [wheel] # Instanciating 2 wheels, for the front and back parts frontWheel = sg.SceneGraphNode("frontWheel") frontWheel.transform = tr.translate(0.3,0,-0.3) frontWheel.childs += [wheelRotation] backWheel = sg.SceneGraphNode("backWheel") backWheel.transform = tr.translate(-0.3,0,-0.3) backWheel.childs += [wheelRotation] # Creating the chasis of the car chasis = sg.SceneGraphNode("chasis") chasis.transform = tr.scale(1,0.7,0.5) chasis.childs += [gpuChasisQuad] # All pieces together car = sg.SceneGraphNode("car") car.childs += [chasis] car.childs += [frontWheel] car.childs += [backWheel] return car
def crearfondoDia(): gpuGrayQuad = es.toGPUShape(bs.createColorQuad(0.2, 0.2, 0.2)) gpuSkyQuad = es.toGPUShape(bs.create2ColorQuad(1,1,1, 0.7373, 0.9961, 1)) gpuGreenQuad = es.toGPUShape(bs.createColorQuad(0.6706,1,0.7137)) gpuYellowCirc = es.toGPUShape(bs.create2ColorCircle([1,0.9961,0.4392],[1,1,1])) #Creamos el cielo con un degradado de blanco a celeste cielo = sg.SceneGraphNode("cielo") cielo.transform = tr.matmul([tr.translate(0,0.5,0),tr.scale(6,1.75,1)]) cielo.childs += [gpuSkyQuad] #Creamos el camino para el auto asfalto = sg.SceneGraphNode("asfalto") asfalto.transform = tr.matmul([tr.translate(0,-0.75,0),tr.scale(6,0.5,1)]) asfalto.childs += [gpuGrayQuad] #Se crea el pasto que se vera entre los arboles pasto = sg.SceneGraphNode("pasto") pasto.transform = tr.matmul([tr.translate(0,-0.2,0) , tr.scale(6,0.7,1)]) pasto.childs += [gpuGreenQuad] #Creamos un sol en la esquina de la pantalla sol = sg.SceneGraphNode("sol") sol.transform = tr.matmul([tr.translate(1,1,0), tr.uniformScale(0.3)]) sol.childs += [gpuYellowCirc] fondo = sg.SceneGraphNode("fondo") fondo.childs += [cielo, asfalto, pasto, sol] return fondo
def move_all(self): self.check_life() if not self.life_status: return self.move(self.movement_queue) movement = tr.identity() if self.next_direction == "W": movement = tr.translate(0, 1, 0) self.locationY += 1 elif self.next_direction == "A": movement = tr.translate(-1, 0, 0) self.locationX += -1 elif self.next_direction == "S": movement = tr.translate(0, -1, 0) self.locationY += -1 elif self.next_direction == "D": movement = tr.translate(1, 0, 0) self.locationX += 1 self.queue_movement(self.next_direction) self.last_direction = self.next_direction if self.check_apple(): self.eat_apple() self.last_position = [self.locationX, self.locationY] self.occupied_positions = [self.last_position ] + self.occupied_positions[1:] sg.findNode(self.model, "snake_head").transform = tr.matmul( [sg.findTransform(self.model, "snake_head"), movement])
def create_enemy(bodyColor, wingColor): # Generate enemy model # bodyColor - tuple (r, g, b) # wingColor - tuple (r, g, b) # Body gpuBody = es.toGPUShape(bs.createColorQuad(*bodyColor)) # Wing gpuWing = es.toGPUShape(enemy_wing_shape(*wingColor)) enemyBody = sg.SceneGraphNode("body") enemyBody.childs = [gpuBody] enemyWing1 = sg.SceneGraphNode("wing1") enemyWing1.transform = tr.translate(0.5, 0, 0) enemyWing1.childs = [gpuWing] enemyWing2 = sg.SceneGraphNode("wing2") enemyWing2.transform = tr.matmul( [tr.translate(-0.5, 0, 0), tr.scale(-1, 1, 1)]) enemyWing2.childs = [gpuWing] enemy = sg.SceneGraphNode("enemyModel") enemy.transform = tr.uniformScale(0.08) enemy.childs = [enemyBody, enemyWing1, enemyWing2] return enemy
def crearfondoNoche(): gpuGrayQuad = es.toGPUShape(bs.createColorQuad(0.1, 0.1, 0.1)) gpuSkyQuad = es.toGPUShape(bs.create2ColorQuad(1,1,1, 0.1451, 0.1569, 0.3137)) gpuGreenQuad = es.toGPUShape(bs.createColorQuad(0.3059,0.651,0.3294)) gpuGrayCirc = es.toGPUShape(bs.create2ColorCircle([0.6941,0.7216,0.7218],[0.8784,0.8902,0.8902])) #Creamos el cielo con un degradado de blanco a azul cielo cielo = sg.SceneGraphNode("cielo") cielo.transform = tr.matmul([tr.translate(0,0.5,0),tr.scale(6,1.75,1)]) cielo.childs += [gpuSkyQuad] #Creamos el camino para el auto asfalto = sg.SceneGraphNode("asfalto") asfalto.transform = tr.matmul([tr.translate(0,-0.75,0),tr.scale(6,0.5,1)]) asfalto.childs += [gpuGrayQuad] #Se crea el pasto que se vera entre los arboles pasto = sg.SceneGraphNode("pasto") pasto.transform = tr.matmul([tr.translate(0,-0.2,0) , tr.scale(6,0.7,1)]) pasto.childs += [gpuGreenQuad] #Creamos una luna en la esquina de la pantalla luna = sg.SceneGraphNode("luna") luna.transform = tr.matmul([tr.translate(1,1,0), tr.uniformScale(0.3)]) luna.childs += [gpuGrayCirc] fondo = sg.SceneGraphNode("fondo") fondo.childs += [cielo, asfalto, pasto, luna] return fondo
def Fondo(fondoE): color = es.toGPUShape(bs.createTextureCube("fondo2.jpg"),GL_REPEAT, GL_NEAREST) obama = es.toGPUShape(bs.createTextureCube("marco.png"),GL_REPEAT, GL_NEAREST) ricardo = es.toGPUShape(bs.createTextureCube("ricardo.jpg"),GL_REPEAT, GL_NEAREST) fondo1 = sg.SceneGraphNode("fondo1") fondo1.transform = tr.matmul([tr.translate(0,0,1) ,tr.uniformScale (15)]) fondo1.childs = [color] fondo2 = sg.SceneGraphNode("fondo2") fondo2.transform = tr.matmul([tr.translate(0,0,1) ,tr.uniformScale (15)]) fondo2.childs = [ricardo] fondo3 = sg.SceneGraphNode("fondo3") fondo3.transform = tr.matmul([tr.translate(0,0,1) ,tr.uniformScale (15)]) fondo3.childs = [obama] fondo = sg.SceneGraphNode("fondo") if fondoE == "C" : fondo.childs = [fondo1] if fondoE == "O" : fondo.childs = [fondo3] if fondoE == "R" : fondo.childs = [fondo2] return fondo
def crearCubos(): NormalCube = es.toGPUShape(bs.createTextureCube("base.png"),GL_REPEAT, GL_NEAREST) RequisitoDeCube = es.toGPUShape(bs.createTextureCube("base1.png"),GL_REPEAT, GL_NEAREST) SelectedCube = es.toGPUShape(bs.createTextureCube("base4.png"),GL_REPEAT, GL_NEAREST) RequisitoCube = es.toGPUShape(bs.createTextureCube("base3.png"),GL_REPEAT, GL_NEAREST) #Aqui se comienzan a crear los cubos de base CuboMorado = sg.SceneGraphNode("CuboMorado") #Se crea la base para el cubo morado CuboMorado.transform = tr.matmul([tr.translate(0.8,0,0), tr.uniformScale(0.5)]) CuboMorado.childs += [NormalCube] CuboTurquesa = sg.SceneGraphNode("CuboTurquesa") CuboTurquesa.transform = tr.matmul([tr.translate(0.27,0,0),tr.uniformScale(0.5)]) CuboTurquesa.childs += [RequisitoDeCube] CuboCeleste = sg.SceneGraphNode("CuboCeleste") CuboCeleste.transform = tr.matmul([tr.translate(-0.27,0,0),tr.uniformScale(0.5)]) CuboCeleste.childs += [RequisitoCube] CuboNaranjo = sg.SceneGraphNode("CuboNaranjo") CuboNaranjo.transform = tr.matmul([tr.translate(-0.8,0,0),tr.uniformScale(0.5)]) CuboNaranjo.childs += [SelectedCube] return CuboMorado,CuboTurquesa,CuboCeleste,CuboNaranjo
def _sample_err(args_): args, x, alpha, beta, V, post = args_ errs = [] post = get_postprocessing(post) K = alpha.shape[0] L = alpha.shape[1] for k in range(K): err = 0 for l in range(L): if args.transformation == 'rot': x_corr = rotate(x, alpha[k, l, 0] + beta[k, 0], args.interpolation) x_interpolate = rotate(rotate(x, alpha[k, l, 0], args.interpolation), beta[k, 0], args.interpolation) elif args.transformation == 'translation': x_corr = translate(x, (alpha[k, l, 0] + beta[k, 0], alpha[k, l, 1] + beta[k, 1]), args.interpolation) x_interpolate = translate(translate(x, (alpha[k, l, 0], alpha[k, l, 1]), args.interpolation), (beta[k, 0], beta[k, 1]), args.interpolation) else: assert(False) x_corr = pre(args, x_corr) x_interpolate = pre(args, x_interpolate) if args.dataset in ['mnist', 'fashionmnist']: x_corr_np = np.expand_dims(np.array(x_corr), axis=-1) x_interpolate_np = np.expand_dims(np.array(x_interpolate), axis=-1) else: x_corr_np = np.array(x_corr) x_interpolate_np = np.array(x_interpolate) x_corr_np_d = x_corr_np / 255.0 x_interpolate_np_d = x_interpolate_np / 255.0 x_corr_np_post, x_interpolate_np_post = post(x_corr_np_d), post(x_interpolate_np_d) if V is not None: x_corr_np_post = V(x_corr_np_post) x_interpolate_np_post = V(x_interpolate_np_post) diff_post = x_corr_np_post - x_interpolate_np_post err_post_crop_l2_post = np.linalg.norm(np.reshape(diff_post, (-1,)), ord=2) err = max(err, err_post_crop_l2_post) errs.append(err) return errs
def __init__(self, count, fondo): fondoa1 = bs.createTextureCube('azul.jpg') gpufondoa1 = es.toGPUShape(fondoa1, GL_REPEAT, GL_NEAREST) fondo.models.append(gpufondoa1) fondo.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(-1, 0, 0.4 + count)])) fondoa2 = bs.createTextureCube('azul.jpg') gpufondoa2 = es.toGPUShape(fondoa2, GL_REPEAT, GL_NEAREST) fondo.models.append(gpufondoa2) fondo.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(0, -1, 0.4 + count)])) fondoa3 = bs.createTextureCube('azul.jpg') gpufondoa3 = es.toGPUShape(fondoa3, GL_REPEAT, GL_NEAREST) fondo.models.append(gpufondoa3) fondo.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(0, 1, 0.4 + count)])) fondoa4 = bs.createTextureCube('azul.jpg') gpufondoa4 = es.toGPUShape(fondoa4, GL_REPEAT, GL_NEAREST) fondo.models.append(gpufondoa4) fondo.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(1, 0, 0.4 + count)]))
def __init__(self): self.models = [] self.transform = [] #crea el "manto de un cubo fondo1 = bs.createTextureCube('fondo.jpg') gpufondo1 = es.toGPUShape(fondo1, GL_REPEAT, GL_NEAREST) self.models.append(gpufondo1) self.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(-1, 0, 0.4)])) fondo2 = bs.createTextureCube('fondo.jpg') gpufondo2 = es.toGPUShape(fondo2, GL_REPEAT, GL_NEAREST) self.models.append(gpufondo1) self.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(0, -1, 0.4)])) fondo3 = bs.createTextureCube('fondo.jpg') gpufondo3 = es.toGPUShape(fondo3, GL_REPEAT, GL_NEAREST) self.models.append(gpufondo1) self.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(0, 1, 0.4)])) fondo4 = bs.createTextureCube('fondo.jpg') gpufondo4 = es.toGPUShape(fondo4, GL_REPEAT, GL_NEAREST) self.models.append(gpufondo1) self.transform.append( tr.matmul([tr.uniformScale(24), tr.translate(1, 0, 0.4)]))
def __init__(self): # Figuras básicas gpu_body_quad = es.toGPUShape(bs.createColorQuad(1, 0.8, 0.8)) # rosado gpu_leg_quad = es.toGPUShape(bs.createColorQuad(1, 0.5, 1)) # rosado fuerte gpu_eye_quad = es.toGPUShape(bs.createColorQuad(1, 1, 1)) # blanco # ... triangulos body = sg.SceneGraphNode('body') body.transform = tr.uniformScale(1) body.childs += [gpu_body_quad] # Creamos las piernas leg = sg.SceneGraphNode('leg') # pierna generica leg.transform = tr.scale(0.25, 0.25, 1) leg.childs += [gpu_leg_quad] # Izquierda leg_izq = sg.SceneGraphNode('legLeft') leg_izq.transform = tr.translate(-0.5, -0.5, 0) # tr.matmul([]).. leg_izq.childs += [leg] leg_der = sg.SceneGraphNode('legRight') leg_der.transform = tr.translate(0.5, -.5, 0) leg_der.childs += [leg] # Ojitos eye = sg.SceneGraphNode('eye') eye.transform = tr.scale(0.25, 0.25, 1) eye.childs += [gpu_eye_quad] eye_izq = sg.SceneGraphNode('eyeLeft') eye_izq.transform = tr.translate(-0.3, 0.5, 0) eye_izq.childs += [eye] eye_der = sg.SceneGraphNode('eyeRight') eye_der.transform = tr.translate(0.3, 0.5, 0) eye_der.childs += [eye] # Ensamblamos el mono mono = sg.SceneGraphNode('chansey') mono.transform = tr.matmul( [tr.scale(0.4, 0.4, 0), tr.translate(0, -1.25, 0)]) mono.childs += [body, leg_izq, leg_der, eye_izq, eye_der] transform_mono = sg.SceneGraphNode('chanseyTR') transform_mono.childs += [mono] self.model = transform_mono self.pos = 0 self.lives = 3 self.killer_eggs = []
def render(self, parent): # FIXME: bit of a hack but can't allow too small labels to be drawn if parent.height > 0: self.font.FaceSize(parent.height) set_color(parent.color, parent.alpha) with matrix_stack(): # note that FTGL uses OpenGL drawing convention by default! translate(parent.x, parent.y + parent.height) mirror_y() self.font.Render(parent.label)
def Movimiento (x,y): if x ==1: return tr.matmul([tr.translate(0,0,0),tr.translate(y*np.cos(t1*y) *np.sin(t1),y*np.cos(t1),0), tr.shearing(np.sin(t1)/50,np.cos(t1)/8,0,0,0,0)]) elif x ==2 : return tr.matmul([tr.translate(np.cos(t1*y), y*np.sin(t1*y)*np.sin(t1*y),0),tr.uniformScale(0.5), tr.shearing(np.sin(t1)/8,np.cos(t1)/8,0,0,0,0)]) elif x ==3 : return tr.matmul([tr.translate(y*np.sin(t1*y),np.cos(t1*y),0), tr.uniformScale(0.8), tr.shearing(np.sin(t1)/8,np.cos(t1)/8,0,0,0,0)]) elif x == 4: return tr.matmul([tr.translate(1000000000000,100000000000000,0)])
def hpStatusDraw(self): # A bar displaying current player HP hpBar = sg.SceneGraphNode("hp_bar") hpBar.childs = [] x = np.arange(0, (self.player.hp) * 0.07, 0.07) for i in range(self.player.hp): hpBlock = sg.SceneGraphNode(f"hp_{i}") hpBlock.transform = tr.translate(x[i], 0.0, 0.0) hpBlock.childs = [self.hpBlockModel] hpBar.childs.append(hpBlock) hpStatus = sg.SceneGraphNode("hp_status") hpStatus.transform = tr.translate(-0.65, 0.9, 0.0) hpStatus.childs = [hpBar] return hpStatus
def drawLifeBar(pipeline, hp): bar0 = ve.createLifeBar(hp > 0) bar0.transform = tr.translate(-0.92, -0.95, 0) bar1 = ve.createLifeBar(hp > 1) bar1.transform = tr.translate(-0.81, -0.95, 0) bar2 = ve.createLifeBar(hp > 2) bar2.transform = tr.translate(-0.70, -0.95, 0) sg.drawSceneGraphNode(bar0, pipeline, "transform") sg.drawSceneGraphNode(bar1, pipeline, "transform") sg.drawSceneGraphNode(bar2, pipeline, "transform")
def crear_persona(self): p = Persona() x = random.uniform(-0.95, 0.95) y = random.uniform(-0.95, 0.95) p.pos = [[x, y], [x, y]] p.model.transform = tr.translate(p.pos[0][0], p.pos[0][1], 0) self.personas.append(p)
def generate_forest_trees_obj(locations, fz, trees_models, leaves_models, scale=0.5): # Generates an obj model with all the trees merged # locations - Matrix of (N,2) of the trees x,y coordinates # fz - Terrain height function fz(x,y)->z # trees_models - List of OBJ trees models # leaves_models - List of OBJ leaves models # scales - Scale factor for the trees scale_tr = tr.uniformScale(scale) forest_trees = [] for i in range(len(locations)): x, y = locations[i] z = fz(x, y) - 0.03 # Lower to avoid floating trees M = tr.matmul([tr.translate(x, y, z), scale_tr]) model = i % len(trees_models) moved_tree = trees_models[model].transform(M) moved_leaves = leaves_models[model].transform(M) moved_tree.join(moved_leaves) forest_trees.append(moved_tree) merged_trees_model = forest_trees[0] for i in range(1, len(forest_trees)): merged_trees_model.join(forest_trees[i]) return merged_trees_model
def plantTrees(zMap, density=1, order=1): # Graph that will contain all the trees forestGraph = sg.SceneGraphNode("forest") xSize = zMap.shape[0] ySize = zMap.shape[1] dx = 2 * MAP_X_SIZE / xSize dy = 2 * MAP_Y_SIZE / ySize x = -MAP_X_SIZE + dx / 2 # Choosing where to plant trees treeCoordinates = np.random.randint(0, 40 / density, zMap.shape) # Plants a tree in a (x, y, z) position for i in range(zMap.shape[0]): y = -MAP_Y_SIZE + dy / 2 for j in range(zMap.shape[1]): if treeCoordinates[i, j] == 0: # Burying the tree a little to ensure it isn't floating normal = terrainNormal(xSize, ySize, zMap, i, j) correction = (abs(normal[0]) + abs(normal[1])) / 5 # Randomizing the order, size and skip parameters np.random.seed(RANDOM + i * j) variance = np.random.uniform() realOrder = max(1, ORDER - int(variance > 0.4)) realSize = tree.SIZE + 0.4 * int(variance > 0.6) + 0.2 * int( variance > 0.8) realSize += (realOrder - 1) * 0.5 realSkip = np.random.randint(0, realOrder**2 + 1) # Randomizing the rule of creation realRule = treeRule(realOrder) # Trees can't be planted close to each other for k in range(i, i + int(realSize + realOrder)): for l in range(j, j + int(realSize + realOrder)): try: treeCoordinates[k, l] = 1 except: pass treeGraph = tree.createTree(realRule, realOrder, realSize, realSkip) treeGraph.transform = tr.translate(x, y, zMap[i, j] - correction) forestGraph.childs += [treeGraph] y += dy x += dx return forestGraph
def addPlanet(planets_coll): # posicion vertical definida por los otros planetas posY = 0 if len(planets_coll.childs) == 0: posY = -1 else: posY = planets_coll.childs[-1].position[1] + posY #Posicion random del planeta planet_posX = -1 + random.random() * 2 planet_posY = posY + PLANET_MIN_POSY + random.random() * (PLANET_MAX_POSY - PLANET_MIN_POSY) planet_velY = PLANET_SPEED planet_index = random.randint(0, len(planets_images) - 1) #se le entrega la textura # se define un tamaño random planet_scale = PLANET_MIN_SIZE + random.random() * (PLANET_MAX_SIZE - PLANET_MIN_SIZE) #se crean un nodo donde se escala planet_object = sg.SceneGraphNode("planet_object") planet_object.transform = tr.scale(planet_scale, WIDTH / HEIGHT * planet_scale, planet_scale) planet_object.childs += [planets_images[planet_index]] #se crea un gameobject con el que se posisiona el planeta planet = gameObject("planet") planet.transform = tr.translate(planet_posX, planet_posY, 0) planet.position = np.array([planet_posX, planet_posY, 0]) planet.velocity = np.array([0, planet_velY, 0]) planet.childs += [planet_object] #se agrega a la coleccion de planetas planets_coll.childs += [planet]
def create_player(): # Generate player model # Body gpuBody = es.toGPUShape(player_body_shape(0.8, 0, 0.07)) # WingUp gpuWingUp = es.toGPUShape(player_upper_wing_shape(0.427, 0.447, 0.458)) # WingDown gpuWingDown = es.toGPUShape(player_lower_wing_shape(0.427, 0.447, 0.458)) #Flame gpuFlame = es.toGPUShape(flame_shape()) playerBody = sg.SceneGraphNode("body") playerBody.childs = [gpuBody] playerUpWing = sg.SceneGraphNode("wing1") playerUpWing.childs = [gpuWingUp] playerDownWing = sg.SceneGraphNode("wing2") playerDownWing.childs = [gpuWingDown] playerEngine = sg.SceneGraphNode("engine") playerEngine.transform = tr.matmul( [tr.translate(0.0, -0.525, 0.0), tr.uniformScale(0.9)]) playerEngine.childs = [gpuFlame] player = sg.SceneGraphNode("playerModel") player.transform = tr.uniformScale(0.1) player.childs = [playerUpWing, playerDownWing, playerBody, playerEngine] return player
def __init__(self, frame_width, frame_height, tiles=50): self.tiles = min(50, max(10, tiles)) self.frame_dim = [frame_width, frame_height] self.locationX = random.randint(0, self.tiles - 1) self.locationY = random.randint(1, self.tiles) # Aspect ratio ar = frame_height / frame_width #aspect_ratio_tr = tr.scale(ar, 1, 0) self.dimensions = [ar * 0.95 * 2 * 4 / 5, 0.95 * 2 * 4 / 5] gpu_apple = es.toGPUShape(my_shapes.apple()) apple = sg.SceneGraphNode('apple') apple.transform = tr.matmul([ tr.scale(self.dimensions[0] / self.tiles, self.dimensions[1] / self.tiles, 0), tr.translate(-self.tiles / 2 + self.locationX + 0.5, -self.tiles / 2 + self.locationY - 0.5, 0) ]) apple.childs += [gpu_apple] apple_tr = sg.SceneGraphNode('apple_TR') apple_tr.childs += [apple] self.model = apple_tr
def __init__(self, frame_width, frame_height, tiles=50): self.tiles = min(50, max(10, tiles)) self.frame_dim = [frame_width, frame_height] # Aspect ratio ar = frame_height / frame_width #aspect_ratio_tr = tr.scale(ar, 1, 0) self.dimensions = [ar * 0.95 * 2 * 4 / 5, 0.95 * 2 * 4 / 5] gpu_tiles = [ es.toGPUShape(bs.createColorQuad(1, 0.91, 0.84)), es.toGPUShape(bs.createColorQuad(0.99, 0.84, 0.61)) ] tiles = [] count = 0 for x in range(self.tiles): tiles.append([]) for y in range(self.tiles): tiles[x].append(sg.SceneGraphNode(f'tile{x}_{y}')) tiles[x][y].transform = tr.matmul([ tr.scale(self.dimensions[0] / self.tiles, self.dimensions[1] / self.tiles, 0), tr.translate(-self.tiles / 2 + x + 0.5, self.tiles / 2 - y - 0.5, 0) ]) tiles[x][y].childs += [gpu_tiles[(x + y) % 2]] board_TR = sg.SceneGraphNode('board_TR') for i in range(len(tiles)): board_TR.childs += tiles[i] self.model = board_TR
def getTransform(showTransform, theta): if showTransform == TR_STANDARD: return tr.identity() elif showTransform == TR_ROTATE_ZP: return tr.rotationZ(theta) elif showTransform == TR_ROTATE_ZM: return tr.rotationZ(-theta) elif showTransform == TR_TRANSLATE: return tr.translate(0.3 * np.cos(theta), 0.3 * np.cos(theta), 0) elif showTransform == TR_UNIFORM_SCALE: return tr.uniformScale(0.7 + 0.5 * np.cos(theta)) elif showTransform == TR_NONUNIF_SCALE: return tr.scale(1.0 - 0.5 * np.cos(1.5 * theta), 1.0 + 0.5 * np.cos(2 * theta), 1.0) elif showTransform == TR_REFLEX_Y: return tr.scale(1, -1, 1) elif showTransform == TR_SHEARING_XY: return tr.shearing(0.3 * np.cos(theta), 0, 0, 0, 0, 0) else: # This should NEVER happend raise Exception()
def move_right(self): model = self.get_model() self.x2 = (self.x2 + 0.05) model.transform = tr.matmul([ tr.uniformScale(self.get_zoom()), tr.translate(self.x2, self.y2, 0) ])
def paintGL(self): gl.glClearColor(0.1, 0.1, 0.1, 1.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # change light's position self.light_pos[0] = np.sin(3 * self.angle) * 2 self.light_pos[2] = np.cos(3 * self.angle) * 2 self.lightingShader.use() self.lightingShader.set_vec3("objectColor", 1.0, 0.5, 0.31) self.lightingShader.set_vec3("lightColor", 1.0, 1.0, 1.0) self.lightingShader.set_vec3v("lightPos", self.light_pos) # view/projection transforms projection = trans.perspective(np.radians(45), self.width() / self.height(), 0.1, 100.0) view = trans.translate(np.identity(4), np.array((0, 0, -5), np.float32)) view = trans.rotate(view, np.radians(0), np.array((1, 0, 0), np.float32)) self.lightingShader.set_mat4("projection", projection) self.lightingShader.set_mat4("view", view) # world transformation model = np.identity(4) self.lightingShader.set_mat4("model", model) # render the cube gl.glBindVertexArray(self.cube_vertex_array) gl.glDrawArrays(gl.GL_TRIANGLES, 0, self.icosphere.count)