Jump to content
  • Advertisement
Sign in to follow this  
MilchoPenchev

3D texture access violation

This topic is 2681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!