OpenGL texture from SDL Surface in Go

huangapple go评论95阅读模式
英文:

OpenGL texture from SDL Surface in Go

问题

我正在使用go-sdl2go-gl,并尝试从sdl surface创建一个gl纹理。在C语言中,我会通过调用glTexImage2D并将surface->pixels作为最后一个参数来实现。然而,在go-sdl中,pixels是一个私有字段,而且我应该使用Pixels()来访问它,但它返回的是[]byte类型,与期望最后一个参数为unsafe.Pointergl.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())

huangapple
  • 本文由 发表于 2022年7月15日 01:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72984246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定