world: fix worldgen

This commit is contained in:
Edgaru089 2022-02-02 01:52:02 +08:00
parent 37ee76e9fc
commit 0e4e17ced7
3 changed files with 51 additions and 8 deletions

42
internal/util/unsafe.go Normal file
View File

@ -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
}

View File

@ -7,6 +7,7 @@ import (
"edgaru089.ml/go/gl01/internal/asset" "edgaru089.ml/go/gl01/internal/asset"
"edgaru089.ml/go/gl01/internal/io" "edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/util"
"edgaru089.ml/go/gl01/internal/util/itype" "edgaru089.ml/go/gl01/internal/util/itype"
"github.com/go-gl/gl/all-core/gl" "github.com/go-gl/gl/all-core/gl"
) )
@ -91,9 +92,9 @@ func (c *Chunk) updateRender() {
select { select {
case vert := <-c.vertUpdate: case vert := <-c.vertUpdate:
gl.BindBuffer(gl.ARRAY_BUFFER, c.vbo) 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.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.vbolen = len(vert[0])
c.water.vbolen = len(vert[1]) c.water.vbolen = len(vert[1])
default: // do nothing default: // do nothing

View File

@ -13,11 +13,11 @@ const (
Beta = 2 // harmonic scaling/spacing Beta = 2 // harmonic scaling/spacing
N = 3 // iterations 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 Sealevel = 63 // Space with Y=63 and lower should be the sea
Highest = 84 // Highest part of the terrain Highest = 74 // Highest part of the terrain
Lowest = 60 // Lowest part of the terrain Lowest = 54 // Lowest part of the terrain
) )
var perlins map[int64]*perlin.Perlin 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 x := 0; x < packworld.ChunkSizeX; x++ {
for z := 0; z < packworld.ChunkSizeZ; z++ { 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(offX+x)/AltitudeDensity,
float64(offZ+z)/AltitudeDensity)) float64(offZ+z)/AltitudeDensity)/2+0.5))
//log.Printf("height = %d (noise=%.5f)", height, p.Noise2D(float64(offX+x), float64(offZ+z))) //log.Printf("height = %d (noise=%.5f)", height, p.Noise2D(float64(offX+x)/AltitudeDensity, float64(offZ+z)/AltitudeDensity)/2+0.5)
// water // water
for y := Sealevel; y > height; y-- { for y := Sealevel; y > height; y-- {