OSDN Git Service

ccc304b4dc99342222218167601cab0cfd1daddd
[meshio/pymeshio.git] / examples / opengl / vertexarray.py
1 #!/usr/bin/python\r
2 # coding: utf-8\r
3 \r
4 from OpenGL.GL import *\r
5 import numpy\r
6 \r
7 '''\r
8 頂点配列\r
9 \r
10 ====\r
11 属性\r
12 ====\r
13 * 位置\r
14 '''\r
15 class VertexArray(object):\r
16     def __init__(self, vertices):\r
17         self.vertices=vertices\r
18 \r
19     def __str__(self):\r
20         return "<VertexArray %d>" % len(self.vertices)\r
21 \r
22     def draw(self):\r
23         # 位置属性\r
24         glEnableClientState(GL_VERTEX_ARRAY)\r
25         glVertexPointer(3, GL_FLOAT, 0, self.vertices)\r
26         # 描画\r
27         glDrawArrays(GL_TRIANGLES, 0, len(self.vertices))\r
28         # 後始末\r
29         glDisableClientState(GL_VERTEX_ARRAY)\r
30 \r
31 \r
32 \r
33 '''\r
34 頂点配列\r
35 \r
36 ====\r
37 属性\r
38 ====\r
39 * 位置\r
40 * UV\r
41 '''\r
42 class VertexArrayWithUV(object):\r
43     def __init__(self, vertices, uvarray):\r
44         self.vertices=vertices\r
45         self.uvarray=uvarray\r
46 \r
47     def __str__(self):\r
48         return "<VertexArrayWithUV %d>" % len(self.vertices)\r
49 \r
50     def draw(self):\r
51         # 位置属性\r
52         glEnableClientState(GL_VERTEX_ARRAY)\r
53         glVertexPointer(3, GL_FLOAT, 0, self.vertices)\r
54         # UV属性\r
55         glEnableClientState(GL_TEXTURE_COORD_ARRAY)\r
56         glTexCoordPointer(2, GL_FLOAT, 0, self.uvarray)\r
57         # 描画\r
58         triangle_count=int(len(self.vertices)/3)\r
59         glDrawArrays(GL_TRIANGLES, 0, triangle_count)\r
60         # 後始末\r
61         glDisableClientState(GL_TEXTURE_COORD_ARRAY)\r
62         glDisableClientState(GL_VERTEX_ARRAY)\r
63 \r
64 \r
65 '''\r
66 インデックス参照頂点配列\r
67 \r
68 ====\r
69 属性\r
70 ====\r
71 * 位置\r
72 * UV\r
73 '''\r
74 class IndexedVertexArray(object):\r
75     def __init__(self):\r
76         # vertices\r
77         self.vertices=[]\r
78         self.normal=[]\r
79         self.colors=[]\r
80         self.uvlist=[]\r
81         self.b0=[]\r
82         self.b1=[]\r
83         self.w0=[]\r
84         # indices\r
85         self.materials=[]\r
86         self.indicesMap={}\r
87 \r
88     def addVertex(self, pos, normal, uv, color, b0, b1, w0):\r
89         self.vertices+=pos\r
90         self.normal+=normal\r
91         self.colors+=color\r
92         self.uvlist+=uv\r
93         self.b0.append(b0)\r
94         self.b1.append(b1)\r
95         self.w0.append(w0)\r
96 \r
97     def addMaterial(self, material):\r
98         self.materials.append(material)\r
99         indices=[]\r
100         self.indicesMap[material]=indices\r
101         return indices\r
102 \r
103     def draw(self):\r
104         # 位置属性\r
105         glEnableClientState(GL_VERTEX_ARRAY)\r
106         glVertexPointer(4, GL_FLOAT, 0, self.vertices)\r
107         # UV属性\r
108         glEnableClientState(GL_TEXTURE_COORD_ARRAY)\r
109         glTexCoordPointer(2, GL_FLOAT, 0, self.uvlist)\r
110         # マテリアル毎の描画\r
111         for m in self.materials:\r
112             m.begin()\r
113             # 順序維持\r
114             indices=self.indicesMap[m]\r
115             # indexによる描画\r
116             glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)\r
117             m.end()\r
118         # 後始末\r
119         glDisableClientState(GL_TEXTURE_COORD_ARRAY)\r
120         glDisableClientState(GL_VERTEX_ARRAY)\r
121 \r
122     def optimize(self):\r
123         self.vertices=numpy.array(self.vertices, numpy.float32) \r
124         self.uvlist=numpy.array(self.uvlist, numpy.float32) \r
125         for m, indices in self.indicesMap.items():\r
126             self.indicesMap[m]=numpy.array(indices, numpy.uint32)\r
127 \r
128     def get_boundingbox(self):\r
129         vertices_size=len(self.vertices)\r
130         if(vertices_size==0):\r
131             return ([0, 0, 0], [0, 0, 0])\r
132         print('vertices_size %d' % vertices_size)\r
133         print(self.vertices[0])\r
134         def vertex_gen_factory():\r
135             for i in range(0, vertices_size, 4):\r
136                 yield [\r
137                         self.vertices[i],\r
138                         self.vertices[i+1],\r
139                         self.vertices[i+2]\r
140                         ]\r
141         vertex_gen=vertex_gen_factory()\r
142         v=next(vertex_gen)\r
143         max_v=v[:]\r
144         min_v=v[:]\r
145         for v in vertex_gen:\r
146             if v[0]<min_v[0]:\r
147                 min_v[0]=v[0]\r
148             if v[1]<min_v[1]:\r
149                 min_v[1]=v[1]\r
150             if v[2]<min_v[2]:\r
151                 min_v[2]=v[2]\r
152             if v[0]>max_v[0]:\r
153                 max_v[0]=v[0]\r
154             if v[1]>max_v[1]:\r
155                 max_v[1]=v[1]\r
156             if v[2]>max_v[2]:\r
157                 max_v[2]=v[2]\r
158         return (min_v, max_v)\r
159 \r