Last active
November 8, 2015 22:38
-
-
Save kenwi/fe9f93e334a435919c03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyglet.gl import * | |
#from numpy import linalg as la | |
import numpy as np | |
__author__ = 'kenwi' | |
class Camera(object): | |
settings = { | |
'fov': 90.0, | |
'near': 0.1, | |
'far': 500.0, | |
'height': 0, | |
'width': 0, | |
'mode': 'perspective' # or orthographic | |
} | |
inited = False | |
position = np.matrix([0.0, 0.0, -1.5, 1.0]) # x,y,z,w | |
def place_box(self, x, y, z): | |
self.draw_box() | |
def draw_box(self): | |
pyglet.graphics.draw_indexed(8, pyglet.gl.GL_LINES, | |
[0, 1, 1, 2, 2, 3, 3, 0, # front square | |
4, 5, 5, 6, 6, 7, 7, 4, # back square | |
0, 4, 1, 5, 2, 6, 3, 7], # connectors | |
('v3f', (-1, -1, 0, | |
1, -1, 0, | |
1, 1, 0, | |
-1, 1, 0, | |
-1, -1, -1, | |
1, -1, -1, | |
1, 1, -1, | |
-1, 1, -1))) | |
def draw(self): | |
glClear(GL_COLOR_BUFFER_BIT) | |
m = np.matrix([[1.0, 0.0, 0.0, self.position.item(0)], | |
[0.0, 1.0, 0.0, self.position.item(1)], | |
[0.0, 0.0, 1.0, self.position.item(2)], | |
[0.0, 0.0, 0.0, 1.0]]) | |
v = np.array([0.0, 0.0, -0.01, 1.0]) | |
self.position = np.dot(m, v) | |
for x in range(-5, 5, 2): | |
for y in range(-5, 5, 2): | |
glLoadIdentity() | |
glTranslatef(self.position.item(0)+x, self.position.item(1)+y, self.position.item(2)) | |
self.draw_box() | |
def resize(self, width, height): | |
if not self.inited: | |
self.init_camera(width, height) | |
self.inited = True | |
else: | |
self.settings['height'] = height | |
self.settings['width'] = width | |
print "Resized: %r %r" % (width, height) | |
def init_camera(self, width, height): | |
self.settings['width'] = width | |
self.settings['height'] = height | |
self.init_opengl() | |
self.setup_perspective() | |
print "Initialized camera" | |
def init_opengl(self): | |
glEnable(GL_BLEND) | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
glDepthFunc(GL_LEQUAL) | |
print "Initialized opengl" | |
def config_projection(self): | |
if self.settings['mode'] == 'perspective': | |
self.setup_perspective() | |
elif self.settings['mode'] == 'orthographic': | |
self.setup_orthographic() | |
def setup_perspective(self): | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(self.settings['fov'], float(self.settings['width'])/self.settings['height'], | |
self.settings['near'], self.settings['far']) | |
glMatrixMode(GL_MODELVIEW) | |
#print "Setup perspective projection" | |
def setup_orthographic(self): | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
glOrtho(0, self.settings['width'], 0, self.settings['height'], -1, 1) | |
glMatrixMode(GL_MODELVIEW) | |
class World(object): | |
updateInterval = 20 # per second | |
def update(self, dt): | |
""" | |
:param dt: | |
:return: | |
""" | |
#print "World update call dt: %r" % dt | |
class Window(pyglet.window.Window): | |
def __init__(self, *args, **kwargs): | |
super(Window, self).__init__(*args, **kwargs) | |
self.world = World() | |
self.camera = Camera() | |
self.on_resize = self.camera.resize | |
pyglet.clock.schedule_interval(self.world.update, 1.0/self.world.updateInterval) | |
def on_draw(self): | |
self.camera.config_projection() | |
self.camera.draw() | |
if __name__ == '__main__': | |
win = Window(width=800, height=600, caption='pyglet', resizable=True) | |
pyglet.app.run() | |
""" | |
def swap(a, b): | |
return b, a | |
def swap(a, b): | |
a ^= b | |
b ^= a | |
a ^= b | |
return a, b | |
a = 1 | |
b = 2 | |
a, b = swap(a, b) | |
print a, b | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment