From 410c9544d5585e71f26e85d90ffd142c76f97e43 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 29 Sep 2011 03:39:29 +0900 Subject: [PATCH] implement loading texture in pmdbuilder --- examples/opengl/material.py | 11 ++++++++-- examples/opengl/texture.py | 49 ++++++++++++++++++++++++------------------ examples/opengl/vertexarray.py | 8 ++++++- examples/pmdbuilder.py | 5 +++-- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/examples/opengl/material.py b/examples/opengl/material.py index 734749d..1c0fe38 100644 --- a/examples/opengl/material.py +++ b/examples/opengl/material.py @@ -43,6 +43,12 @@ class MQOMaterial(object): self.vcol=False self.texture=None + def __enter__(self): + self.begin() + + def __exit__(self): + self.end() + def begin(self): glColor4f(*self.rgba) if self.texture: @@ -61,8 +67,9 @@ class MQOMaterial(object): self.texture.end() def onInitialize(self): - if self.texture: - self.texture.onInitialize() + pass + #if self.texture: + # self.texture.onInitialize() @staticmethod def create(src, basedir): diff --git a/examples/opengl/texture.py b/examples/opengl/texture.py index 548e423..cb03f01 100644 --- a/examples/opengl/texture.py +++ b/examples/opengl/texture.py @@ -10,33 +10,22 @@ class Texture(object): def __init__(self, path): self.path=path self.image=None + self.id=0 + self.isInitialized=False def onInitialize(self): - if not self.image: - self.loadImage() - - assert(self.image) - if self.createTexture(): - return True - - def loadImage(self): - self.image=Image.open(self.path) - if self.image: - print("load image:", self.path) - return True - else: - print("failt to load image:", self.path) - return False + self.isInitialized=False def createTexture(self): - self.texture=glGenTextures(1) - if self.texture==0: + self.id=glGenTextures(1) + if self.id==0: print("fail to glGenTextures") return False + print("createTexture: %d" % self.id) channels=len(self.image.getbands()) w, h=self.image.size - glBindTexture(GL_TEXTURE_2D, self.texture) + glBindTexture(GL_TEXTURE_2D, self.id) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) @@ -53,10 +42,28 @@ class Texture(object): 0, GL_RGB, GL_UNSIGNED_BYTE, self.image.tostring()) def begin(self): - glEnable(GL_TEXTURE_2D) - glBindTexture(GL_TEXTURE_2D, self.texture) + if not self.isInitialized: + try: + # load image + if not self.image: + self.image=Image.open(self.path) + if self.image: + print("load image:", self.path) + else: + print("failt to load image:", self.path) + return + # createTexture + if self.image: + self.createTexture() + except Exception as e: + print(e) + return + finally: + self.isInitialized=True + if self.id!=0: + glEnable(GL_TEXTURE_2D) + glBindTexture(GL_TEXTURE_2D, self.id) def end(self): glDisable(GL_TEXTURE_2D) - diff --git a/examples/opengl/vertexarray.py b/examples/opengl/vertexarray.py index 5b1a0c4..4dc51c3 100644 --- a/examples/opengl/vertexarray.py +++ b/examples/opengl/vertexarray.py @@ -84,7 +84,7 @@ class IndexedVertexArray(object): self.materials=[] self.indicesMap={} - def addVertex(self, pos, normal, color, uv, b0, b1, w0): + def addVertex(self, pos, normal, uv, color, b0, b1, w0): self.vertices+=pos self.normal+=normal self.colors+=color @@ -103,13 +103,19 @@ class IndexedVertexArray(object): # 位置属性 glEnableClientState(GL_VERTEX_ARRAY) 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) def optimize(self): diff --git a/examples/pmdbuilder.py b/examples/pmdbuilder.py index e41112c..96feccb 100644 --- a/examples/pmdbuilder.py +++ b/examples/pmdbuilder.py @@ -44,8 +44,9 @@ def build(path): m.diffuse[1], m.diffuse[2], m.diffuse[3]) - if m.texture!="": - texturepath="%s/%s" % (basedir, m.texture) + texturefile=m.texture.decode('cp932') + if texturefile!="": + texturepath=os.path.join(basedir, texturefile) if not texturepath in textureMap: texture=opengl.texture.Texture(texturepath) textureMap[texturepath]=texture -- 2.11.0