From a2aac66072e1037a90172a366f43beb5886cf245 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 28 Sep 2011 10:44:12 +0900 Subject: [PATCH] first commit --- examples/opengl/__init__.py | 61 ++++++++++++++++++++++++++++++++++++++++ examples/opengl/baseview.py | 59 +++++++++++++++++++++++++++++++++++++++ examples/opengl/rokuro.py | 68 +++++++++++++++++++++++++++++++++++++++++++++ examples/pymeshviewer.py | 61 ++++++++++++++++++++++++++++++++++++++++ examples/togl.py | 33 ++++++++++++++++++++++ examples/triangle.py | 20 +++++++++++++ 6 files changed, 302 insertions(+) create mode 100644 examples/opengl/__init__.py create mode 100644 examples/opengl/baseview.py create mode 100644 examples/opengl/rokuro.py create mode 100644 examples/pymeshviewer.py create mode 100644 examples/togl.py create mode 100644 examples/triangle.py diff --git a/examples/opengl/__init__.py b/examples/opengl/__init__.py new file mode 100644 index 0000000..75c50a2 --- /dev/null +++ b/examples/opengl/__init__.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# coding: utf-8 + +from OpenGL.GL import * +import re +from .baseview import * + + +DELEGATE_PATTERN=re.compile('^on[A-Z]') + +class BaseController(object): + def __init__(self, view, root): + self.view=view + self.root=root + self.isInitialized=False + self.delegate(view) + self.delegate(root) + + def delegate(self, to): + for name in dir(to): + if DELEGATE_PATTERN.match(name): + method = getattr(to, name) + setattr(self, name, method) + + def onUpdate(*args):pass + def onLeftDown(*args):pass + def onLeftUp(*args):pass + def onMiddleDown(*args):pass + def onMiddleUp(*args):pass + def onRightDown(*args):pass + def onRightUp(*args):pass + def onMotion(*args):pass + def onResize(*args):pass + def onWheel(*args):pass + def onKeyDown(*args):pass + def onInitialize(*args):pass + + def initialize(self): + self.view.onResize() + glEnable(GL_DEPTH_TEST) + # ‰Šú‰»Žž‚̌Ăяo‚µ + self.onInitialize() + + def draw(self): + if not self.isInitialized: + self.initialize() + self.isInitialized=True + # OpenGLƒoƒbƒtƒ@‚̃NƒŠƒA + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + # “Š‰es—ñ‚̃NƒŠƒA + glMatrixMode(GL_PROJECTION) + glLoadIdentity() + self.view.updateProjection() + # ƒ‚ƒfƒ‹ƒrƒ…[s—ñ‚̃NƒŠƒA + glMatrixMode(GL_MODELVIEW) + glLoadIdentity() + # OpenGL•`‰æ + self.view.updateView() + self.root.draw() + glFlush() + diff --git a/examples/opengl/baseview.py b/examples/opengl/baseview.py new file mode 100644 index 0000000..a029dc5 --- /dev/null +++ b/examples/opengl/baseview.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# coding: utf-8 + +from OpenGL.GL import * + +class BaseView(object): + def __init__(self): + self.x=0 + self.y=0 + self.w=1 + self.h=1 + self.isLeftDown=False + self.isMiddelDown=False + self.isRightDown=False + + def updateProjection(self): + pass + + def updateView(self): + pass + + def onResize(self, w=None, h=None): + self.w=w or self.w + self.h=h or self.h + glViewport(0, 0, self.w, self.h) + + def onLeftDown(self, x, y): + self.isLeftDown=True + self.x=x + self.y=y + + def onLeftUp(self, x, y): + self.isLeftDown=False + + def onMiddleDown(self, x, y): + self.isMiddelDown=True + self.x=x + self.y=y + + def onMiddleUp(self, x, y): + self.isMiddelDown=False + + def onRightDown(self, x, y): + self.isRightDown=True + self.x=x + self.y=y + + def onRightUp(self, x, y): + self.isRightDown=False + + def onMotion(self, x, y): + print("onMotion", x, y) + + def printMatrix(self, m): + print(m[0][0], m[0][1], m[0][2], m[0][3]) + print(m[1][0], m[1][1], m[1][2], m[1][3]) + print(m[2][0], m[2][1], m[2][2], m[2][3]) + print(m[3][0], m[3][1], m[3][2], m[3][3]) + diff --git a/examples/opengl/rokuro.py b/examples/opengl/rokuro.py new file mode 100644 index 0000000..fd9eaff --- /dev/null +++ b/examples/opengl/rokuro.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# coding: utf-8 + +from OpenGL.GL import * +from OpenGL.GLU import * + +from . import baseview + +class RokuroView(baseview.BaseView): + def __init__(self, distance): + super(RokuroView, self).__init__() + self.w=1 + self.h=1 + self.head=0 + self.pitch=0 + self.distance=distance + self.shiftX=0 + self.shiftY=0 + self.aspect=1 + self.n=1 + self.f=10000 + + def onResize(self, w=None, h=None): + super(RokuroView, self).onResize(w, h) + self.aspect=float(self.w)/float(self.h) + + def dolly(self, d): + if d>0: + self.distance*=1.1 + elif d<0: + self.distance*=0.9 + + def shift(self, dx, dy): + self.shiftX+=dx + self.shiftY+=dy + + def rotate(self, head, pitch): + self.head+=head + self.pitch+=pitch + + def updateProjection(self): + gluPerspective(30, self.aspect, self.n, self.f) + + def updateView(self): + glTranslate(self.shiftX, self.shiftY, -self.distance) + glRotate(self.head, 0, 1, 0) + glRotate(self.pitch, 1, 0, 0) + + def onMotion(self, x, y): + redraw=False + if self.isLeftDown: + self.dolly(y-self.y) + redraw=True + if self.isMiddelDown: + self.shift(x-self.x, self.y-y) + redraw=True + if self.isRightDown: + self.rotate(x-self.x, y-self.y) + redraw=True + self.x=x + self.y=y + return redraw + + def onWheel(self, d): + if d!=0: + self.dolly(d) + return True + diff --git a/examples/pymeshviewer.py b/examples/pymeshviewer.py new file mode 100644 index 0000000..2063c8e --- /dev/null +++ b/examples/pymeshviewer.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# coding: utf-8 + +import sys +import tkinter +import tkinter.filedialog +import togl +import opengl +import opengl.rokuro +import triangle + + +class Frame(tkinter.Frame): + def __init__(self, width, height, master=None, **kw): + super(Frame, self).__init__(master, **kw) + self.master.title('pymeshio viewer') + self.current='.' + # setup menu + menu_bar = tkinter.Menu(self) + self.master.config(menu=menu_bar) + + menu_file = tkinter.Menu(menu_bar, tearoff=False) + menu_bar.add_cascade(label='FILE', menu=menu_file, underline=0) + + menu_file.add_command(label='Open', under=0, command=self.onOpen) + + # setup opengl widget + self.glworld=opengl.BaseController(opengl.rokuro.RokuroView(25), triangle.Triangle(5)) + glwidget=togl.Widget(self, self.glworld, width=width, height=height) + glwidget.pack(fill=tkinter.BOTH, expand=True) + + # event binding + self.bind('', self.onKeyDown) + self.bind('', lambda e: self.glworld.onWheel(-e.delta) and glwidget.onDraw()) + + def onOpen(self): + filename=tkinter.filedialog.askopenfilename( + filetypes=[ + ('poloygon model files', '*.mqo;*.pmd'), + ], + initialdir=self.current) + print('open: %s' % filename) + + def onKeyDown(self, event): + key=event.keycode + if key==27: + # Escape + sys.exit() + if key==81: + # q + sys.exit() + else: + print("keycode: %d" % key) + + +if __name__ == '__main__': + f = Frame(width=600, height=600) + f.pack(fill=tkinter.BOTH, expand=True) + f.focus_set() + f.mainloop() + diff --git a/examples/togl.py b/examples/togl.py new file mode 100644 index 0000000..1aa06d6 --- /dev/null +++ b/examples/togl.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# coding: utf-8 + +import OpenGL.Tk + + +class Widget(OpenGL.Tk.RawOpengl): + def __init__(self, master, engine, *args, **kw): + super(Widget, self).__init__(master, *args, **kw) + self.engine=engine + self.bind('', self.onDraw) + self.bind('', self.onDraw) + self.bind('', self.onResize) + self.bind('', lambda e: self.engine.onLeftDown(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onLeftUp(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onMotion(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onMiddleDown(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onMiddleUp(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onMotion(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onRightDown(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onRightUp(e.x, e.y) and self.onDraw()) + self.bind('', lambda e: self.engine.onMotion(e.x, e.y) and self.onDraw()) + + def onDraw(self, *dummy): + self.tk.call(self._w, 'makecurrent') + self.update_idletasks() + self.engine.draw() + self.tk.call(self._w, 'swapbuffers') + + def onResize(self, event): + self.engine.onResize(event.width, event.height) + self.onDraw() + diff --git a/examples/triangle.py b/examples/triangle.py new file mode 100644 index 0000000..26bee60 --- /dev/null +++ b/examples/triangle.py @@ -0,0 +1,20 @@ +# coding: utf-8 +from OpenGL.GL import * + + +class Triangle(object): + def __init__(self, size): + self.size=size + + def draw(self): + # ŽOŠpŒ`•`‰æŠJŽn + glBegin(GL_TRIANGLES) + # ¶‰º + glVertex(-self.size, -self.size) + # ‰E‰º + glVertex(self.size, -self.size) + # ã + glVertex(0, self.size) + # ŽOŠpŒ`•`‰æI—¹ + glEnd() + -- 2.11.0