Created
July 17, 2017 12:06
-
-
Save ruoyu0088/7279eb6a4847a9df2beac9e4b5dcf1e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import math, os, pyglet, sys | |
from pyglet.gl import * | |
class World(pyglet.window.Window): | |
def __init__(self, scale=10, center_pos=(0, 0, -5), speed=1.0, | |
*args, **kwargs): | |
super(World, self).__init__(*args, **kwargs) | |
self.scale = scale | |
self.center_pos = center_pos | |
self.speed = speed | |
glClearColor(1.0, 1.0, 1.0, 0.0) | |
glEnable(GL_DEPTH_TEST) | |
self.load_textures() | |
self.clock = 0 | |
pyglet.clock.schedule_interval(self.update, 1 / 60.0) | |
self.rotate_x = 0 | |
def load_textures(self): | |
image = pyglet.image.load(os.path.join("imgs", "chessboard.png")) | |
self.texture = image.get_texture() | |
glEnable(self.texture.target) | |
def on_key_press(self, symbol, modifiers): | |
pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png') | |
def update(self, _): | |
self.on_draw() | |
self.clock += .01 | |
def on_draw(self): | |
glClearColor(0.5, 0.5, 0.5, 1) | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glLoadIdentity() | |
self.draw_images() | |
def draw_images(self): | |
glPushMatrix() | |
glTranslatef(0, 0, -40) | |
glRotatef( 10, 1, 0, 0) | |
glRotatef( 30, 0, 1, 0) | |
w = 10 | |
h = 10 | |
z = 0 | |
#glBindTexture(self.texture.target, self.texture.id) | |
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, | |
('v3f', (-w, -h, z, w, -h, z, w, h, z, -w, h, z)), | |
('t2f', (0, 0, 1, 0, 1, 1, 0, 1)) | |
) | |
glPopMatrix() | |
def draw_images2(self): | |
angle_base = (self.clock * self.speed * 50) % 360 | |
angle_delta = 360. / len(self.textures) | |
glPushMatrix() | |
dx = dy = dz = 0 | |
dz = -200 | |
texture = self.textures[0] | |
glTranslatef(dx + self.center_pos[0], self.center_pos[1], | |
dz + self.center_pos[2]) | |
glRotatef(self.rotate_x, 1, 0, 0) | |
self.rotate_x += 1 | |
glBindTexture(texture.target, texture.id) | |
rect_w = 6 | |
rect_h = 5 | |
z = 0 | |
glBegin(GL_QUADS) | |
glTexCoord2f(0.0, 0.0); glVertex3f(-rect_w, -rect_h, z) | |
glTexCoord2f(1.0, 0.0); glVertex3f( rect_w, -rect_h, z) | |
glTexCoord2f(1.0, 1.0); glVertex3f( rect_w, rect_h, z) | |
glTexCoord2f(0.0, 1.0); glVertex3f(-rect_w, rect_h, z) | |
glEnd() | |
glPopMatrix() | |
def on_resize(self, width, height): | |
glViewport(0, 0, width, height) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(30.0, width / float(height), 0.1, 1000.0) | |
glMatrixMode(GL_MODELVIEW) | |
if __name__ == "__main__": | |
window = World(width=800, height=600) | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment