pyopengl 过剩输入

发布于 2024-12-18 05:17:43 字数 2917 浏览 1 评论 0原文

包含 GLUT 图形的窗口出现后,我想在终端中输入:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): season winter
# Now changing season to winter
(cmd): event meteor
# Now meteoring otherwise peaceful landscape
(cmd): season summer
# Now changing season to summer
(cmd): exit
#bye ^_^
user@computer:

理想情况下,我想将 python cmd 与 GLUT 的 glutKeyboardFunc 集成。我的尝试失败了(同时允许其中之一,而不是两者。还有窗口或终端是否具有焦点的问题)。

下面是一些示例代码,显示一个旋转的茶壶。目前,按“m”将调用流星善良(存根),但能够输入例如“meteor 500”会更好。

#! /usr/bin/env python
''' 
Code is a reduced version of http://www.seethroughskin.com/blog/?p=771
'''
import OpenGL 
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

import time, sys

class dizzyTea:

    global rotY

    def __init__(self):
        self.main()

    def InitGL(self,Width, Height):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)
        glShadeModel(GL_SMOOTH)  
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()       
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)     

    # The main drawing function. 
    def DrawGLScene(self):
        global rotY
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()                    # Reset The View 
        glTranslatef(-0.5, 0.0, -6.0)
        glRotatef(rotY,0.0,1.0,0.0)
        glutWireTeapot(1.0)
        glScalef(0.3,0.3,0.3)
        glutSwapBuffers()
        rotY += 1.0

    # The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
    def keyPressed(self,*args):

        # If escape is pressed, kill everything.
        if args[0] == '\x1b':
            sys.exit()
        elif args[0] == 'm':
            print "Now meteoring otherwise peaceful teapot"
            # meteor shenanigans

    def main(self):
        global window
        global rotY
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(640, 480)
        glutInitWindowPosition(0, 0)
        window = glutCreateWindow("Jeff Molofee's desecrated GL Code Tutorial")
        glutDisplayFunc(self.DrawGLScene)
        glutIdleFunc(self.DrawGLScene)
        glutKeyboardFunc(self.keyPressed)
        self.InitGL(800, 600)
        rotY = 0.0
        glutMainLoop()

if __name__ == "__main__":
    x = dizzyTea()

我可以使用 glutKeyboardFunc 将字符收集到全局字符串中,提供相同的功能效果,但用户会盲打。 “print somestring,”允许在同一行上打印,但是逗号意味着在键入时不会显示输出。另外,“print '\b'”(退格键)并不普遍工作......

基本上我不想:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): s
(cmd): se
(cmd): sea
(cmd): seas
...etc

输入一个命令

使用限制:

  • pyopengl
  • GLUT

(尽管其他答案对于任性的未来人来说是受欢迎的)寻求解决不同的问题)

After the window containing GLUT graphics appears, I would like to enter input in the terminal:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): season winter
# Now changing season to winter
(cmd): event meteor
# Now meteoring otherwise peaceful landscape
(cmd): season summer
# Now changing season to summer
(cmd): exit
#bye ^_^
user@computer:

Ideally I would like to integrate python cmd with GLUT's glutKeyboardFunc. My attempts failed (allows one or the other at once, not both. Also problems with whether the window or terminal had focus).

Here is some example code, which displays a spinning teapot. Currently, pressing 'm' will invoke meteory goodness (stub), but being able to enter e.g. "meteor 500" would be preferable.

#! /usr/bin/env python
''' 
Code is a reduced version of http://www.seethroughskin.com/blog/?p=771
'''
import OpenGL 
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

import time, sys

class dizzyTea:

    global rotY

    def __init__(self):
        self.main()

    def InitGL(self,Width, Height):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)
        glShadeModel(GL_SMOOTH)  
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()       
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)     

    # The main drawing function. 
    def DrawGLScene(self):
        global rotY
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()                    # Reset The View 
        glTranslatef(-0.5, 0.0, -6.0)
        glRotatef(rotY,0.0,1.0,0.0)
        glutWireTeapot(1.0)
        glScalef(0.3,0.3,0.3)
        glutSwapBuffers()
        rotY += 1.0

    # The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
    def keyPressed(self,*args):

        # If escape is pressed, kill everything.
        if args[0] == '\x1b':
            sys.exit()
        elif args[0] == 'm':
            print "Now meteoring otherwise peaceful teapot"
            # meteor shenanigans

    def main(self):
        global window
        global rotY
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(640, 480)
        glutInitWindowPosition(0, 0)
        window = glutCreateWindow("Jeff Molofee's desecrated GL Code Tutorial")
        glutDisplayFunc(self.DrawGLScene)
        glutIdleFunc(self.DrawGLScene)
        glutKeyboardFunc(self.keyPressed)
        self.InitGL(800, 600)
        rotY = 0.0
        glutMainLoop()

if __name__ == "__main__":
    x = dizzyTea()

I can collect characters into a global string using glutKeyboardFunc, giving the same functional effect, however the user would be typing blind. "print somestring," allows printing on the same line, however the comma means the output does not get displayed whilst typing. Also "print '\b'" (backspace) doesn't work universally...

basically I don't want to have:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): s
(cmd): se
(cmd): sea
(cmd): seas
...etc

to type out one command

Restriction of using:

  • pyopengl
  • GLUT

(Though other answers are welcome for wayward future-people looking to solve a different problem)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-12-25 05:17:43
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def keyPressed(self,*args):
    if args[0] == '\x08':
        self.keyCache = self.keyCache[:-1]
    elif args[0] == '\x1b':
        sys.exit()
    elif args[0] == 'm':
        print "Now meteoring otherwise peaceful teapot"
        # meteor shenanigans
    else:
        self.keyCache += args[0]
    sys.stdout.write(self.keyCache +"                                                  \r")#print "keypress: <",self.keyCache,">"
    sys.stdout.flush()

并添加一个新的类变量“keyCache”。

然后只需使用正常的打印刷新将数据写入同一行即可。唯一的问题是,您必须在缓存的击键后写入几个空格,否则当您使用退格键时,删除的元素仍将出现在屏幕上。

另一种选择是并行键盘线程,仅用于处理来自命令行的击键。最大的问题是 glut 不能在窗口关闭时提供良好的回调,因此您必须找到一种替代方法来终止线程。

# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def keyPressed(self,*args):
    if args[0] == '\x08':
        self.keyCache = self.keyCache[:-1]
    elif args[0] == '\x1b':
        sys.exit()
    elif args[0] == 'm':
        print "Now meteoring otherwise peaceful teapot"
        # meteor shenanigans
    else:
        self.keyCache += args[0]
    sys.stdout.write(self.keyCache +"                                                  \r")#print "keypress: <",self.keyCache,">"
    sys.stdout.flush()

And add a new class variable 'keyCache'.

Then just use normal print flushing to write data to the same line. The only hacky bit is that you have to write several blank spaces after the cached keystrokes, otherwise when you use backspace the deleted elements will still be on the screen.

Another alternative would be a parallel keyboard thread just to handle keystrokes from the command line. The big problem there is that glut doesn't provide a nice call back for when the window closes so you'd have to figure out an alternative way to kill your thread.

无法回应 2024-12-25 05:17:43

您应该使用 cmd [0] 框架来构建解释器,并在单独的线程中启动 cmd.cmdloop 。
0. http://docs.python.org/3/library/cmd.html

you should use the cmd [0] framework to build your interpreter, and launch cmd.cmdloop in a separate thread.
0. http://docs.python.org/3/library/cmd.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文