Skip to content

Commit 5f7d637

Browse files
committed
Unlit and Nontextured Visualization Options
Could help with things like object picking by allowing devs to render objects as separate colors using glColor in an offscreen render, so checking which object a user is clicking on for example is as simple as checking the color of that pixel.
1 parent a30aee1 commit 5f7d637

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

pywavefront/visualization.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,28 @@ def same(v):
5656
}
5757

5858

59-
def draw(instance):
59+
def draw(instance, unlit=False, useTexture=True):
6060
"""Generic draw function"""
6161
# Draw Wavefront instance
6262
if isinstance(instance, Wavefront):
63-
draw_materials(instance.materials)
63+
draw_materials(instance.materials, unlit=unlit, useTexture=useTexture)
6464
# Draw single material
6565
elif isinstance(instance, Material):
66-
draw_material(instance)
66+
draw_material(instance, unlit=unlit, useTexture=useTexture)
6767
# Draw dict of materials
6868
elif isinstance(instance, dict):
69-
draw_materials(instance)
69+
draw_materials(instance, unlit=unlit, useTexture=useTexture)
7070
else:
7171
raise ValueError("Cannot figure out how to draw: {}".format(instance))
7272

7373

74-
def draw_materials(materials):
74+
def draw_materials(materials, unlit=False, useTexture=True):
7575
"""Draw a dict of meshes"""
7676
for name, material in materials.items():
77-
draw_material(material)
77+
draw_material(material, unlit=unlit, useTexture=useTexture)
7878

7979

80-
def draw_material(material, face=GL_FRONT_AND_BACK):
80+
def draw_material(material, face=GL_FRONT_AND_BACK, unlit=False, useTexture=True):
8181
"""Draw a single material"""
8282
if material.gl_floats is None:
8383
material.gl_floats = (GLfloat * len(material.vertices))(*material.vertices)
@@ -93,23 +93,27 @@ def draw_material(material, face=GL_FRONT_AND_BACK):
9393
glEnable(GL_DEPTH_TEST)
9494
glCullFace(GL_BACK)
9595

96-
# Fall back to ambient texture if no diffuse
97-
texture = material.texture or material.texture_ambient
98-
if texture and material.has_uvs:
99-
bind_texture(texture)
100-
else:
101-
glDisable(GL_TEXTURE_2D)
102-
103-
glMaterialfv(face, GL_DIFFUSE, gl_light(material.diffuse))
104-
glMaterialfv(face, GL_AMBIENT, gl_light(material.ambient))
105-
glMaterialfv(face, GL_SPECULAR, gl_light(material.specular))
106-
glMaterialfv(face, GL_EMISSION, gl_light(material.emissive))
107-
glMaterialf(face, GL_SHININESS, min(128.0, material.shininess))
108-
glEnable(GL_LIGHT0)
109-
110-
if material.has_normals:
111-
glEnable(GL_LIGHTING)
112-
else:
96+
if useTexture:
97+
# Fall back to ambient texture if no diffuse
98+
texture = material.texture or material.texture_ambient
99+
if texture and material.has_uvs:
100+
bind_texture(texture)
101+
else:
102+
glDisable(GL_TEXTURE_2D)
103+
104+
if not unlit:
105+
glMaterialfv(face, GL_DIFFUSE, gl_light(material.diffuse))
106+
glMaterialfv(face, GL_AMBIENT, gl_light(material.ambient))
107+
glMaterialfv(face, GL_SPECULAR, gl_light(material.specular))
108+
glMaterialfv(face, GL_EMISSION, gl_light(material.emissive))
109+
glMaterialf(face, GL_SHININESS, min(128.0, material.shininess))
110+
glEnable(GL_LIGHT0)
111+
112+
if material.has_normals:
113+
glEnable(GL_LIGHTING)
114+
else:
115+
glDisable(GL_LIGHTING)
116+
else:
113117
glDisable(GL_LIGHTING)
114118

115119
glInterleavedArrays(vertex_format, 0, material.gl_floats)

0 commit comments

Comments
 (0)