Browse Source

add TopoLoader

malf 2 years ago
parent
commit
c7b7bc4cef
5 changed files with 73 additions and 20 deletions
  1. 5 5
      Main.gd
  2. 1 1
      Player.gd
  3. 57 0
      TopoLoader.gd
  4. 1 1
      chunk.gd
  5. 9 13
      flat_tile.gd

+ 5 - 5
Main.gd

@@ -7,15 +7,15 @@ extends Spatial
 #var tileGenScene = preload("res://TileGenerator.tscn")
 var Chunk = preload("res://chunk.tscn")
 
-var minlat = -90
-var maxlat = 90
+var minlat = 0
+var maxlat = 10
 var minlon = 0
-var maxlon = 360
+var maxlon = 10
 var steplat = 1
 var steplon = 1
 
-var resolution = 10
-var radius = 20
+var resolution = 256
+var radius = 1737.4
 
 var curlat = 0
 var curlon = 0

+ 1 - 1
Player.gd

@@ -18,7 +18,7 @@ var mouseDelta : Vector2 = Vector2()
 onready var camera : Camera = get_node("Camera")
 
 func _ready():
-	move_and_slide(Common.latLonToGlobal(0,0,11), Vector3.UP)
+	move_and_slide(Common.latLonToGlobal(0,0,1750), Vector3.UP)
 	ray = RayCast.new()
 	.get_parent().add_child(ray)
 

+ 57 - 0
TopoLoader.gd

@@ -0,0 +1,57 @@
+extends Node
+
+
+var datafile = "user://SLDEM2015_256_60S_60N_000_360_FLOAT.IMG"
+var samplesPerLine = 92160
+var lines = 30720
+var offset = 1737.4
+var scaleFact = 1
+var resolution = 256
+
+var latMin = -60
+var latMax = 60
+var lonMin = 0
+var lonMax = 360
+
+var cache = {}
+var file: File
+
+# Called when the node enters the scene tree for the first time.
+func open():
+	file = File.new()
+	file.open(datafile, File.READ)
+	
+func close():
+	file.close()
+	queue_free()
+	
+func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
+	if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
+		print("out of scope!")
+		return
+		
+	var lat = latMinIN
+	while lat <= latMaxIN:
+		cache[lat]={}
+		file.seek(lat*resolution*samplesPerLine*4 + lonMinIN*resolution*4)
+		var lon = lonMinIN
+		while lon <= lonMaxIN:
+			cache[lat][lon] = file.get_float()
+			lon += 1.0/float(resolution)
+		lat += 1.0/float(resolution)
+	return
+	
+func read(lat,lon):
+	if _validate(lat,lon):
+		return cache[lat][lon]
+	else:
+		print("out of scope!")
+
+func _validate(lat,lon):
+	if lat <= latMax and lat >= latMin and lon <= lonMax and lon >= lonMin:
+		return true
+	return false
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+#func _process(delta):
+#	pass

+ 1 - 1
chunk.gd

@@ -23,7 +23,7 @@ func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN, radiusIN:fl
 func _ready():
 	.get_node("Mesh").setParams(latMin, latMax, lonMin, lonMax, resolution, radius)
 	.get_node("Mesh").genMesh()
-	.translate(.get_node("Mesh").origin)
+	#.translate(.get_node("Mesh").origin)
 
 # Called every frame. 'delta' is the elapsed time since the previous frame.
 #func _process(delta):

+ 9 - 13
flat_tile.gd

@@ -1,6 +1,7 @@
 extends MeshInstance
 
 var Point = preload("res://point.tscn")
+var TopoLoader = preload("res://TopoLoader.gd")
 
 var latMin:float = 0
 var latMax:float = 1
@@ -109,6 +110,9 @@ func genIndex():
 	return indices
 		
 func genSphereCoords():
+	var tl = TopoLoader.new()
+	tl.open()
+	tl.cacheArea(latMin, latMax, lonMin, lonMax)
 	var verts = PoolVector3Array()
 	var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
 	verts.resize(size)
@@ -118,19 +122,11 @@ func genSphereCoords():
 	var v_idx = 0
 	for y in range(0,resolution+1):
 		for x in range(0,resolution+1):
-			verts[v_idx] = Vector3(latMin+latStep*y, lonMin+lonStep*x, radius)
+			var lat = latMin+latStep*y
+			var lon = lonMin+lonStep*x
+			verts[v_idx] = Vector3(lat, lon, radius+tl.read(lat,lon))
 			v_idx+=1
-	#var v_idx = 0
-	#var lat := float(latMin)
-	#while lat <= float(latMax):
-	#	var lon := float(lonMin)
-	#	while lon <= float(lonMax):
-	#		verts[v_idx] = Vector3(lat, lon, radius)
-	#		v_idx+=1
-	#		lon += (lonMax-lonMin)/resolution
-	#	lat += (latMax-latMin)/resolution
-	#print("verts")
-	#print(len(verts))
+	tl.close()
 	return verts
 
 
@@ -141,7 +137,7 @@ func convertToCartesian(verts:PoolVector3Array):
 	for vert in verts:
 		if not origin:
 			origin = vert			
-		carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3(), origin)
+		carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3())#, origin)
 		v_idx+=1
 	#print("Cartesian")
 	#print(len(carVerts))