我现在正在开发一个体素游戏。一开始,我使用了一个纹理图集来存储所有的体素纹理,它可以正常工作。之后,我决定在我的游戏中使用贪婪网格化技术,因此纹理图集就无用了。我阅读了一些文章,说应该使用纹理数组代替。然后我尝试使用纹理数组来进行贴图。但是,在我的游戏中得到的结果却全部是黑色的。那么我错过了什么吗?
这是我的纹理图集(600 x 600)
以下是我的Texture2DArray类,我使用这个类来读取和保存纹理数组。
这是我的纹理图集(600 x 600)
![](https://istack.dev59.com/S9OtX.webp)
Texture2DArray::Texture2DArray() : Internal_Format(GL_RGBA8), Image_Format(GL_RGBA), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Wrap_R(GL_REPEAT), Filter_Min(GL_NEAREST), Filter_Max(GL_NEAREST), Width(0), Height(0)
{
glGenTextures(1, &this->ID);
}
void Texture2DArray::Generate(GLuint width, GLuint height, unsigned char* data)
{
this->Width = width;
this->Height = height;
glBindTexture(GL_TEXTURE_2D_ARRAY, this->ID);
// I cannot decide what the texture array layer (depth) should be (I put here is 1 for layer number)
//Can anyone explain to me how to decide the texture layer here?
glTexImage3D(GL_TEXTURE_2D_ARRAY, 1, this->Internal_Format, this->Width, this->Height, 0, 1 , this->Image_Format, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, this->Wrap_S);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, this->Wrap_T);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_R, this->Wrap_R);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, this->Filter_Min);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, this->Filter_Max);
//unbind this texture for another creating texture
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
}
void Texture2DArray::Bind() const
{
glBindTexture(GL_TEXTURE_2D_ARRAY, this->ID);
}
这是我的片元着色器
#version 330 core
uniform sampler2DArray ourTexture;
in vec2 texCoord;
out vec4 FragColor;
void main(){
// 1 (the layer number) just for testing
FragColor = texture(ourTexture,vec3(texCoord, 1));
}
这是我的顶点着色器
#version 330 core
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inTexCoord;
out vec2 texCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_Position = projection * view * vec4(inPos,1.0f);
texCoord = inTexCoord;
}
这是我的渲染结果。
编辑1:
我发现纹理图集与纹理数组不兼容,因为纹理图集是一个网格,所以OpenGL无法确定应该从哪里开始。因此,我创建了一张垂直纹理(18 x 72)并尝试了一次,但仍然全黑。
我已经检查过在使用纹理之前绑定纹理。
ptr
应该是data + iy*rowLen*tileH + ix*tileSizeX;
(乘以tileH
),否则复制的起始位置将会偏移像素行而不是瓦片行。之前和之后。 - MinebomberrowLen = tilesX * tileSizeX
和tileSizeX = tileW * channels
- Rabbid76