Here is the improved code I am now moving from file to PBO as suggested but still doesn’t work? Anyone see what maybe wrong?
Thanks
GenerateTexture2DArrayPBO(std::vector<std::string>& filenames, int wrapMode, bool flag, unsigned int textureSize,
unsigned int textureChannels, std::string& arrayName)
{
unsigned int pboBuffer = 0;
unsigned int imageTypeFormat = 0, imageTypeInternal = 0;
unsigned int arrayDepth = filenames.size();
void* pboMemory = NULL;
bool bgr = CheckChannelOrder(filenames.at(0));
switch(textureChannels * 8)
{
case 8:
imageTypeFormat = GL_LUMINANCE;
imageTypeInternal = flag ? GL_COMPRESSED_LUMINANCE : GL_LUMINANCE8;
break;
case 16:
break;
case 24:
imageTypeFormat = bgr ? GL_BGR : GL_RGB;
imageTypeInternal = flag ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT : GL_RGB8;
break;
case 32:
imageTypeFormat = bgr ? GL_BGRA :GL_RGBA;
imageTypeInternal = flag ? GL_COMPRESSED_RGBA_S3TC_DXT5_EXT : GL_RGBA8;
break;
default:
break;
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, imageTypeInternal, 64, 64, arrayDepth, 0, imageTypeFormat, GL_UNSIGNED_BYTE, NULL);
glGenBuffers(1, &pboBuffer);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pboBuffer);
glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, GetId(arrayName));
if(gameSetupData.enableMipmaps)
{
glGenerateMipmapEXT(GL_TEXTURE_2D_ARRAY_EXT);
//glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else
{
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_S, wrapMode);
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_T, wrapMode);
glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_R, wrapMode);
for(int i = 0; i < arrayDepth; ++i)
{
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, 64*64*textureChannels, NULL, GL_STREAM_DRAW);
pboMemory = glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
LoadTGAFile(filenames[i].c_str(), pboMemory);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, 0, 0, i, 64, 64, 1,
imageTypeFormat, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glDeleteBuffers(1, &pboBuffer);
if(gameSetupData.enableMipmaps)
glGenerateMipmapEXT(GL_TEXTURE_2D_ARRAY_EXT);
return true;
}
bool LoadTGAFile(const char *filename, void* pboMemory)
{
unsigned char TGAheader[12] = {0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
unsigned char TGAcompare[12] = {0}; // Used To Compare TGA Header
unsigned char header[6] = {0}; // First 6 Useful Bytes From The Header
unsigned int bytesPerPixel = 0; // Holds Number Of Bytes Per Pixel Used In The TGA File
unsigned int bpp = 0; // Hold number of bits per pixel
unsigned int temp = 0; // Temporary Variable
unsigned int imageSize = 0;
unsigned int imageWidth = 0, imageHeight = 0;
FILE *file = fopen(filename, "rb"); // Open The TGA File
if(file == NULL ||
fread(TGAcompare, 1, sizeof(TGAcompare), file) != sizeof(TGAcompare) ||
memcmp(TGAheader, TGAcompare, sizeof(TGAheader)) != 0 || fread(header, 1, sizeof(header), file) != sizeof(header))
{
if(file == NULL)
{
//kill app here
return false;
}
else
{
//kill app here
fclose(file);
return false;
}
}
imageWidth = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
imageHeight = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
if(imageWidth <= 0 || imageHeight <= 0 || (header[4] != 24 && header[4] != 32))
{
//kill app here
fclose(file);
return false;
}
bpp = header[4];// Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = bpp / 8;// Divide By 8 To Get The Bytes Per Pixel
imageSize = imageWidth * imageHeight * bytesPerPixel;// Calculate The Memory Required For The TGA Data
if(fread(pboMemory, 1, imageSize, file) != imageSize)
{
//kill app here
fclose(file);
return false;
}
fclose(file);
return true;
}