class ColorMesh: def __init__(self, attributes, index=None): self.vertex_array = VertexArray(attributes, index) self.shader = Shader(COLOR_VERT_POS, COLOR_FRAG_POS) def draw(self, projection, view, model, primitive=GL.GL_TRIANGLES, color_shader=None, **param): names = ['view', 'projection', 'model'] if color_shader is None: color_shader = self.shader loc = {n: GL.glGetUniformLocation(color_shader.glid, n) for n in names} GL.glUseProgram(color_shader.glid) GL.glEnable(GL.GL_CULL_FACE) GL.glUniformMatrix4fv(loc['view'], 1, True, view) GL.glUniformMatrix4fv(loc['projection'], 1, True, projection) GL.glUniformMatrix4fv(loc['model'], 1, True, model) # draw triangle as GL_TRIANGLE vertex array, draw array call self.vertex_array.draw(primitive) def set_shader_direct(self, shader): self.shader = shader def set_shader_indirect(self, vert_shader, frag_shader): self.shader = Shader(vert_shader, frag_shader)
class TexturedPlane: """ Simple first textured object """ def __init__(self, file): # feel free to move this up in the viewer as per other practicals self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG) # triangle and face buffers vertices = 100 * np.array(((-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0)), np.float32) faces = np.array(((0, 1, 2), (0, 2, 3)), np.uint32) self.vertex_array = VertexArray([vertices], faces) # interactive toggles self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT, GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE]) self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST), (GL.GL_LINEAR, GL.GL_LINEAR), (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)]) self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter) self.file = file # setup texture and upload it to GPU self.texture = Texture(file, self.wrap_mode, *self.filter_mode) self.pressable = [False, False] def draw(self, projection, view, model, win=None, **_kwargs): # some interactive elements if glfw.get_key(win, glfw.KEY_F6) == glfw.RELEASE: self.pressable[0] = True if glfw.get_key(win, glfw.KEY_F7) == glfw.RELEASE: self.pressable[1] = True if self.pressable[0] and glfw.get_key(win, glfw.KEY_F6) == glfw.PRESS: self.pressable[0] = False self.wrap_mode = next(self.wrap) self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode) if self.pressable[1] and glfw.get_key(win, glfw.KEY_F7) == glfw.PRESS: self.pressable[1] = False self.filter_mode = next(self.filter) self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode) GL.glUseProgram(self.shader.glid) # projection geometry loc = GL.glGetUniformLocation(self.shader.glid, 'modelviewprojection') GL.glUniformMatrix4fv(loc, 1, True, projection @ view @ model) # texture access setups loc = GL.glGetUniformLocation(self.shader.glid, 'diffuseMap') GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid) GL.glUniform1i(loc, 0) self.vertex_array.draw(GL.GL_TRIANGLES) # leave clean state for easier debugging GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glUseProgram(0)
class TexturedMesh: def __init__(self, texture, attributes, indices): self.vertex_array = VertexArray(attributes, indices) self.shader = Shader(TEXTURE_FILE_VERT, TEXTURE_FILE_FRAG) # interactive toggles self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT, GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE]) self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST), (GL.GL_LINEAR, GL.GL_LINEAR), (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)]) self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter) self.file = texture # setup texture and upload it to GPU self.texture = Texture(texture, self.wrap_mode, *self.filter_mode) self.pressable = [False, False] #C MOI KI LA FE CLOCHETEU def draw(self, projection, view, model, win=None, primitive=GL.GL_TRIANGLES, **param): if win is None: print("Win not defined!") return # some interactive elements if glfw.get_key(win, glfw.KEY_F6) == glfw.RELEASE: self.pressable[0] = True if glfw.get_key(win, glfw.KEY_F7) == glfw.RELEASE: self.pressable[1] = True if self.pressable[0] and glfw.get_key(win, glfw.KEY_F6) == glfw.PRESS: self.pressable[0] = False self.wrap_mode = next(self.wrap) self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode) if self.pressable[1] and glfw.get_key(win, glfw.KEY_F7) == glfw.PRESS: self.pressable[1] = False self.filter_mode = next(self.filter) self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode) GL.glUseProgram(self.shader.glid) # projection geometry loc = GL.glGetUniformLocation(self.shader.glid, 'modelviewprojection') GL.glUniformMatrix4fv(loc, 1, True, projection @ view @ model) # texture access setups loc = GL.glGetUniformLocation(self.shader.glid, 'texMap') GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid) GL.glUniform1i(loc, 0) self.vertex_array.draw(primitive) # leave clean state for easier debugging GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glUseProgram(0)
def __init__(self, attributes, bone_nodes, bone_offsets, index=None): # setup shader attributes for linear blend skinning shader self.vertex_array = VertexArray(attributes, index) # feel free to move this up in Viewer as shown in previous practicals self.skinning_shader = Shader(SKINNING_VERT, COLOR_FRAG) # store skinning data self.bone_nodes = bone_nodes self.bone_offsets = bone_offsets
def __init__(self, texture, attributes, indices): self.vertex_array = VertexArray(attributes, indices) self.shader = Shader(TEXTURE_FILE_VERT, TEXTURE_FILE_FRAG) # interactive toggles self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT, GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE]) self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST), (GL.GL_LINEAR, GL.GL_LINEAR), (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)]) self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter) self.file = texture # setup texture and upload it to GPU self.texture = Texture(texture, self.wrap_mode, *self.filter_mode) self.pressable = [False, False] #C MOI KI LA FE CLOCHETEU
def __init__(self, lightDirection, complexite_rocher=6, seuilChangement=0, taille_initiale_rayon=2, intensite_reduction_longueur=3, height_spike=0.5, color=[0.4, 0.4, 0.4], borne_intensite=0.2): super().__init__() rocher_array, normales, centre = createRandomRockIterative( complexite_rocher, seuilChangement, taille_initiale_rayon, intensite_reduction_longueur, borne_intensite) self.vertex_array = VertexArray(attributes=[rocher_array, normales]) self.shader = Shader(ROCHER_VERT, ROCHER_FRAG) self.lightDirection = lightDirection self.centre = centre self.color = color
class TriangleTriforce: def __init__(self, lightDirection): rocher_array, normales = essaiTriangleTriforce() self.vertex_array = VertexArray(attributes=[rocher_array, normales]) self.shader = Shader(TRI_VERT, TRI_FRAG) self.lightDirection = lightDirection def draw(self, projection, view, model, **param): names = ['view', 'projection', 'model', 'lightDirection'] loc = {n: GL.glGetUniformLocation(self.shader.glid, n) for n in names} GL.glUseProgram(self.shader.glid) GL.glUniformMatrix4fv(loc['view'], 1, True, view) GL.glUniformMatrix4fv(loc['projection'], 1, True, projection) GL.glUniformMatrix4fv(loc['model'], 1, True, model) GL.glUniform3fv(loc['lightDirection'], 1, self.lightDirection) # draw triangle as GL_TRIANGLE vertex array, draw array call self.vertex_array.draw(GL.GL_TRIANGLES)
def __init__(self): # feel free to move this up in the viewer as per other practicals self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG) # triangle and face buffers vertices = 100 * np.array(((-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0)), np.float32) faces = np.array(((0, 1, 2), (0, 2, 3)), np.uint32) self.vertex_array = VertexArray([vertices], faces) # interactive toggles self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT, GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE]) self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST), (GL.GL_LINEAR, GL.GL_LINEAR), (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)]) self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter) # setup texture and upload it to GPU self.texture = TexturePerlin(self.wrap_mode, *self.filter_mode) self.pressable = [False, False] #C MOI KI LA FE CLOCHETEU
class SkinnedMesh: """class of skinned mesh nodes in scene graph """ def __init__(self, attributes, bone_nodes, bone_offsets, index=None): # setup shader attributes for linear blend skinning shader self.vertex_array = VertexArray(attributes, index) # feel free to move this up in Viewer as shown in previous practicals self.skinning_shader = Shader(SKINNING_VERT, COLOR_FRAG) # store skinning data self.bone_nodes = bone_nodes self.bone_offsets = bone_offsets def draw(self, projection, view, _model, **_kwargs): """ skinning object draw method """ shid = self.skinning_shader.glid GL.glUseProgram(shid) # setup camera geometry parameters loc = GL.glGetUniformLocation(shid, 'projection') GL.glUniformMatrix4fv(loc, 1, True, projection) loc = GL.glGetUniformLocation(shid, 'view') GL.glUniformMatrix4fv(loc, 1, True, view) # bone world transform matrices need to be passed for skinning for bone_id, node in enumerate(self.bone_nodes): if not isinstance(node, Axis): bone_matrix = node.world_transform @ self.bone_offsets[bone_id] bone_loc = GL.glGetUniformLocation(shid, 'boneMatrix[%d]' % bone_id) GL.glUniformMatrix4fv(bone_loc, 1, True, bone_matrix) # draw mesh vertex array self.vertex_array.draw(GL.GL_TRIANGLES) # leave with clean OpenGL state, to make it easier to detect problems GL.glUseProgram(0)
def __init__(self, nom_image, light_direction, longueur_arrete=0.5, translate_x=0, translate_y=0, translate_z=0, scale_total=1): super().__init__() matrice_image = misc.imread(nom_image) self.terrain_array, self.normales = generateTerrainFromImage( matrice_image, longueur_arrete, translate_x, translate_y, translate_z, scale_total) self.x_terrain = sorted([v[0] for v in self.terrain_array]) self.z_terrain = sorted([v[2] for v in self.terrain_array]) self.height_for_2d_position = {(v[0], v[2]): v[1] for v in self.terrain_array} self.vertex_array = VertexArray( attributes=[self.terrain_array, self.normales]) self.shader = Shader(TERRAIN_VERT, TERRAIN_FRAG) self.light_direction = light_direction self.translation_y = translate_y self.scale = scale_total
class Rocher(Node): def __init__(self, lightDirection, complexite_rocher=6, seuilChangement=0, taille_initiale_rayon=2, intensite_reduction_longueur=3, height_spike=0.5, color=[0.4, 0.4, 0.4], borne_intensite=0.2): super().__init__() rocher_array, normales, centre = createRandomRockIterative( complexite_rocher, seuilChangement, taille_initiale_rayon, intensite_reduction_longueur, borne_intensite) self.vertex_array = VertexArray(attributes=[rocher_array, normales]) self.shader = Shader(ROCHER_VERT, ROCHER_FRAG) self.lightDirection = lightDirection self.centre = centre self.color = color # self.add(Axis()) def draw(self, projection, view, model, **param): names = ['view', 'projection', 'model', 'lightDirection', 'color'] loc = {n: GL.glGetUniformLocation(self.shader.glid, n) for n in names} GL.glUseProgram(self.shader.glid) GL.glUniformMatrix4fv(loc['view'], 1, True, view) GL.glUniformMatrix4fv(loc['projection'], 1, True, projection) GL.glUniformMatrix4fv(loc['model'], 1, True, model) GL.glUniform3fv(loc['lightDirection'], 1, self.lightDirection) GL.glUniform3fv(loc['color'], 1, self.color) # draw triangle as GL_TRIANGLE vertex array, draw array call self.vertex_array.draw(GL.GL_TRIANGLES) super().draw(projection, view, model, **param)
def __init__(self, attributes, index=None): self.vertex_array = VertexArray(attributes, index) self.shader = Shader(COLOR_VERT_POS, COLOR_FRAG_POS)
def __init__(self, lightDirection): rocher_array, normales = essaiTriangleTriforce() self.vertex_array = VertexArray(attributes=[rocher_array, normales]) self.shader = Shader(TRI_VERT, TRI_FRAG) self.lightDirection = lightDirection
class Terrain(Node): def __init__(self, nom_image, light_direction, longueur_arrete=0.5, translate_x=0, translate_y=0, translate_z=0, scale_total=1): super().__init__() matrice_image = misc.imread(nom_image) self.terrain_array, self.normales = generateTerrainFromImage( matrice_image, longueur_arrete, translate_x, translate_y, translate_z, scale_total) self.x_terrain = sorted([v[0] for v in self.terrain_array]) self.z_terrain = sorted([v[2] for v in self.terrain_array]) self.height_for_2d_position = {(v[0], v[2]): v[1] for v in self.terrain_array} self.vertex_array = VertexArray( attributes=[self.terrain_array, self.normales]) self.shader = Shader(TERRAIN_VERT, TERRAIN_FRAG) self.light_direction = light_direction self.translation_y = translate_y self.scale = scale_total def find_height(self, x, z): """ Return the height for an x and a z """ x_index = bisect_left(self.x_terrain, x) z_index = bisect_left(self.z_terrain, z) if x_index < len(self.x_terrain) - 2: x_finded = self.x_terrain[x_index + 1] else: x_finded = self.x_terrain[-1] if z_index < len(self.z_terrain) - 2: z_finded = self.z_terrain[z_index + 1] else: z_finded = self.z_terrain[-1] return self.height_for_2d_position.get((x_finded, z_finded)) def find_position(self, x, z): x_index = bisect_left(self.x_terrain, x) z_index = bisect_left(self.z_terrain, z) x_finded = self.x_terrain[x_index + 1] z_finded = self.z_terrain[z_index + 1] # print("x_finded, z_finded = ({}, {})".format(x_finded, z_finded)) # print("array x : ", self.x_terrain[x_index-1:x_index+2]) # print("array z : ", self.z_terrain[z_index-1:z_index+2]) return (x_finded, self.height_for_2d_position.get( (x_finded, z_finded)), z_finded) def draw(self, projection, view, model, **param): names = [ 'view', 'projection', 'model', 'light_direction', 'translation', 'scale' ] loc = {n: GL.glGetUniformLocation(self.shader.glid, n) for n in names} GL.glUseProgram(self.shader.glid) GL.glUniformMatrix4fv(loc['view'], 1, True, view) GL.glUniformMatrix4fv(loc['projection'], 1, True, projection) GL.glUniformMatrix4fv(loc['model'], 1, True, model) GL.glUniform3fv(loc['light_direction'], 1, self.light_direction) GL.glUniform1f(loc['translation'], self.translation_y) GL.glUniform1f(loc['scale'], self.scale) # draw triangle as GL_TRIANGLE vertex array, draw array call self.vertex_array.draw(GL.GL_TRIANGLES) super().draw(projection, view, model, **param)