コード例 #1
0
    def createMap(self):

        height = 10.0
        img = PNMImage(Filename('resources/map1.bmp'))
        shape = BulletHeightfieldShape(img, height, ZUp)
        node = BulletRigidBodyNode('Map')
        node.setMass(99999999)
        node.addShape(shape)
        self.world.attachRigidBody(node)
        offset = img.getXSize() / 2.0 - 0.5
        terrain = GeoMipTerrain('terrain')
        terrain.setHeightfield(img)
        terrainNP = terrain.getRoot()
        terrainNP.setSz(height)
        terrainNP.setPos(-offset, -offset, -height / 2.0)
        #terrain.setColorMap('resources/map1color.bmp')
        terrain.setAutoFlatten(GeoMipTerrain.AFMOff)
        terrain.generate()

        return Map(terrainNP,node)
コード例 #2
0
    def createMap(self):

        height = 10.0
        img = PNMImage(Filename('resources/map1.bmp'))
        shape = BulletHeightfieldShape(img, height, ZUp)
        node = BulletRigidBodyNode('Map')
        node.setMass(99999999)
        node.addShape(shape)
        self.world.attachRigidBody(node)
        offset = img.getXSize() / 2.0 - 0.5
        terrain = GeoMipTerrain('terrain')
        terrain.setHeightfield(img)
        terrainNP = terrain.getRoot()
        terrainNP.setSz(height)
        terrainNP.setPos(-offset, -offset, -height / 2.0)
        #terrain.setColorMap('resources/map1color.bmp')
        terrain.setAutoFlatten(GeoMipTerrain.AFMOff)
        terrain.generate()

        return Map(terrainNP, node)
コード例 #3
0
class gameLocation():
    def __init__(self):
        self.terrain=GeoMipTerrain("Terrain")
        self.camera=cameraHandler()
        taskMgr.add(self.update,'location_update')
    
    def loadTerrain(self,hfFile):
        self.terrain.setHeightfield(Filename(hfFile))
        self.terrain.setBlockSize(32)
        self.terrain.setFactor(64)
        self.terrain.setMinLevel(2)
        self.terrain.getRoot().reparentTo(render)
        self.terrain.getRoot().setSz (30)
        self.terrain.generate()
        self.terrain.setFocalPoint(base.camera)
        gnodes=self.terrain.getRoot().findAllMatches("+GeomNode")
        for gnode in gnodes:
            gnode.node().setIntoCollideMask(BitMask32.bit(1))
        
    def setTexture(self,texFile,sx,sy):
        self.terrain.getRoot().setTexture(loader.loadTexture(texFile))
        self.terrain.getRoot().setTexScale(TextureStage.getDefault(), sx, sy)
        self.terrain.getRoot().getTexture().setMinfilter(Texture.FTLinearMipmapLinear)
        
    def setLights(self,ambient_l,camera_l):
        self.ambientLight = render.attachNewNode( AmbientLight( "ambientLight" ))
        self.pointLight = camera.attachNewNode( PointLight( "PointLight" ) )
        self.ambientLight.node().setColor(ambient_l)
        self.pointLight.node().setColor(camera_l)
        render.setLight( self.ambientLight )
        render.setLight( self.pointLight )
    
    def update(self,task):
        self.terrain.update()
        #self.terrain.getRoot().setRenderModeWireframe()
        return task.cont 
コード例 #4
0
ファイル: heightfield2.py プロジェクト: borgified/Hockfire
class heightfieldClass:
	def __init__( self, cameraPos ):
		#try:
		#	import psyco
		#	psyco.full()
		#except ImportError:
		#	pass
		self.cameraPos = cameraPos
		self.setupHeightfield()
		taskMgr.doMethodLater(5.0, self.mapUpdateTask, 'UpdateLOD')
	
	def mapUpdateTask( self, task ):
		self.updateHeightField()
		return Task.again
	
	def setupHeightfield( self ):
		
		self.terrain = GeoMipTerrain("mySimpleTerrain")
		
		p = Perlin.Perlin( persistance = 0.50, seed = 0 )
		myImage = p.imgNoise2D(int(TEXSIZE)+1,True)
		
		
		self.terrain.setHeightfield(myImage)
		#self.terrain.setBruteforce(True)
		
		self.terrain.setBlockSize(33)
		self.terrain.setFactor(200)#TEXSIZE/(2**4) * 10)
		self.terrain.setFocalPoint(base.camera.getPos())
		self.terrain.getRoot().reparentTo(render)
		#self.terrain.setAutoFlatten(GeoMipTerrain.AFMStrong)
		
		taskMgr.add(self.updateTask, "update")
		
		self.mTerrainHeight = MAPSIZE/20#(TEXSIZE/50)
		self.mHorizontalScale = MAPSIZE/TEXSIZE
		
		self.mHeightFieldNode = self.terrain.getRoot()
		self.mHeightFieldNode.setPos( -MAPSIZE/2, -MAPSIZE/2, 0 )
		self.mHeightFieldNode.setSx(self.mHorizontalScale)
		self.mHeightFieldNode.setSy(self.mHorizontalScale)
		self.mHeightFieldNode.setSz(self.mTerrainHeight)
		
		self.terrain.generate()
		
		self.ts0 = TextureStage( 'dirtL0' )
		self.ts1 = TextureStage( 'dirtL1' )
		self.ts2 = TextureStage( 'dirtL3' )
		
		self.tex0 = loader.loadTexture( 'data/textures/ground/mud-tile-2.png' )
		
		self.tex0
		#self.ts0.setMode(TextureStage.MAdd)
		scale = 1.0#8.0/1.0
		self.mHeightFieldNode.setTexScale( self.ts0, scale, scale )
		self.mHeightFieldNode.setTexture( self.ts0, self.tex0, 1 )
		
		#self.ts1.setMode(TextureStage.MAdd)
		scale = 32.0/1.0
		self.mHeightFieldNode.setTexScale( self.ts1, scale, scale )
		self.mHeightFieldNode.setTexture( self.ts1, self.tex0, 1 )
		
		#self.ts2.setMode(TextureStage.MAdd)
		scale = 128.0/1.0
		self.mHeightFieldNode.setTexScale( self.ts2, scale, scale )
		self.mHeightFieldNode.setTexture( self.ts2, self.tex0, 1 )
		
		self.updateHeightField()
	
	def updateTask(self,task):
		posX = base.camera.getX()  + MAPSIZE/2
		posY = base.camera.getY()  + MAPSIZE/2
		self.terrain.setFocalPoint(posX, posY)
		self.terrain.update()
		return task.cont
	
	def updateHeightField( self ):
		pass
		''' recalculate heightfield
		
		
		posX, posY = self.world2MapPos( ( self.cameraPos.getX(), self.cameraPos.getY() ) )
		self.mHeightFieldTesselator.setFocalPoint( int(posX), int(posY) )
		
		self.mHeightFieldNode.reparentTo(render) 
		
		
		'''
	
	def world2MapPos( self, in_pos ):
		posX = (in_pos[0] + MAPSIZE/2) / self.mHorizontalScale
		posY = (in_pos[1] + MAPSIZE/2) / self.mHorizontalScale
		return (posX, posY)
	
	def get_elevation( self, in_pos ):
		''' returns the elevation of the heightField at a specific 3d location
		it is not 100% correct...
		'''
		posX = (in_pos[0] + MAPSIZE/2.0) / self.mHorizontalScale
		posY = (in_pos[1] + MAPSIZE/2.0) / self.mHorizontalScale
		return self.terrain.getElevation(posX ,posY ) * self.mTerrainHeight
コード例 #5
0
ファイル: terrain.py プロジェクト: asceth/devsyn
class Terrain:
  """initialize"""
  def __init__(self, ancestor = None, size = 65, alt = 2.0, div = 5, tile_theme = 'random', retro = True):
    print "_________________"
    print "Class Terrain"
    self.ancestor = ancestor

    # initialize basic properties
    self.alt = alt
    self.div = div
    self.divsep = 1.0
    self.retro = retro
    self.map_path = APP_PATH + 'media/maps/'
    self.texture_path = APP_PATH + 'media/tiles/advanced/'
    self.themes = ['advanced']

    self.tiles = [{'rgb':(0, 50, 255), 'tex':APP_PATH + 'media/tiles/advanced/water.png', 'score':2, 'h':0}]


    # create terrain nodepath
    self.root = NodePath("rootTerrain")
    self.root.setSz(1.0)
    self.root.reparentTo(self.ancestor.root)

    # make terrain pickable as 'terrain'
    self.ancestor.picker.make_pickable(self.root, 'terrain')

    # create a random heightmap of given size
    self.heightmap = DiamondMap(self.map_path + "hmap.png", size, alt)

    # generate data from hmap and cmap
    self.data = self.make_data(self.map_path + "hmap.png")

    # generate subdata from data
    self.subdata = self.make_subdata(base_data = self.data, div = self.div)

    self.geomip_terrain = GeoMipTerrain("geomip_terrain")
    self.geomip_terrain.setHeightfield(Filename(self.map_path + "hmap.png"))
    self.geomip_terrain.getRoot().setSz(20)
    self.geomip_terrain.setBlockSize(32)
    self.geomip_terrain.setFactor(400)
    self.geomip_terrain.setMinLevel(1.5)
    self.geomip_terrain.setFocalPoint(base.camera)
    self.geomip_terrain.getRoot().reparentTo(self.root)
    self.geomip_terrain.generate()


    tex0 = base.loader.loadTexture(self.texture_path + "water.jpg")
    tex0.setMinfilter(Texture.FTLinearMipmapLinear)
    tex1 = base.loader.loadTexture(self.texture_path + "desert.png")
    tex1.setMinfilter(Texture.FTLinearMipmapLinear)
    tex2 = base.loader.loadTexture(self.texture_path + "water.jpg")
    tex2.setMinfilter(Texture.FTLinearMipmapLinear)
    tex3 = base.loader.loadTexture(self.texture_path + "rock.png")
    tex3.setMinfilter(Texture.FTLinearMipmapLinear)
    tex4 = base.loader.loadTexture(self.texture_path + "desert.png")
    tex4.setMinfilter(Texture.FTLinearMipmapLinear)
    tex5 = base.loader.loadTexture(self.texture_path + "sand.jpg")
    tex5.setMinfilter(Texture.FTLinearMipmapLinear)
    tex6 = base.loader.loadTexture(self.texture_path + "default.png")
    tex6.setMinfilter(Texture.FTLinearMipmapLinear)

    # set mutiltextures
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex0'),tex0 )
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex1'),tex1 )
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex2'),tex2 )
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex3'),tex3 )
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex4'),tex4 )
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex5'),tex5 )
    self.geomip_terrain.getRoot().setTexture( TextureStage('tex6'),tex6 )

    # load shader
    self.geomip_terrain.getRoot().setShader(base.loader.loadShader(APP_PATH + 'media/cg/terraintexture.sha'))

    base.taskMgr.add(self.update_terrain, "update-terrain")

    # generate mesh tiles
    #for a in range(0, len(self.subdata)):
    #  for b in range(0, len(self.subdata[a])):
        # generate base mesh
    #    mesh = self.make_base(a, b)
        # generate layer mesh
    #    for i in range(0, len(self.tiles)):
    #      layer = self.make_layer(i, a, b)
    print "    /terrain mesh created in ", self.div, "x", self.div, "tiles"
    # generate num cubes (?)
    self.num_cubes()

  def update_terrain(self, task):
    self.geomip_terrain.update()
    return task.cont

  """make data matrix from heightmap"""
  def make_data(self, hmapfile):
    # open heightmap for reading pixel data
    heightmap = PNMImage()
    heightmap.read(Filename(hmapfile))
    xs = heightmap.getXSize()
    ys = heightmap.getYSize()

    # generate data bi-dimensional array
    data = []
    for x in range(xs):
      data.append([])
      for y in range(ys):
        # set data dictionary properties
        # name
        name = "cell_" + str(x) + "_" + str(y)
        # height
        height = (heightmap.getXel(x, ys - y - 1)[0] * 10)

        if self.retro == True:
          if height < 1 :
            height = height / 5
            height = int(height)
        # c and rgb
        c = [random.random(), random.random(), random.random()]
        rgb = (int(c[0] * 255), int(c[1] * 255), int(c[2] * 255))
        # default texture
        texture = self.tiles[0]['tex']
        texturenum = 0
        score = self.tiles[0]['score']

        # from rgb we assign tex and score
        for n in range(len(self.tiles)):
          if rgb == self.tiles[n]['rgb']:
            texture = self.tiles[n]['tex']
            texturenum = n
            score = self.tiles[n]['score']
            break

        # set terrain data dictionary
        data[x].append({'name':name, 'h':height, 'c':c, 'rgb':rgb, 'tex':texture,
                        'texnum':texturenum, 'score':score})
    return data

  """make subdata matrix from data"""
  def make_subdata(self, base_data = [[]], div = 3):
    # generate subdata
    subdata = []
    for a in range(0, div):
      subdata.append([])
      for b in range(0, div):
        subdata[a].append([])
        # get max, max2
        xmax = int(len(base_data) / div)
        xmax2 = xmax + 1

        if len(base_data) / (div * 1.0) == xmax:
          if a == div - 1:
            xmax2 -= 1
        else:
          if a == div - 1:
            xmax2 += 1

        ymax = int(len(base_data[a]) / div )
        ymax2 = ymax + 1
        if len(base_data[0]) / (div * 1.0) == ymax:
          if b == div - 1:
            ymax2 -= 1
        else:
          if b == div - 1:
            ymax2 += 1

        # get x1, y1
        x1 = a * xmax
        y1 = b * ymax

        # generate subdata
        for x in range(0, xmax2):
          subdata[a][b].append([])
          for y in range(0, ymax2):
            subdata[a][b][x].append([])
            subdata[a][b][x][y] = base_data[x + x1][y + y1]
    return subdata

  """make terrain base mesh from data"""
  def make_base(self, a, b):
    # get data
    data = self.subdata[a][b]
    # set vertex data
    vdata = GeomVertexData('plane', GeomVertexFormat.getV3n3c4t2(), Geom.UHStatic)
    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    color = GeomVertexWriter(vdata, 'color')
    uv = GeomVertexWriter(vdata, 'texcoord')

    # set vertices
    number = 0
    for x in range(0, len(data) - 1):
      for y in range(0, len(data[x]) - 1):
        # get vertex data
        v1 = Vec3(x, y, data[x][y]['h'])
        v2 = Vec3(x + 1, y, data[x+1][y]['h'])
        v3 = Vec3(x + 1, y + 1, data[x+1][y+1]['h'])
        v4 = Vec3(x, y + 1, data[x][y+1]['h'])
        n = (0, 0, 1) # normal

        # assign vertex colors + alpha
        option = 1 # black
        if option == 1:
          c = 0
          c1 = [c, c, c, 1]
          c2 = [c, c, c, 1]
          c3 = [c, c, c, 1]
          c4 = [c, c, c, 1]
        # option2: color vertices
        if option == 2:
          alpha = 1.0
          c1 = [data[x][y]['c'][0], data[x][y]['c'][1],
                data[x][y]['c'][2], alpha]
          c2 = [data[x+1][y]['c'][0], data[x+1][y]['c'][1],
                data[x+1][y]['c'][2], alpha]
          c3 = [data[x+1][y+1]['c'][0], data[x+1][y+1]['c'][1],
                data[x+1][y+1]['c'][2], alpha]
          c4 = [data[x][y+1]['c'][0], data[x][y+1]['c'][1],
                data[x][y+1]['c'][2], alpha]

        if option == 3:
          c1 = self.color_vertex(v1)
          c2 = self.color_vertex(v2)
          c3 = self.color_vertex(v3)
          c4 = self.color_vertex(v4)

        vertex.addData3f(v1)
        normal.addData3f(*n)
        color.addData4f(*c1)
        uv.addData2f(0,0)

        vertex.addData3f(v2)
        normal.addData3f(*n)
        color.addData4f(*c2)
        uv.addData2f(1,0)

        vertex.addData3f(v3)
        normal.addData3f(*n)
        color.addData4f(*c3)
        uv.addData2f(1,1)

        vertex.addData3f(v1)
        normal.addData3f(*n)
        color.addData4f(*c1)
        uv.addData2f(0,0)

        vertex.addData3f(v3)
        normal.addData3f(*n)
        color.addData4f(*c3)
        uv.addData2f(1,1)

        vertex.addData3f(v4)
        normal.addData3f(*n)
        color.addData4f(*c4)
        uv.addData2f(0,1)

#         # add vertex h
#         vertex.addData3f(v1)
#         # normal.addData3f(*n)
#         vertex.addData3f(v2)
#         # normal.addData3f(*n)
#         vertex.addData3f(v3)
#         # normal.addData3f(*n)
#         vertex.addData3f(v1)
#         # normal.addData3f(*n)
#         vertex.addData3f(v3)
#         # normal.addData3f(*n)
#         vertex.addData3f(v4)
#         # normal.addData3f(*n)
#         # add vertex color
#         color.addData4f(*c1)
#         color.addData4f(*c2)
#         color.addData4f(*c3)
#         color.addData4f(*c1)
#         color.addData4f(*c3)
#         color.addData4f(*c4)

        # iterate
        number = number + 2

    # add triangles
    prim = GeomTriangles(Geom.UHStatic)
    for n in range(number):
      prim.addVertices((n * 3) + 2, (n * 3) + 0, (n * 3) + 1)
    prim.closePrimitive()

    # make geom
    geom = Geom(vdata)
    geom.addPrimitive(prim)

    # make geom node
    node = GeomNode("base" + "_" + str(a) + "_" + str(b))
    node.addGeom(geom)

    # make mesh nodePath
    mesh = NodePath(node)
    # set render order
    mesh.setBin("", 1)

    # locate mesh
    mesh.setPos(self.divsep * (a * int(len(self.data[a]) / self.div)),
                self.divsep * (b * int(len(self.data[b]) / self.div)), 0)

    # reparent mesh
    mesh.reparentTo(self.root)

    # return mesh
    return mesh

  def color_vertex(self, vertex):
    alpha = 0.8
    height = float(vertex[2])

    if height < 0.2:
      height += 0.2

    if height < 1:
      return [(height / 2.0), 0, 0, alpha]
    elif height < 4:
      return [0, 0, (height / 7.0), alpha]
    elif height < 7:
      return [0, ((height - 3.0) / 7.0), (height / 7.0), alpha]
    elif height < 8:
      return [0, (height / 8.0), (height / 9.0), alpha]
    elif height < 10:
      return [(height / 14), (height / 10.0), (height / 12.0), alpha]
    else:
      return [(height / 12.0), (height / 12.0), (height / 12.0), alpha]

  def num_cubes(self):
    # create axis cubes
    self.num_cubes = []
    for n in range(len(self.data)):
      self.num_cubes.append(meshes.loadModel(parent = self.ancestor.root,
                                             path = APP_PATH + "media/models/shapes/cube",
                                             name = "num_cube" + str(n),
                                             size = 0.05, pos = (n, -0.5, 0),
                                             color = None))


  def make_layer(self, i, a, b):
    # get data
    data = self.subdata[a][b]

    # set color + alpha of vertex texture
    def ap(n):
      alpha = 0
      if i == n:
        alpha = 1.0
      return alpha
    def tp(n):
      list = [0, 0, 0, 0]
      if i == n:
        list = [1, 1, 1, 0.75]
      return list

    # set vertex data
    vdata = GeomVertexData('plane', GeomVertexFormat.getV3n3c4t2(), Geom.UHStatic)
    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    color = GeomVertexWriter(vdata, 'color')
    uv = GeomVertexWriter(vdata, 'texcoord')

    # set vertices
    number = 0
    for x in range(0, len(data) - 1):
      for y in range(0, len(data[x]) - 1):
        # get vertex data
        v1 = Vec3(x, y, data[x][y]['h'])
        c1 = data[x][y]['c']
        t1 = data[x][y]['texnum']
        v2 = Vec3(x+1, y, data[x+1][y]['h'])
        c2 = data[x+1][y]['c']
        t2 = data[x+1][y]['texnum']
        v3 = Vec3(x+1, y+1, data[x+1][y+1]['h'])
        c3 = data[x+1][y+1]['c']
        t3 = data[x+1][y+1]['texnum']
        v4 = Vec3(x, y+1, data[x][y+1]['h'])
        c4 = data[x][y+1]['c']
        t4 = data[x][y+1]['texnum']
        n=(0, 0, 1) # normal

        # assign vertex colors + alpha
        a1, a2, a3, a4 = ap(t1), ap(t2), ap(t3), ap(t4)
        t1, t2, t3, t4 = tp(t1), tp(t2), tp(t3), tp(t4)

        if v1[2]==0:
          t1 = [data[x][y]['c'][0], data[x][y]['c'][1], data[x][y]['c'][2],
                a1]
        if v2[2]==0:
          t2 = [data[x+1][y]['c'][0], data[x+1][y]['c'][1],
                data[x+1][y]['c'][2], a2]
        if v3[2]==0:
          t3 = [data[x+1][y+1]['c'][0], data[x+1][y+1]['c'][1],
                data[x+1][y+1]['c'][2], a3]
        if v4[2]==0:
          t4 = [data[x][y+1]['c'][0], data[x][y+1]['c'][1],
                data[x][y+1]['c'][2], a4]

        if a1 == 0 and a2 == 0 and a3 == 0 and a4 == 0:
          continue

        # add vertices
        vertex.addData3f(v1)
        normal.addData3f(*n)
        color.addData4f(*t1)
        uv.addData2f(0,0)

        vertex.addData3f(v2)
        normal.addData3f(*n)
        color.addData4f(*t2)
        uv.addData2f(1,0)

        vertex.addData3f(v3)
        normal.addData3f(*n)
        color.addData4f(*t3)
        uv.addData2f(1,1)

        vertex.addData3f(v1)
        normal.addData3f(*n)
        color.addData4f(*t1)
        uv.addData2f(0,0)

        vertex.addData3f(v3)
        normal.addData3f(*n)
        color.addData4f(*t3)
        uv.addData2f(1,1)

        vertex.addData3f(v4)
        normal.addData3f(*n)
        color.addData4f(*t4)
        uv.addData2f(0,1)

        number = number + 2

    # add triangles
    prim = GeomTriangles(Geom.UHStatic)
    for n in range(number):
      prim.addVertices((n * 3) + 2, (n * 3) + 0, (n * 3) + 1)
    prim.closePrimitive()

    # make geom
    geom = Geom(vdata)
    geom.addPrimitive(prim)

    # make geom node
    node = GeomNode("layer" + str(i) + "_" + str(a) + "_" + str(b))
    node.addGeom(geom)

    # make mesh nodePath
    mesh = NodePath(node)

    # load and assign texture
    txfile = self.tiles[i]['tex']
    tx = base.loader.loadTexture(txfile)
    tx.setMinfilter(Texture.FTLinearMipmapLinear)
    mesh.setDepthTest(DepthTestAttrib.MLessEqual)
    mesh.setDepthWrite(False)
    mesh.setTransparency(True)
    mesh.setTexture(tx)

    # set render order
    mesh.setBin("", 1)

    # locate mesh
    mesh.setPos(self.divsep * (a * int(len(self.data[a]) / self.div)),
                self.divsep * (b * int(len(self.data[b]) / self.div)), 0.001)

    # reparent mesh
    mesh.reparentTo(self.root)

    # return mesh
    return mesh


  def getCellPos(self, x, y):
    """ returns the center location of the current cell at the given location """
    # get cell vertices
    v1, v2, v3, v4 = self.getCellVertices(x, y)
    # get cell center point
    x = (v1[0] + v2[0] + v3[0] + v4[0]) / 4.0
    y = (v1[1] + v2[1] + v3[1] + v4[1]) / 4.0
    z = (v1[2] + v2[2] + v3[2] + v4[2]) / 4.0
    return x, y, z

  def getElevation(self, x, y):
    """ returns the terrain elevation at the given xy location
        Uses bilinear interpolation (thanks pro-soft for let me know about that)
        (1-a)(1-b)      a(1-b)    where a and b are the horizontal and vertical
        b(1-a)            ab      distances to the nearest top-left pixel.
    """

    v1, v2, v3, v4 = self.getCellVertices(x, y)
    # get elevation: bilinear interpolation
    a = abs(x - v4[0]) # x distance to top-left
    b = abs(y - v4[1]) # y distance to top-right
    i1 = v4[2] # top-left
    i2 = v3[2] # top-right
    i3 = v1[2] # bottom-left
    i4 = v2[2] # bottom-right
    # bilinear interpolation equation
    z = (1-a) * (1-b) * i1 + (a) * (1-b) * i2 + (1-a) * (b) * i3 + (a)*(b) * i4

    # solve exceptions (not working)
    ## h1,h2,h3,h4=self.getElevationExceptions(v1[2],v2[2],v3[2],v4[2])
    ## i1=h4#top-left
    ## i2=h3#top-right
    ## i3=h1#bottom-left
    ## i4=h2#bottom-right
    ## #bilinear interpolation equation
    ## z=(1-a)*(1-b)*i1 + (a)*(1-b)*i2 + (1-a)*(b)*i3 + (a)*(b)*i4
    return z


  def getCellVertices(self,x,y,debug=False):
    """ calculates the location of the 4 vertices of the current cell
        at the given location
    """
    # vertex 1
    x1 = int(x)
    y1 = int(y)
    z1 = self.data[x1][y1]['h']
    if debug == True:
      self.cube1.setPos(x1, y1, z1)
    # vertex2
    x2 = x1 + 1
    y2 = y1
    z2 = self.data[x2][y2]['h']
    if debug == True:
      self.cube2.setPos(x2, y2, z2)
    #vertex3
    x3 = x1 + 1
    y3 = y1 + 1
    z3 = self.data[x3][y3]['h']
    if debug == True:
      self.cube3.setPos(x3, y3, z3)
    #vertex4
    x4 = x1
    y4 = y1 + 1
    z4 = self.data[x4][y4]['h']
    if debug==True:
      self.cube4.setPos(x4, y4, z4)

    return (x1,y1,z1),(x2,y2,z2),(x3,y3,z3),(x4,y4,z4)


  def getElevationExceptions(self,h1,h2,h3,h4):
    d = 1.05
    d2 = 0.95
    if h1 == h2 and h1 == h3 and h1 != h4:
      h1 = h1 * d
      h2 = h2 * d
      h3 = h3 * d
      h4 = h4 * d2
    if h1==h2 and h1==h4 and h1!=h3:
      h1 = h1 * d
      h2 = h2 * d
      h4 = h4 * d
      h3 = h3 * d2
    if h1==h3 and h1==h4 and h1!=h2:
      h1 = h1 * d
      h3 = h3 * d
      h4 = h4 * d
      h2 = h2 * d2
    if h2==h3 and h2==h4 and h2!=h1:
      h2 = h2 * d
      h3 = h3 * d
      h4 = h4 * d
      h1 = h1 * d2

    return h1, h2, h3, h4
コード例 #6
0
ファイル: heightfield.py プロジェクト: borgified/Hockfire
class Heightfield(DirectObject):# threading.Thread):
	#def __init__(self):
	#	threading.Thread.__init__(self)#, name="test") 
	
	
	def __init__( self ):
		'''try:
			import psyco
			psyco.full()
		except ImportError:
			pass'''
		
		self.ts0 = TextureStage( 'dirtL0' )
		self.ts1 = TextureStage( 'dirtL1' )
		self.ts2 = TextureStage( 'dirtL3' )
		self.tex0 = loader.loadTexture( 'mud-tile.png' )
		
		self.mTerrainHeight = MAPSIZE*3
		self.mHorizontalScale = MAPSIZE/TEXSIZE
		
		size = int(TEXSIZE) + 1
		pb = Perlin.Perlin( persistance = 0.500, smooth = False, seed = random.random() )
		myImage2 = pb.imgNoise2D(size,True)
		
		self.myImage=PNMImage(size,size)
		self.myImage.makeGrayscale()
		self.myImage.setMaxval( (2<<16)-1 )
		
		line = lineDrawer.LineDrawer(self.myImage,(42,180),(13,240),30)
		
		for x in range(size):
			for y in range(size):
				if self.myImage.getGray(x,y) > myImage2.getGray(x,y):
					gray = self.myImage.getGray(x,y) - 0.5
				else:
					gray = myImage2.getGray(x,y) - 0.5
				self.myImage.setGray(x,y,gray + 0.5)
		
		#size = int(TEXSIZE) + 1
		#randSeed = random.random()
		#p1 = Perlin.Perlin( persistance = 0.500, smooth = False, seed = randSeed )
		#self.myImage = p1.imgNoise2D(size,True)
		
		self.terrain1 = GeoMipTerrain("myTerrain1")
		self.terrain2 = GeoMipTerrain("myTerrain2")
		self.setupHeightfield(self.terrain1)
		self.setupHeightfield(self.terrain2)
		
		self.terrain1.getRoot().reparentTo(render)
		self.terrain2.getRoot().reparentTo(render)
		
		self.accept( "g", self.flattenArea)
		self.accept( "u", self.updateWithNewImage)
	
	def setupHeightfield( self , terrain):
		
		terrain.setHeightfield(self.myImage)
		terrain.setBruteforce(True)
		
		terrain.setBlockSize(64)
		terrain.setNear(128)
		terrain.setFar(512)
		terrain.setFocalPoint(base.camera.getPos(render))
		
		taskMgr.add(self.updateTask, "update")
		
		mHeightFieldNode = terrain.getRoot()
		mHeightFieldNode.setPos( -MAPSIZE/2, -MAPSIZE/2, - self.mTerrainHeight/2)
		mHeightFieldNode.setSx(self.mHorizontalScale)
		mHeightFieldNode.setSy(self.mHorizontalScale)
		mHeightFieldNode.setSz(self.mTerrainHeight)
		
		terrain.generate()
		
		scale = 1.0
		mHeightFieldNode.setTexScale( self.ts0, scale, scale )
		mHeightFieldNode.setTexture( self.ts0, self.tex0, 1 )
		
		scale = 32.0
		mHeightFieldNode.setTexScale( self.ts1, scale, scale )
		mHeightFieldNode.setTexture( self.ts1, self.tex0, 1 )
		
		scale = 128.0
		mHeightFieldNode.setTexScale( self.ts2, scale, scale )
		mHeightFieldNode.setTexture( self.ts2, self.tex0, 1 )
		
	
	def flattenArea( self ):
		tilePos = (500,500)
		tileSize = (4000,4000)
		
		imgTilePos = self.world2MapPos(tilePos)
		imgTileSize = self.world2MapPos( (tilePos[0] + tileSize[0], tilePos[1] + tileSize[1]) )
		imgTileSize = (imgTileSize[0] - imgTilePos[0], imgTileSize[1] - imgTilePos[1])
		
		tileSquare = PNMImage(Filename("tile.png"))
		
		tileStamp = PNMImage(int(imgTileSize[0] * (5/3)),int(imgTileSize[1] * (5/3)))
		tileStamp.makeGrayscale()
		tileStamp.addAlpha()
		
		tileStamp.gaussianFilterFrom(1, tileSquare)
		
		count = 4
		total = 0.0
		
		selectXLow = int(imgTilePos[0] + imgTileSize[0] * 0.25)
		selectXHigh = int(imgTilePos[0] + imgTileSize[0] * 0.75)
		selectYLow = int(imgTilePos[1] + imgTileSize[1] * 0.25)
		selectYHigh = int(imgTilePos[1] + imgTileSize[1] * 0.75)
		
		total += self.myImage.getGray(selectXLow,selectYLow)
		total += self.myImage.getGray(selectXLow,selectYLow)
		total += self.myImage.getGray(selectXHigh,selectYHigh)
		total += self.myImage.getGray(selectXHigh,selectYHigh)
		average = total/count
		
		tileStamp.fill(average)
		
		edgeWidth = imgTilePos[0]*(1/3)
		
		self.myImage.blendSubImage(tileStamp, int( imgTilePos[0]-edgeWidth), 
											  int( imgTilePos[1]-edgeWidth), 
										0, 0, int(imgTileSize[0]*( 5/3 )  ), 
											  int(imgTileSize[1]*( 5/3 )  ), 1)
		
	def getCurrentTerrain(self):
		if self.terrain2.getRoot().isHidden():
			return self.terrain1
		else:
			return self.terrain2
	
	def getHiddenTerrain(self):
		if self.terrain1.getRoot().isHidden():
			return self.terrain1
		else:
			return self.terrain2
	
	def updateWithNewImage(self):
		posX = base.camera.getX()  + MAPSIZE/2
		posY = base.camera.getY()  + MAPSIZE/2
		if self.terrain2.getRoot().isHidden():
			self.terrain2.setHeightfield(self.myImage)
			self.terrain2.setFocalPoint(posX, posY)
			if Thread.isThreadingSupported():
				thread.start_new_thread(self.updateWithNewImageThread,(self.terrain2,1))
			else:
				self.updateWithNewImageThread(self.terrain2)
			self.terrain1.getRoot().hide()
			self.terrain2.getRoot().show()
			print "done"
		else:
			self.terrain1.setHeightfield(self.myImage)
			self.terrain1.setFocalPoint(posX, posY)
			if Thread.isThreadingSupported():
				thread.start_new_thread(self.updateWithNewImageThread,(self.terrain1,1))
			else:
				self.updateWithNewImageThread(self.terrain1)
			self.terrain2.getRoot().hide()
			self.terrain1.getRoot().show()
			print "done2"
		
			
	def updateWithNewImageThread(self,terrain,blag=1):
		terrain.update()
	
	def updateTask(self,task):
		posX = base.camera.getX(render)  + MAPSIZE/2
		posY = base.camera.getY(render)  + MAPSIZE/2
		self.getCurrentTerrain().setFocalPoint(posX, posY)
		self.getCurrentTerrain().update()
		return task.cont
	
	def world2MapPos( self, in_pos ):
		result = (0,0)
		if abs(in_pos[0]) <= MAPSIZE/2.0 and abs(in_pos[1]) <= MAPSIZE/2.0:
			posX = (in_pos[0] + MAPSIZE/2.0) / self.mHorizontalScale
			posY = (in_pos[1] + MAPSIZE/2.0) / self.mHorizontalScale
			result = (posX,posY)
		return result
	
	def get_elevation( self, in_pos ):
		result = 0
		if abs(in_pos[0]) <= MAPSIZE/2.0 and abs(in_pos[1]) <= MAPSIZE/2.0:
			posX = (in_pos[0] + MAPSIZE/2.0) / self.mHorizontalScale
			posY = (in_pos[1] + MAPSIZE/2.0) / self.mHorizontalScale
			result = (self.getCurrentTerrain().getElevation(posX ,posY ) * self.mTerrainHeight) - self.mTerrainHeight/2
		
		return result
コード例 #7
0
ファイル: heightfield.py プロジェクト: borgified/Hockfire
class Heightfield:
	def __init__( self, cameraPos ):
		#try:
		#	import psyco
		#	psyco.full()
		#except ImportError:
		#	pass
		self.cameraPos = cameraPos
		self.setupHeightfield()
		taskMgr.doMethodLater(5.0, self.mapUpdateTask, 'UpdateLOD')
	
	def mapUpdateTask( self, task ):
		self.updateHeightField()
		return Task.again
	
	def setupHeightfield( self ):
		
		self.terrain = GeoMipTerrain("mySimpleTerrain")
		
		size = int(TEXSIZE) + 1
		randSeed = random.random()
		p1 = Perlin.Perlin( persistance = 0.500, smooth = False, seed = randSeed )
		self.myImage = p1.imgNoise2D(size,True)
		
		self.terrain.setHeightfield(self.myImage)
		#self.terrain.setBruteforce(True)
		
		self.terrain.setBlockSize(32)
		self.terrain.setNear(128)
		self.terrain.setFar(512)
		self.terrain.setFocalPoint(base.camera.getPos(render))
		self.terrain.getRoot().reparentTo(render)
		
		taskMgr.add(self.updateTask, "update")
		
		self.mTerrainHeight = MAPSIZE*3#/20#(TEXSIZE/50)
		self.mHorizontalScale = MAPSIZE/TEXSIZE
		
		self.mHeightFieldNode = self.terrain.getRoot()
		self.mHeightFieldNode.setPos( -MAPSIZE/2, -MAPSIZE/2, - self.mTerrainHeight/2)
		self.mHeightFieldNode.setSx(self.mHorizontalScale)
		self.mHeightFieldNode.setSy(self.mHorizontalScale)
		self.mHeightFieldNode.setSz(self.mTerrainHeight)
		
		self.terrain.generate()
		
		self.ts0 = TextureStage( 'dirtL0' )
		self.ts1 = TextureStage( 'dirtL1' )
		self.ts2 = TextureStage( 'dirtL3' )
		
		self.tex0 = loader.loadTexture( 'data/environment/textures/mud-tile.png' )
		
		self.tex0
		scale = 1.0
		self.mHeightFieldNode.setTexScale( self.ts0, scale, scale )
		self.mHeightFieldNode.setTexture( self.ts0, self.tex0, 1 )
		
		scale = 32.0
		self.mHeightFieldNode.setTexScale( self.ts1, scale, scale )
		self.mHeightFieldNode.setTexture( self.ts1, self.tex0, 1 )
		
		scale = 128.0
		self.mHeightFieldNode.setTexScale( self.ts2, scale, scale )
		self.mHeightFieldNode.setTexture( self.ts2, self.tex0, 1 )
		
		self.updateHeightField()
	
	def updateTask(self,task):
		posX = base.camera.getX(render)  + MAPSIZE/2
		posY = base.camera.getY(render)  + MAPSIZE/2
		self.terrain.setFocalPoint(posX, posY)
		self.terrain.update()
		return task.cont
	
	def updateHeightField( self ):
		pass
		''' recalculate heightfield
		
		
		posX, posY = self.world2MapPos( ( self.cameraPos.getX(), self.cameraPos.getY() ) )
		self.mHeightFieldTesselator.setFocalPoint( int(posX), int(posY) )
		
		self.mHeightFieldNode.reparentTo(render) 
		
		
		'''
	
	def world2MapPos( self, in_pos ):
		posX = (in_pos[0] + MAPSIZE/2) / self.mHorizontalScale
		posY = (in_pos[1] + MAPSIZE/2) / self.mHorizontalScale
		return (posX, posY)
	
	'''
	def get_elevation( self, in_pos ):
		'''''' returns the elevation of the heightField at a specific 3d location
		it is not 100% correct...
		''''''
		posX = (in_pos[0] + MAPSIZE/2.0) / self.mHorizontalScale
		posY = (in_pos[1] + MAPSIZE/2.0) / self.mHorizontalScale
		return self.terrain.getElevation(posX ,posY ) * self.mTerrainHeight
	'''
	
	def get_elevation( self, in_pos ):
		result = 0
		if abs(in_pos[0]) <= MAPSIZE/2.0 and abs(in_pos[1]) <= MAPSIZE/2.0:
			posX = (in_pos[0] + MAPSIZE/2.0) / self.mHorizontalScale
			posY = (in_pos[1] + MAPSIZE/2.0) / self.mHorizontalScale
			result = (self.terrain.getElevation(posX ,posY ) * self.mTerrainHeight) - self.mTerrainHeight/2
		
		return result
コード例 #8
0
    def setHeightField(self, filename):
        """Set the GeoMip heightfield from a heightmap image."""

        GeoMipTerrain.setHeightfield(self, filename)
コード例 #9
0
ファイル: terraintile.py プロジェクト: Vetrik/python-utils
	def setHeightField(self, filename):
		"Set the GeoMip heightfield from a heightmap image."""
		
		GeoMipTerrain.setHeightfield(self, filename)
コード例 #10
0
                                          texscale] in self.AlphaMaps.items():
            TexturePool.releaseTexture(alphamap)
            TexturePool.releaseTexture(detailTex)
        self.Root.clearShader()
        self.Root.clearTexture()


if __name__ == '__main__':
    from direct.directbase import DirectStart
    from pandac.PandaModules import *
    n = NodePath('s')
    n.reparentTo(render)
    a = ShaderNode(n)
    tex0 = 'examples/terrain-mix.png'
    tex1 = 'examples/grass.png'
    tex2 = 'examples/dirt.png'
    tex3 = 'examples/stone.png'
    #a.SetTextureMap(tex0)
    a.AddAlphaMap(tex1, tex0, alphamapchannel="r", texscale=5)
    a.AddAlphaMap(tex2, tex0, alphamapchannel="g", texscale=5)
    a.AddAlphaMap(tex3, tex0, alphamapchannel="b", texscale=50)
    terrain = GeoMipTerrain('test')
    terrain.setHeightfield(Filename('examples/height.png'))
    #  terrainNode = terrain.getRoot()
    terrain.getRoot().reparentTo(n)
    terrain.getRoot().setSz(25)
    terrain.generate()
    a.Initialize()

    run()
コード例 #11
0
    #  TexturePool.releaseTexture(tex)
    for [alphamap, alphamapchannel], [detailTex, texscale] in self.AlphaMaps.items():
      TexturePool.releaseTexture(alphamap)
      TexturePool.releaseTexture(detailTex)
    self.Root.clearShader()
    self.Root.clearTexture()

if __name__ == '__main__':
  from direct.directbase import DirectStart
  from pandac.PandaModules import *
  n = NodePath('s')
  n.reparentTo(render)
  a = ShaderNode(n)
  tex0 = 'examples/terrain-mix.png'
  tex1 = 'examples/grass.png'
  tex2 = 'examples/dirt.png'
  tex3 = 'examples/stone.png'
  #a.SetTextureMap(tex0)
  a.AddAlphaMap(tex1, tex0, alphamapchannel = "r", texscale = 5)
  a.AddAlphaMap(tex2, tex0, alphamapchannel = "g", texscale = 5)
  a.AddAlphaMap(tex3, tex0, alphamapchannel = "b", texscale = 50)
  terrain = GeoMipTerrain('test')
  terrain.setHeightfield(Filename('examples/height.png'))
#  terrainNode = terrain.getRoot()
  terrain.getRoot().reparentTo(n)
  terrain.getRoot().setSz(25)
  terrain.generate()
  a.Initialize()

  run()