Multiple Texture ... copy p-buffer...crashs

I was trying to create two textures use real-time snap shots of my scene. It display well and act well when there’s only one texture object. When there’s more than one texture object, it doesn’t display the one specified second, and what’s more, it crashes if I try to move the display window… it seems like a memory thing, though I assigned memory space for each texture object, they didn’t use that. They use something else instead, which cause the first texture correct, and make the second no place to write…

Here’s the part of my code, hope someone can give me any suggestion on where the problem might be. Thank you!

**** INIT **************************
unsigned int *texture1, *texture2

pic1 = (unsigned int ) malloc(MAPSIZE * MAPSIZE * 3sizeof(int));
pic2 = (unsigned int ) malloc(MAPSIZE * MAPSIZE * 3sizeof(int));

********* create first texture object ***************
glGenTextures(1, &texture[0]);

// define textures
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, 3, MAPSIZE, MAPSIZE, 0, GL_RGB, GL_UNSIGNED_INT, pic1);


CREATE SECOND TEXTURE OBJECT


glGenTextures(1, &texture[1]);
printf("to here1
");
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, 3, MAPSIZE, MAPSIZE, 0, GL_RGB, GL_UNSIGNED_INT, pic2);


Create snapshots (in another function)


glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, MAPSIZE, MAPSIZE, 0);

glPushMatrix();
//do something ***
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, MAPSIZE, MAPSIZE, 0);

Your code looks OK. Make sure the window is large enough to accomodate MAPSIZE reads. Also make sure that MAPSIZE is a power of two. I’m slightly worried about the internal format of 3 vs GL_RGB this is a difference between the teximage and the copyteximage calls, these should really match exactly but I thing these are in fact synonymous.

Typo stuff: Your texture1 and texture2 declared pointers I assume are just forgotten edits for pic1 and pic2 image array pointers. The array of texture handles that you access as texture[0] and texture[1] needs to have space and I don’t see that array allocated or declared. You can also generate 2 texture handles at once into the array of handles instead of doing one at a time.

These are all trivial and I’m sure it’s just the way you’ve posted the code.

You could check for errors from OpenGL to see if that turns anything up, but things on the whole look reasonable.

The only other niggle I have is specifying an unsigned int as the data format. This is huge and wasteful and I see that you actually allocate the space. That’s about 3X what you need, 6X if you consider you make two images for a temporary image you don’t need. Best allocate ints without the *3 and just use packed data specifying a packed format. Then only create ONE image array since it’s a placeholder for the copy. Draw with this and turn the copy on for each texture one at a time after you confirm you’re rendering correctly with it, just as a further test. In addition you can free this application memory immediately after the glteximage2d call.