我正在尝试创建多个纹理二维数组来纹理高度图以及纹理的凹凸图。
问题是当我加载多个数组时,它会覆盖第一个数组的纹理数据。
#include "TextureArray.h"
#include "Debug.h"
TextureArray::TextureArray(const char* directory, int number)
{
initBuffers();
int width = 0;
int height = 0;
for(int i = 0; i < number; i++)
{
char buffer[256];
sprintf(buffer, "%s%i.png", directory, i);
if(!this->LoadFile(buffer, number, i, &width, &height))
{
Destroy();
return;
}
}
}
void TextureArray::initBuffers()
{
glGenTextures(1, &textures);
glBindTexture(GL_TEXTURE_3D, textures);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
void TextureArray::Destroy()
{
glDeleteTextures(1, &textures);
}
bool TextureArray::LoadFile(char* file, int size, int index, int* width, int* height)
{
Texture* texture = LoadTextureFromFile(file, false);
if(texture == NULL)
{
return false;
}
unsigned char* data = texture->data;
if(data)
{
if(index == 0)
{
*width = texture->width;
*height = texture->height;
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, texture->bitDepth == ImageType::RGBA ? GL_RGBA : GL_RGB, *width, *height, size, 0, texture->bitDepth == ImageType::RGBA ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, NULL);
checkError("I==0");
}
if(texture->width != *width || texture->height != *height)
{
return false;
}
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, index, texture->width, texture->height, 1, texture->bitDepth == ImageType::RGBA ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data);
checkError(file);
free(data);
}
free(texture);
return true;
}
以下是对此代码的调用:
textureArray = new TextureArray("textures/", "bumpmaps/", TEXTURE_ARRAY_SIZE);
//bumpMapArray = new TextureArray("bumpmaps/", TEXTURE_ARRAY_SIZE);