import pygame from pygame.locals import * from gl import Renderer import shaders deltaTime = 0.0 # Inicializacion de pygame pygame.init() clock = pygame.time.Clock() screenSize = (960, 540) screen = pygame.display.set_mode(screenSize, DOUBLEBUF | OPENGL) # Inicializacion de nuestro Renderer en OpenGL r = Renderer(screen) r.setShaders(shaders.vertex_shader, shaders.fragment_shader) r.createObjects() cubeX = 0 cubeZ = 0 isPlaying = True while isPlaying: # Para revisar si una tecla esta presionada keys = pygame.key.get_pressed() if keys[pygame.K_a]: cubeX -= 2 * deltaTime if keys[pygame.K_d]: cubeX += 2 * deltaTime
def render(): # Initializr pygame and our own renderer based on OpenGl deltaTime = 0.0 clock = pygame.time.Clock() screenSize = (960, 540) screen = pygame.display.set_mode(screenSize, DOUBLEBUF | OPENGL) pygame.display.set_caption('OpenGl') renderer = Renderer(screen) # Set shaders and create objects renderer.setShaders(shaders.vertex_shader, shaders.fragment_shader) renderer.modelList.append( Model('tri_coca.obj', 'coca.png', glm.vec3(0.4, 0.4, 0.4))) renderer.modelList.append( Model('tri_hamster.obj', 'hamster.bmp', glm.vec3(3 / 4, 3 / 4, 3 / 4), glm.vec3(0, 90, 0))) renderer.modelList.append( Model('tri_R2.obj', 'R2.bmp', glm.vec3(1.5 / 3, 1.5 / 3, 1.5 / 3))) renderer.modelList.append( Model('tri_apple.obj', 'apple.jpg', glm.vec3(15, 15, 15))) renderer.modelList.append( Model('tri_heart.obj', 'heart.png', glm.vec3(7, 7, 7))) #Initial Camera Position and Rotation renderer.camPos = glm.vec3(0, 0, 10) cameraX, cameraY, cameraZ = renderer.camPos pitch = renderer.pitchCam yaw = renderer.yawCam roll = renderer.rollCam renderer.pointLight.z = 20 #Check if we are tryng to render the screen isRendering = True radius = 10 camAngle = 0 a = 1 while isRendering: # Check if any key is pressed keys = pygame.key.get_pressed() #Movement in camera X, camera Y and camera Z. # if keys[pygame.K_a]: # cameraX -= 2 * deltaTime # if keys[pygame.K_d]: # cameraX += 2 * deltaTime # if keys[pygame.K_e]: # if(cameraZ>4): # cameraZ -= 2 * deltaTime # if keys[pygame.K_q]: # if(cameraZ<15): # cameraZ += 2 * deltaTime # if keys[pygame.K_s]: # cameraY -= 2 * deltaTime # if keys[pygame.K_w]: # cameraY += 2 * deltaTime if keys[pygame.K_a]: camAngle -= 1 cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius if keys[pygame.K_d]: camAngle += 1 cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius if keys[pygame.K_e]: if (radius > 4): radius -= 2 * deltaTime cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius if keys[pygame.K_q]: if (radius < 15): radius += 2 * deltaTime cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius if keys[pygame.K_s]: if (cameraY >= -radius): cameraY -= 5 * deltaTime if keys[pygame.K_w]: if (cameraY <= radius): cameraY += 5 * deltaTime #Reset Camera to Default if keys[pygame.K_r]: cameraX = 0 cameraY = 0 cameraZ = 10 camAngle = 0 radius = 10 if (pygame.mouse.get_pressed()[0] == 1): mouseMove = pygame.mouse.get_rel() if (mouseMove[0] != 0): camAngle += mouseMove[0] cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius elif (mouseMove[1] != 0): if (mouseMove[1] > 0): nextCamY = cameraY + abs(mouseMove[1] * 0.2) if (not nextCamY >= radius): cameraY = nextCamY else: nextCamY = cameraY - abs(mouseMove[1] * 0.2) if (not nextCamY <= -radius): cameraY = nextCamY # #Rotation in camera # if keys[pygame.K_i]: # pitch -= 10 * deltaTime # if keys[pygame.K_o]: # pitch += 10 * deltaTime # if keys[pygame.K_k]: # yaw -= 10 * deltaTime # if keys[pygame.K_l]: # yaw += 10 * deltaTime # if keys[pygame.K_j]: # roll -= 10 * deltaTime # if keys[pygame.K_h]: # roll += 10 * deltaTime for ev in pygame.event.get(): if ev.type == pygame.QUIT: isRendering = False elif ev.type == pygame.KEYDOWN: # Checking for any other key pressed to exit #Filled Mode if ev.key == pygame.K_1: renderer.filledMode() #WireframeMode elif ev.key == pygame.K_2: renderer.wireframeMode() #Change object elif ev.key == pygame.K_SPACE: renderer.modelShow = (renderer.modelShow + 1) % len( renderer.modelList) if (renderer.modelShow == 4): pygame.mixer.music.load("heartbeat.mp3") pygame.mixer.music.play(-1) # heart_shader didnt work:( renderer.setShaders(shaders.vertex_shader, shaders.fragment_shader) elif ((renderer.modelShow == 0)): pygame.mixer.music.load("bgmusic.mp3") pygame.mixer.music.play(-1) renderer.setShaders(shaders.vertex_shader, shaders.fragment_shader) elif ev.key == pygame.K_ESCAPE: isRendering = False #Mouse Movement elif ev.type == pygame.MOUSEBUTTONDOWN: if ev.button == 3: #Reset cam pos cameraX = 0 cameraY = 0 cameraZ = 10 camAngle = 0 radius = 10 elif ev.button == 4: if (radius > 4): radius -= 10 * deltaTime cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius elif ev.button == 5: if (radius < 15): radius += 10 * deltaTime cameraX = sin(radians(camAngle)) * radius cameraZ = cos(radians(camAngle)) * radius # Traslate and rotate our camera renderer.translateCamera(cameraX, cameraY, cameraZ) renderer.rotateCamera(pitch, yaw, roll) # Render loop try: renderer.render() except Exception as e: print(e) pygame.display.flip() clock.tick(60) deltaTime = clock.get_time() / 1000
import pygame from pygame.locals import * from gl import Renderer import shaders delta_time = 0.0 pygame.init() clock = pygame.time.Clock() SCREEN_SIZE = (960, 540) SCREEN = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF | OPENGL) renderer = Renderer(SCREEN) renderer.set_shaders(shaders.vertex_shader, shaders.fragment_shader) renderer.create_objects() cube_x = 0 cube_z = 0 camera_x = 0 camera_z = 3 camera_pitch = 0 camera_yaw = 0 camera_roll = 0 isRunning = True while isRunning: keys = pygame.key.get_pressed()
def modelos(): screenSize = (960, 540) screen = pygame.display.set_mode(screenSize, DOUBLEBUF | OPENGL) # Inicializacion de nuestro Renderer en OpenGL r = Renderer(screen) # HAMSTER r.camPosition.z = 1 r.camPosition.y = 0 r.pointLight.y = 50 r.pointLight.z = 50 r.setShaders(shaders.vertex_shader, shaders.fragment_shader) m = Model('./models/Hamster.obj', './models/Hamster.bmp') pygame.mixer.music.load('sonidos/caricatura.mp3') pygame.mixer.music.play(10) r.modelList.append(m) isPlaying = True while isPlaying: # Para revisar si una tecla esta presionada keys = pygame.key.get_pressed() # Movimientos de camara if keys[K_d]: r.camPosition.x += 1 * deltaTime if keys[K_a]: r.camPosition.x -= 1 * deltaTime if keys[K_w]: r.camPosition.z -= 1 * deltaTime if keys[K_s]: r.camPosition.z += 1 * deltaTime if keys[K_e]: r.camPosition.y -= 1 * deltaTime if keys[K_q]: r.camPosition.y += 1 * deltaTime if keys[K_r]: m.rotation.y += 3 * deltaTime if keys[K_t]: m.rotation.y -= 3 * deltaTime if keys[K_y]: m.rotation.x += 3 * deltaTime if keys[K_u]: m.rotation.x -= 3 * deltaTime if keys[K_i]: m.rotation.z += 3 * deltaTime if keys[K_o]: m.rotation.z -= 3 * deltaTime # REGRESAR A INSTRUCTIVO if keys[K_m]: intro() # cambiar de modelos if keys[K_c]: # MUJER r.modelList.clear() r.camPosition.z = 50 r.camPosition.y = 150 r.pointLight.y = 0 r.pointLight.z = 300 m = Model('./models/mujer.obj', './models/mujer.bmp') r.modelList.append(m) if keys[K_v]: r.modelList.clear() # MUJER E HIJO r.camPosition.z = 50 r.camPosition.y = 150 r.pointLight.y = 0 r.pointLight.z = 300 m = Model('./models/mujer_hijo.obj', './models/mujer_hijo.bmp') r.modelList.append(m) if keys[K_b]: r.modelList.clear() # HAMSTER r.camPosition.z = 1 r.camPosition.y = 0 r.pointLight.y = 50 r.pointLight.z = 50 m = Model('./models/Hamster.obj', './models/Hamster.bmp') r.modelList.append(m) if keys[K_n]: r.modelList.clear() # HOMBRE r.camPosition.z = 50 r.camPosition.y = 170 r.camPosition.x = 5 r.pointLight.y = 0 r.pointLight.z = 300 m = Model('./models/hombre.obj', './models/hombre.bmp') r.modelList.append(m) for ev in pygame.event.get(): if ev.type == pygame.QUIT: isPlaying = False elif ev.type == pygame.KEYDOWN: # para revisar en el momento que se presiona una tecla if ev.key == pygame.K_1: r.filledMode() elif ev.key == pygame.K_2: r.wireframeMode() elif ev.key == pygame.K_ESCAPE: isPlaying = False elif ev.type == pygame.MOUSEBUTTONDOWN: r.pointLight.x = -r.pointLight.x - 100 r.pointLight.y = -r.pointLight.y - 100 # Main Renderer Loop r.render() pygame.display.flip() clock.tick(60) deltaTime = clock.get_time() / 1000 pygame.quit()