def init_context(name, modelview_matrix, projection_matrix): state = get_state() with open(state["shaders"][name]['vertex_shader_path']) as f: vertex_shader_text = f.read() vertex_shader = shaders.compileShader(vertex_shader_text, GL_VERTEX_SHADER) with open(state["shaders"][name]['fragment_shader_path']) as f: fragment_shader_text = f.read() fragment_shader = shaders.compileShader(fragment_shader_text, GL_FRAGMENT_SHADER) shader = shaders.compileProgram(vertex_shader, fragment_shader) #print "init interface state: ", state position_location = glGetAttribLocation(shader, 'position') normal_location = glGetAttribLocation(shader, 'normal') color_location = glGetAttribLocation(shader, 'color') modelview_location = glGetUniformLocation(shader, 'modelViewMatrix') projection_location = glGetUniformLocation(shader, 'projectionMatrix') contexts[name] = { 'shader': shader, 'modelview': { 'location': modelview_location, 'matrix': modelview_matrix }, 'projection': { 'location': projection_location, 'matrix': projection_matrix }, 'position_location': position_location, 'color_location': color_location, 'normal_location': normal_location, 'thing': state[name] }
def idleFunc(): # do state stuff. one = time() state = get_state() idle(state) two = time() #print "idleFunc idle time: ", (two - one) glutPostRedisplay()
def onMouseMove(x, y): """ mouse moved but not clicked. """ #print "onMouseMove: {} {}".format(x, y) state = get_state() height = state["camera"]["resolution"][1] mouse_on_move(state, x, height - y)
def onMouseClick(button, mouse_state, x, y): #print "onMouseClick: {} {} {} {}".format(button, mouse_state, x, y) state = get_state() if mouse_state == 0: event = "MOUSE_DOWN" else: event = "MOUSE_UP" height = state["camera"]["resolution"][1] mouse_on_click(state, x, height - y, event)
def run(): global window 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 resolution = get_state()["camera"]["resolution"] glutInitWindowSize(int(resolution[0]), int(resolution[1])) # 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("Waffle") # 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(idleFunc) # Register the function called when our window is resized. glutReshapeFunc(ReSizeGLScene) # Register the function called when the keyboard is pressed. glutKeyboardFunc(keyDown) glutKeyboardUpFunc(keyUp) glutMouseFunc(onMouseClick) glutMotionFunc(onMouseClickMove) glutPassiveMotionFunc(onMouseMove) # Initialize our window. InitGL() init_world() init_interface() # Start Event Processing Engine glutMainLoop()
def update_world_matrices(): #print '_update_world_matrices 1' state = get_state() camera = state["camera"] placement = camera["placement"] position = placement["position"] orientation = placement["orientation"] front = orientation["front"] up = orientation_up(orientation) # OpenGL matrices are column major, so transpose it. projection_matrix = np.array(perspective_make(camera["fovy"], camera_aspect(camera), camera["near"] , camera["far"]), 'f') # OpenGL matrices are column major, so transpose it. modelview_matrix = np.array(look_at_make(position, sum_arrays(position, front), up), 'f') #print 'repositioning finished: {}\n {}'.format(self.projection_matrix, self.modelview_matrix) return modelview_matrix, projection_matrix
def init_interface(): state = get_state() resolution = state["camera"]["resolution"] projection_matrix = np.array(ortho_make(0, resolution[0], 0, resolution[1], -10.0, 10.0), 'f') modelview_matrix = np.array(matrix_identity(), 'f') init_context("interface", modelview_matrix, projection_matrix)
def keyUp(key, x, y): #print "keyUp: {} {} {}".format(key, x, y) state = get_state() keyboard_on_event(state, key, "KEY_UP")
def keyDown(key, x, y): #print "keyDown: {} {} {}".format(key, x, y) state = get_state() #record("key released: {}".format(event.name)) keyboard_on_event(state, key, "KEY_DOWN")