def setup_renderer(window_type, size=(256, 256), light_spec=None, cam_spec=None): """ Sets up the LightBase rendering stuff.""" # Initialize lbase = LightBase() rootnode = lbase.rootnode if window_type == "onscreen": output = lbase.make_window(size, "window") elif window_type == "offscreen": output = lbase.make_buffer(size, "buffer") elif window_type == "texture": output, tex = lbase.make_texture_buffer(size, "texturebuffer", mode='RTMCopyRam') else: raise ValueError("Unknown window type: %s" % window_type) # Clear out frame contents lbase.render_frame() lbase.render_frame() # Set up a camera if cam_spec is None: cam_spec = {} # print('cam_spec', cam_spec) cam_x, cam_z, cam_y = cam_spec.get('cam_pos', (0., -20., 0.)) scene_width = cam_spec.get('scene_width', 3.) nm = np.sqrt(cam_x**2 + cam_y**2 + cam_z**2) fov = 2. * np.degrees(np.arctan(scene_width / (2. * nm))) cam_lx, cam_lz, cam_ly = cam_spec.get('cam_lookat', (0., 0., 0.)) cam_rot = cam_spec.get('cam_rot', 0.) camera = lbase.make_camera(output) lens = camera.node().getLens() lens.setMinFov(fov) # Position the camera camera_rot = rootnode.attachNewNode('camera_rot') lbase.cameras.reparentTo(camera_rot) lbase.cameras.setPos(cam_x, cam_z, cam_y) lbase.cameras.lookAt(cam_lx, cam_lz, cam_ly) camera_rot.setH(cam_rot) # Lights lights = LightBase.make_lights(light_spec=light_spec) lights.reparentTo(rootnode) for light in lights.getChildren(): rootnode.setLight(light) # # Pause while setup finishes # time.sleep(0.02) return lbase, output
def setup_renderer(window_type, size=(256, 256), light_spec=None, cam_spec=None): """ Sets up the LightBase rendering stuff.""" # Initialize lbase = LightBase() rootnode = lbase.rootnode if window_type == "onscreen": output = lbase.make_window(size, "window") elif window_type == "offscreen": output = lbase.make_buffer(size, "buffer") elif window_type == "texture": output, tex = lbase.make_texture_buffer(size, "texturebuffer", mode='RTMCopyRam') else: raise ValueError("Unknown window type: %s" % window_type) # Clear out frame contents lbase.render_frame() lbase.render_frame() # Set up a camera if cam_spec is None: cam_spec = {} print('cam_spec', cam_spec) cam_x, cam_z, cam_y = cam_spec.get('cam_pos', (0., -20., 0.)) scene_width = cam_spec.get('scene_width', 3.) nm = np.sqrt(cam_x**2 + cam_y**2 + cam_z**2) fov = 2. * np.degrees(np.arctan(scene_width / (2. * nm))) cam_lx, cam_lz, cam_ly = cam_spec.get('cam_lookat', (0., 0., 0.)) cam_rot = cam_spec.get('cam_rot', 0.) camera = lbase.make_camera(output) lens = camera.node().getLens() lens.setMinFov(fov) # Position the camera camera_rot = rootnode.attachNewNode('camera_rot') lbase.cameras.reparentTo(camera_rot) lbase.cameras.setPos(cam_x, cam_z, cam_y) lbase.cameras.lookAt(cam_lx, cam_lz, cam_ly) camera_rot.setH(cam_rot) # Lights lights = LightBase.make_lights(light_spec=light_spec) lights.reparentTo(rootnode) for light in lights.getChildren(): rootnode.setLight(light) # # Pause while setup finishes # time.sleep(0.02) return lbase, output
""" Inputs proposed state, returns fwd/bak probabilities.""" # TODO: Not currently implemented... fwdprob = bakprob = 0 return fwdprob, bakprob ## if __name__ == "__main__": import cPickle as pickle import matplotlib.pyplot as plt import matplotlib.cm as cm import time LightBase.destroy_windows() def plot_scores(sampler): scores = sampler.store_info["score"] proposal_scores = sampler.store_info["proposal_score"] accepted = sampler.store_info["accepted"] accepted_idx = np.flatnonzero(accepted) N = scores.size # Plot plt.figure(20) plt.clf() plt.subplot(2, 1, 1) plt.plot(np.vstack((proposal_scores, scores)).T) plt.plot(accepted_idx, scores[accepted_idx], "go") plt.subplot(2, 1, 2) plt.plot(np.zeros(N), "k")
def construct_scene(lbase, modelpath, bgpath, scale, pos, hpr, bgscale, bghp, texture=None, internal_canonical=False, check_penetration=False, light_spec=None, use_envmap=False, shader=None, world_coords=False): """ Constructs the scene per the parameters. """ # Default scene is lbase's rootnode if bgpath is not None: bgpath = mt.resolve_bg_path(bgpath) rootnode = lbase.rootnode # Modelpath points to the model .egg/.bam file if isstring(modelpath): modelpaths = [modelpath] scales = [scale] poses = [pos] hprs = [hpr] textures = [texture] else: modelpaths = modelpath scales = scale poses = pos hprs = hpr textures = texture texmodes = [] for _i, _t in enumerate(textures): if isinstance(_t, tuple): texfile, texmode = _t texmodes.append(texmode) textures[_i] = texfile else: texmodes.append(TexGenAttrib.MWorldNormal) assert hasattr(modelpaths, '__iter__') assert hasattr(scales, '__iter__') assert hasattr(poses, '__iter__') assert hasattr(hprs, '__iter__') assert hasattr(textures, '__iter__') assert len(modelpaths) == len(scales) == len(hprs) == len(poses) == len( textures), (len(modelpaths), len(scales), len(hprs), len(poses), len(textures)) modelpaths = map(mt.resolve_model_path, modelpaths) modelpaths = map(cm.autogen_egg, modelpaths) textures = map(mt.resolve_texture_path, textures) objnodes = [] for mpth, scale, hpr, pos, t, tm in zip(modelpaths, scales, hprs, poses, textures, texmodes): objnode = tools.read_file(lbase.loader.loadModel, mpth) if t is not None: #ts = TextureStage('ts') ts = TextureStage.get_default() ts.setMode(TextureStage.MReplace) tex = tools.read_file(lbase.loader.loadTexture, t) objnode.setTexGen(ts, tm) objnode.setTexture(tex, 6) robjnode = rootnode.attachNewNode('root_' + objnode.get_name()) objnode.reparentTo(robjnode) if internal_canonical: vertices = np.array(objnode.getTightBounds()) initial_scale_factor = max(abs(vertices[0] - vertices[1])) cscale = 1.2 / initial_scale_factor ppos = vertices.mean(0) * cscale objnode.setPos(-ppos[0], -ppos[1], -ppos[2]) objnode.setScale(cscale, cscale, cscale) if world_coords: refnodeX = rootnode.attachNewNode('world_coords_x') refnodeY = rootnode.attachNewNode('world_coords_y') refnodeZ = rootnode.attachNewNode('world_coords_z') robjnode.wrtReparentTo(refnodeZ) refnodeZ.setH(refnodeX, hpr[2]) robjnode.wrtReparentTo(refnodeY) refnodeY.setP(refnodeX, hpr[1]) robjnode.wrtReparentTo(refnodeX) refnodeX.setR(refnodeX, hpr[0]) robjnode.wrtReparentTo(rootnode) else: robjnode.setHpr(hpr[2], hpr[1], hpr[0]) robjnode.setScale(scale[0], scale[0], scale[0]) robjnode.setPos(pos[0], -pos[2], pos[1]) robjnode.setTwoSided(1) objnodes.append(robjnode) if check_penetration: for (i, n1) in enumerate(objnodes): for j, n2 in enumerate(objnodes[i + 1:]): p = is_penetrating(n1, n2) if p: for onode in objnodes: onode.removeNode() raise PenetrationError(i, j, n1, n2) # Environment map if bgpath and use_envmap: envtex = tools.read_file(lbase.loader.loadTexture, bgpath) # Map onto object ts = TextureStage('env') ts.setMode(TextureStage.MBlendColorScale) if not isinstance(use_envmap, list): use_envmap = [use_envmap] * len(objnodes) for _objnode, ue in zip(objnodes, use_envmap): if ue: if isstring(ue): envtex0 = tools.read_file(lbase.loader.loadTexture, mt.resolve_texture_path(ue)) else: envtex0 = envtex _objnode.setTexGen(ts, TexGenAttrib.MEyeSphereMap) _objnode.setTexture(ts, envtex0) if bgpath and not np.isinf(bghp[0]): bgtex = tools.read_file(lbase.loader.loadTexture, bgpath) # Set as background #plane = cm.autogen_egg(mt.resolve_model_path('plane2d')) bgnode = lbase.loader.loadModel('smiley') # Get material list bgnode.clearMaterial() bgnode.clearTexture() bgnode.setAttrib( CullFaceAttrib.make(CullFaceAttrib.MCullCounterClockwise)) bgnode.setTexture(bgtex, 2) c = 5. bgnode.setScale(c * bgscale[0], c * bgscale[0], c * bgscale[0]) bgnode.setPos(0, 0, 0) #0) bgnode.setHpr(bghp[0], bghp[1], 0.) # Detach point light plight1 = lbase.rootnode.find('**/plight1') if plight1: plight1.detachNode() elif bgpath: bgnode = NodePath("empty-bgnode") imageObject = OnscreenImage(image=bgpath, pos=(0, 0, 0), scale=tuple(bgscale), parent=bgnode, base=lbase) else: bgnode = NodePath("empty-bgnode") bgnode.reparentTo(rootnode) if shader is not None: vshaderpath, fshaderpath = shader rootnode.setShaderAuto() shader = Shader.load(Shader.SLGLSL, vshaderpath, fshaderpath) rootnode.setShader(shader) if light_spec is not None: lbase.rootnode.clearLight() lights = LightBase.make_lights(light_spec=light_spec, lname='scene_lights') lights.reparentTo(rootnode) for light in lights.getChildren(): rootnode.setLight(light) return objnodes, bgnode
def construct_scene(lbase, modelpath, bgpath, scale, pos, hpr, bgscale, bghp, texture=None, internal_canonical=False, check_penetration=False, light_spec=None, use_envmap=False): """ Constructs the scene per the parameters. """ # Default scene is lbase's rootnode if bgpath is not None: bgpath = mt.resolve_bg_path(bgpath) rootnode = lbase.rootnode # Modelpath points to the model .egg/.bam file if isinstance(modelpath, str): modelpaths = [modelpath] scales = [scale] poses = [pos] hprs = [hpr] textures = [texture] else: modelpaths = modelpath scales = scale poses = pos hprs = hpr textures = texture texmodes = [] for _i, _t in enumerate(textures): if isinstance(_t, tuple): texfile, texmode = _t texmodes.append(texmode) textures[_i] = texfile else: texmodes.append(TexGenAttrib.MWorldNormal) assert hasattr(modelpaths, '__iter__') assert hasattr(scales, '__iter__') assert hasattr(poses, '__iter__') assert hasattr(hprs, '__iter__') assert hasattr(textures, '__iter__') assert len(modelpaths) == len(scales) == len(hprs) == len(poses) == len(textures), (len(modelpaths), len(scales), len(hprs), len(poses), len(textures)) modelpaths = map(mt.resolve_model_path, modelpaths) modelpaths = map(cm.autogen_egg, modelpaths) textures = map(mt.resolve_texture_path, textures) objnodes = [] for mpth, scale, hpr, pos, t, tm in zip(modelpaths, scales, hprs, poses, textures, texmodes): objnode = tools.read_file(lbase.loader.loadModel, mpth) if t is not None: #ts = TextureStage('ts') ts = TextureStage.get_default() ts.setMode(TextureStage.MReplace) tex = tools.read_file(lbase.loader.loadTexture, t) objnode.setTexGen(ts, tm) objnode.setTexture(tex, 6) robjnode = rootnode.attachNewNode('root_' + objnode.get_name()) objnode.reparentTo(robjnode) if internal_canonical: vertices = np.array(objnode.getTightBounds()) initial_scale_factor = max(abs(vertices[0]-vertices[1])) cscale = 1.2/initial_scale_factor ppos = vertices.mean(0) * cscale objnode.setPos(-ppos[0], -ppos[1], -ppos[2]) objnode.setScale(cscale, cscale, cscale) robjnode.setScale(scale[0], scale[0], scale[0]) robjnode.setPos(pos[0], -pos[2], pos[1]) robjnode.setHpr(hpr[2], hpr[1], hpr[0]) robjnode.setTwoSided(1) objnodes.append(robjnode) if check_penetration: for (i, n1) in enumerate(objnodes): for j, n2 in enumerate(objnodes[i+1:]): p = is_penetrating(n1, n2) if p: for onode in objnodes: onode.removeNode() raise PenetrationError(i, j, n1, n2) # Environment map if bgpath and use_envmap: envtex = tools.read_file(lbase.loader.loadTexture, bgpath) # Map onto object ts = TextureStage('env') ts.setMode(TextureStage.MBlendColorScale) if not isinstance(use_envmap, list): use_envmap = [use_envmap] * len(objnodes) for _objnode, ue in zip(objnodes, use_envmap): if ue: if isinstance(ue, str): envtex0 = tools.read_file(lbase.loader.loadTexture, mt.resolve_texture_path(ue)) else: envtex0 = envtex _objnode.setTexGen(ts, TexGenAttrib.MEyeSphereMap) _objnode.setTexture(ts, envtex0) if bgpath and not np.isinf(bghp[0]): bgtex = tools.read_file(lbase.loader.loadTexture, bgpath) # Set as background #plane = cm.autogen_egg(mt.resolve_model_path('plane2d')) bgnode = lbase.loader.loadModel('smiley') # Get material list bgnode.clearMaterial() bgnode.clearTexture() bgnode.setAttrib(CullFaceAttrib.make( CullFaceAttrib.MCullCounterClockwise)) bgnode.setTexture(bgtex, 2) c = 5. bgnode.setScale(c * bgscale[0], c * bgscale[0], c * bgscale[0]) bgnode.setPos(0, 0, 0) #0) bgnode.setHpr(bghp[0], bghp[1], 0.) # Detach point light plight1 = lbase.rootnode.find('**/plight1') if plight1: plight1.detachNode() elif bgpath: bgnode = NodePath("empty-bgnode") imageObject = OnscreenImage(image = bgpath, pos = (0, 0, 0), scale=tuple(bgscale), parent=bgnode, base=lbase) else: bgnode = NodePath("empty-bgnode") bgnode.reparentTo(rootnode) if light_spec is not None: lbase.rootnode.clearLight() lights = LightBase.make_lights(light_spec=light_spec, lname='scene_lights') lights.reparentTo(rootnode) for light in lights.getChildren(): rootnode.setLight(light) return objnodes, bgnode
# TODO: Not currently implemented... fwdprob = bakprob = 0 return fwdprob, bakprob ## if __name__ == "__main__": import cPickle as pickle import matplotlib.pyplot as plt import matplotlib.cm as cm import time LightBase.destroy_windows() def plot_scores(sampler): scores = sampler.store_info["score"] proposal_scores = sampler.store_info["proposal_score"] accepted = sampler.store_info["accepted"] accepted_idx = np.flatnonzero(accepted) N = scores.size # Plot plt.figure(20) plt.clf() plt.subplot(2, 1, 1) plt.plot(np.vstack((proposal_scores, scores)).T) plt.plot(accepted_idx, scores[accepted_idx], "go") plt.subplot(2, 1, 2) plt.plot(np.zeros(N), "k")