-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.py
72 lines (60 loc) · 2.36 KB
/
material.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def decodeK(chunks):
if len(chunks) == 2:
return [float(chunks[1]), float(chunks[1]), float(chunks[1])]
else:
return [float(chunk) for chunk in chunks[1:]]
def decodeD(chunks):
if chunks[1] == '-halo':
return (True, float(chunks[2]))
else:
return (False, float(chunks[1]))
class Material(object):
def __init__(self, lines):
self.d = (False, 1.0)
self.Ka = None
self.Kd = None
self.Ks = None
self.Tf = None
self.Ns = None
self.Ni = None
self.illum = None
self.sharpness = 60
for line in lines:
chunks = line.split()
if len(chunks) > 0:
if chunks[0] == 'd': self.d = decodeD(chunks)
elif chunks[0] == 'Ka': self.Ka = decodeK(chunks)
elif chunks[0] == 'Kd': self.Kd = decodeK(chunks)
elif chunks[0] == 'Ks': self.Ks = decodeK(chunks)
elif chunks[0] == 'Tf': self.Tf = decodeK(chunks)
elif chunks[0] == 'Ns': self.Ns = float(chunks[1]) / 1000 * 128
elif chunks[0] == 'Ni': self.Ni = float(chunks[1])
elif chunks[0] == 'illum': self.illum = int(chunks[1])
elif chunks[0] == 'sharpness': self.sharpness = int(chunks[1])
self.listID = glGenLists(1); glNewList(self.listID, GL_COMPILE_AND_EXECUTE)
glMaterial(GL_FRONT_AND_BACK, GL_AMBIENT, self.Ka + [self.d[1]])
glMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE, self.Kd + [self.d[1]])
glMaterial(GL_FRONT_AND_BACK, GL_SPECULAR, self.Ks + [self.d[1]])
glMaterial(GL_FRONT_AND_BACK, GL_SHININESS, self.Ns)
glEndList()
def setMaterial(self):
glCallList(self.listID)
# Illumination Models:
# 0 Color on and Ambient off
# 1 Color on and Ambient on
# 2 Highlight on
# 3 Reflection on and Ray trace on
# 4 Transparency: Glass on
# Reflection: Ray trace on
# 5 Reflection: Fresnel on and Ray trace on
# 6 Transparency: Refraction on
# Reflection: Fresnel off and Ray trace on
# 7 Transparency: Refraction on
# Reflection: Fresnel on and Ray trace on
# 8 Reflection on and Ray trace off
# 9 Transparency: Glass on
# Reflection: Ray trace off
# 10 Casts shadows onto invisible surfaces