英文:
OpenGL texture from SDL Surface in Go
问题
我正在使用go-sdl2和go-gl,并尝试从sdl surface创建一个gl纹理。在C语言中,我会通过调用glTexImage2D
并将surface->pixels
作为最后一个参数来实现。然而,在go-sdl
中,pixels
是一个私有字段,而且我应该使用Pixels()
来访问它,但它返回的是[]byte
类型,与期望最后一个参数为unsafe.Pointer
的gl.TexImage2D
不兼容。
image, err := img.Load("img.png")
if err != nil {
//...
}
var texture uint32
gl.GenTextures(1, &texture)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.W, image.H, 0, gl.RGBA, gl.UNSIGNED_BYTE, image.Pixels())
> cannot use image.Pixels() (value of type []byte) as type unsafe.Pointer in argument to gl.TexImage2D
那么在Go语言中,从sdl surface创建gl纹理的正确方法是什么呢?
英文:
I'm using go-sdl2 and go-gl and I'm trying to create a gl texture from sdl surface. In C I would do that by calling glTexImage2D
with surface->pixels
as the last argument. In go-sdl
, however, pixels
is a private field and Pixels()
(that I'm supposed to use to access it) returns []byte
, which is incompatible with gl.TexImage2D
that expects the last argument to be an unsafe.Pointer
.
image, err := img.Load("img.png")
if err != nil {
//...
}
var texture uint32
gl.GenTextures(1, &texture)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.W, image.H, 0, gl.RGBA, gl.UNSIGNED_BYTE, image.Pixels())
> cannot use image.Pixels() (value of type []byte) as type unsafe.Pointer in argument to gl.TexImage2D
So what would be the proper way of creating gl texture from sdl surface in go?
答案1
得分: 0
原文翻译如下:
原来有一个名为 Surface.Data 的函数可以返回实际的指针。
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.W, image.H, 0, gl.RGBA, gl.UNSIGNED_BYTE, image.Data())
英文:
It turns out that there is Surface.Data function that returns the actual pointer.
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.W, image.H, 0, gl.RGBA, gl.UNSIGNED_BYTE, image.Data())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论