暮色呼如
如果您的目标只是将图像加载到屏幕上,这里有一种使用 OpenGL 和 GLFW 的直接方法。您可以使用像 Fyne 这样的库来执行此操作,但它已经在内部使用了 OpenGL 和 GLFW(以及许多其他依赖项),因此您最好去掉中间人。这是我的样板程序,它使用 OpenGL 纹理和帧缓冲区将图像 blits 到屏幕。package mainimport ( "image" "runtime" "github.com/go-gl/gl/all-core/gl" "github.com/go-gl/glfw/v3.3/glfw")func init() { // GLFW: This is needed to arrange that main() runs on main thread. // See documentation for functions that are only allowed to be called from the main thread. runtime.LockOSThread()}func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(640, 480, "My Window", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() err = gl.Init() if err != nil { panic(err) } var texture uint32 { gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.BindImageTexture(0, texture, 0, false, 0, gl.WRITE_ONLY, gl.RGBA8) } var framebuffer uint32 { gl.GenFramebuffers(1, &framebuffer) gl.BindFramebuffer(gl.FRAMEBUFFER, framebuffer) gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0) gl.BindFramebuffer(gl.READ_FRAMEBUFFER, framebuffer) gl.BindFramebuffer(gl.DRAW_FRAMEBUFFER, 0) } for !window.ShouldClose() { var w, h = window.GetSize() var img = image.NewRGBA(image.Rect(0, 0, w, h)) // ------------------------- // MODIFY OR LOAD IMAGE HERE // ------------------------- gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, int32(w), int32(h), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(img.Pix)) gl.BlitFramebuffer(0, 0, int32(w), int32(h), 0, 0, int32(w), int32(h), gl.COLOR_BUFFER_BIT, gl.LINEAR) window.SwapBuffers() glfw.PollEvents() }}