Created
January 24, 2020 07:02
-
-
Save kkent030315/e18d011cae47f51cb002c267d3446205 to your computer and use it in GitHub Desktop.
PyPixelBox Project Sample01
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 * | |
#ワールドクラス | |
class World: | |
def __init__(self): | |
#Vertexを保管する場所 | |
self.batch = pyglet.graphics.Batch() | |
#3次元ワールド座標を定義 | |
x = 0.5 | |
y = 0 | |
z = -2 | |
#テクスチャーをロード | |
self.loaded_texture = self.load_texture('texture.png') | |
texture_coordinates = ('t2f', (0, 0, | |
1, 0, | |
1, 1, | |
0, 1,)) | |
#バッチ(Vertexリスト)に追加 | |
self.batch.add(4, GL_QUADS, self.loaded_texture, | |
('v3f', (x, y, z, | |
x, y, z+1, | |
x, y+1, z+1, | |
x, y+1, z,)), texture_coordinates) | |
#描画を行う | |
def draw(self): | |
self.batch.draw()#バッチの中にあるVertexを全て描画 | |
#テクスチャをロードする関数 | |
def load_texture(self, path): | |
texture = pyglet.image.load(path).get_texture()#pygletのテクスチャローダーを使います | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) | |
return pyglet.graphics.TextureGroup(texture) | |
#pyglet.window.Window を継承 | |
class Window(pyglet.window.Window): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
#ワールドクラスの初期化 | |
self.world = World() | |
def on_draw(self): | |
#スクリーンをクリア | |
self.clear() | |
#ワールドは3次元なので、3D描画モードに設定 | |
self.render_mode_3d() | |
#ワールドを描画 | |
self.world.draw() | |
def render_mode_3d(self): | |
self.render_mode_projection() | |
#視野を設定 | |
gluPerspective(70, self.width / self.height, 0.05, 1000) | |
self.render_mode_modelview() | |
def render_mode_2d(self): | |
self.render_mode_projection() | |
#描画領域 0からwindow_width、 0からwindow_height | |
gluOrtho2D(0, self.width, 0, self.height) | |
self.render_mode_modelview() | |
def render_mode_projection(self): | |
glMatrixMode(GL_PROJECTION)#投影変換モード | |
glLoadIdentity()#変換処理の累積を消去 | |
def render_mode_modelview(self): | |
glMatrixMode(GL_MODELVIEW)#モデリング変換モード(視野変換) | |
glLoadIdentity()#変換処理の累積を消去 | |
if __name__ == '__main__': | |
window = Window(width=1280, height=720, caption='PyPixelBox', resizable=True) | |
glClearColor(0.5, 0.7, 1, 1)#クリア時の色を空色に設定 | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment