3d gui in python

Solutions on MaxInterview for 3d gui in python by the best coders in the world

showing results for - "3d gui in python"
Elena
06 Jan 2019
1import pyglet
2from pyglet.gl import *
3from pyglet.window import key
4from OpenGL.GLUT import *
5
6WINDOW   = 400
7INCREMENT = 5
8
9class Window(pyglet.window.Window):
10
11   # Cube 3D start rotation
12   xRotation = yRotation = 30  
13
14   def __init__(self, width, height, title=''):
15       super(Window, self).__init__(width, height, title)
16       glClearColor(0, 0, 0, 1)
17        glEnable(GL_DEPTH_TEST)  
18
19   def on_draw(self):
20       # Clear the current GL Window
21       self.clear()
22
23       # Push Matrix onto stack
24       glPushMatrix()
25
26       glRotatef(self.xRotation, 1, 0, 0)
27       glRotatef(self.yRotation, 0, 1, 0)
28
29       # Draw the six sides of the cube
30       glBegin(GL_QUADS)
31
32       # White
33       glColor3ub(255, 255, 255)
34       glVertex3f(50,50,50)
35
36       # Yellow
37       glColor3ub(255, 255, 0)
38       glVertex3f(50,-50,50)
39
40       # Red
41       glColor3ub(255, 0, 0)
42       glVertex3f(-50,-50,50)
43       glVertex3f(-50,50,50)
44
45       # Blue
46       glColor3f(0, 0, 1)
47       glVertex3f(-50,50,-50)
48
49         # <… more color defines for cube faces>
50
51       glEnd()
52
53       # Pop Matrix off stack
54       glPopMatrix()
55
56   def on_resize(self, width, height):
57       # set the Viewport
58       glViewport(0, 0, width, height)
59
60       # using Projection mode
61       glMatrixMode(GL_PROJECTION)
62       glLoadIdentity()
63
64       aspectRatio = width / height
65       gluPerspective(35, aspectRatio, 1, 1000)
66
67       glMatrixMode(GL_MODELVIEW)
68       glLoadIdentity()
69       glTranslatef(0, 0, -400)
70
71
72   def on_text_motion(self, motion):
73       if motion == key.UP:
74            self.xRotation -= INCREMENT
75       elif motion == key.DOWN:
76           self.xRotation += INCREMENT
77       elif motion == key.LEFT:
78           self.yRotation -= INCREMENT
79       elif motion == key.RIGHT:
80           self.yRotation += INCREMENT
81
82            
83if __name__ == '__main__':
84   Window(WINDOW, WINDOW, 'Pyglet Colored Cube')
85   pyglet.app.run()Copy