Textures are now supported

This commit is contained in:
Thomas FORGIONE 2016-11-30 10:08:37 +01:00
parent 7a9e1a741e
commit 8d31b13da7
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
4 changed files with 34 additions and 8 deletions

View File

@ -4,6 +4,7 @@ vec3 ambientLight = vec3(0.2,0.2,0.2);
vec3 directionnalLight = normalize(vec3(10,5,7)); vec3 directionnalLight = normalize(vec3(10,5,7));
vec3 directionnalLightFactor = vec3(0.5,0.5,0.5); vec3 directionnalLightFactor = vec3(0.5,0.5,0.5);
uniform sampler2D tex;
void main() { void main() {
@ -11,5 +12,11 @@ void main() {
vec3 lambertFactor = max(vec3(0.0,0.0,0.0), dot(directionnalLight, fNormal) * directionnalLightFactor); vec3 lambertFactor = max(vec3(0.0,0.0,0.0), dot(directionnalLight, fNormal) * directionnalLightFactor);
vec4 texel = texture2D(tex, gl_TexCoord[0].xy); vec4 texel = texture2D(tex, gl_TexCoord[0].xy);
gl_FragColor = vec4(ambientFactor + lambertFactor, 1.0); vec4 noTexColor = vec4(ambientFactor + lambertFactor, 1.0);
vec4 color = texture2D(tex,gl_TexCoord[0].st);
vec4 fragColor = noTexColor * color;
gl_FragColor = fragColor;
} }

View File

@ -77,7 +77,7 @@ class ModelParser:
def add_face(self, face): def add_face(self, face):
if self.current_part is None or face.material != self.current_part.material: if self.current_part is None or face.material != self.current_part.material:
self.current_part = MeshPart(self) self.current_part = MeshPart(self)
self.current_part.material = face.material self.current_part.material = face.material if face.material is not None else Material.DEFAULT_MATERIAL
self.parts.append(self.current_part) self.parts.append(self.current_part)
self.current_part.add_face(face) self.current_part.add_face(face)
@ -136,9 +136,11 @@ class ModelParser:
def generate_face_normals(self): def generate_face_normals(self):
self.normals = [Normal() for i in sum(list(map(lambda x: len(x), self.faces)))] # Build array of faces
faces = sum(map(lambda x: x.faces, self.parts), [])
self.normals = [Normal()] * len(faces)
for (index, face) in enumerate(sum(self.faces, [])): for (index, face) in enumerate(faces):
v1 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.b.vertex]) v1 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.b.vertex])
v2 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.c.vertex]) v2 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.c.vertex])

View File

@ -1,6 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
class Material: class Material:
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.Ka = None self.Ka = None
@ -48,6 +50,13 @@ class Material:
def unbind(self): def unbind(self):
pass pass
import PIL.Image
Material.DEFAULT_MATERIAL=Material('')
Material.DEFAULT_MATERIAL.Ka = 1.0
Material.DEFAULT_MATERIAL.Kd = 0.0
Material.DEFAULT_MATERIAL.Ks = 0.0
Material.DEFAULT_MATERIAL.map_Kd = PIL.Image.new("RGBA", (1,1), "white")
class MeshPart: class MeshPart:
def __init__(self, parent): def __init__(self, parent):

View File

@ -43,11 +43,20 @@ def main(args):
running = True running = True
# Load and parse the model
model = load_model(args.input) model = load_model(args.input)
# Initializes OpenGL textures
model.init_textures() model.init_textures()
# Compute normals if not already computed
if len(model.normals) == 0:
model.generate_vertex_normals()
# Generate vbos for smooth rendering
model.generate_vbos() model.generate_vbos()
# shader = DefaultShader() shader = DefaultShader()
while running: while running:
for event in pg.event.get(): for event in pg.event.get():
@ -65,7 +74,6 @@ def main(args):
# Update physics # Update physics
controls.update() controls.update()
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glMatrixMode(gl.GL_MODELVIEW) gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity() gl.glLoadIdentity()
@ -74,9 +82,9 @@ def main(args):
gl.glPushMatrix() gl.glPushMatrix()
controls.apply() controls.apply()
# shader.bind() shader.bind()
model.draw() model.draw()
# shader.unbind() shader.unbind()
gl.glPopMatrix() gl.glPopMatrix()
gl.glFlush() gl.glFlush()