From: ousttrue Date: Wed, 28 Sep 2011 17:19:10 +0000 (+0900) Subject: implement pmdbuilder X-Git-Url: http://git.osdn.jp/view?a=commitdiff_plain;h=65211f29fc6e1e540cc3e1c515753f860653382b;hp=6fcd334d6c80537283ab6afb30603d243178c768;p=meshio%2Fpymeshio.git implement pmdbuilder --- diff --git a/examples/cube.mqo b/examples/cube.mqo deleted file mode 100644 index e56de65..0000000 --- a/examples/cube.mqo +++ /dev/null @@ -1,52 +0,0 @@ -Metasequoia Document -Format Text Ver 1.0 - -Scene { - pos 48.0899 60.6742 2400.0000 - lookat 0.0000 0.0000 0.0000 - head 0.0000 - pich 0.0000 - ortho 1 - zoom2 3.1250 - amb 0.250 0.250 0.250 -} -Material 6 { - "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) - "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") - "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) - "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) - "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) - "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) -} -Object "obj1" { - depth 0 - folding 0 - scale 1.000000 1.000000 1.000000 - rotation 0.000000 0.000000 0.000000 - translation 0.000000 0.000000 0.000000 - visible 15 - locking 0 - shading 1 - facet 59.5 - color 0.898 0.498 0.698 - color_type 0 - vertex 8 { - -100.0000 100.0000 100.0000 - -100.0000 -100.0000 100.0000 - 100.0000 100.0000 100.0000 - 100.0000 -100.0000 100.0000 - 100.0000 100.0000 -100.0000 - 100.0000 -100.0000 -100.0000 - -100.0000 100.0000 -100.0000 - -100.0000 -100.0000 -100.0000 - } - face 6 { - 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) - 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) - 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) - 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) - 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) - 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) - } -} -Eof diff --git a/examples/front.jpg b/examples/front.jpg deleted file mode 100644 index 19bb34e..0000000 Binary files a/examples/front.jpg and /dev/null differ diff --git a/examples/opengl/material.py b/examples/opengl/material.py index 29698e3..734749d 100644 --- a/examples/opengl/material.py +++ b/examples/opengl/material.py @@ -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() diff --git a/examples/opengl/vertexarray.py b/examples/opengl/vertexarray.py index 749b6e0..5b1a0c4 100644 --- a/examples/opengl/vertexarray.py +++ b/examples/opengl/vertexarray.py @@ -68,49 +68,52 @@ class VertexArrayWithUV(object): 属性 ==== * 位置 +* UV ''' class IndexedVertexArray(object): - def __init__(self, vertices, indices): - self.vertices=vertices - self.indices=indices + def __init__(self): + # vertices + self.vertices=[] + self.normal=[] + self.colors=[] + self.uvlist=[] + self.b0=[] + self.b1=[] + self.w0=[] + # indices + self.materials=[] + self.indicesMap={} + + def addVertex(self, pos, normal, color, uv, b0, b1, w0): + self.vertices+=pos + self.normal+=normal + self.colors+=color + self.uvlist+=uv + self.b0.append(b0) + self.b1.append(b1) + self.w0.append(w0) + + def addMaterial(self, material): + self.materials.append(material) + indices=[] + self.indicesMap[material]=indices + return indices def draw(self): # 位置属性 glEnableClientState(GL_VERTEX_ARRAY) - glVertexPointer(3, GL_FLOAT, 0, self.vertices) - # indexによる描画 - glDrawElements(GL_TRIANGLES, len(self.indices), - GL_UNSIGNED_INT, self.indices) + glVertexPointer(4, GL_FLOAT, 0, self.vertices) + # マテリアル毎の描画 + for m in self.materials: + # 順序維持 + indices=self.indicesMap[m] + # indexによる描画 + glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices) # 後始末 glDisableClientState(GL_VERTEX_ARRAY) - -''' -インデックス参照頂点配列 - -==== -属性 -==== -* 位置 -* 色 -''' -class IndexedVertexArrayWithColor(object): - def __init__(self, vertices, colors, indices): - self.vertices=vertices - self.colors=colors - self.indices=indices - - def draw(self): - # 位置属性 - glEnableClientState(GL_VERTEX_ARRAY) - glVertexPointer(3, GL_FLOAT, 0, self.vertices) - # 色属性 - glEnableClientState(GL_COLOR_ARRAY) - glColorPointer(3, GL_FLOAT, 0, self.colors) - # indexによる描画 - glDrawElements(GL_TRIANGLES, len(self.indices), - GL_UNSIGNED_INT, self.indices) - # 後始末 - glDisableClientState(GL_COLOR_ARRAY) - glDisableClientState(GL_VERTEX_ARRAY) + def optimize(self): + pass + #for v in self.vertexArrayMap.values(): + # v.vertices=numpy.array(v.vertices, 'f') diff --git a/examples/pmdbuilder.py b/examples/pmdbuilder.py new file mode 100644 index 0000000..e41112c --- /dev/null +++ b/examples/pmdbuilder.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# coding: utf-8 + +import time +import os +import pymeshio.pmd +import opengl.material +import opengl.texture +import opengl.vertexarray + + +def build(path): + # load scenee + t=time.time() + io=pymeshio.pmd.IO() + if not io.read(path): + return + print(time.time()-t, "sec") + # build + basedir=os.path.dirname(path) + indexedVertexArray=opengl.vertexarray.IndexedVertexArray() + for v in io.vertices: + # left-handed y-up to right-handed y-up + indexedVertexArray.addVertex( + (v.pos[0], v.pos[1], -v.pos[2], 1), + (v.normal[0], v.normal[1], -v.normal[2]), + (v.uv[0], v.uv[1]), + (1, 1, 1, 1), + v.bone0, v.bone1, v.weight0) + + # material + textureMap={} + faceIndex=0 + def indices(): + for i in io.indices: + yield i + indexGen=indices() + for i, m in enumerate(io.materials): + print(i, m) + material=opengl.material.MQOMaterial() + material.vcol=True + material.rgba=( + m.diffuse[0], + m.diffuse[1], + m.diffuse[2], + m.diffuse[3]) + if m.texture!="": + texturepath="%s/%s" % (basedir, m.texture) + if not texturepath in textureMap: + texture=opengl.texture.Texture(texturepath) + textureMap[texturepath]=texture + material.texture=textureMap[texturepath] + indices=indexedVertexArray.addMaterial(material) + indices+=[next(indexGen) for n in range(m.vertex_count)] + indexedVertexArray.optimize() + return indexedVertexArray + diff --git a/examples/pymeshviewer.py b/examples/pymeshviewer.py index 06146b3..491433b 100644 --- a/examples/pymeshviewer.py +++ b/examples/pymeshviewer.py @@ -9,6 +9,7 @@ import togl import opengl import opengl.rokuro import mqobuilder +import pmdbuilder class Frame(tkinter.Frame): @@ -40,6 +41,9 @@ class Frame(tkinter.Frame): ('poloygon model files', '*.mqo;*.pmd'), ], initialdir=self.current) + self.load(path) + + def load(self, path): model=self.loadModel(path) if not model: print('fail to load %s' % path) @@ -70,5 +74,7 @@ if __name__ == '__main__': f = Frame(width=600, height=600) f.pack(fill=tkinter.BOTH, expand=True) f.focus_set() + if len(sys.argv)>1: + f.load(sys.argv[1]) f.mainloop()