OSDN Git Service

fix import_mqo.py
[meshio/pymeshio.git] / examples / opengl / vertexarray.py
index 749b6e0..b9dee7b 100644 (file)
@@ -2,6 +2,7 @@
 # coding: utf-8\r
 \r
 from OpenGL.GL import *\r
+import numpy\r
 \r
 '''\r
 頂点配列\r
@@ -60,6 +61,29 @@ class VertexArrayWithUV(object):
         glDisableClientState(GL_TEXTURE_COORD_ARRAY)\r
         glDisableClientState(GL_VERTEX_ARRAY)\r
 \r
+    def get_boundingbox(self):\r
+        vertices_size=len(self.vertices)\r
+        if(vertices_size==0):\r
+            return ([0, 0, 0], [0, 0, 0])\r
+        def vertex_gen_factory():\r
+            for i in range(0, vertices_size, 3):\r
+                yield [\r
+                        self.vertices[i],\r
+                        self.vertices[i+1],\r
+                        self.vertices[i+2]\r
+                        ]\r
+        vertex_gen=vertex_gen_factory()\r
+        v=next(vertex_gen)\r
+        max_v=v[:]\r
+        min_v=v[:]\r
+        for v in vertex_gen:\r
+            min_v[0]=min(min_v[0], v[0]) \r
+            min_v[1]=min(min_v[1], v[1]) \r
+            min_v[2]=min(min_v[2], v[2]) \r
+            max_v[0]=max(max_v[0], v[0]) \r
+            max_v[1]=max(max_v[1], v[1]) \r
+            max_v[2]=max(max_v[2], v[2]) \r
+        return (min_v, max_v)\r
 \r
 '''\r
 インデックス参照頂点配列\r
@@ -68,49 +92,89 @@ 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, uv, color, 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
+        # UV属性\r
+        glEnableClientState(GL_TEXTURE_COORD_ARRAY)\r
+        glTexCoordPointer(2, GL_FLOAT, 0, self.uvlist)\r
+        # マテリアル毎の描画\r
+        for m in self.materials:\r
+            m.begin()\r
+            # 順序維持\r
+            indices=self.indicesMap[m]\r
+            # indexによる描画\r
+            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)\r
+            m.end()\r
         # 後始末\r
+        glDisableClientState(GL_TEXTURE_COORD_ARRAY)\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
+        self.vertices=numpy.array(self.vertices, numpy.float32) \r
+        self.uvlist=numpy.array(self.uvlist, numpy.float32) \r
+        for m, indices in self.indicesMap.items():\r
+            self.indicesMap[m]=numpy.array(indices, numpy.uint32)\r
+\r
+    def get_boundingbox(self):\r
+        vertices_size=len(self.vertices)\r
+        if(vertices_size==0):\r
+            return ([0, 0, 0], [0, 0, 0])\r
+        def vertex_gen_factory():\r
+            for i in range(0, vertices_size, 4):\r
+                yield [\r
+                        self.vertices[i],\r
+                        self.vertices[i+1],\r
+                        self.vertices[i+2]\r
+                        ]\r
+        vertex_gen=vertex_gen_factory()\r
+        v=next(vertex_gen)\r
+        max_v=v[:]\r
+        min_v=v[:]\r
+        for v in vertex_gen:\r
+            if v[0]<min_v[0]:\r
+                min_v[0]=v[0]\r
+            if v[1]<min_v[1]:\r
+                min_v[1]=v[1]\r
+            if v[2]<min_v[2]:\r
+                min_v[2]=v[2]\r
+            if v[0]>max_v[0]:\r
+                max_v[0]=v[0]\r
+            if v[1]>max_v[1]:\r
+                max_v[1]=v[1]\r
+            if v[2]>max_v[2]:\r
+                max_v[2]=v[2]\r
+        return (min_v, max_v)\r
 \r