milestone: GL 客户端构建运行成功(zig cc + go-gl)——窗口/世界/渲染/HUD 全链路 12 秒无崩溃;修复越界查询与编码损坏
This commit is contained in:
@@ -86,7 +86,7 @@ func (cg *chunkGL) destroy() {
|
||||
|
||||
// DrawChunks 绘制全部区块网格(三 pass,渲染.md §6)。
|
||||
// chunks:本帧要绘制的区块(已由视锥剔除过滤)。
|
||||
func (r *Renderer) DrawChunks(cx, cz func(i int) (int32, int32), n int) {
|
||||
func (r *Renderer) DrawChunks(get func(i int) (int32, int32), n int) {
|
||||
gl.UseProgram(r.prog)
|
||||
gl.ActiveTexture(gl.TEXTURE0)
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.atlas)
|
||||
@@ -99,21 +99,21 @@ func (r *Renderer) DrawChunks(cx, cz func(i int) (int32, int32), n int) {
|
||||
|
||||
// pass 1+2:不透明与 cutout(alpha test,不排序)
|
||||
gl.Uniform1f(cutLoc, 0)
|
||||
drawPass(r, n, cx, cz, false, false)
|
||||
drawPass(r, get, n, false, false)
|
||||
gl.Uniform1f(cutLoc, 1)
|
||||
drawPass(r, n, cx, cz, false, true)
|
||||
drawPass(r, get, n, false, true)
|
||||
// pass 3:半透明(关闭深度写入,远→近排序由调用方保证顺序)
|
||||
gl.Uniform1f(cutLoc, 0)
|
||||
gl.DepthMask(false)
|
||||
drawPass(r, n, cx, cz, true, false)
|
||||
drawPass(r, get, n, true, false)
|
||||
gl.DepthMask(true)
|
||||
gl.BindVertexArray(0)
|
||||
}
|
||||
|
||||
// drawPass 按通道绘制。
|
||||
func drawPass(r *Renderer, n int, cx, cz func(i int) (int32, int32), translucent, cutout bool) {
|
||||
func drawPass(r *Renderer, get func(i int) (int32, int32), n int, translucent, cutout bool) {
|
||||
for i := 0; i < n; i++ {
|
||||
x, z := cx(i), cz(i)
|
||||
x, z := get(i)
|
||||
cg, ok := r.chunks[chunkKey(x, z)]
|
||||
if !ok {
|
||||
continue
|
||||
|
||||
@@ -164,7 +164,7 @@ func (r *Renderer) SetAtlas(img image.Image) {
|
||||
gl.BindTexture(gl.TEXTURE_2D, r.atlas)
|
||||
b := img.Bounds()
|
||||
w, h := int32(b.Dx()), int32(b.Dy())
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageBytes(img))
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(imageBytes(img)))
|
||||
gl.GenerateMipmap(gl.TEXTURE_2D)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
||||
@@ -172,7 +172,7 @@ func (r *Renderer) SetAtlas(img image.Image) {
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
||||
}
|
||||
|
||||
// imageBytes 把 image.Image 转 RGBA 字节流(预乘无关紧要)。
|
||||
// imageBytes 把 image.Image 转 RGBA 字节流。
|
||||
func imageBytes(img image.Image) []byte {
|
||||
b := img.Bounds()
|
||||
out := make([]byte, 0, b.Dx()*b.Dy()*4)
|
||||
@@ -237,4 +237,4 @@ void main() {
|
||||
float b = max(sky, blk * 0.9 + 0.06);
|
||||
fragColor = vec4(c.rgb * b, c.a);
|
||||
}
|
||||
`
|
||||
`
|
||||
Reference in New Issue
Block a user