3D texture access violation

Started by
1 comment, last by MilchoPenchev 13 years, 1 month ago
I'm not a fan of dumping code like this, but I'm getting a headache from trying to figure this out, so I'm going to ask.

I'm trying to create a 3d texture out of 4 256x256 images. My code looks like this:

GLuint TextureLoader::Load3dTexture(vector<string> images, bool smooth, bool repeat)
{
unsigned int numTextures = images.size();
if (numTextures == 0)
return 0;

vector<GLubyte*> texturesData;
vector<Vec2i> texturesSizes;
texturesData.resize(numTextures);
texturesSizes.resize(numTextures);

for (unsigned int i = 0; i < numTextures; i++)
{ // collect all the data
loadImage(images, &(texturesData), &(texturesSizes.x), &(texturesSizes.y));
}

int w = texturesSizes[0].x, h = texturesSizes[0].y;

unsigned int dataIndex = 0;
int dataSize = w*h*numTextures;
GLubyte *completeData = new GLubyte[dataSize];

for (unsigned int i = 0; i < numTextures; i++)
{
if (texturesSizes.x != w || texturesSizes.y != h)
{
for (unsigned int j = 0; j < texturesData.size(); j++)
{
delete [] texturesData[j];
}
delete []completeData;
cout << "Error loading textures for 3d texture! Different sizes!\n";
return 0;
}

// copy over the actual data
for (int k = 0; k < w*h; k++)
{
completeData[dataIndex] = texturesData[k];
dataIndex += 1;
}
}

GLuint TextureID;
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_3D, TextureID);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, w, h, numTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, completeData);

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE);

glGenerateMipmap(GL_TEXTURE_3D);

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, smooth ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, smooth ? GL_LINEAR : GL_NEAREST);

glBindTexture(GL_TEXTURE_3D, 0);

for (unsigned int j = 0; j < texturesData.size(); j++)
{
delete [] texturesData[j];
}
delete []completeData;

return TextureID;
}


The line
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, w, h, numTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, completeData);
is where I get an access violation error.



The 'loadImage(..)' function works correctly, I used it earlier to load and texture using a 2d texture. I'm using GLEW for the 3d texture extension, and it's supported on my card, I checked. I enable 3d texturing via glEnable(GL_TEXTURE_3D);

The "completeData" array is exactly as big as w*h*numTextures (as you can see above). Does the glTexImage3d(...) require the pointer to be to a 3d or 2d array? Am I missing something? Any help is appreciated.
Advertisement
Shouldn't the completeData size be (w*h*numTextures*4), 4 bytes per pixel? If you're loading RGBA than opengl expects to load that much data.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Oh god you're right! I even had to do that exact same copy over in my previous function, and now I totally forgot. Great.

I could blame it on it being 1am, but that's such a stupid mistake....

Thanks!

This topic is closed to new replies.

Advertisement