OSDN Git Service

implement pmdbuilder
authorousttrue <ousttrue@gmail.com>
Wed, 28 Sep 2011 17:19:10 +0000 (02:19 +0900)
committerousttrue <ousttrue@gmail.com>
Wed, 28 Sep 2011 17:19:10 +0000 (02:19 +0900)
examples/cube.mqo [deleted file]
examples/front.jpg [deleted file]
examples/opengl/material.py
examples/opengl/vertexarray.py
examples/pmdbuilder.py [new file with mode: 0644]
examples/pymeshviewer.py

diff --git a/examples/cube.mqo b/examples/cube.mqo
deleted file mode 100644 (file)
index e56de65..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-Metasequoia Document\r
-Format Text Ver 1.0\r
-\r
-Scene {\r
-       pos 48.0899 60.6742 2400.0000\r
-       lookat 0.0000 0.0000 0.0000\r
-       head 0.0000\r
-       pich 0.0000\r
-       ortho 1\r
-       zoom2 3.1250\r
-       amb 0.250 0.250 0.250\r
-}\r
-Material 6 {\r
-       "mat1" shader(3) col(1.000 0.000 0.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\r
-       "mat2" shader(3) col(0.000 0.000 1.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00) tex("front.jpg")\r
-       "mat3" shader(3) col(1.000 1.000 0.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\r
-       "mat4" shader(3) col(0.000 0.749 0.169 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\r
-       "mat5" shader(3) col(1.000 1.000 1.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\r
-       "mat6" shader(3) col(1.000 0.000 1.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\r
-}\r
-Object "obj1" {\r
-       depth 0\r
-       folding 0\r
-       scale 1.000000 1.000000 1.000000\r
-       rotation 0.000000 0.000000 0.000000\r
-       translation 0.000000 0.000000 0.000000\r
-       visible 15\r
-       locking 0\r
-       shading 1\r
-       facet 59.5\r
-       color 0.898 0.498 0.698\r
-       color_type 0\r
-       vertex 8 {\r
-               -100.0000 100.0000 100.0000\r
-               -100.0000 -100.0000 100.0000\r
-               100.0000 100.0000 100.0000\r
-               100.0000 -100.0000 100.0000\r
-               100.0000 100.0000 -100.0000\r
-               100.0000 -100.0000 -100.0000\r
-               -100.0000 100.0000 -100.0000\r
-               -100.0000 -100.0000 -100.0000\r
-       }\r
-       face 6 {\r
-               4 V(0 2 3 1) M(1) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\r
-               4 V(2 4 5 3) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\r
-               4 V(4 6 7 5) M(2) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\r
-               4 V(6 0 1 7) M(3) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\r
-               4 V(6 4 2 0) M(4) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\r
-               4 V(1 3 5 7) M(5) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\r
-       }\r
-}\r
-Eof\r
diff --git a/examples/front.jpg b/examples/front.jpg
deleted file mode 100644 (file)
index 19bb34e..0000000
Binary files a/examples/front.jpg and /dev/null differ
index 29698e3..734749d 100644 (file)
@@ -38,12 +38,13 @@ Material
 * テクスチャー
 '''
 class MQOMaterial(object):
-    def __init__(self, rgba):
-        self.rgba=rgba
+    def __init__(self):
+        self.rgba=(0, 0, 0, 0)
+        self.vcol=False
         self.texture=None
 
     def begin(self):
-        glColor4f(self.rgba.r, self.rgba.g, self.rgba.b, self.rgba.a)
+        glColor4f(*self.rgba)
         if self.texture:
             self.texture.begin()
 
index 749b6e0..5b1a0c4 100644 (file)
@@ -68,49 +68,52 @@ class VertexArrayWithUV(object):
 属性\r
 ====\r
 * 位置\r
+* UV\r
 '''\r
 class IndexedVertexArray(object):\r
-    def __init__(self, vertices, indices):\r
-        self.vertices=vertices\r
-        self.indices=indices\r
+    def __init__(self):\r
+        # vertices\r
+        self.vertices=[]\r
+        self.normal=[]\r
+        self.colors=[]\r
+        self.uvlist=[]\r
+        self.b0=[]\r
+        self.b1=[]\r
+        self.w0=[]\r
+        # indices\r
+        self.materials=[]\r
+        self.indicesMap={}\r
+\r
+    def addVertex(self, pos, normal, color, uv, b0, b1, w0):\r
+        self.vertices+=pos\r
+        self.normal+=normal\r
+        self.colors+=color\r
+        self.uvlist+=uv\r
+        self.b0.append(b0)\r
+        self.b1.append(b1)\r
+        self.w0.append(w0)\r
+\r
+    def addMaterial(self, material):\r
+        self.materials.append(material)\r
+        indices=[]\r
+        self.indicesMap[material]=indices\r
+        return indices\r
 \r
     def draw(self):\r
         # 位置属性\r
         glEnableClientState(GL_VERTEX_ARRAY)\r
-        glVertexPointer(3, GL_FLOAT, 0, self.vertices)\r
-        # indexによる描画\r
-        glDrawElements(GL_TRIANGLES, len(self.indices),\r
-                GL_UNSIGNED_INT, self.indices)\r
+        glVertexPointer(4, GL_FLOAT, 0, self.vertices)\r
+        # マテリアル毎の描画\r
+        for m in self.materials:\r
+            # 順序維持\r
+            indices=self.indicesMap[m]\r
+            # indexによる描画\r
+            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)\r
         # 後始末\r
         glDisableClientState(GL_VERTEX_ARRAY)\r
 \r
-\r
-'''\r
-インデックス参照頂点配列\r
-\r
-====\r
-属性\r
-====\r
-* 位置\r
-* 色\r
-'''\r
-class IndexedVertexArrayWithColor(object):\r
-    def __init__(self, vertices, colors, indices):\r
-        self.vertices=vertices\r
-        self.colors=colors\r
-        self.indices=indices\r
-\r
-    def draw(self):\r
-        # 位置属性\r
-        glEnableClientState(GL_VERTEX_ARRAY)\r
-        glVertexPointer(3, GL_FLOAT, 0, self.vertices)\r
-        # 色属性\r
-        glEnableClientState(GL_COLOR_ARRAY)\r
-        glColorPointer(3, GL_FLOAT, 0, self.colors)\r
-        # indexによる描画\r
-        glDrawElements(GL_TRIANGLES, len(self.indices),\r
-                GL_UNSIGNED_INT, self.indices)\r
-        # 後始末\r
-        glDisableClientState(GL_COLOR_ARRAY)\r
-        glDisableClientState(GL_VERTEX_ARRAY)\r
+    def optimize(self):\r
+        pass\r
+        #for v in self.vertexArrayMap.values():\r
+        #    v.vertices=numpy.array(v.vertices, 'f') \r
 \r
diff --git a/examples/pmdbuilder.py b/examples/pmdbuilder.py
new file mode 100644 (file)
index 0000000..e41112c
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/env python\r
+# coding: utf-8\r
+\r
+import time\r
+import os\r
+import pymeshio.pmd\r
+import opengl.material\r
+import opengl.texture\r
+import opengl.vertexarray\r
+\r
+\r
+def build(path):\r
+    # load scenee\r
+    t=time.time()\r
+    io=pymeshio.pmd.IO()\r
+    if not io.read(path):\r
+        return\r
+    print(time.time()-t, "sec")\r
+    # build\r
+    basedir=os.path.dirname(path)\r
+    indexedVertexArray=opengl.vertexarray.IndexedVertexArray()\r
+    for v in io.vertices:\r
+        # left-handed y-up to right-handed y-up                \r
+        indexedVertexArray.addVertex(\r
+                (v.pos[0], v.pos[1], -v.pos[2], 1), \r
+                (v.normal[0], v.normal[1], -v.normal[2]), \r
+                (v.uv[0], v.uv[1]), \r
+                (1, 1, 1, 1),\r
+                v.bone0, v.bone1, v.weight0)\r
+    \r
+    # material\r
+    textureMap={}\r
+    faceIndex=0\r
+    def indices():\r
+        for i in io.indices:\r
+            yield i\r
+    indexGen=indices()\r
+    for i, m in enumerate(io.materials):\r
+        print(i, m)\r
+        material=opengl.material.MQOMaterial()\r
+        material.vcol=True\r
+        material.rgba=(\r
+                m.diffuse[0], \r
+                m.diffuse[1], \r
+                m.diffuse[2], \r
+                m.diffuse[3])\r
+        if m.texture!="":\r
+            texturepath="%s/%s" % (basedir, m.texture)\r
+            if not texturepath in textureMap:\r
+                texture=opengl.texture.Texture(texturepath)\r
+                textureMap[texturepath]=texture\r
+            material.texture=textureMap[texturepath]\r
+        indices=indexedVertexArray.addMaterial(material)\r
+        indices+=[next(indexGen) for n in range(m.vertex_count)]\r
+    indexedVertexArray.optimize()\r
+    return indexedVertexArray\r
+\r
index 06146b3..491433b 100644 (file)
@@ -9,6 +9,7 @@ import togl
 import opengl\r
 import opengl.rokuro\r
 import mqobuilder\r
+import pmdbuilder\r
 \r
 \r
 class Frame(tkinter.Frame):\r
@@ -40,6 +41,9 @@ class Frame(tkinter.Frame):
                     ('poloygon model files', '*.mqo;*.pmd'),\r
                     ], \r
                 initialdir=self.current)\r
+        self.load(path)\r
+\r
+    def load(self, path):\r
         model=self.loadModel(path)\r
         if not model:\r
             print('fail to load %s' % path)\r
@@ -70,5 +74,7 @@ if __name__ == '__main__':
     f = Frame(width=600, height=600)\r
     f.pack(fill=tkinter.BOTH, expand=True)\r
     f.focus_set()\r
+    if len(sys.argv)>1:\r
+        f.load(sys.argv[1])\r
     f.mainloop()\r
 \r