|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# PyWavefront |
| 3 | +# Copyright (c) 2018 Kurt Yoder |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions |
| 8 | +# are met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above copyright |
| 13 | +# notice, this list of conditions and the following disclaimer in |
| 14 | +# the documentation and/or other materials provided with the |
| 15 | +# distribution. |
| 16 | +# * Neither the name of PyWavefront nor the names of its |
| 17 | +# contributors may be used to endorse or promote products |
| 18 | +# derived from this software without specific prior written |
| 19 | +# permission. |
| 20 | +# |
| 21 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | +# POSSIBILITY OF SUCH DAMAGE. |
| 33 | +# ---------------------------------------------------------------------------- |
| 34 | + |
| 35 | +from pyglet.gl import * |
| 36 | + |
| 37 | +from pywavefront.material import Material |
| 38 | +from pywavefront.mesh import Mesh |
| 39 | +from pywavefront.texture import Texture |
| 40 | + |
| 41 | +class _Material(object): |
| 42 | + """ |
| 43 | + Overrides the drawing routines for the Material class. |
| 44 | + This class should not be used directly. Its purpose is to shadow |
| 45 | + the methods to be overriden. |
| 46 | + """ |
| 47 | + |
| 48 | + def gl_light(self, lighting): |
| 49 | + """Return a GLfloat with length 4, containing the 4 lighting values.""" |
| 50 | + return (GLfloat * 4)(*(lighting)) |
| 51 | + |
| 52 | + def draw(self, face=GL_FRONT_AND_BACK): |
| 53 | + if self.texture: |
| 54 | + self.texture.draw() |
| 55 | + else: |
| 56 | + glDisable(GL_TEXTURE_2D) |
| 57 | + |
| 58 | + glMaterialfv(face, GL_DIFFUSE, self.gl_light(self.diffuse)) |
| 59 | + glMaterialfv(face, GL_AMBIENT, self.gl_light(self.ambient)) |
| 60 | + glMaterialfv(face, GL_SPECULAR, self.gl_light(self.specular)) |
| 61 | + glMaterialfv(face, GL_EMISSION, self.gl_light(self.emissive)) |
| 62 | + glMaterialf(face, GL_SHININESS, self.shininess) |
| 63 | + |
| 64 | + if self.gl_floats is None: |
| 65 | + self.gl_floats = (GLfloat * len(self.vertices))(*self.vertices) |
| 66 | + self.triangle_count = len(self.vertices) / 8 |
| 67 | + glInterleavedArrays(GL_T2F_N3F_V3F, 0, self.gl_floats) |
| 68 | + glDrawArrays(GL_TRIANGLES, 0, int(self.triangle_count)) |
| 69 | + |
| 70 | + |
| 71 | +setattr(Material, "gl_light", _Material.gl_light) |
| 72 | +setattr(Material, "draw", _Material.draw) |
| 73 | + |
| 74 | + |
| 75 | +class _Mesh(object): |
| 76 | + """ |
| 77 | + Overrides the drawing routines for the Mesh class. |
| 78 | + This class should not be used directly. Its purpose is to shadow |
| 79 | + the methods to be overriden. |
| 80 | + """ |
| 81 | + def draw(self): |
| 82 | + glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) |
| 83 | + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT) |
| 84 | + glEnable(GL_CULL_FACE) |
| 85 | + glCullFace(GL_BACK) |
| 86 | + for material in self.materials: |
| 87 | + material.draw() |
| 88 | + glPopAttrib() |
| 89 | + glPopClientAttrib() |
| 90 | + |
| 91 | +setattr(Mesh, "draw", _Mesh.draw) |
| 92 | + |
| 93 | + |
| 94 | +class _Texture(object): |
| 95 | + |
| 96 | + def init(self, path): |
| 97 | + self.image_name = path |
| 98 | + self.load_image() |
| 99 | + |
| 100 | + def draw(self): |
| 101 | + if not self.image: |
| 102 | + self.load_image() |
| 103 | + |
| 104 | + glEnable(self.image.target) |
| 105 | + glBindTexture(self.image.target, self.image.id) |
| 106 | + gl.glTexParameterf(self.image.target, |
| 107 | + gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP) |
| 108 | + gl.glTexParameterf(self.image.target, |
| 109 | + gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP) |
| 110 | + |
| 111 | + def load_image(self): |
| 112 | + self.image = pyglet.image.load(self.image_name).texture |
| 113 | + self.verify_dimensions() |
| 114 | + |
| 115 | +setattr(Texture, "__init__", _Texture.init) |
| 116 | +setattr(Texture, "draw", _Texture.draw) |
| 117 | +setattr(Texture, "load_image", _Texture.load_image) |
0 commit comments