render: move render_world_helper, remove chunks drawn behind view

This commit is contained in:
2022-01-27 01:09:45 +08:00
parent d8df67bc20
commit 7b6c16789c
11 changed files with 257 additions and 56 deletions

View File

@ -6,6 +6,7 @@ import (
"unsafe"
"edgaru089.ml/go/gl01/internal/asset"
"edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/util/itype"
"github.com/go-gl/gl/all-core/gl"
)
@ -67,14 +68,45 @@ func (c *Chunk) updateRender() {
select {
case vert := <-c.vertUpdate:
log.Printf("Chunk [%d,%d]: received len=%d", c.X, c.Z, len(vert))
gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(Vertex{}))*len(vert), gl.Ptr(vert), gl.DYNAMIC_DRAW)
c.vbolen = len(vert)
default: // do nothing
}
}
var checkViewOffset = []itype.Vec3d{
{0, 0, 0},
{ChunkSizeX, 0, 0},
{0, 0, ChunkSizeZ},
{ChunkSizeX, 0, ChunkSizeZ},
{0, ChunkSizeY, 0},
{ChunkSizeX, ChunkSizeY, 0},
{0, ChunkSizeY, ChunkSizeZ},
{ChunkSizeX, ChunkSizeY, ChunkSizeZ},
}
// checkView checks if the chunk is in the front-facing direction of the view.
func (c *Chunk) checkView(from, facing itype.Vec3d) bool {
off := itype.Vec3d{
float64(c.X * ChunkSizeX),
0,
float64(c.Z * ChunkSizeZ),
}
for _, check := range checkViewOffset {
ok := off.Add(check).Add(from.Negative()).Dot(facing) > 0
if ok {
return true
}
}
return false
}
func (c *Chunk) Render() {
if !c.checkView(io.ViewPos, io.ViewDir) || !c.checkView(io.RenderPos, io.RenderDir) {
return
}
c.Bind()
c.updateRender()
gl.DrawArrays(gl.TRIANGLES, 0, int32(c.vbolen))