我被glReadPixels困扰:似乎没有数据写入输出数组。

从手册(https://www.opengl.org/sdk/docs/man/docbook4/xhtml/glReadPixels.xml)可以写到,如果生成错误,则不会对数据内容进行任何更改。但是glGetError()显然不会返回任何错误。

我真的不明白这是怎么回事...这是在golang中的代码,该代码对屏幕上的随机点进行采样。它始终打印[0 0 0],尽管没有像素应该是黑色的(甚至不是背景)。

package main

import (
    "fmt"
    "github.com/go-gl/gl/v4.1-core/gl"
    "github.com/veandco/go-sdl2/sdl"
    "math/rand"
    "os"
    "runtime"
)

func main() {
    // Random crashes, this seems to fix
    runtime.LockOSThread()

    var wWidth, wHeight int = 500, 500

    // Initialize SDL
    if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
        panic(err)
    }
    defer sdl.Quit()

    // Initialize OpenGL
    if err := gl.Init(); err != nil {
        panic(err)
    }

    // Create the OpenGL-capable window
    win, err := sdl.CreateWindow(
        "glReadPixels Test",
        sdl.WINDOWPOS_CENTERED,
        sdl.WINDOWPOS_CENTERED,
        wWidth, wHeight,
        sdl.WINDOW_OPENGL)

    if err != nil {
        panic(err)
    }
    defer win.Destroy()

    // Setup OpenGL context
    var ctx sdl.GLContext
    ctx, err = sdl.GL_CreateContext(win)
    if err != nil {
        panic(err)
    }
    defer sdl.GL_DeleteContext(ctx)

    // Settings
    initGL(wWidth, wHeight)

    for {
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch event.(type) {
            case *sdl.QuitEvent:
                os.Exit(0)
            }
        }

        // Clear and draw
        gl.Clear(gl.COLOR_BUFFER_BIT)
        gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)

        rx, ry := rand.Intn(wWidth), rand.Intn(wHeight)

        gl.ReadBuffer(gl.FRONT) // Not working with gl.BACK either
        data := make([]byte, 3)
        //data[0], data[1], data[2] = 123, 213, 132 // Test if it's overwritten
        gl.ReadPixels(int32(rx), int32(ry), 1, 1, gl.RGB, gl.UNSIGNED_BYTE, gl.Ptr(&data))
        fmt.Println("Read at", rx, ry, data)

        // Show on window
        sdl.GL_SwapWindow(win)

        // Check for errors...
        if gl.GetError() != gl.NO_ERROR {
            fmt.Println("GL ERROR Somewhere!")
        }

        sdl.Delay(uint32(300))
    }
}

func initGL(winW, winH int) {
    gl.ClearColor(0.2, 0.2, 0.3, 1.0)
    gl.Viewport(0, 0, int32(winW), int32(winH))

    // Shader sources
    vertShaderSrc := gl.Str(`
#version 130

in vec3 vertPos;
in vec2 vertUV;

out vec2 fragUV;

void main() {
    gl_Position = vec4(vertPos, 1.0f);
    fragUV = vertUV;
}
` + "\x00")

    fragShaderSrc := gl.Str(`#version 130
in vec2 fragUV;
out vec3 outColor;

void main() {
    if (fragUV.x < 0.5 ^^ fragUV.y < 0.5)
        outColor = vec3(1.0, 0.0, 0.0);
    else
        outColor = vec3(0.0, 0.0, 1.0);
}
` + "\x00")

    // Shaders
    var vShader, fShader uint32
    vShader = gl.CreateShader(gl.VERTEX_SHADER)
    fShader = gl.CreateShader(gl.FRAGMENT_SHADER)
    gl.ShaderSource(vShader, 1, &vertShaderSrc, nil)
    gl.ShaderSource(fShader, 1, &fragShaderSrc, nil)
    gl.CompileShader(vShader)
    gl.CompileShader(fShader)

    // Program
    var progr uint32 = gl.CreateProgram()
    gl.AttachShader(progr, vShader)
    gl.AttachShader(progr, fShader)
    gl.BindAttribLocation(progr, 0, gl.Str("vertPos\x00"))
    gl.BindAttribLocation(progr, 1, gl.Str("vertUV\x00"))
    gl.LinkProgram(progr)
    gl.UseProgram(progr)

    const N float32 = -0.5
    const P float32 = 0.5

    // Setup the "canvas": a simple quad
    canvasData := []float32{
        N, N, 0, 0.0, 0.0,
        P, N, 0, 1.0, 0.0,
        N, P, 0, 0.0, 1.0,
        P, P, 0, 1.0, 1.0,
    }

    var vboID uint32
    gl.GenBuffers(1, &vboID)
    gl.BindBuffer(gl.ARRAY_BUFFER, vboID)
    gl.BufferData(gl.ARRAY_BUFFER, len(canvasData)*4, gl.Ptr(canvasData), gl.STATIC_DRAW)

    var vaoID uint32
    gl.GenVertexArrays(1, &vaoID)
    gl.BindVertexArray(vaoID)

    gl.EnableVertexAttribArray(0)
    gl.EnableVertexAttribArray(1)

    gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))
    gl.VertexAttribPointer(1, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))
}

最佳答案

问题可能

gl.ReadPixels(int32(rx), int32(ry), 1, 1, gl.RGB, gl.UNSIGNED_BYTE, gl.Ptr(&data))

具体来说
gl.Ptr(&data)

该功能的文档是:
// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.

您正在将POINTER传递给 slice 。如果您看一下实现,这是有问题的。
func Ptr(data interface{}) unsafe.Pointer {
    if data == nil {
        return unsafe.Pointer(nil)
    }
    var addr unsafe.Pointer
    v := reflect.ValueOf(data)
    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())
        }
    case reflect.Uintptr:
        addr = unsafe.Pointer(v.Pointer())
    case reflect.Slice:


addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
    default:
        panic(fmt.Sprintf("Unsupported type %s; must be a pointer, slice, or array", v.Type()))
    }
    return addr
}

如您所见,如果将指针传递给除基本uint/float/int之外的其他任何内容,则它会静默失败,并且如果类型不正确,则返回空unsafe.Pointer。因此,您要么要传递gl.Ptr(&data[0])要么gl.Ptr(data)。说实话,这不是段错误,实在令人惊讶。

关于opengl - glReadPixels不会将数据写入数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33740157/

10-09 13:30