我正在使用GoGL和OpenGL
我使用TEXTURE_2D_ARRAY加载纹理渲染一个立方体,但结果是一个黑色立方体。
我确定正确指定了纹理单元,因为当我在ActiveTexture(gl.TEXTURE3)
加载模式下使用TEXTURE_CUBE_MAP
时,它是显示目的,但现在使用TEXTURE_2D_ARRAY看起来像纹理不起作用。
纹理加载这样的代码
func GenBindTexture(TextureType uint32) uint32{
var texture uint32
gl.GenTextures(1, &texture)
gl.ActiveTexture(gl.TEXTURE3)
gl.BindTexture(TextureType, texture)
//gl.TexEnvi()
return texture
}
func TestTextureArray(){
fmt.Println("texture number:",GenBindTexture(gl.TEXTURE_2D_ARRAY))
rgba,err:=DrawRGBA("Texture/T1.png")
if err!=nil{
fmt.Println("Err:",err)
return
}
gl.TextureStorage3D(
gl.TEXTURE_2D_ARRAY,
1,
gl.RGBA,
int32(rgba.Rect.Size().X),int32(rgba.Rect.Size().Y),
1,
)
fmt.Printf("error is invlid option:%x\n",gl.GetError())//in here get INVALID_ENUM
gl.TexSubImage3D(
gl.TEXTURE_2D_ARRAY,
0,
0,0,int32(0),
int32(rgba.Rect.Size().X),int32(rgba.Rect.Size().Y),1,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix),//they are color,i'm not set for random ,just test
)
fmt.Printf("error is invlid option:%x\n",gl.GetError())//in here get INVALID_Value
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)//
}
渲染和统一设置
TestTextureArray()
textureUniform := gl.GetUniformLocation(program, gl.Str("tex1\x00"))
gl.Uniform1i(textureUniform, 3) //传输tex1为对应的纹理单元id ,如果tex1已经被binding=纹理单元id则不用如此操作
//gl.Uniform1i(0,0)
fmt.Printf("error is:%x\n",gl.GetError())
gl.ClearColor(0.9, 0.9, 0.9, 0.6)
angle := 0.0
previousTime := glfw.GetTime()
for !window.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Update
time := glfw.GetTime()
elapsed := time - previousTime
previousTime = time
angle += elapsed
model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
// Render
gl.UseProgram(program)
gl.UniformMatrix4fv(modelUniform, 1, false,&model[0])
gl.BindVertexArray(vao)
//gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 6*4)
Render(program)
window.SwapBuffers()
glfw.PollEvents()
}
}
func Render(program uint32){
for CubeFace:=0;CubeFace!=6;CubeFace++ {
SetUniformVar(program,"Layer",CubeFace)
//gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 6*4)
gl.DrawElementsBaseVertex(gl.TRIANGLE_STRIP,4,gl.UNSIGNED_INT,gl.PtrOffset(0),int32(CubeFace*4))
}
}
和像这样的着色器代码
var VertexShaderMulti=`
# version 450
uniform mat4 projection;
uniform mat4 camera;
uniform mat4 model;
in vec3 vert;
in vec2 vertTexCoord;
out vec2 uv;
void main() {
uv = vertTexCoord;
gl_Position = projection * camera * model * vec4(vert, 1.3);
}
` + "\x00"
var FragmentShaderMulti=`
# version 450
uniform sampler2DArray tex1;
uniform int Layer=2;
in vec2 uv;
out vec4 outputColor;
void main(void){
outputColor=texture(tex1, vec3(uv,0));
}
`+ "\x00"
现在我使用RenderDoc实际上找到sampler2DArray tex1值
sampler2DArray tex1
任何帮助,我将非常感激!
答案 0 :(得分:1)
最后我找到了原因,gl.GetError()
gl.TextureStorage3D
后INVALID_ENUM
使用gl.RGBA
,引用此document表示内部格式存在问题,因此我更改{{1} } gl.RGBA8
一切都很完美〜它有多个纹理绑定一个纹理单元a beautiful cube〜
感谢每一个回答者,我并不孤单^ v ^