예제 #1
0
파일: Actor.py 프로젝트: rbgross/pyTANG
 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
예제 #2
0
파일: Actor.py 프로젝트: Pallavistar/pyTANG
 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
예제 #3
0
    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
예제 #4
0
    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
예제 #5
0
 vertobj = glGenVertexArrays(1)
 glBindVertexArray(vertobj)
 # Setup the VBO (using the fancy VBO Object from pyopengl, doing it "manually" would also be a possibility)
 vertbuf = VBO(cubedata, GL_STATIC_DRAW)
 vertbuf.bind()
 glEnableVertexAttribArray(positionloc)
 glEnableVertexAttribArray(colorloc)
 glVertexAttribPointer(positionloc, 4, GL_FLOAT, GL_TRUE, 8 * 4, vertbuf+0) # "+0" since we need to create an offset.
 glVertexAttribPointer(colorloc, 4, GL_FLOAT, GL_TRUE, 8 * 4, vertbuf+16) # 4 * 4 Bytes per float.
 vertbuf.unbind() # We can unbind the VBO, since it's linked to the VAO
 # glBindVertexArray(0)
 
 running = True
 t = time.time()
 rotation = 0.0
 while running:
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     glUseProgram(prog)
     glUniformMatrix4fv(mvploc, 1, GL_TRUE, hm.rotation(mvp, rotation,[0,1,0]).tolist())
     # glBindVertexArray(vertobj)
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 14)
     glDrawArrays(GL_POINTS, 0, 14)
     glfwSwapBuffers()
     # glfwPollEvents() # This would poll for key/mouse events manually.
     # Do the rotation thing...
     rotation += (time.time() - t) / 5 * 360
     t = time.time()
     # Stop running if window gets closed.
     running = running and glfwGetWindowParam(GLFW_OPENED)
     
 glfwTerminate()
예제 #6
0
    vertbuf = VBO(cubedata, GL_STATIC_DRAW)
    vertbuf.bind()
    glEnableVertexAttribArray(positionloc)
    glEnableVertexAttribArray(colorloc)
    glVertexAttribPointer(positionloc, 4, GL_FLOAT, GL_TRUE, 8 * 4, vertbuf +
                          0)  # "+0" since we need to create an offset.
    glVertexAttribPointer(colorloc, 4, GL_FLOAT, GL_TRUE, 8 * 4,
                          vertbuf + 16)  # 4 * 4 Bytes per float.
    vertbuf.unbind()  # We can unbind the VBO, since it's linked to the VAO
    # glBindVertexArray(0)

    running = True
    t = time.time()
    rotation = 0.0
    while running:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glUseProgram(prog)
        glUniformMatrix4fv(mvploc, 1, GL_TRUE,
                           hm.rotation(mvp, rotation, [0, 1, 0]).tolist())
        # glBindVertexArray(vertobj)
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 14)
        glDrawArrays(GL_POINTS, 0, 14)
        glfwSwapBuffers()
        # glfwPollEvents() # This would poll for key/mouse events manually.
        # Do the rotation thing...
        rotation += (time.time() - t) / 5 * 360
        t = time.time()
        # Stop running if window gets closed.
        running = running and glfwGetWindowParam(GLFW_OPENED)

    glfwTerminate()