File: opengl_window.py

package info (click to toggle)
netgen 6.2.2501%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,980 kB
  • sloc: cpp: 165,197; tcl: 6,310; python: 2,804; sh: 522; makefile: 87
file content (57 lines) | stat: -rw-r--r-- 1,954 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

class Window():
    xold = -1;
    yold = -1;
    mode = 'r'
    modes = {0:'r', 1:'m', 2:'z'}
    drawfunc = None
    mousefunc = None

    def draw(self):
        glutSetWindow(self.handle)
        self.drawfunc()

    def __init__( self, name=b"Window", width=500, height=500, drawfunc=None, mousefunc=None ):
#        glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS) 
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
        glutInitWindowSize(width, height)                      # set window size
        glutInitWindowPosition(0, 0)                           # set window position
        self.handle = glutCreateWindow(b"ngs")                      # create window with title
        glutMotionFunc(self.motionHandler)
        glutMouseFunc(self.mouseHandler)
        glutPassiveMotionFunc(self.passiveMotionHandler)
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        pnear = 0.1;
        pfar = 10;
        gluPerspective(20.0, 1.0*width / height, pnear, pfar);
        glViewport(0, 0, width, height);
        glMatrixMode(GL_MODELVIEW);
        self.drawfunc = drawfunc
        self.mousefunc = mousefunc
        if drawfunc:
            glutDisplayFunc(self.draw)                               # set draw function callback

    def motionHandler(self, x, y ):
        self.mousefunc(self.xold,self.yold, x,y, self.mode)  # 'm','z'
        self.xold = x
        self.yold = y
        glutPostRedisplay()

    def passiveMotionHandler(self, x, y ):
        self.xold = x
        self.yold = y

    def mouseHandler(self, button, state, x, y ):
        print(button,state,x,y)
        if button<3:
            if state==0:
                self.mode = self.modes[button]
            else:
                self.mode = 'r'


glutInit("mainwin")                                   # initialize glut