Updated mesh

This commit is contained in:
Thomas FORGIONE 2017-01-18 15:00:39 +01:00
parent 66964e1450
commit db3f3065b9
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
1 changed files with 40 additions and 1 deletions

View File

@ -1,7 +1,13 @@
class Material: class Material:
"""Represents a material
It contains its constants and its texturess. It is also usable with OpenGL
"""
def __init__(self, name): def __init__(self, name):
""" Creates an empty material
:param name: name of the material:
"""
self.name = name self.name = name
self.Ka = None self.Ka = None
self.Kd = None self.Kd = None
@ -10,6 +16,10 @@ class Material:
self.id = None self.id = None
def init_texture(self): def init_texture(self):
""" Initializes the OpenGL texture of the current material
To be simple, calls glGenTextures and stores the given id
"""
import OpenGL.GL as gl import OpenGL.GL as gl
@ -37,6 +47,8 @@ class Material:
) )
def bind(self): def bind(self):
"""Binds the material to OpenGL
"""
from OpenGL import GL as gl from OpenGL import GL as gl
gl.glEnable(gl.GL_TEXTURE_2D) gl.glEnable(gl.GL_TEXTURE_2D)
@ -46,11 +58,15 @@ class Material:
gl.glBindTexture(gl.GL_TEXTURE_2D, self.id) gl.glBindTexture(gl.GL_TEXTURE_2D, self.id)
def unbind(self): def unbind(self):
"""Disables the GL_TEXTURE_2D flag of OpenGL
"""
from OpenGL import GL as gl from OpenGL import GL as gl
gl.glDisable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_TEXTURE_2D)
Material.DEFAULT_MATERIAL=Material('') Material.DEFAULT_MATERIAL=Material('')
"""Material that is used when no material is specified
"""
Material.DEFAULT_MATERIAL.Ka = 1.0 Material.DEFAULT_MATERIAL.Ka = 1.0
Material.DEFAULT_MATERIAL.Kd = 0.0 Material.DEFAULT_MATERIAL.Kd = 0.0
Material.DEFAULT_MATERIAL.Ks = 0.0 Material.DEFAULT_MATERIAL.Ks = 0.0
@ -62,7 +78,13 @@ except ImportError:
pass pass
class MeshPart: class MeshPart:
"""A part of a 3D model that is bound to a single material
"""
def __init__(self, parent): def __init__(self, parent):
"""Creates a mesh part
:param parent: the global model with all the information
"""
self.parent = parent self.parent = parent
self.material = None self.material = None
self.vertex_vbo = None self.vertex_vbo = None
@ -72,13 +94,23 @@ class MeshPart:
self.faces = [] self.faces = []
def init_texture(self): def init_texture(self):
"""Initializes the material of the current parent
"""
if self.material is not None: if self.material is not None:
self.material.init_texture() self.material.init_texture()
def add_face(self, face): def add_face(self, face):
"""Adds a face to this MeshPart
:param face: face to add
"""
self.faces.append(face) self.faces.append(face)
def generate_vbos(self): def generate_vbos(self):
"""Generates the vbo for this MeshPart
Creates the arrays that are necessary for smooth rendering
"""
from OpenGL.arrays import vbo from OpenGL.arrays import vbo
from numpy import array from numpy import array
@ -125,7 +157,10 @@ class MeshPart:
self.color_vbo = vbo.VBO(array(c, 'f')) self.color_vbo = vbo.VBO(array(c, 'f'))
def draw(self): def draw(self):
"""Draws the current MeshPart
Binds the material, and draws the model
"""
if self.material is not None: if self.material is not None:
self.material.bind() self.material.bind()
@ -138,6 +173,10 @@ class MeshPart:
self.material.unbind() self.material.unbind()
def draw_from_vbos(self): def draw_from_vbos(self):
"""Simply calls the OpenGL drawArrays function
Sets the correct vertex arrays and draws the part
"""
import OpenGL.GL as gl import OpenGL.GL as gl