tags:

views:

50

answers:

2

I have some (OpenCV) code that generates images. I'm displaying these using OpenGL. When new images are created I run the following function (each time) with the same texture name and a new image:

void loadCVTexture(GLuint& texture, const cv::Mat_<Vec3f>& image){
  if(texture != 0){
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image.cols, image.rows, GL_BGR, GL_FLOAT, image.data);
  } else {
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, image.cols, image.rows, 0, GL_BGR, GL_FLOAT, image.data);
  }
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}

I initialize the first image before glutMainLoop() and it displays correctly. It is given the id 1. When I update the image again the picture does not change. (I have confirmed that the display function is being called, and that the image is different.)

Edit: Another clue, I have sub-windows. If I comment-out my other window the code works as expected.

A: 

Are you trying to show new images in a sequence instead of the existing ones?
In which case you just need to change the image.data, not create a new texture binding.

Martin Beckett
Yes (I updated the question to be a bit more clear). I thought that the array is copied when you call either of these functions. Is that not the case? If so, my image data will definitely go out of scope.
Geoff
Assumign they are the same size/format/etc just copy the new pixel data to image.data see http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml
Martin Beckett
+2  A: 

Since it works correctly without "sub-windows", my guess would be that you have multiple OpenGL contexts in your application, and that the updating of the texture happens with the wrong context active.

Try putting the texture uploading into your display function and see if that makes a difference.

Thomas
Correct! Thanks! I have now put `glutSetWindow(leftPanel);` before the call to the above function.
Geoff