def load_mtl(self, path): f_mtl = open(path, 'r') for line in f_mtl.readlines(): line = line.strip() if len(line) <= 0 or line[0] == '#': # commit continue name, params = line.split(' ', 1) params = params.strip().split(' ') if name == 'newmtl': m = Material() self.mtl[params[0]] = m elif name == 'Ns': m.Ns = float(params[0]) elif line.find('Ni') == 0: m.Ni = float(params[0]) elif line.find('d') == 0: m.d = float(params[0]) elif line.find('Tr') == 0: m.Tr = float(params[0]) elif line.find('Tf') == 0: m.Tf[0] = float(params[0]) m.Tf[1] = float(params[1]) m.Tf[2] = float(params[2]) elif line.find('illum') == 0: m.illum = int(float(params[0])) elif line.find('Ka') == 0: m.Ka[0] = float(params[0]) m.Ka[1] = float(params[1]) m.Ka[2] = float(params[2]) elif line.find('Kd') == 0: m.Kd[0] = float(params[0]) m.Kd[1] = float(params[1]) m.Kd[2] = float(params[2]) elif line.find('Ks') == 0: m.Ks[0] = float(params[0]) m.Ks[1] = float(params[1]) m.Ks[2] = float(params[2]) elif line.find('Ke') == 0: m.Ke[0] = float(params[0]) m.Ke[1] = float(params[1]) m.Ke[2] = float(params[2]) elif line.find('map_Ka') == 0: m.map_Ka_str = params[0] p = os.path.split(path)[0] m.map_Ka_id = load_texture(os.path.join(p, m.map_Ka_str)) elif line.find('map_Kd') == 0: m.map_Kd_str = params[0] p = os.path.split(path)[0] m.map_Kd_id = load_texture(os.path.join(p, m.map_Kd_str)) f_mtl.close()
def loadTextures(in_path, loadFromDirectory, loadFromData): """ Loads all textures into a single height x width x depth(s) array. Inputs: in_path - path to texture(s) loadFromDirectory - is the path a directory? loadFromData - should all textures be treated as data files? Output: Returns array of texture data and list of cooresponding InputTextureFile objects. """ if (loadFromDirectory): files = sorted([ f for f in os.listdir(in_path) if os.path.isfile(os.path.join(in_path, f)) ]) else: in_path, base = os.path.split(in_path) files = [base] textures = [] textureData = None for f in files: fpath = os.path.join(in_path, f) if (loadFromData or os.path.splitext(f)[1] == ".data"): data = weight_data.read_tex_from_path(fpath)[0] isFloatTexture, isDataFile = True, True else: data = numpy.array(texture.load_texture(fpath)) isFloatTexture = not issubclass(data.dtype.type, numpy.integer) if (not isFloatTexture): data = data / 255.0 isDataFile = False data = numpy.expand_dims(data, -1) if len(data.shape) < 3 else data textures.append( InputTextureFile(f, data.shape[2], isFloatTexture, isDataFile)) textureData = data if textureData is None else (numpy.concatenate( (textureData, data), axis=2)) print() return textureData, textures
def main(): global delta_time, last_frame glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) window = glfw.create_window(SRC_WIDTH, SRC_HEIGHT, "learnOpenGL", None, None) if not window: glfw.terminate() raise ValueError("Failed to create window") glfw.make_context_current(window) glfw.set_framebuffer_size_callback(window, framebuffer_size_callback) glfw.set_cursor_pos_callback(window, mouse_callback) glfw.set_scroll_callback(window, scroll_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) gl.glEnable(gl.GL_DEPTH_TEST) lamp_shader = Shader(CURDIR / "shaders/1.lamp.vs", CURDIR / "shaders/6.1.lamp.fs") lighting_shader = Shader(CURDIR / "shaders/6.multiple_lights.vs", CURDIR / "shaders/6.multiple_lights.fs") vertices = [ # positions normals texture coords -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, -0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, -0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0 ] vertices = (c_float * len(vertices))(*vertices) cube_positions = [(0.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -2.2, -2.5), (-3.8, -2.0, -12.3), (2.4, -0.4, -3.5), (-1.7, 3.0, -7.5), (1.3, -2.0, -2.5), (1.5, 2.0, -2.5), (1.5, 0.2, -1.5), (-1.3, 1.0, -1.5)] point_light_positions = [ (0.7, 0.2, 2.0), (2.3, -3.3, -4.0), (-4.0, 2.0, -12.0), (0.0, 0.0, -3.0), ] light_settings = { "desert": { "clear_color": (.75, .52, .30, 1.0), "direction_color": (.4, .4, .4), "spot_color": (.2, .2, .2), "point_colors": [(1.0, 0.0, 0.0), (1.0, 0.6, 0.0), (1.0, 0.0, 0.0), (1.0, 0.6, 0.0)] }, "factory": { "clear_color": (0.01, 0.01, 0.01, 1.0), "direction_color": (.4, .4, .4), "spot_color": (.2, .2, .2), "point_colors": [(.17, .17, .49), (.17, .17, .49), (.17, .17, .49), (.17, .17, .49)] }, "horror": { "clear_color": (0.0, 0.0, 0.0, 1.0), "direction_color": (.01, .01, .01), "spot_color": (.2, .2, .2), "point_colors": [(.3, .1, .1), (.3, .1, .1), (.3, .1, .1), (.3, .1, .1)] }, "biochemical_lab": { "clear_color": (0.9, 0.9, 0.9, 1.0), "direction_color": (.4, .4, .4), "spot_color": (.2, .2, .2), "point_colors": [(0.4, 0.7, 0.1), (0.4, 0.7, 0.1), (0.4, 0.7, 0.1), (0.4, 0.7, 0.1)] }, } cube_vao = gl.glGenVertexArrays(1) vbo = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(vertices), vertices, gl.GL_STATIC_DRAW) gl.glBindVertexArray(cube_vao) # -- position attribute gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(0)) gl.glEnableVertexAttribArray(0) # -- normal attribute gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(3 * sizeof(c_float))) gl.glEnableVertexAttribArray(1) # -- texture coordinate gl.glVertexAttribPointer(2, 2, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(6 * sizeof(c_float))) gl.glEnableVertexAttribArray(2) # -- second configure light vao (vbo is the same) light_vao = gl.glGenVertexArrays(1) gl.glBindVertexArray(light_vao) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(0)) gl.glEnableVertexAttribArray(0) # -- load texture diffuse_map = load_texture("container2.png") specular_map = load_texture("container2_specular.png") # -- shader configuration lighting_shader.use() lighting_shader.set_int("material.diffuse", 0) lighting_shader.set_int("material.specular", 1) LIGHTS = light_settings.get("horror") while not glfw.window_should_close(window): # -- time logic current_frame = glfw.get_time() delta_time = current_frame - last_frame last_frame = current_frame # -- input process_input(window) # -- render gl.glClearColor(*LIGHTS.get("clear_color")) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) lighting_shader.use() lighting_shader.set_vec3("viewPos", camera.position) lighting_shader.set_float("material.shininess", 32.0) # -- directional light lighting_shader.set_vec3("dirLight.direction", [-0.2, -1.0, -0.3]) lighting_shader.set_vec3("dirLight.ambient", [0.05, 0.05, 0.05]) lighting_shader.set_vec3("dirLight.diffuse", LIGHTS.get("direction_color")) lighting_shader.set_vec3("dirLight.specular", [0.5, 0.5, 0.5]) # -- point light 1 lighting_shader.set_vec3("pointLights[0].position", point_light_positions[0]) lighting_shader.set_vec3("pointLights[0].ambient", [0.05, 0.05, 0.05]) lighting_shader.set_vec3("pointLights[0].diffuse", LIGHTS.get("point_colors")[0]) lighting_shader.set_vec3("pointLights[0].specular", [1.0, 1.0, 1.0]) lighting_shader.set_float("pointLights[0].constant", 1.0) lighting_shader.set_float("pointLights[0].linear", 0.09) lighting_shader.set_float("pointLights[0].quadratic", 0.032) # -- point light 2 lighting_shader.set_vec3("pointLights[1].position", point_light_positions[1]) lighting_shader.set_vec3("pointLights[1].ambient", [0.05, 0.05, 0.05]) lighting_shader.set_vec3("pointLights[1].diffuse", LIGHTS.get("point_colors")[1]) lighting_shader.set_vec3("pointLights[1].specular", [1.0, 1.0, 1.0]) lighting_shader.set_float("pointLights[1].constant", 1.0) lighting_shader.set_float("pointLights[1].linear", 0.09) lighting_shader.set_float("pointLights[1].quadratic", 0.032) # -- point light 3 lighting_shader.set_vec3("pointLights[2].position", point_light_positions[2]) lighting_shader.set_vec3("pointLights[2].ambient", [0.05, 0.05, 0.05]) lighting_shader.set_vec3("pointLights[2].diffuse", LIGHTS.get("point_colors")[2]) lighting_shader.set_vec3("pointLights[2].specular", [1.0, 1.0, 1.0]) lighting_shader.set_float("pointLights[2].constant", 1.0) lighting_shader.set_float("pointLights[2].linear", 0.09) lighting_shader.set_float("pointLights[2].quadratic", 0.032) # -- point light 4 lighting_shader.set_vec3("pointLights[3].position", point_light_positions[3]) lighting_shader.set_vec3("pointLights[3].ambient", [0.05, 0.05, 0.05]) lighting_shader.set_vec3("pointLights[3].diffuse", LIGHTS.get("point_colors")[3]) lighting_shader.set_vec3("pointLights[3].specular", [1.0, 1.0, 1.0]) lighting_shader.set_float("pointLights[3].constant", 1.0) lighting_shader.set_float("pointLights[3].linear", 0.09) lighting_shader.set_float("pointLights[3].quadratic", 0.032) # -- spotLight lighting_shader.set_vec3("spotLight.position", camera.position) lighting_shader.set_vec3("spotLight.direction", camera.front) lighting_shader.set_vec3("spotLight.ambient", [0.0, 0.0, 0.0]) lighting_shader.set_vec3("spotLight.diffuse", LIGHTS.get("spot_color")) lighting_shader.set_vec3("spotLight.specular", [1.0, 1.0, 1.0]) lighting_shader.set_float("spotLight.constant", 1.0) lighting_shader.set_float("spotLight.linear", 0.09) lighting_shader.set_float("spotLight.quadratic", 0.032) lighting_shader.set_float("spotLight.cutOff", math.cos(math.radians(12.5))) lighting_shader.set_float("spotLight.outerCutOff", math.cos(math.radians(15.0))) # -- view.projection transformations projection = Matrix44.perspective_projection(camera.zoom, SRC_WIDTH / SRC_HEIGHT, 0.1, 100.0) view = camera.get_view_matrix() lighting_shader.set_mat4("projection", projection) lighting_shader.set_mat4("view", view) # -- world transformation model = Matrix44.identity() lighting_shader.set_mat4("model", model) # -- bind diffuse map gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, diffuse_map) # -- bind specular map gl.glActiveTexture(gl.GL_TEXTURE1) gl.glBindTexture(gl.GL_TEXTURE_2D, specular_map) # -- render continers gl.glBindVertexArray(cube_vao) for idx, position in enumerate(cube_positions): angle = 20.0 * idx rotation = matrix44.create_from_axis_rotation([1.0, 0.3, 0.5], math.radians(angle)) translation = Matrix44.from_translation(position) model = translation * rotation lighting_shader.set_mat4('model', model) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) # -- draw lamp object(s) lamp_shader.use() lamp_shader.set_mat4("projection", projection) lamp_shader.set_mat4("view", view) gl.glBindVertexArray(light_vao) for idx, pos in enumerate(point_light_positions): model = Matrix44.identity() model *= Matrix44.from_translation(pos) model *= Matrix44.from_scale(Vector3([.2, .2, .2])) lamp_shader.set_mat4("model", model) lamp_shader.set_vec3("color", LIGHTS.get("point_colors")[idx]) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) glfw.swap_buffers(window) glfw.poll_events() gl.glDeleteVertexArrays(1, id(cube_vao)) gl.glDeleteVertexArrays(1, id(light_vao)) gl.glDeleteBuffers(1, id(vbo)) glfw.terminate()
def main(): global delta_time, last_frame glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) window = glfw.create_window(SRC_WIDTH, SRC_HEIGHT, "learnOpenGL", None, None) if not window: glfw.terminate() raise ValueError("Failed to create window") glfw.make_context_current(window) glfw.set_framebuffer_size_callback(window, framebuffer_size_callback) glfw.set_cursor_pos_callback(window, mouse_callback) glfw.set_scroll_callback(window, scroll_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) gl.glEnable(gl.GL_DEPTH_TEST) lamp_shader = Shader(CURDIR / "shaders/1.lamp.vs", CURDIR / "shaders/1.lamp.fs") lighting_shader = Shader(CURDIR / "shaders/5.3.light_casters.vs", CURDIR / "shaders/5.3.light_casters.fs") vertices = [ # positions normals texture coords -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, -0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, -0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0 ] vertices = (c_float * len(vertices))(*vertices) cube_positions = [(0.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -2.2, -2.5), (-3.8, -2.0, -12.3), (2.4, -0.4, -3.5), (-1.7, 3.0, -7.5), (1.3, -2.0, -2.5), (1.5, 2.0, -2.5), (1.5, 0.2, -1.5), (-1.3, 1.0, -1.5)] cube_vao = gl.glGenVertexArrays(1) vbo = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(vertices), vertices, gl.GL_STATIC_DRAW) gl.glBindVertexArray(cube_vao) # -- position attribute gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(0)) gl.glEnableVertexAttribArray(0) # -- normal attribute gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(3 * sizeof(c_float))) gl.glEnableVertexAttribArray(1) # -- texture coordinate gl.glVertexAttribPointer(2, 2, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(6 * sizeof(c_float))) gl.glEnableVertexAttribArray(2) # -- second configure light vao (vbo is the same) light_vao = gl.glGenVertexArrays(1) gl.glBindVertexArray(light_vao) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(0)) gl.glEnableVertexAttribArray(0) # -- load texture diffuse_map = load_texture("container2.png") specular_map = load_texture("container2_specular.png") # -- shader configuration lighting_shader.use() lighting_shader.set_int("material.diffuse", 0) lighting_shader.set_int("material.specular", 1) while not glfw.window_should_close(window): # -- time logic current_frame = glfw.get_time() delta_time = current_frame - last_frame last_frame = current_frame # -- input process_input(window) # -- render gl.glClearColor(0.1, 0.1, 0.1, 1.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) lighting_shader.use() lighting_shader.set_vec3("light.position", camera.position) lighting_shader.set_vec3("light.direction", camera.front) lighting_shader.set_float("light.cutOff", math.cos(math.radians(12.5))) lighting_shader.set_vec3("viewPos", camera.position) # -- light properties lighting_shader.set_vec3("light.ambient", Vector3([0.1, 0.1, 0.1])) # we configure the diffuse intensity slightly higher; the right lighting conditions differ with each lighting method and environment. # each environment and lighting type requires some tweaking to get the best out of your environment. lighting_shader.set_vec3("light.diffuse", Vector3([0.8, 0.8, 0.8])) lighting_shader.set_vec3("light.specular", Vector3([1.0, 1.0, 1.0])) lighting_shader.set_float("light.constant", 1.0) lighting_shader.set_float("light.linear", 0.09) lighting_shader.set_float("light.quadratic", 0.032) # -- material properties lighting_shader.set_float("material.shininess", 32.0) # -- view.projection transformations projection = Matrix44.perspective_projection(camera.zoom, SRC_WIDTH / SRC_HEIGHT, 0.1, 100.0) view = camera.get_view_matrix() lighting_shader.set_mat4("projection", projection) lighting_shader.set_mat4("view", view) # -- world transformation model = Matrix44.identity() lighting_shader.set_mat4("model", model) # -- bind diffuse map gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, diffuse_map) # -- bind specular map gl.glActiveTexture(gl.GL_TEXTURE1) gl.glBindTexture(gl.GL_TEXTURE_2D, specular_map) # -- render continers gl.glBindVertexArray(cube_vao) for idx, position in enumerate(cube_positions): angle = 20.0 * idx rotation = matrix44.create_from_axis_rotation([1.0, 0.3, 0.5], math.radians(angle)) translation = Matrix44.from_translation(position) model = translation * rotation lighting_shader.set_mat4('model', model) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) # # -- draw lamp object # lamp_shader.use() # lamp_shader.set_mat4("projection", projection) # lamp_shader.set_mat4("view", view) # model = Matrix44.identity() # model *= Matrix44.from_translation(light_pos) # model *= Matrix44.from_scale(Vector3([.2, .2, .2])) # lamp_shader.set_mat4("model", model) # gl.glBindVertexArray(light_vao) # gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) glfw.swap_buffers(window) glfw.poll_events() gl.glDeleteVertexArrays(1, id(cube_vao)) gl.glDeleteVertexArrays(1, id(light_vao)) gl.glDeleteBuffers(1, id(vbo)) glfw.terminate()
# Test inside face for all pixels in the bounding box mask[inbox[:, 1], inbox[:, 0]] |= points_in_triangle(face, inbox) print_progress(1.0) print() return ~(mask) if __name__ == "__main__": import obj_reader import texture from find_seam import find_seam, seam_to_UV from util import * mesh = obj_reader.quads_to_triangles(obj_reader.load_obj( '../models/cow.obj')) texture = numpy.array(texture.load_texture( "../textures/cow/Cow_Monster_N.png")) height, width, depth = (texture.shape + (1,))[:3] N = width * height textureVec = texture.reshape(N, -1) print("\nFinding seam of model") seam, boundary, foldovers = find_seam(mesh) uv_seam, uv_boundary, uv_foldovers = seam_to_UV( mesh, seam, boundary, foldovers) print("Done\n") print("Number of edges along the seam: %d" % (len(seam) * 2)) print("Number of edges along the boundary: %d" % len(boundary)) print("Number of foldover edges: %d\n" % len(foldovers))
# Test inside face for all pixels in the bounding box mask[inbox[:, 1], inbox[:, 0]] |= points_in_triangle(face, inbox) return ~mask if __name__ == "__main__": import obj_reader import texture from find_seam import find_seam, seam_to_UV mesh = obj_reader.quads_to_triangles( obj_reader.load_obj('../models/cow.obj')) texture = numpy.array( texture.load_texture("../textures/cow/Cow_Monster_N.png")) height, width, depth = (texture.shape + (1, ))[:3] N = width * height textureVec = texture.reshape(N, -1) logging.info("\nFinding seam of model") seam, boundary, foldovers = find_seam(mesh) uv_seam, uv_boundary, uv_foldovers = seam_to_UV(mesh, seam, boundary, foldovers) logging.info("Done\n") logging.info("Number of edges along the seam: %d" % (len(seam) * 2)) logging.info("Number of edges along the boundary: %d" % len(boundary)) logging.info("Number of foldover edges: %d\n" % len(foldovers))
def main(): global delta_time, last_frame if not glfw.init(): raise ValueError("Failed to initialize glfw") glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) window = glfw.create_window(SRC_WIDTH, SRC_HEIGHT, "learnOpenGL", None, None) if not window: glfw.terminate() raise ValueError("Failed to create window") glfw.make_context_current(window) glfw.set_framebuffer_size_callback(window, framebuffer_size_callback) glfw.set_cursor_pos_callback(window, mouse_callback) glfw.set_scroll_callback(window, scroll_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) gl.glEnable(gl.GL_DEPTH_TEST) lamp_shader = Shader(CURDIR / "shaders/1.lamp.vs", CURDIR / "shaders/1.lamp.fs") lighting_shader = Shader(CURDIR / "shaders/4.1.lighting_maps.vs", CURDIR / "shaders/4.1.lighting_maps.fs") vertices = [ # positions normals texture coords -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, -0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, -0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0 ] vertices = (c_float * len(vertices))(*vertices) cube_vao = gl.glGenVertexArrays(1) vbo = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(vertices), vertices, gl.GL_STATIC_DRAW) gl.glBindVertexArray(cube_vao) # -- position attribute gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(0)) gl.glEnableVertexAttribArray(0) # -- normal attribute gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(3 * sizeof(c_float))) gl.glEnableVertexAttribArray(1) # -- texture coordinate gl.glVertexAttribPointer(2, 2, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(6 * sizeof(c_float))) gl.glEnableVertexAttribArray(2) # -- second configure light vao (vbo is the same) light_vao = gl.glGenVertexArrays(1) gl.glBindVertexArray(light_vao) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 8 * sizeof(c_float), c_void_p(0)) gl.glEnableVertexAttribArray(0) # -- load texture diffuse_map = load_texture("container2.png") # -- shader configuration lighting_shader.use() lighting_shader.set_int("material.diffuse", 0) while not glfw.window_should_close(window): # -- time logic current_frame = glfw.get_time() delta_time = current_frame - last_frame last_frame = current_frame # -- input process_input(window) # -- render gl.glClearColor(0.1, 0.1, 0.1, 1.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) lighting_shader.use() lighting_shader.set_vec3("light.position", light_pos) lighting_shader.set_vec3("viewPos", camera.position) # -- light properties lighting_shader.set_vec3("light.ambient", Vector3([0.2, 0.2, 0.2])) lighting_shader.set_vec3("light.diffuse", Vector3([0.5, 0.5, 0.5])) lighting_shader.set_vec3("light.specular", Vector3([1.0, 1.0, 1.0])) # -- material properties lighting_shader.set_vec3("material.specular", Vector3([0.5, 0.5, 0.5])) lighting_shader.set_float("material.shininess", 32.0) # -- view.projection transformations projection = Matrix44.perspective_projection(camera.zoom, SRC_WIDTH / SRC_HEIGHT, 0.1, 100.0) view = camera.get_view_matrix() lighting_shader.set_mat4("projection", projection) lighting_shader.set_mat4("view", view) # -- world transformation model = Matrix44.identity() lighting_shader.set_mat4("model", model) # -- bind diffuse map gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, diffuse_map) # -- render cube gl.glBindVertexArray(cube_vao) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) # -- draw lamp object lamp_shader.use() lamp_shader.set_mat4("projection", projection) lamp_shader.set_mat4("view", view) model = Matrix44.identity() model *= Matrix44.from_translation(light_pos) model *= Matrix44.from_scale(Vector3([.2, .2, .2])) lamp_shader.set_mat4("model", model) gl.glBindVertexArray(light_vao) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) glfw.swap_buffers(window) glfw.poll_events() gl.glDeleteVertexArrays(1, id(cube_vao)) gl.glDeleteVertexArrays(1, id(light_vao)) gl.glDeleteBuffers(1, id(vbo)) glfw.terminate()
def normal_sphere(r, divide, tex, normal, lighting=True, fv4=GLfloat * 4): if (not have_extension('GL_ARB_multitexture') or not have_extension('GL_ARB_texture_env_combine') or not have_extension('GL_EXT_texture_env_dot3')): print 'No hardware normal mapping support. No bumping for you.' return sphere(r, divide, divide, tex, lighting) from texture import load_texture normal = load_texture(normal) with glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT): glEnable(GL_CULL_FACE) glCullFace(GL_BACK) glActiveTextureARB(GL_TEXTURE0_ARB) glBindTexture(GL_TEXTURE_2D, normal) glEnable(GL_TEXTURE_2D) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB) glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGBA_ARB) glActiveTextureARB(GL_TEXTURE1_ARB) glBindTexture(GL_TEXTURE_2D, tex) glEnable(GL_TEXTURE_2D) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB) glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE) if lighting: glDisable(GL_BLEND) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, fv4(1, 1, 1, 0)) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fv4(1, 1, 1, 0)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 125) else: glDisable(GL_LIGHTING) glBindTexture(GL_TEXTURE_2D, tex) twopi_divide = TWOPI / divide pi_divide = pi / divide with glSection(GL_TRIANGLE_STRIP): for j in xrange(divide + 1): phi1 = j * twopi_divide phi2 = (j + 1) * twopi_divide for i in xrange(divide + 1): theta = i * pi_divide s = phi2 / TWOPI t = theta / pi dx, dy, dz = sin(theta) * cos(phi2), sin(theta) * sin(phi2), cos(theta) glNormal3f(dx, dy, dz) glMultiTexCoord2fARB(GL_TEXTURE0_ARB, s, 1 - t) glMultiTexCoord2fARB(GL_TEXTURE1_ARB, s, 1 - t) glVertex3f(r * dx, r * dy, r * dz) s = phi1 / TWOPI # x dx, dy = sin(theta) * cos(phi1), sin(theta) * sin(phi1) glNormal3f(dx, dy, dz) glMultiTexCoord2fARB(GL_TEXTURE0_ARB, s, 1 - t) glMultiTexCoord2fARB(GL_TEXTURE1_ARB, s, 1 - t) glVertex3f(r * dx, r * dy, r * dz)