Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Combine pygame and OpenGL to demonstrate a range
- # of control and drawing options
- # 23_12_21
- import pygame
- from pygame.locals import *
- from OpenGL.GL import *
- from OpenGL.GLU import *
- import sys
- print('\nLEFT, RIGHT, UP, DOWN: move cube in X or Y directions\n')
- print('CTRL-UP, CTRL-DOWN moves cube in Z direction\n')
- print('SPACE: toggle cube recede\n')
- print('CTRL-m: toggle cursor (two methods)\n')
- print('w: toggle wireframe\n')
- print('MOUSEWHEEL: move cube in Z direction (two methods)\n')
- print('Left mouse button double click: toggle rotation\n')
- print('Left mouse button: move cube around in X, Y directions\n')
- print('ESC, RETURN, and middle mouse button: exit\n')
- '''
- NOTES:
- can't use screen.fill(col) on OpenGL "surfaces"
- can't use pygame.display.update() on OpenGL
- glVertex3f(*q]) ≍ glVertex3fv(q) where q is (x,y,z)
- x = not x ≍ x ^= 1
- COLLECT INFO:
- MOUSE = pygame.mouse.get_pressed()
- KEYS = pygame.key.get_pressed()
- MODS = pygame.key.get_mods()
- PYGAME EXIT TEMPLATE:
- for event in pygame.event.get():
- # the X button
- if event.type == QUIT:
- pygame.quit()
- sys.exit() # OR running = False OR return
- if event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- pygame.quit()
- sys.exit() # OR running = False OR return
- FPS CONTROL:
- FPS = 30
- FPSCLOCK = pygame.time.Clock()
- ...
- FPSCLOCK.tick(FPS)
- OR...
- pygame.time.Clock().tick(1000./15.)
- OR...
- pygame.time.wait(15)
- THE CUBE:
- 6 +--------+ 7
- /| /|
- / | / |
- / | / |
- 3 +--------+ 2 |
- | 5 +----|---+ 4
- | / | /
- | / | /
- |/ |/
- 0 +--------+ 1
- '''
- vertices = ((-1,-1,1), (1,-1,1), (1,1,1), (-1,1,1),
- (1,-1,-1), (-1,-1,-1), (-1,1,-1), (1,1,-1))
- faces = ((0,1,2,3), (1,4,7,2), (4,5,6,7),
- (5,0,3,6), (3,2,7,6), (5,4,1,0))
- colors = ((1,0,0), (0,0,1), (1,1,0), (1,0,1), (0,1,0), (0,1,1))
- edges = ((0,1), (0,3), (0,5), (1,2), (1,4), (2,3),
- (2,7), (3,6), (4,5), (4,7), (5,6), (6,7))
- def solid_cube():
- glBegin(GL_QUADS)
- for face in range(6):
- glColor3f(*colors[face])
- for i in faces[face]:
- glVertex3f(*vertices[i])
- glEnd()
- def wireframe_cube():
- glLineWidth(4)
- glBegin(GL_LINES)
- for e in edges:
- for v in e:
- glVertex3f(*vertices[v])
- glEnd()
- def make_chessboard():
- g.chessboard = glGenLists(1)
- glNewList(g.chessboard, GL_COMPILE)
- xgridsize, zgridsize = 8, 8
- sizex, sizez = 110, 110
- y = -110
- startz = -2
- glBegin(GL_QUADS)
- for x in range(-xgridsize//2, xgridsize//2):
- for z in range(startz, -(zgridsize-startz), -1):
- n = x+z & 1
- glColor3f(n, n, n)
- glVertex3f( x*sizex, y, z*sizez)
- glVertex3f((x+1)*sizex, y, z*sizez)
- glVertex3f((x+1)*sizex, y, (z+1)*sizez)
- glVertex3f( x*sizex, y, (z+1)*sizez)
- glEnd()
- glEndList()
- def main():
- pygame.init()
- display = (1000, 800)
- screen = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
- pygame.display.set_caption('ESC or RETURN or middle button exits')
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- glEnable(GL_LINE_SMOOTH)
- glEnable(GL_POLYGON_SMOOTH)
- glEnable(GL_DEPTH_TEST)
- glDepthFunc(GL_LESS)
- make_chessboard()
- angle = 1.
- x = 0.
- y = 0.
- z = -6.
- RECEDE_FLAG = 0
- MOUSE_FLAG = 1
- pygame.mouse.set_visible(MOUSE_FLAG)
- MOUSEWHEEL_METHOD = 'method1'
- MODIFER_METHOD = 'method2'
- WIREFRAME_FLAG = 0
- ROTATION_FLAG = 1
- mousex, mousey = pygame.mouse.get_pos()
- TIMER_EVENT = USEREVENT
- clicked = 0
- TIMEOUT = 400 # milliseconds
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- return
- if event.type == KEYDOWN:
- if event.key in (K_ESCAPE, K_RETURN, K_q):
- pygame.quit()
- return
- if event.key == K_w:
- WIREFRAME_FLAG ^= 1
- if event.key == K_SPACE:
- RECEDE_FLAG ^= 1
- if event.key == K_m:
- if MODIFER_METHOD == 'method1':
- mods = pygame.key.get_mods()
- if mods & KMOD_CTRL:
- MOUSE_FLAG ^= 1
- pygame.mouse.set_visible(MOUSE_FLAG)
- elif MODIFER_METHOD == 'method2':
- if event.mod & KMOD_CTRL:
- MOUSE_FLAG ^= 1
- pygame.mouse.set_visible(MOUSE_FLAG)
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- if clicked:
- ROTATION_FLAG ^= 1
- else:
- clicked = 1
- pygame.time.set_timer(TIMER_EVENT, TIMEOUT)
- elif event.type == TIMER_EVENT:
- clicked = 0
- pygame.time.set_timer(TIMER_EVENT, 0)
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 2:
- pygame.quit()
- return
- if MOUSEWHEEL_METHOD == 'method1':
- # MOUSEWHEEL FORWARD
- if event.button == 4:
- z -= .5
- # MOUSEWHEEL BACKWARD
- if event.button == 5:
- z += .5
- if MOUSEWHEEL_METHOD == 'method2':
- if event.type == MOUSEWHEEL:
- # event.y = +1: FORWARD
- # event.y = -1: BACKWARD
- z -= event.y / 2.
- MOUSE = pygame.mouse.get_pressed()
- if MOUSE[0]:
- x1, y1 = pygame.mouse.get_rel()
- x += x1 / 100
- y -= y1 / 100
- else:
- mousex, mousey = pygame.mouse.get_rel()
- KEYS = pygame.key.get_pressed()
- if KEYS[K_LEFT]:
- x -= 0.05
- if KEYS[K_RIGHT]:
- x += 0.05
- MODS = pygame.key.get_mods()
- if KEYS[K_UP]:
- if MODS & KMOD_CTRL:
- z -= .4
- else:
- y += 0.05
- if KEYS[K_DOWN]:
- if MODS & KMOD_CTRL:
- z += .4
- else:
- y -= 0.05
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
- glClearColor(0.1, 0.1, 0.1, 1)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluPerspective(60.0, display[0]/display[1], 1.0, 10000.0)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glCallList(g.chessboard)
- glTranslatef(x, y, z)
- if RECEDE_FLAG:
- z -= 0.07
- glRotatef(angle, 1., 1., 1.)
- if ROTATION_FLAG:
- angle += 1.
- if WIREFRAME_FLAG:
- glColor3f(0.0, 0.0, 1.0)
- wireframe_cube()
- else:
- solid_cube()
- glColor3f(0.0, 0.0, 0.0)
- wireframe_cube()
- pygame.display.flip()
- # pygame.time.wait(15)
- pygame.time.Clock().tick(1000./15.)
- class g: ...
- if __name__ == '__main__':
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement