From 0e4e17ced7f2be3f9295458fdbd1b5e4762c935a Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Wed, 2 Feb 2022 01:52:02 +0800 Subject: [PATCH] world: fix worldgen --- internal/util/unsafe.go | 42 +++++++++++++++++++++++++++++ internal/world/render.go | 5 ++-- internal/world/worldgen/worldgen.go | 12 ++++----- 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 internal/util/unsafe.go diff --git a/internal/util/unsafe.go b/internal/util/unsafe.go new file mode 100644 index 0000000..c2dd105 --- /dev/null +++ b/internal/util/unsafe.go @@ -0,0 +1,42 @@ +package util + +import ( + "fmt" + "reflect" + "unsafe" +) + +// Ptr returns the pointer to these things: +// - First element of a slice or array, nil if it has zero length; +// - The pointer itself if the parameter is a pointer, pointing to an scalar value. +// It panics otherwise. +func Ptr(d interface{}) (addr unsafe.Pointer) { + if d == nil { + return unsafe.Pointer(nil) + } + + v := reflect.ValueOf(d) + switch v.Type().Kind() { + case reflect.Ptr: + e := v.Elem() + switch e.Kind() { + case + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, + reflect.Float32, reflect.Float64: + addr = unsafe.Pointer(e.UnsafeAddr()) + default: + panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind())) + } + + case reflect.Slice, reflect.Array: + if v.Len() != 0 { + addr = unsafe.Pointer(v.Index(0).UnsafeAddr()) + } + case reflect.Uintptr: + addr = unsafe.Pointer(d.(uintptr)) + default: + panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type())) + } + return +} diff --git a/internal/world/render.go b/internal/world/render.go index ca68a51..1eabb66 100644 --- a/internal/world/render.go +++ b/internal/world/render.go @@ -7,6 +7,7 @@ import ( "edgaru089.ml/go/gl01/internal/asset" "edgaru089.ml/go/gl01/internal/io" + "edgaru089.ml/go/gl01/internal/util" "edgaru089.ml/go/gl01/internal/util/itype" "github.com/go-gl/gl/all-core/gl" ) @@ -91,9 +92,9 @@ func (c *Chunk) updateRender() { select { case vert := <-c.vertUpdate: gl.BindBuffer(gl.ARRAY_BUFFER, c.vbo) - gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(Vertex{}))*len(vert[0]), gl.Ptr(vert[0]), gl.DYNAMIC_DRAW) + gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(Vertex{}))*len(vert[0]), util.Ptr(vert[0]), gl.DYNAMIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, c.water.vbo) - gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(Vertex{}))*len(vert[1]), gl.Ptr(vert[1]), gl.DYNAMIC_DRAW) + gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(Vertex{}))*len(vert[1]), util.Ptr(vert[1]), gl.DYNAMIC_DRAW) c.vbolen = len(vert[0]) c.water.vbolen = len(vert[1]) default: // do nothing diff --git a/internal/world/worldgen/worldgen.go b/internal/world/worldgen/worldgen.go index 8699d81..48863b5 100644 --- a/internal/world/worldgen/worldgen.go +++ b/internal/world/worldgen/worldgen.go @@ -13,11 +13,11 @@ const ( Beta = 2 // harmonic scaling/spacing N = 3 // iterations - AltitudeDensity = 10 // Density of the noise in altitude + AltitudeDensity = 20 // Density of the noise in altitude Sealevel = 63 // Space with Y=63 and lower should be the sea - Highest = 84 // Highest part of the terrain - Lowest = 60 // Lowest part of the terrain + Highest = 74 // Highest part of the terrain + Lowest = 54 // Lowest part of the terrain ) var perlins map[int64]*perlin.Perlin @@ -50,10 +50,10 @@ func Chunk(chunk *packworld.Chunk, world *packworld.World, seed int64) { for x := 0; x < packworld.ChunkSizeX; x++ { for z := 0; z < packworld.ChunkSizeZ; z++ { - height := Lowest + int(float64(Highest-Lowest)*p.Noise2D( + height := Lowest + int(float64(Highest-Lowest)*(p.Noise2D( float64(offX+x)/AltitudeDensity, - float64(offZ+z)/AltitudeDensity)) - //log.Printf("height = %d (noise=%.5f)", height, p.Noise2D(float64(offX+x), float64(offZ+z))) + float64(offZ+z)/AltitudeDensity)/2+0.5)) + //log.Printf("height = %d (noise=%.5f)", height, p.Noise2D(float64(offX+x)/AltitudeDensity, float64(offZ+z)/AltitudeDensity)/2+0.5) // water for y := Sealevel; y > height; y-- {