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