def newGame(): global thickness thickness=1 render.setState(RenderState.makeEmpty()) model.setState(RenderState.makeEmpty()) func5() print('NEW GAME')
def __init__(self, id = 0, plane = Plane(0, 0, 1, 0), solid = None): MapWritable.__init__(self, base.document) self.id = id self.material = FaceMaterial() self.vertices = [] self.isSelected = False self.plane = plane self.solid = solid self.hasGeometry = False self.vdata = None # Different primitive representations of this face. self.geom3D = None self.geom3DLines = None self.geom2D = None # Index into the Solid's GeomNode of the Geoms we render for this face. self.index3D = -1 self.index3DLines = -1 self.index2D = -1 # RenderState for each Geom we render for this face. self.state3D = RenderState.makeEmpty() self.state3DLines = RenderState.make(AntialiasAttrib.make(AntialiasAttrib.MLine), ColorAttrib.makeFlat(Vec4(1, 1, 0, 1))) self.state2D = RenderState.makeEmpty() if solid: self.setColor(self.solid.color) # Not None if face is a displacement. self.dispInfo = None
def addFace(self, meshVertices, state=RenderState.makeEmpty()): row = self.vwriter.getWriteRow() for vert in meshVertices: self.vwriter.setData3f(vert.pos) self.nwriter.setData3f(vert.normal) self.twriter.setData2f(vert.texcoord) for view in self.views: view.generateIndices()
def make_cloud_node(pts, col=LColorf(1.0, 0.0, 0.0, 1.0)): ptCloudData = GeomVertexData("PointCloudData", GeomVertexFormat.getV3c4(), GeomEnums.UH_static) vertexWriter = GeomVertexWriter(ptCloudData, Thread.getCurrentThread()) vertexWriter.setColumn("vertex") colorWriter = GeomVertexWriter(ptCloudData, Thread.getCurrentThread()) colorWriter.setColumn("color") for (x, y, z) in pts: vertexWriter.addData3(x, y, z) colorWriter.addData4(col) geomPts = GeomPoints(GeomEnums.UH_static) geomPts.addConsecutiveVertices(0, len(pts)) geomPts.closePrimitive() geom = Geom(ptCloudData) geom.addPrimitive(geomPts) node = GeomNode("PointCloudNode") node.addGeom(geom, RenderState.makeEmpty()) return node
def make_patch_node(pts, col=LColorf(0.0, 1.0, 0.0, 1.0)): splinePatchData = GeomVertexData("SplinePatchData", GeomVertexFormat.getV3c4(), GeomEnums.UH_static) vertexWriter = GeomVertexWriter(splinePatchData, Thread.getCurrentThread()) vertexWriter.setColumn("vertex") colorWriter = GeomVertexWriter(splinePatchData, Thread.getCurrentThread()) colorWriter.setColumn("color") for (x, y, z) in pts: vertexWriter.addData3(x, y, z) colorWriter.addData4(col) geomLines = GeomLines(GeomEnums.UH_static) geomLines.addConsecutiveVertices(0, len(pts)) geomLines.addVertex(0) geomLines.closePrimitive() geom = Geom(splinePatchData) geom.addPrimitive(geomLines) node = GeomNode("SplinePatchNode") node.addGeom(geom, RenderState.makeEmpty()) return node
def __init__(self): # Initialize the ShowBase class from which we inherit, which will # create a window and set up everything we need for rendering into it. ShowBase.__init__(self) self.setBackgroundColor((0, 0, 0, 0)) # Preliminary capabilities check. if not self.win.getGsg().getSupportsBasicShaders(): self.t = addTitle("Firefly Demo: Video driver reports that Cg " "shaders are not supported.") return if not self.win.getGsg().getSupportsDepthTexture(): self.t = addTitle("Firefly Demo: Video driver reports that depth " "textures are not supported.") return # This algorithm uses two offscreen buffers, one of which has # an auxiliary bitplane, and the offscreen buffers share a single # depth buffer. This is a heck of a complicated buffer setup. self.modelbuffer = self.makeFBO("model buffer", 1) self.lightbuffer = self.makeFBO("light buffer", 0) # Creation of a high-powered buffer can fail, if the graphics card # doesn't support the necessary OpenGL extensions. if self.modelbuffer is None or self.lightbuffer is None: self.t = addTitle("Toon Shader: Video driver does not support " "multiple render targets") return # Create four render textures: depth, normal, albedo, and final. # attach them to the various bitplanes of the offscreen buffers. self.texDepth = Texture() self.texDepth.setFormat(Texture.FDepthStencil) self.texAlbedo = Texture() self.texNormal = Texture() self.texFinal = Texture() self.modelbuffer.addRenderTexture(self.texDepth, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPDepthStencil) self.modelbuffer.addRenderTexture(self.texAlbedo, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor) self.modelbuffer.addRenderTexture(self.texNormal, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPAuxRgba0) self.lightbuffer.addRenderTexture(self.texFinal, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor) # Set the near and far clipping planes. self.cam.node().getLens().setNear(50.0) self.cam.node().getLens().setFar(500.0) lens = self.cam.node().getLens() # This algorithm uses three cameras: one to render the models into the # model buffer, one to render the lights into the light buffer, and # one to render "plain" stuff (non-deferred shaded) stuff into the # light buffer. Each camera has a bitmask to identify it. self.modelMask = 1 self.lightMask = 2 self.plainMask = 4 self.modelcam = self.makeCamera(self.modelbuffer, lens=lens, scene=render, mask=self.modelMask) self.lightcam = self.makeCamera(self.lightbuffer, lens=lens, scene=render, mask=self.lightMask) self.plaincam = self.makeCamera(self.lightbuffer, lens=lens, scene=render, mask=self.plainMask) # Panda's main camera is not used. self.cam.node().setActive(0) # Take explicit control over the order in which the three # buffers are rendered. self.modelbuffer.setSort(1) self.lightbuffer.setSort(2) self.win.setSort(3) # Within the light buffer, control the order of the two cams. self.lightcam.node().getDisplayRegion(0).setSort(1) self.plaincam.node().getDisplayRegion(0).setSort(2) # By default, panda usually clears the screen before every # camera and before every window. Tell it not to do that. # Then, tell it specifically when to clear and what to clear. self.modelcam.node().getDisplayRegion(0).disableClears() self.lightcam.node().getDisplayRegion(0).disableClears() self.plaincam.node().getDisplayRegion(0).disableClears() self.cam.node().getDisplayRegion(0).disableClears() self.cam2d.node().getDisplayRegion(0).disableClears() self.modelbuffer.disableClears() self.win.disableClears() self.modelbuffer.setClearColorActive(1) self.modelbuffer.setClearDepthActive(1) self.lightbuffer.setClearColorActive(1) self.lightbuffer.setClearColor((0, 0, 0, 1)) # Miscellaneous stuff. self.disableMouse() self.camera.setPos(-9.112, -211.077, 46.951) self.camera.setHpr(0, -7.5, 2.4) random.seed() # Calculate the projection parameters for the final shader. # The math here is too complex to explain in an inline comment, # I've put in a full explanation into the HTML intro. proj = self.cam.node().getLens().getProjectionMat() proj_x = 0.5 * proj.getCell(3, 2) / proj.getCell(0, 0) proj_y = 0.5 * proj.getCell(3, 2) proj_z = 0.5 * proj.getCell(3, 2) / proj.getCell(2, 1) proj_w = -0.5 - 0.5 * proj.getCell(1, 2) # Configure the render state of the model camera. tempnode = NodePath(PandaNode("temp node")) tempnode.setAttrib( AlphaTestAttrib.make(RenderAttrib.MGreaterEqual, 0.5)) tempnode.setShader(loader.loadShader("model.sha")) tempnode.setAttrib(DepthTestAttrib.make(RenderAttrib.MLessEqual)) self.modelcam.node().setInitialState(tempnode.getState()) # Configure the render state of the light camera. tempnode = NodePath(PandaNode("temp node")) tempnode.setShader(loader.loadShader("light.sha")) tempnode.setShaderInput("texnormal", self.texNormal) tempnode.setShaderInput("texalbedo", self.texAlbedo) tempnode.setShaderInput("texdepth", self.texDepth) tempnode.setShaderInput("proj", (proj_x, proj_y, proj_z, proj_w)) tempnode.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OOne, ColorBlendAttrib.OOne)) tempnode.setAttrib( CullFaceAttrib.make(CullFaceAttrib.MCullCounterClockwise)) # The next line causes problems on Linux. # tempnode.setAttrib(DepthTestAttrib.make(RenderAttrib.MGreaterEqual)) tempnode.setAttrib(DepthWriteAttrib.make(DepthWriteAttrib.MOff)) self.lightcam.node().setInitialState(tempnode.getState()) # Configure the render state of the plain camera. rs = RenderState.makeEmpty() self.plaincam.node().setInitialState(rs) # Clear any render attribs on the root node. This is necessary # because by default, panda assigns some attribs to the root # node. These default attribs will override the # carefully-configured render attribs that we just attached # to the cameras. The simplest solution is to just clear # them all out. render.setState(RenderState.makeEmpty()) # My artist created a model in which some of the polygons # don't have textures. This confuses the shader I wrote. # This little hack guarantees that everything has a texture. white = loader.loadTexture("models/white.jpg") render.setTexture(white, 0) # Create two subroots, to help speed cull traversal. self.lightroot = NodePath(PandaNode("lightroot")) self.lightroot.reparentTo(render) self.modelroot = NodePath(PandaNode("modelroot")) self.modelroot.reparentTo(render) self.lightroot.hide(BitMask32(self.modelMask)) self.modelroot.hide(BitMask32(self.lightMask)) self.modelroot.hide(BitMask32(self.plainMask)) # Load the model of a forest. Make it visible to the model camera. # This is a big model, so we load it asynchronously while showing a # load text. We do this by passing in a callback function. self.loading = addTitle("Loading models...") self.forest = NodePath(PandaNode("Forest Root")) self.forest.reparentTo(render) self.forest.hide(BitMask32(self.lightMask | self.plainMask)) loader.loadModel([ "models/background", "models/foliage01", "models/foliage02", "models/foliage03", "models/foliage04", "models/foliage05", "models/foliage06", "models/foliage07", "models/foliage08", "models/foliage09"], callback=self.finishLoading) # Cause the final results to be rendered into the main window on a # card. self.card = self.lightbuffer.getTextureCard() self.card.setTexture(self.texFinal) self.card.reparentTo(render2d) # Panda contains a built-in viewer that lets you view the results of # your render-to-texture operations. This code configures the viewer. self.bufferViewer.setPosition("llcorner") self.bufferViewer.setCardSize(0, 0.40) self.bufferViewer.setLayout("vline") self.toggleCards() self.toggleCards() # Firefly parameters self.fireflies = [] self.sequences = [] self.scaleseqs = [] self.glowspheres = [] self.fireflysize = 1.0 self.spheremodel = loader.loadModel("misc/sphere") # Create the firefly model, a fuzzy dot dotSize = 1.0 cm = CardMaker("firefly") cm.setFrame(-dotSize, dotSize, -dotSize, dotSize) self.firefly = NodePath(cm.generate()) self.firefly.setTexture(loader.loadTexture("models/firefly.png")) self.firefly.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.M_add, ColorBlendAttrib.O_incoming_alpha, ColorBlendAttrib.O_one)) # these allow you to change parameters in realtime self.accept("escape", sys.exit, [0]) self.accept("arrow_up", self.incFireflyCount, [1.1111111]) self.accept("arrow_down", self.decFireflyCount, [0.9000000]) self.accept("arrow_right", self.setFireflySize, [1.1111111]) self.accept("arrow_left", self.setFireflySize, [0.9000000]) self.accept("v", self.toggleCards) self.accept("V", self.toggleCards)
def getStateFromMaterial(prim_material, texture_cache, col_inst=None): state = RenderState.makeEmpty() mat = Material() texattr = TextureAttrib.makeAllOff() hasDiffuse = False if prim_material and prim_material.effect: diffuse = getattr(prim_material.effect, 'diffuse', None) transparent = getattr(prim_material.effect, 'transparent', None) if isinstance(diffuse, collada.material.Map) and isinstance( transparent, collada.material.Map): if diffuse.sampler.surface.image == transparent.sampler.surface.image: #some exporters put the same map in the diffuse channel # and the transparent channel when they don't really mean to transparent = None if isinstance(diffuse, collada.material.Map) or isinstance( transparent, collada.material.Map): diffuseMap = None transparentMap = None diffuseInitColor = None if isinstance(diffuse, collada.material.Map): diffuseMap = diffuse else: diffuseInitColor = v4fromtuple(diffuse) if isinstance(transparent, collada.material.Map): transparentMap = transparent if diffuseMap == transparentMap: transparentMap = None diffuseTexture = getTexture(color=diffuseMap, alpha=transparentMap, texture_cache=texture_cache, diffuseinit=diffuseInitColor) texattr = addTextureStage('tsDiff', TextureStage.MModulate, texattr, diffuseTexture) hasDiffuse = True if type(diffuse) is tuple: mat.setDiffuse(v4fromtuple(diffuse)) # hack to look for sketchup version < 8 where transparency was exported flipped # also ColladaMaya v2.03b had this same issue flip_alpha = False if col_inst and col_inst.assetInfo: for contributor in col_inst.assetInfo.contributors: tool_name = contributor.authoring_tool if tool_name is None: continue split = tool_name.split() if len(split) == 3 and \ split[0].strip().lower() == 'google' and \ split[1].strip().lower() == 'sketchup': version = split[2].strip().split('.') try: major_version = int(version[0]) if major_version < 8: flip_alpha = True except (ValueError, TypeError): continue try: collada_maya_idx = split.index('ColladaMaya') if split[collada_maya_idx + 1] == 'v2.03b': flip_alpha = True except (ValueError, IndexError): continue if type(transparent) is tuple: trR, trG, trB = transparent[0], transparent[1], transparent[2] trA = transparent[3] if len(transparent) > 3 else 1.0 else: trR, trG, trB = 1.0, 1.0, 1.0 trA = 1.0 transparency = getattr(prim_material.effect, 'transparency', 1.0) if transparency is None: transparency = 1.0 a_one = prim_material.effect.opaque_mode == collada.material.OPAQUE_MODE.A_ONE if a_one: alphaR = alphaG = alphaB = alphaA = transparency * trA else: alphaR = transparency * trR alphaG = transparency * trG alphaB = transparency * trB alphaA = luminance([trR, trG, trB]) flip_alpha = not flip_alpha if flip_alpha: alphaR = 1.0 - alphaR alphaG = 1.0 - alphaG alphaB = 1.0 - alphaB alphaA = 1.0 - alphaA if alphaA < 1.0: state = state.addAttrib( ColorScaleAttrib.make(VBase4(alphaR, alphaG, alphaB, alphaA))) emission = getattr(prim_material.effect, 'emission', None) if isinstance(emission, collada.material.Map): emissionTexture = getTexture(alpha=emission, texture_cache=texture_cache) texattr = addTextureStage('tsEmiss', TextureStage.MGlow, texattr, emissionTexture) elif type(emission) is tuple: mat.setEmission(v4fromtuple(emission)) ambient = getattr(prim_material.effect, 'ambient', None) if type(ambient) is tuple: mat.setAmbient(v4fromtuple(ambient)) specular = getattr(prim_material.effect, 'specular', None) if isinstance(specular, collada.material.Map): specularTexture = getTexture(color=specular, texture_cache=texture_cache) texattr = addTextureStage('tsSpec', TextureStage.MGloss, texattr, specularTexture) mat.setSpecular(VBase4(0.1, 0.1, 0.1, 1.0)) elif type(specular) is tuple: mat.setSpecular(v4fromtuple(specular)) shininess = getattr(prim_material.effect, 'shininess', None) #this sets a sane value for blinn shading if shininess <= 1.0: if shininess < 0.01: shininess = 1.0 shininess = shininess * 128.0 mat.setShininess(shininess) bumpmap = getattr(prim_material.effect, 'bumpmap', None) if isinstance(bumpmap, collada.material.Map): bumpTexture = getTexture(color=bumpmap, texture_cache=texture_cache) texattr = addTextureStage('tsBump', TextureStage.MNormal, texattr, bumpTexture) if prim_material.effect.double_sided: state = state.addAttrib( CullFaceAttrib.make(CullFaceAttrib.MCullNone)) if hasDiffuse: state = state.addAttrib(DepthOffsetAttrib.make(1)) state = state.addAttrib(MaterialAttrib.make(mat)) state = state.addAttrib(texattr) return state
def getStateFromMaterial(prim_material, texture_cache, col_inst=None): state = RenderState.makeEmpty() mat = Material() texattr = TextureAttrib.makeAllOff() hasDiffuse = False if prim_material and prim_material.effect: diffuse = getattr(prim_material.effect, 'diffuse', None) transparent = getattr(prim_material.effect, 'transparent', None) if isinstance(diffuse, collada.material.Map) and isinstance(transparent, collada.material.Map): if diffuse.sampler.surface.image == transparent.sampler.surface.image: #some exporters put the same map in the diffuse channel # and the transparent channel when they don't really mean to transparent = None if isinstance(diffuse, collada.material.Map) or isinstance(transparent, collada.material.Map): diffuseMap = None transparentMap = None diffuseInitColor = None if isinstance(diffuse, collada.material.Map): diffuseMap = diffuse else: diffuseInitColor = v4fromtuple(diffuse) if isinstance(transparent, collada.material.Map): transparentMap = transparent if diffuseMap == transparentMap: transparentMap = None diffuseTexture = getTexture(color=diffuseMap, alpha=transparentMap, texture_cache=texture_cache, diffuseinit=diffuseInitColor) texattr = addTextureStage('tsDiff', TextureStage.MModulate, texattr, diffuseTexture) hasDiffuse = True if type(diffuse) is tuple: mat.setDiffuse(v4fromtuple(diffuse)) # hack to look for sketchup version < 8 where transparency was exported flipped # also ColladaMaya v2.03b had this same issue flip_alpha = False if col_inst and col_inst.assetInfo: for contributor in col_inst.assetInfo.contributors: tool_name = contributor.authoring_tool if tool_name is None: continue split = tool_name.split() if len(split) == 3 and \ split[0].strip().lower() == 'google' and \ split[1].strip().lower() == 'sketchup': version = split[2].strip().split('.') try: major_version = int(version[0]) if major_version < 8: flip_alpha = True except (ValueError, TypeError): continue try: collada_maya_idx = split.index('ColladaMaya') if split[collada_maya_idx + 1] == 'v2.03b': flip_alpha = True except (ValueError, IndexError): continue if type(transparent) is tuple: trR, trG, trB = transparent[0], transparent[1], transparent[2] trA = transparent[3] if len(transparent) > 3 else 1.0 else: trR, trG, trB = 1.0, 1.0, 1.0 trA = 1.0 transparency = getattr(prim_material.effect, 'transparency', 1.0) if transparency is None: transparency = 1.0 a_one = prim_material.effect.opaque_mode == collada.material.OPAQUE_MODE.A_ONE if a_one: alphaR = alphaG = alphaB = alphaA = transparency * trA else: alphaR = transparency * trR alphaG = transparency * trG alphaB = transparency * trB alphaA = luminance([trR, trG, trB]) flip_alpha = not flip_alpha if flip_alpha: alphaR = 1.0 - alphaR alphaG = 1.0 - alphaG alphaB = 1.0 - alphaB alphaA = 1.0 - alphaA if alphaA < 1.0: state = state.addAttrib(ColorScaleAttrib.make(VBase4(alphaR, alphaG, alphaB, alphaA))) emission = getattr(prim_material.effect, 'emission', None) if isinstance(emission, collada.material.Map): emissionTexture = getTexture(alpha=emission, texture_cache=texture_cache) texattr = addTextureStage('tsEmiss', TextureStage.MGlow, texattr, emissionTexture) elif type(emission) is tuple: mat.setEmission(v4fromtuple(emission)) ambient = getattr(prim_material.effect, 'ambient', None) if type(ambient) is tuple: mat.setAmbient(v4fromtuple(ambient)) specular = getattr(prim_material.effect, 'specular', None) if isinstance(specular, collada.material.Map): specularTexture = getTexture(color=specular, texture_cache=texture_cache) texattr = addTextureStage('tsSpec', TextureStage.MGloss, texattr, specularTexture) mat.setSpecular(VBase4(0.1, 0.1, 0.1, 1.0)) elif type(specular) is tuple: mat.setSpecular(v4fromtuple(specular)) shininess = getattr(prim_material.effect, 'shininess', None) #this sets a sane value for blinn shading if shininess <= 1.0: if shininess < 0.01: shininess = 1.0 shininess = shininess * 128.0 mat.setShininess(shininess) bumpmap = getattr(prim_material.effect, 'bumpmap', None) if isinstance(bumpmap, collada.material.Map): bumpTexture = getTexture(color=bumpmap, texture_cache=texture_cache) texattr = addTextureStage('tsBump', TextureStage.MNormal, texattr, bumpTexture) if prim_material.effect.double_sided: state = state.addAttrib(CullFaceAttrib.make(CullFaceAttrib.MCullNone)) if hasDiffuse: state = state.addAttrib(DepthOffsetAttrib.make(1)) state = state.addAttrib(MaterialAttrib.make(mat)) state = state.addAttrib(texattr) return state
def __init__(self, parentIndex, renderState=RenderState.makeEmpty()): # parentIndex of -1 == root self.renderState = renderState self.parentIndex = parentIndex
def __init__(self, geomVertexFormat, renderState=RenderState.makeEmpty()): self.geomVertexFormat = geomVertexFormat self.renderState = renderState
def __init__(self,parentIndex,renderState=RenderState.makeEmpty()): # parentIndex of -1 == root self.renderState=renderState self.parentIndex=parentIndex
def __init__(self,geomVertexFormat,renderState=RenderState.makeEmpty()): self.geomVertexFormat=geomVertexFormat self.renderState=renderState