def __init__(self): self.context = Context.getInstance() # NOTE must be initialized self.logger = logging.getLogger(__name__) self.windowWidth = 640 self.windowHeight = 480 self.initialize() self.colorShader = Shader(self.context.getResourcePath('shaders', 'ADS.vert'), self.context.getResourcePath('shaders', 'ADS.frag')) self.proj = hm.perspective(hm.identity(), 35, float(self.windowWidth) / self.windowHeight, 1.0, 1000.0) self.view = hm.lookat(hm.identity(), np.array([0.0, 0.0, 0.0, 1.0], dtype = np.float32), np.array([0.0, 0.0, 1.0, 1.0], dtype = np.float32), np.array([0.0, -1.0, 0.0, 1.0], dtype = np.float32)) self.cameraMatrix = np.dot(self.proj, self.view) self.setCameraMatrix(self.cameraMatrix)
def draw(self, baseTransform=hm.identity()): # TODO draw and recurse down only when this Actor is enabled # TODO move to a more generic approach? # e.g.:- for component in self.components: component.apply() # But how do we ensure order is maintained? (Mesh must be rendered after applying Transform and Material) OrderedDict? self.transform = baseTransform try: if hasattr(self, 'transform_matrix' ): # if there is a full transform, use it self.transform = np.dot(self.transform, self.transform_matrix) else: self.transform = hm.translation( self.transform, self.components['Transform'].translation ) # TODO make transform relative to parent, not absolute self.transform = hm.rotation( self.transform, self.components['Transform'].rotation[0], [1, 0, 0]) self.transform = hm.rotation( self.transform, self.components['Transform'].rotation[1], [0, 1, 0]) self.transform = hm.rotation( self.transform, self.components['Transform'].rotation[2], [0, 0, 1]) self.transform = hm.scale(self.transform, self.components['Transform'].scale) except KeyError, AttributeError: # Transform component not present or incomplete/invalid pass # use base (parent) transform (?) - should get set in next step
def __init__(self): Tool.__init__(self) # * Initialize ZMQ context and open subscriber, publisher sockets self.logger.debug("ZMQ version: {}, PyZMQ version: {}".format(zmq.zmq_version(), zmq.pyzmq_version())) # ** Context self.zmqContext = zmq.Context() # ** Subscriber self.subSocket = self.zmqContext.socket(zmq.SUB) self.subSocket.connect(self.sub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[sub] Connected to {}".format(self.sub_address)) # ** Subscriber topics for input messages self.subSocket.setsockopt(zmq.SUBSCRIBE, self.sub_topic) self.subSocket.setsockopt(zmq.LINGER, self.sub_socket_linger) self.logger.debug("[sub]Subscribed to topic \"{}\"".format(self.sub_topic)) time.sleep(0.005) # mandatory sleep for ZMQ backend # ** Publisher self.pubSocket = self.zmqContext.socket(zmq.PUB) self.pubSocket.bind(self.pub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[pub] Bound to {}".format(self.pub_address)) # * Initialize other members self.valid = False self.buttons = [0, 0] # primary, secondary self.transform = hm.translation(hm.identity(), self.position_offset) #self.position = self.position_offset self.loop = True # TODO ensure this is properly shared across threads # * Start sensing loop self.senseThread = Thread(target=self.senseLoop) self.senseThread.daemon = True # to prevent indefinite wait on recv() self.senseThread.start() time.sleep(0.005) # sleep to allow child thread to run
def __init__(self): self.context = Context.getInstance() # NOTE must contain renderer assert hasattr(self.context, 'renderer') and isinstance(self.context.renderer, Renderer), "Context does not contain renderer of correct type" self.logger = logging.getLogger(__name__) self.hideCube = False self.transform = hm.identity() self.light = Light(self.context.renderer) self.actorFactory = ActorFactory(self.context.renderer) self.actors = [] self.actorsById = dict()
def __init__(self): self.context = Context.getInstance() # NOTE must be initialized self.logger = logging.getLogger(__name__) self.windowWidth = 640 self.windowHeight = 480 self.initialize() self.colorShader = Shader( self.context.getResourcePath('shaders', 'ADS.vert'), self.context.getResourcePath('shaders', 'ADS.frag')) self.proj = hm.perspective(hm.identity(), 35, float(self.windowWidth) / self.windowHeight, 1.0, 1000.0) self.view = hm.lookat( hm.identity(), np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float32), np.array([0.0, 0.0, 1.0, 1.0], dtype=np.float32), np.array([0.0, -1.0, 0.0, 1.0], dtype=np.float32)) self.cameraMatrix = np.dot(self.proj, self.view) self.setCameraMatrix(self.cameraMatrix)
def __init__(self): self.context = Context.getInstance() # NOTE must contain renderer assert hasattr(self.context, 'renderer') and isinstance( self.context.renderer, Renderer), "Context does not contain renderer of correct type" self.logger = logging.getLogger(__name__) self.hideCube = False self.transform = hm.identity() self.light = Light(self.context.renderer) self.actorFactory = ActorFactory(self.context.renderer) self.actors = [] self.actorsById = dict()
def draw(self, baseTransform=hm.identity()): # TODO draw and recurse down only when this Actor is enabled # TODO move to a more generic approach? # e.g.:- for component in self.components: component.apply() # But how do we ensure order is maintained? (Mesh must be rendered after applying Transform and Material) OrderedDict? self.transform = baseTransform try: if hasattr(self, 'transform_matrix'): # if there is a full transform, use it self.transform = np.dot(self.transform, self.transform_matrix) else: self.transform = hm.translation(self.transform, self.components['Transform'].translation) # TODO make transform relative to parent, not absolute self.transform = hm.rotation(self.transform, self.components['Transform'].rotation[0], [1, 0, 0]) self.transform = hm.rotation(self.transform, self.components['Transform'].rotation[1], [0, 1, 0]) self.transform = hm.rotation(self.transform, self.components['Transform'].rotation[2], [0, 0, 1]) self.transform = hm.scale(self.transform, self.components['Transform'].scale) except KeyError, AttributeError: # Transform component not present or incomplete/invalid pass # use base (parent) transform (?) - should get set in next step
def __init__(self, renderer, isTransient=False): self.renderer = renderer # TODO make renderer a component? self.isTransient = isTransient # if transient, will not be exported (in XML, etc.) # Common attributes self.id = None self.description = None self.parent = None # Dictionary to store all components by name self.components = dict() # List of child actors self.children = [] # TODO Consider a finalize() method that copies out references to key components for fast retrieval, # and possibly pre-computes some results (such as a composite transform matrix) # Set visibility to True upon initialization self.visible = True # only affects this actor, not its children # Temp variables that are useful to retain self.transform = hm.identity()
def __init__(self): Tool.__init__(self) # * Initialize ZMQ context and open subscriber, publisher sockets self.logger.debug("ZMQ version: {}, PyZMQ version: {}".format( zmq.zmq_version(), zmq.pyzmq_version())) # ** Context self.zmqContext = zmq.Context() # ** Subscriber self.subSocket = self.zmqContext.socket(zmq.SUB) self.subSocket.connect(self.sub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[sub] Connected to {}".format(self.sub_address)) # ** Subscriber topics for input messages self.subSocket.setsockopt(zmq.SUBSCRIBE, self.sub_topic) self.subSocket.setsockopt(zmq.LINGER, self.sub_socket_linger) self.logger.debug("[sub]Subscribed to topic \"{}\"".format( self.sub_topic)) time.sleep(0.005) # mandatory sleep for ZMQ backend # ** Publisher self.pubSocket = self.zmqContext.socket(zmq.PUB) self.pubSocket.bind(self.pub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[pub] Bound to {}".format(self.pub_address)) # * Initialize other members self.valid = False self.buttons = [0, 0] # primary, secondary self.transform = hm.translation(hm.identity(), self.position_offset) #self.position = self.position_offset self.loop = True # TODO ensure this is properly shared across threads # * Start sensing loop self.senseThread = Thread(target=self.senseLoop) self.senseThread.daemon = True # to prevent indefinite wait on recv() self.senseThread.start() time.sleep(0.005) # sleep to allow child thread to run
def pollInput(self): currentTime = time.clock() elapsedTime = currentTime - self.timer self.timer = currentTime #tempWheelPosition = glfw.GetMouseWheel() #if tempWheelPosition != self.wheelPosition: #self.wheelPosition = tempWheelPosition #self.setView(hm.lookat(hm.identity(), np.array([0.0, 0.0, 55.0 - self.wheelPosition, 1.0], dtype = np.float32), np.array([0.0, 0.0, 0.0, 1.0], dtype = np.float32))) if glfw.GetKey('M'): print "Initializing manual control" self.manualControl = True self.context.scene.transform = hm.translation( hm.identity(), [0, 0, 60]) mouseX, mouseY = glfw.GetMousePos() self.calcArcBallVector(mouseX, mouseY) time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('P'): print "Stopping manual control" self.manualControl = False time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('A') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [0, 1, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('D') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [0, 1, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('W') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [1, 0, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('S') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [1, 0, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('Q') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [0, 0, 1]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('E') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [0, 0, 1]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('1'): print "1 pressed" if glfw.GetKey('2'): print "2 pressed" if glfw.GetKey('3'): print "3 pressed" if glfw.GetKey('X'): self.context.scene.dump() time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('T'): self.context.task.toggle() time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('I'): inputSnapshot = self.context.cubeTracker.imageIn # grab current input image as snapshot cv2.imshow("Input snapshot", inputSnapshot) # show snapshot in a window #cv2.imwrite(self.input_snapshot_file, inputSnapshot) # write snapshot to file (NOTE doesn't work!) #print "Input snapshot saved to {}".format(self.input_snapshot_file) time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey(glfw.KEY_ESC): if not self.quitting: self.doQuit = True self.quitting = True if not self.leftPressed and glfw.GetMouseButton( glfw.MOUSE_BUTTON_LEFT): self.leftPressed = True self.context.scene.hideCube = not self.context.scene.hideCube if not glfw.GetMouseButton(glfw.MOUSE_BUTTON_LEFT): self.leftPressed = False if not self.rightPressed and glfw.GetMouseButton( glfw.MOUSE_BUTTON_RIGHT): self.rightPressed = True self.oldMouseX, self.oldMouseY = glfw.GetMousePos() self.curMouseX = self.oldMouseX self.curMouseY = self.oldMouseY if not glfw.GetMouseButton(glfw.MOUSE_BUTTON_RIGHT): self.rightPressed = False if self.rightPressed: #OK self.curMouseX, self.curMouseY = glfw.GetMousePos() #OK if self.curMouseX != self.oldMouseX or self.curMouseY != self.oldMouseY: #OK oldVec = self.calcArcBallVector(self.oldMouseX, self.oldMouseY) #OK curVec = self.calcArcBallVector(self.curMouseX, self.curMouseY) #OK angle = math.acos(min(1.0, np.dot(oldVec, curVec))) #OK cameraAxis = np.cross(oldVec, curVec) #OK cameraAxis /= np.linalg.norm( cameraAxis, ord=2) # normalize cameraAxis to be a unit vector cameraToObjectCoords = np.linalg.inv( np.dot(self.context.renderer.view[:-1, :-1], self.context.scene.transform[:-1, :-1])) #??? cameraAxisObjectCoords = np.dot(cameraToObjectCoords, cameraAxis) #OK self.context.scene.transform = hm.rotation( self.context.scene.transform, math.degrees(angle), cameraAxisObjectCoords) #OK self.oldMouseX = self.curMouseX #OK self.oldMouseY = self.curMouseY #OK
def pollInput(self): currentTime = time.clock() elapsedTime = currentTime - self.timer self.timer = currentTime # tempWheelPosition = glfw.GetMouseWheel() # if tempWheelPosition != self.wheelPosition: # self.wheelPosition = tempWheelPosition # self.setView(hm.lookat(hm.identity(), np.array([0.0, 0.0, 55.0 - self.wheelPosition, 1.0], dtype = np.float32), np.array([0.0, 0.0, 0.0, 1.0], dtype = np.float32))) if glfw.GetKey("M"): print "Initializing manual control" self.manualControl = True self.context.scene.transform = hm.translation(hm.identity(), [0, 0, 60]) mouseX, mouseY = glfw.GetMousePos() self.calcArcBallVector(mouseX, mouseY) time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey("P"): print "Stopping manual control" self.manualControl = False time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey("A") and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [0, 1, 0]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform ) if glfw.GetKey("D") and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [0, 1, 0]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform ) if glfw.GetKey("W") and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [1, 0, 0]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform ) if glfw.GetKey("S") and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [1, 0, 0]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform ) if glfw.GetKey("Q") and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [0, 0, 1]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform ) if glfw.GetKey("E") and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [0, 0, 1]), self.context.scene.transform ) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform ) if glfw.GetKey("1"): print "1 pressed" if glfw.GetKey("2"): print "2 pressed" if glfw.GetKey("3"): print "3 pressed" if glfw.GetKey("X"): self.context.scene.dump() time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey("T"): self.context.task.toggle() time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey("I"): inputSnapshot = self.context.cubeTracker.imageIn # grab current input image as snapshot cv2.imshow("Input snapshot", inputSnapshot) # show snapshot in a window # cv2.imwrite(self.input_snapshot_file, inputSnapshot) # write snapshot to file (NOTE doesn't work!) # print "Input snapshot saved to {}".format(self.input_snapshot_file) time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey(glfw.KEY_ESC): if not self.quitting: self.doQuit = True self.quitting = True if not self.leftPressed and glfw.GetMouseButton(glfw.MOUSE_BUTTON_LEFT): self.leftPressed = True self.context.scene.hideCube = not self.context.scene.hideCube if not glfw.GetMouseButton(glfw.MOUSE_BUTTON_LEFT): self.leftPressed = False if not self.rightPressed and glfw.GetMouseButton(glfw.MOUSE_BUTTON_RIGHT): self.rightPressed = True self.oldMouseX, self.oldMouseY = glfw.GetMousePos() self.curMouseX = self.oldMouseX self.curMouseY = self.oldMouseY if not glfw.GetMouseButton(glfw.MOUSE_BUTTON_RIGHT): self.rightPressed = False if self.rightPressed: # OK self.curMouseX, self.curMouseY = glfw.GetMousePos() # OK if self.curMouseX != self.oldMouseX or self.curMouseY != self.oldMouseY: # OK oldVec = self.calcArcBallVector(self.oldMouseX, self.oldMouseY) # OK curVec = self.calcArcBallVector(self.curMouseX, self.curMouseY) # OK angle = math.acos(min(1.0, np.dot(oldVec, curVec))) # OK cameraAxis = np.cross(oldVec, curVec) # OK cameraAxis /= np.linalg.norm(cameraAxis, ord=2) # normalize cameraAxis to be a unit vector cameraToObjectCoords = np.linalg.inv( np.dot(self.context.renderer.view[:-1, :-1], self.context.scene.transform[:-1, :-1]) ) # ??? cameraAxisObjectCoords = np.dot(cameraToObjectCoords, cameraAxis) # OK self.context.scene.transform = hm.rotation( self.context.scene.transform, math.degrees(angle), cameraAxisObjectCoords ) # OK self.oldMouseX = self.curMouseX # OK self.oldMouseY = self.curMouseY # OK
def resizeWindow(width, height): global mvp, modelview_mat, perspective_mat glViewport(0, 0, width, height) perspective_mat = hm.perspective(hm.identity(), 70, float(width) / height, 0.1, 10.0) mvp = np.dot(perspective_mat, modelview_mat)
1, 1,-1, 1, 1, 1, 1, 0, #5 -1, 1,-1, 1, 0, 1, 1, 0, #6 1, 1, 1, 1, 1, 1, 0, 0, #7 -1, 1, 1, 1, 0, 1, 0, 0, #8 -1,-1, 1, 1, 0, 0, 0, 0, #9 -1, 1,-1, 1, 0, 1, 1, 0, #10 -1,-1,-1, 1, 0, 0, 1, 0, #11 1,-1,-1, 1, 1, 0, 1, 0, #12 -1,-1, 1, 1, 0, 0, 0, 0, #13 1,-1, 1, 1, 1, 0, 0, 0, #14 ], dtype = np.float32) campos = np.array([2.5, 1.5, 2.5, 1], dtype = np.float32) center = np.array([0.0,0.0,0.0,1.0], dtype = np.float32) modelview_mat = hm.lookat(hm.identity(), campos, center) perspective_mat = None mvp = None def resizeWindow(width, height): global mvp, modelview_mat, perspective_mat glViewport(0, 0, width, height) perspective_mat = hm.perspective(hm.identity(), 70, float(width) / height, 0.1, 10.0) mvp = np.dot(perspective_mat, modelview_mat) running = True # A light example for a key callback. def keypress(key, action): if key == GLFW_KEY_ESC: global running
0, #13 1, -1, 1, 1, 1, 0, 0, 0, #14 ], dtype=np.float32) campos = np.array([2.5, 1.5, 2.5, 1], dtype=np.float32) center = np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float32) modelview_mat = hm.lookat(hm.identity(), campos, center) perspective_mat = None mvp = None def resizeWindow(width, height): global mvp, modelview_mat, perspective_mat glViewport(0, 0, width, height) perspective_mat = hm.perspective(hm.identity(), 70, float(width) / height, 0.1, 10.0) mvp = np.dot(perspective_mat, modelview_mat) running = True