def moveCamera(): phi = controller.cameraPhi theta = controller.cameraTheta # Where to look atX = controller.xPos + np.cos(theta) * np.sin(phi) atY = controller.yPos + np.sin(theta) * np.sin(phi) atZ = 0.5 + np.cos(phi) viewPos = np.array([controller.xPos, controller.yPos, 0.5]) viewUp = np.array([0, 0, 1]) return tr.lookAt(viewPos, np.array([atX, atY, atZ]), viewUp), viewPos
def moveCamera(): # Angles of a spherical system, determined by the controller phi = controller.cameraPhi theta = controller.cameraTheta # Zoom level of the camera, how far is from the center zoom = controller.cameraZoom # Camera position in cartesian coordinates camX = zoom * np.sin(phi) * np.cos(theta) camY = zoom * np.sin(phi) * np.sin(theta) camZ = zoom * np.cos(phi) # Phi direction in cartesian coordinates upX = -np.cos(phi) * np.cos(theta) upY = -np.cos(phi) * np.sin(theta) upZ = np.sin(phi) viewPos = np.array([camX, camY, camZ]) viewUp = np.array([upX, upY, upZ]) return tr.lookAt(viewPos, np.array([0, 0, 0.4]), viewUp), viewPos
# As we work in 3D, we need to check which part is in front, # and which one is at the back glEnable(GL_DEPTH_TEST) # Creating shapes on GPU memory cuboM, cuboC, cuboT,cuboN = crearCubos() # Using the same view and projection matrices in the whole application projection = tr.perspective(45, float(width)/float(height), 0.1, 100) glUniformMatrix4fv(glGetUniformLocation(mvcPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) view = tr.lookAt( np.array([0.5,-3,3]), np.array([0,0,0]), np.array([0,0,1]) ) glUniformMatrix4fv(glGetUniformLocation(mvcPipeline.shaderProgram, "view"), 1, GL_TRUE, view) while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Filling or not the shapes depending on the controller state if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else:
# Update camera values camera_theta -= 2.0*dt*(controller.right - controller.left) camera_phi -= 2.0*dt*(controller.up - controller.down) camera_phi = np.clip(camera_phi, 0+0.00001, np.pi/2) # view matrix is NaN when phi=0 camera_r += 2.0*dt*(controller.zoomOut - controller.zoomIn) camera_r = np.clip(camera_r, 0.7, 3) cam_x = camera_r * np.sin(camera_phi) * np.sin(camera_theta) cam_y = camera_r * np.sin(camera_phi) * np.cos(camera_theta) cam_z = camera_r * np.cos(camera_phi) viewPos = np.array([cam_x,cam_y,cam_z]) view = tr.lookAt( viewPos, np.array([0,0,0.5]), np.array([0,0,1]) ) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Filling or not the shapes depending on the controller state if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if controller.showAxis: glUseProgram(mvpPipeline.shaderProgram) glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection)
maHB = False controller.uno = False indiceKyJ = 0 else: indiceKyJ += 1 x = controller.x y = controller.y z = -controller.z view = tr.lookAt( np.array([0 + x,-0.0015 + y,7 + z]), np.array([x, y,0]), np.array([0,0,1]) ) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) enzo(embisi) nodoAnterior = sg.findNode(malla, "T" + cursoAnterior.codigo) tapaAnt = nodoAnterior.childs[1] nodoAnterior.childs = [cuboR, tapaAnt] nodoAnterior = sg.findNode(malla, cursoAnterior.codigo) nodoAnterior.transform = tr.scale(1,1,1) for requisitoAnt in cursoAnterior.requisitos:
t0 = t1 # Getting the mouse location in openGL coordinates mousePosX = 2 * (controller.mousePos[0] - width / 2) / width mousePosY = 2 * (height / 2 - controller.mousePos[1]) / height # Setting up the projection transform projection = tr.perspective(45, float(width) / float(height), 0.1, 100) # Setting up the view transform atX = -6 * mousePosX atZ = 5 * mousePosY atPos = np.array([atX, 0, atZ]) view = tr.lookAt(np.array([0, 5, 2]), atPos, np.array([0, 0, 1])) # Setting up the model transform model = tr.identity() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # The landscape is drawn with texture lighting glUseProgram(phongTexturePipeline.shaderProgram) glUniform3f( glGetUniformLocation(phongTexturePipeline.shaderProgram, "La"), 1.0, 1.0, 1.0) glUniform3f( glGetUniformLocation(phongTexturePipeline.shaderProgram, "Ld"), 1.0, 1.0, 1.0)
glEnable(GL_DEPTH_TEST) # Creating shapes on GPU memory gpuAxis = es.toGPUShape(bs.createAxis(7)) redCarNode = createCar(1,0,0) blueCarNode = createCar(0,0,1) blueCarNode.transform = np.matmul(tr.rotationZ(-np.pi/4), tr.translate(3.0,0,0.5)) # Using the same view and projection matrices in the whole application projection = tr.perspective(45, float(width)/float(height), 0.1, 100) glUniformMatrix4fv(glGetUniformLocation(mvcPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) view = tr.lookAt( np.array([5,5,7]), np.array([0,0,0]), np.array([0,0,1]) ) glUniformMatrix4fv(glGetUniformLocation(mvcPipeline.shaderProgram, "view"), 1, GL_TRUE, view) while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Filling or not the shapes depending on the controller state if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else:
# Acá ponemos la rotación de la cámara R = 10 if camera_phi > np.pi/4: camera_phi = np.pi/4 if camera_phi < -1*np.pi/4: camera_phi = -1*np.pi/4 #print(camera_phi) camX = R * np.sin(camera_theta) camY = R * np.cos(camera_theta) camZ = R * camera_phi viewPos = np.array([camX, camY, camZ]) # Eye and Look-At of the camera positioned to the carrot view = tr.lookAt( np.array([x_carrot + camX, y_carrot + camY, z_carrot + camZ]), np.array([x_carrot, y_carrot, z_carrot]), np.array([0, 0, 1]) ) # Setting up the projection transform projection = tr.perspective(60, float(width)/float(height), 0.1, 100) #projection = tr.ortho(0, 600, 0, 600, 0.1, 100) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Filling or not the shapes depending on the controller state if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
def Amy_jump(estructura): if __name__ == "__main__": # Initialize glfw if not glfw.init(): sys.exit() width = 700 height = 700 window = glfw.create_window(width, height, "Amy Jump", None, None) if not window: glfw.terminate() sys.exit() glfw.make_context_current(window) # Connecting the callback function 'on_key' to handle keyboard events glfw.set_key_callback(window, on_key) # Defining shader programs pipeline = ls.SimpleGouraudShaderProgram() texture_pipeline = es.SimpleTextureModelViewProjectionShaderProgram() color_pipeline = es.SimpleModelViewProjectionShaderProgram() tex_pipeline = es.SimpleTextureTransformShaderProgram() # Telling OpenGL to use our shader program glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.85, 0.85, 0.85, 1.0) # As we work in 3D, we need to check which part is in front, # and which one is at the back glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Creamos los objetos # Amy gpuAmy = es.toGPUShape(shape=readOBJ('amy.obj', (1, 0, 0.5))) # Anillo (trofeo) gpuAnillo = es.toGPUShape(shape=readOBJ('ring.obj', (0.9, 0.9, 0))) # Fondo fondo = Fondo() #Piso piso = bs.createTextureCube('piso.jpg') gpupiso = es.toGPUShape(piso, GL_REPEAT, GL_LINEAR) piso_transform = tr.matmul( [tr.uniformScale(24), tr.translate(0, 0, -0.53)]) # Pantalla de inicio amyi = es.toGPUShape(bs.createTextureQuad("amy2.png", 1, 1), GL_REPEAT, GL_LINEAR) # Pantala de perdida eggman = es.toGPUShape(bs.createTextureQuad("eggman.png", 1, 1), GL_REPEAT, GL_LINEAR) # Pantalla de ganador amyf = es.toGPUShape(bs.createTextureQuad("am.png", 1, 1), GL_REPEAT, GL_LINEAR) # Barras barras = CreateBarras() barras.create(estructura) #Revisamos el tiempo t0 = glfw.get_time() # Posiciones de Amy posX = 0 posY = 0 posZ = 0.5 #Cuenta los fondo que ya se crearon count = 1 # Indica hasta que altura se debe llegar al saltar alt = 0 # Dice si se puede saltar o no salto = True #Indica cuando se "pierde" por saltar mal perder = False #Indica cuando se pierde por una flecha perder2 = False # Dice si se debe mandar una flecha de ataque arrow = True #Se crea la flecha con las posiciones hacia Amy flechas = Flecha(posX, posY, posZ) #Tamaño inicial de la imagen de partida, de perdida y de ganador ag = 2 eg = 0 af = 0 # rotaciones de Amy rotz = np.pi rotx = np.pi / 2 roty = 0 #Indica si ya estuvo en una barra bt = False #Rotaciones y posiciones del aro ra = 0 x = 0 y = 0 #Indica si gana o pierde ganar = False while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # Getting the time difference from the previous iteration t1 = glfw.get_time() dt = t1 - t0 t0 = t1 #Se define la vista "fija" del juego view = tr.lookAt(np.array([12, -5, 8 + (posZ - 0.5)]), np.array([posX * 0.4, posY * 0.4, posZ]), np.array([0, 0, 1])) #Cambia la vista mientras se mantengan apretadas las teclas if (glfw.get_key(window, glfw.KEY_B) == glfw.PRESS): view = tr.lookAt(np.array([0, 1, 17.5 + (posZ)]), np.array([0, 0, 0]), np.array([0, 0, 1])) if (glfw.get_key(window, glfw.KEY_N) == glfw.PRESS): view = tr.lookAt(np.array([-12, -5, 7.5 + posZ]), np.array([posX, posY, posZ]), np.array([0, 0, 1])) if (glfw.get_key(window, glfw.KEY_M) == glfw.PRESS): view = tr.lookAt(np.array([0, 10, -0.5 + posZ]), np.array([posX, posY, posZ]), np.array([0, 0, 1])) # Setting up the projection transform projection = tr.perspective(60, float(width) / float(height), 0.1, 100) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #Dibujamos el fondo fondo.Draw(texture_pipeline, projection, view) # Revisamos si se debe crear otro fondo if posZ > 13 * count: fondoazul = FondoAzul(count, fondo) count += 1 #Dibujamos el piso glUseProgram(texture_pipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(texture_pipeline.shaderProgram, "model"), 1, GL_TRUE, piso_transform) glUniformMatrix4fv( glGetUniformLocation(texture_pipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(texture_pipeline.shaderProgram, "view"), 1, GL_TRUE, view) texture_pipeline.drawShape(gpupiso) # Luz glUseProgram(pipeline.shaderProgram) glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "La"), 1.0, 1.0, 1.0) glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ld"), 1.0, 1.0, 1.0) glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ls"), 1.0, 1.0, 1.0) glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ka"), 0.3, 0.3, 0.3) glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Kd"), 0.9, 0.9, 0.9) glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ks"), 0.2, 0.2, 0.2) glUniform3f( glGetUniformLocation(pipeline.shaderProgram, "lightPosition"), 0, 0, posZ + 5) glUniform3f( glGetUniformLocation(pipeline.shaderProgram, "viewPosition"), 0, 0, posZ + 5) glUniform1ui( glGetUniformLocation(pipeline.shaderProgram, "shininess"), 1000) glUniform1f( glGetUniformLocation(pipeline.shaderProgram, "constantAttenuation"), 0.01) glUniform1f( glGetUniformLocation(pipeline.shaderProgram, "linearAttenuation"), 0.1) glUniform1f( glGetUniformLocation(pipeline.shaderProgram, "quadraticAttenuation"), 0.01) #Dibujamos a Amy glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.matmul([ tr.translate(posX, posY, posZ), tr.rotationZ(rotz), tr.rotationX(rotx), tr.rotationY(roty), tr.uniformScale(0.4) ])) pipeline.drawShape(gpuAmy) #Dibujamos el anillo a la altura de la ultima barra glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.matmul([ tr.translate(x, y, barras.altura * 2), tr.rotationZ(ra), tr.uniformScale(0.5) ])) pipeline.drawShape(gpuAnillo) # Dibujamos la pantalla inicial glUseProgram(tex_pipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(tex_pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([tr.translate(0, 0, 0), tr.uniformScale(ag)])) tex_pipeline.drawShape(amyi) # Dibujamos la pantalla de perdida glUniformMatrix4fv( glGetUniformLocation(tex_pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([tr.translate(0, 0, 0), tr.uniformScale(eg)])) tex_pipeline.drawShape(eggman) # Dibujamos la pantalla de ganador glUniformMatrix4fv( glGetUniformLocation(tex_pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([tr.translate(0, 0, 0), tr.uniformScale(af)])) tex_pipeline.drawShape(amyf) # Hace una copia de la lista de barras b = barras.barras #Indica que hay gravedad por lo cual amy va a caer mientras no este en una plataforma grav = True #Cambia el angulo de de rotación del aro ra -= 2 * dt #Revisa que la posición de las barras calza con Amy for i in range(len(b)): if posX <= 2 * b[i].posx + 1 and posX >= 2 * b[ i].posx - 1 and posY <= 2 * b[ i].posy + 1 and posY >= 2 * b[ i].posy - 1 and posZ <= b[ i].posz * 0.3 + 1.4 and posZ >= b[ i].posz * 0.3 + 1.25: if b[i].real == False: #Revisa si la barra es real, de serlo no la dibuja y no apaga la gravedad b[i].dibujo = False else: grav = False #si no, apaga la gravedad bt = True #indica que ya se subio una barra # Implementa la gravedad if posZ > 0.6 and grav == True: posZ -= 3 * dt #Mueve a Amy dependiendo de la tecla que se precione if (glfw.get_key(window, glfw.KEY_A) == glfw.PRESS): if posX > -6: posX -= 5 * dt if (glfw.get_key(window, glfw.KEY_D) == glfw.PRESS): if posX < 6: posX += 5 * dt if (glfw.get_key(window, glfw.KEY_W) == glfw.PRESS): if posY < 6: posY += 5 * dt if (glfw.get_key(window, glfw.KEY_S) == glfw.PRESS): if posY > -6: posY -= 5 * dt #Implementa los saltos if (glfw.get_key(window, glfw.KEY_SPACE) == glfw.PRESS): #si se puede saltar if salto == True: salto = False #se bloquea otro salto alt = 2 + posZ #calcula hasta donde debe llegar #si no pierde y no llega al limite de altura if alt >= posZ and perder == False: posZ += 5 * dt #salta #Si esta en una barra y aún no pierde if grav == False and perder2 == False: salto = True #Puede volver a saltar perder = False #aún no pierde #Inidica que perdio if alt < posZ + 0.01: perder = True # Si es que se puede hacer una flecha if arrow == True: ti = t1 #Revisamos el tiempo actual arrow = False #decimos que no se pueden hacer mas flechas flechas = Flecha(posX, posY, posZ) #se indica las posicones de la flecha # Si ya pasa un tiempo especifico if ti + 3 < t1: arrow = True # se puede hacer otra flecha #Revisamos que una flecha toca a Amy if flechas.posx <= posX + 0.1 and flechas.posx >= posX - 0.1 and flechas.posy <= -posY * 0.2 + 0.1 and flechas.posy >= -posY * 0.2 - 0.1 and flechas.posz <= posZ * 0.2 + 0.1 and flechas.posz >= posZ * 0.2 - 0.1: perder2 = True #Pierde #Mueve las flechas a partir de un tiempo en especifico if t1 > 8: flechas.update(4 * dt) flechas.draw(pipeline, projection, view) #Hace el movimiento de la imagen inicial if t1 < 8: if ag > 0: ag -= dt #Si pierde genera la imagen de game over y Amy empieza a rotar if ((posZ >= 0.5 and posZ <= 0.6 and grav and bt) or perder2 or (salto == False and posZ >= 0.5 and posZ <= 0.6)) and ganar == False: if eg < 2: eg += 0.8 * dt rotx -= 2 * dt rotz -= 2 * dt #Indica cuando gana if barras.altura * 2 - posZ <= 0.5 and barras.altura * 2 - posZ >= 0 and grav == False: ganar = True #Mueve el aro a Amy if x < 1.7 * posX: x += 5 * dt elif x >= 1.7 * posX: x -= 5 * dt if y < 1 * posY: y += 5 * dt elif y >= 1 * posY: y -= 5 * dt #Hace el movimiento de la imagen de ganador if af < 2: af += 0.8 * dt #Dibuja las barras barras.draw(color_pipeline, projection, view) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) glfw.terminate()
# Setting up the clear screen color glClearColor(0.85, 0.85, 0.85, 1.0) # As we work in 3D, we need to check which part is in front, # and which one is at the back glEnable(GL_DEPTH_TEST) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) # Using the same view and projection matrices in the whole application projection = tr.perspective(45, float(width) / float(height), 0.1, 100) # Generaremos diversas cámaras. static_view = tr.lookAt( np.array([10, 10, 5]), # eye np.array([0, 0, 0]), # at np.array([0, 0, 1]) # up ) view = static_view skyBox = bs.createTextureCube('skybox.png') sun = model.generateSphereShapeNormals(20, 20) GPUsun = es.toGPUShape(sun) GPUSkyBox = es.toGPUShape(skyBox, GL_REPEAT, GL_LINEAR) skybox_transform = tr.uniformScale(20) t0 = glfw.get_time() while not glfw.window_should_close(window): # Using GLFW to check for input events