OSDN Git Service

b9dee7b8bb0e4b08751e6b67e0be765d0d295aee
[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     def get_boundingbox(self):\r
65         vertices_size=len(self.vertices)\r
66         if(vertices_size==0):\r
67             return ([0, 0, 0], [0, 0, 0])\r
68         def vertex_gen_factory():\r
69             for i in range(0, vertices_size, 3):\r
70                 yield [\r
71                         self.vertices[i],\r
72                         self.vertices[i+1],\r
73                         self.vertices[i+2]\r
74                         ]\r
75         vertex_gen=vertex_gen_factory()\r
76         v=next(vertex_gen)\r
77         max_v=v[:]\r
78         min_v=v[:]\r
79         for v in vertex_gen:\r
80             min_v[0]=min(min_v[0], v[0]) \r
81             min_v[1]=min(min_v[1], v[1]) \r
82             min_v[2]=min(min_v[2], v[2]) \r
83             max_v[0]=max(max_v[0], v[0]) \r
84             max_v[1]=max(max_v[1], v[1]) \r
85             max_v[2]=max(max_v[2], v[2]) \r
86         return (min_v, max_v)\r
87 \r
88 '''\r
89 インデックス参照頂点配列\r
90 \r
91 ====\r
92 属性\r
93 ====\r
94 * 位置\r
95 * UV\r
96 '''\r
97 class IndexedVertexArray(object):\r
98     def __init__(self):\r
99         # vertices\r
100         self.vertices=[]\r
101         self.normal=[]\r
102         self.colors=[]\r
103         self.uvlist=[]\r
104         self.b0=[]\r
105         self.b1=[]\r
106         self.w0=[]\r
107         # indices\r
108         self.materials=[]\r
109         self.indicesMap={}\r
110 \r
111     def addVertex(self, pos, normal, uv, color, b0, b1, w0):\r
112         self.vertices+=pos\r
113         self.normal+=normal\r
114         self.colors+=color\r
115         self.uvlist+=uv\r
116         self.b0.append(b0)\r
117         self.b1.append(b1)\r
118         self.w0.append(w0)\r
119 \r
120     def addMaterial(self, material):\r
121         self.materials.append(material)\r
122         indices=[]\r
123         self.indicesMap[material]=indices\r
124         return indices\r
125 \r
126     def draw(self):\r
127         # 位置属性\r
128         glEnableClientState(GL_VERTEX_ARRAY)\r
129         glVertexPointer(4, GL_FLOAT, 0, self.vertices)\r
130         # UV属性\r
131         glEnableClientState(GL_TEXTURE_COORD_ARRAY)\r
132         glTexCoordPointer(2, GL_FLOAT, 0, self.uvlist)\r
133         # マテリアル毎の描画\r
134         for m in self.materials:\r
135             m.begin()\r
136             # 順序維持\r
137             indices=self.indicesMap[m]\r
138             # indexによる描画\r
139             glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)\r
140             m.end()\r
141         # 後始末\r
142         glDisableClientState(GL_TEXTURE_COORD_ARRAY)\r
143         glDisableClientState(GL_VERTEX_ARRAY)\r
144 \r
145     def optimize(self):\r
146         self.vertices=numpy.array(self.vertices, numpy.float32) \r
147         self.uvlist=numpy.array(self.uvlist, numpy.float32) \r
148         for m, indices in self.indicesMap.items():\r
149             self.indicesMap[m]=numpy.array(indices, numpy.uint32)\r
150 \r
151     def get_boundingbox(self):\r
152         vertices_size=len(self.vertices)\r
153         if(vertices_size==0):\r
154             return ([0, 0, 0], [0, 0, 0])\r
155         def vertex_gen_factory():\r
156             for i in range(0, vertices_size, 4):\r
157                 yield [\r
158                         self.vertices[i],\r
159                         self.vertices[i+1],\r
160                         self.vertices[i+2]\r
161                         ]\r
162         vertex_gen=vertex_gen_factory()\r
163         v=next(vertex_gen)\r
164         max_v=v[:]\r
165         min_v=v[:]\r
166         for v in vertex_gen:\r
167             if v[0]<min_v[0]:\r
168                 min_v[0]=v[0]\r
169             if v[1]<min_v[1]:\r
170                 min_v[1]=v[1]\r
171             if v[2]<min_v[2]:\r
172                 min_v[2]=v[2]\r
173             if v[0]>max_v[0]:\r
174                 max_v[0]=v[0]\r
175             if v[1]>max_v[1]:\r
176                 max_v[1]=v[1]\r
177             if v[2]>max_v[2]:\r
178                 max_v[2]=v[2]\r
179         return (min_v, max_v)\r
180 \r