Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyglet
- import pyglet.gl as pgl
- from pyglet.window import key
- import OpenGL.GL as ogl
- import math
- WINDOW = 1000
- tr = 90
- INCREMENT = 5
- transparant = False
- def draw_small():
- pgl.glBegin(ogl.GL_QUADS)
- pgl.glColor3ub(255, 0, 0)
- pgl.glVertex3f(-150, -100, -50)
- pgl.glVertex3f(-150, -50, -50)
- pgl.glVertex3f(-100, -50, -50)
- pgl.glVertex3f(-100, -100, -50)
- pgl.glColor3ub(0, 255, 0)
- pgl.glVertex3f(-150, -100, -50)
- pgl.glVertex3f(-150, -50, -50)
- pgl.glVertex3f(-150, -50, 0)
- pgl.glVertex3f(-150, -100, 0)
- pgl.glColor3ub(255, 0, 255)
- pgl.glVertex3f(-150, -50, -50)
- pgl.glVertex3f(-150, -50, 0)
- pgl.glVertex3f(-100, -50, 0)
- pgl.glVertex3f(-100, -50, -50)
- pgl.glColor3ub(255, 255, 0)
- pgl.glVertex3f(-100, -100, -50)
- pgl.glVertex3f(-100, -50, -50)
- pgl.glVertex3f(-100, -50, 0)
- pgl.glVertex3f(-100, -100, 0)
- pgl.glColor3ub(0, 0, 255)
- pgl.glVertex3f(-150, -100, -50)
- pgl.glVertex3f(-100, -100, -50)
- pgl.glVertex3f(-100, -100, 0)
- pgl.glVertex3f(-150, -100, 0)
- pgl.glColor3ub(0, 255, 255)
- pgl.glVertex3f(-150, -100, 0)
- pgl.glVertex3f(-100, -100, 0)
- pgl.glVertex3f(-100, -50, 0)
- pgl.glVertex3f(-150, -50, 0)
- pgl.glEnd()
- def draw_big():
- angle = 0
- t = 0
- x = y = z = 0
- a = 50
- b = 100
- if not transparant:
- pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
- else:
- pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)
- pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
- for i in range(0, tr + 1):
- pgl.glColor3ub(255, 0, 0)
- pgl.glVertex3f(x + a * math.cos(i * 2 * math.pi / tr), y + b * math.sin(i * 2 * math.pi / tr), z + 8)
- pgl.glVertex3f(x, y, z + 8)
- pgl.glEnd()
- for j in range(1, 9):
- pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
- for i in range(0, tr + 1):
- pgl.glColor3ub(0, 255, 0)
- x1 = a * math.cos(i * 2 * math.pi / tr)
- y1 = b * math.sin(i * 2 * math.pi / tr)
- x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
- y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
- pgl.glVertex3f(x2, y2, z + j*8)
- x1 = a * math.cos(i * 2 * math.pi / tr)
- y1 = b * math.sin(i * 2 * math.pi / tr)
- x2 = x1 * math.cos((t+16) * math.pi / 360) + y1 * math.sin((t+16) * math.pi / 360)
- y2 = - x1 * math.sin((t+16) * math.pi / 360) + y1 * math.cos((t+16) * math.pi / 360)
- pgl.glVertex3f(x2, y2, z + j * 8 + 8)
- t = t + 16
- pgl.glEnd()
- pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
- pgl.glColor3ub(0, 0, 255)
- t = 128
- angle = angle + t
- for i in range(0, tr + 1):
- x1 = x + a * math.cos(i * 2 * math.pi / tr)
- y1 = y + b * math.sin(i * 2 * math.pi / tr)
- x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
- y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
- pgl.glVertex3f(x2, y2, z + 72)
- pgl.glVertex3f(x, y, z + 72)
- angle = angle + i
- pgl.glEnd()
- class Window(pyglet.window.Window):
- xRotation = yRotation = zRotation = 30
- zoom = 1
- far = 100
- dist = 35
- x = y = z = 0
- def __init__(self, width, height, title = '') :
- super(Window, self).__init__( 1300,1000, title)
- pgl.glClearColor(0, 0, 0, 1)
- pgl.glEnable(pgl.GL_DEPTH_TEST)
- def on_draw(self) :
- self.clear()
- pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
- # объемный куб без проекции правый верхний угол
- pgl.glLoadIdentity()
- pgl.glViewport(650, 500, 650, 500)
- pgl.glMatrixMode(ogl.GL_PROJECTION)
- pgl.glLoadIdentity()
- pgl.gluPerspective(self.dist, 1.3, 1, 1000)
- pgl.glMatrixMode(ogl.GL_MODELVIEW)
- pgl.glTranslatef(0, 0, -400)
- pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
- draw_small()
- pgl.glPushMatrix()
- pgl.glTranslatef(self.x, self.y, self.z)
- pgl.glRotatef(self.xRotation, 1, 0, 0)
- pgl.glRotatef(self.yRotation, 0, 1, 0)
- pgl.glRotatef(self.zRotation, 0, 0, 1)
- pgl.glScalef(self.zoom, self.zoom, self.zoom)
- draw_big()
- pgl.glPopMatrix()
- # фронтальная проекция правый нижний угол
- pgl.glLoadIdentity()
- pgl.glViewport(650, 0, 650, 500)
- pgl.glMatrixMode(pgl.GL_PROJECTION)
- pgl.glLoadIdentity()
- pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 600)
- pgl.glMatrixMode(pgl.GL_MODELVIEW)
- pgl.glLoadIdentity()
- pgl.glTranslatef(0, 0, -100)
- pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
- draw_small()
- pgl.glPushMatrix()
- pgl.glTranslatef(self.x, self.y, self.z)
- pgl.glRotatef(self.xRotation, 1, 0, 0)
- pgl.glRotatef(self.yRotation, 0, 1, 0)
- pgl.glRotatef(self.zRotation, 0, 0, 1)
- pgl.glScalef(self.zoom, self.zoom, self.zoom)
- draw_big()
- pgl.glPopMatrix()
- # верхняя проекция левый верхний угол
- pgl.glLoadIdentity()
- pgl.glViewport(0, 500, 650, 500)
- pgl.glMatrixMode(pgl.GL_PROJECTION)
- pgl.glLoadIdentity()
- pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 600)
- pgl.glMatrixMode(pgl.GL_MODELVIEW)
- pgl.glLoadIdentity()
- pgl.glTranslatef(0, 0, -100)
- pgl.glRotatef(90, 1, 0, 0)
- pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
- draw_small()
- pgl.glPushMatrix()
- pgl.glTranslatef(self.x, self.y, self.z)
- pgl.glRotatef(self.xRotation, 1, 0, 0)
- pgl.glRotatef(self.yRotation, 0, 1, 0)
- pgl.glRotatef(self.zRotation, 0, 0, 1)
- pgl.glScalef(self.zoom, self.zoom, self.zoom)
- draw_big()
- pgl.glPopMatrix()
- # боковая проекция левый нижний угол
- pgl.glLoadIdentity()
- pgl.glViewport(0, 0, 650, 500)
- pgl.glMatrixMode(pgl.GL_PROJECTION)
- pgl.glLoadIdentity()
- pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 600)
- pgl.glMatrixMode(pgl.GL_MODELVIEW)
- pgl.glLoadIdentity()
- pgl.glTranslatef(0, 0, -100)
- pgl.glRotatef(-90, 0, 1, 0)
- pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
- draw_small()
- pgl.glPushMatrix()
- pgl.glTranslatef(self.x, self.y, self.z)
- pgl.glRotatef(self.xRotation, 1, 0, 0)
- pgl.glRotatef(self.yRotation, 0, 1, 0)
- pgl.glRotatef(self.zRotation, 0, 0, 1)
- pgl.glScalef(self.zoom, self.zoom, self.zoom)
- draw_big()
- pgl.glPopMatrix()
- def on_text_motion(self, motion):
- if motion == key.UP:
- self.xRotation -= INCREMENT
- elif motion == key.DOWN:
- self.xRotation += INCREMENT
- elif motion == key.LEFT:
- self.yRotation -= INCREMENT
- elif motion == key.RIGHT:
- self.yRotation += INCREMENT
- def on_key_press(self, symbol, modifiers):
- global transparant, dist,tr
- if symbol == key.T and not transparant:
- transparant = True
- elif symbol == key.T and transparant:
- transparant = False
- elif symbol == key.W:
- self.x +=2.5
- elif symbol == key.S:
- self.x -=2.5
- elif symbol == key.A:
- self.y -=2.5
- elif symbol == key.D:
- self.y +=2.5
- elif symbol == key.Q:
- self.z -=2.5
- elif symbol == key.E:
- self.z +=2.5
- elif symbol == key.Z:
- self.zRotation += INCREMENT
- elif symbol == key.X:
- self.zRotation -= INCREMENT
- elif symbol == key.ESCAPE:
- pyglet.app.exit()
- elif symbol == key.P:
- tr +=5
- elif symbol == key.O:
- if tr > 5:
- tr -=5
- def on_mouse_scroll(self,x, y, scroll_x, scroll_y):
- if scroll_y < 0:
- self.zoom += 0.1
- elif scroll_y > 0 and self.zoom - 0.1 >0:
- self.zoom -= 0.1
- Window(WINDOW, WINDOW, 'Cube')
- pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement