Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically changing a texture in OpenGL

Tags:

opencv

opengl

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.

like image 532
Geoff Avatar asked Oct 26 '22 11:10

Geoff


1 Answers

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.

like image 81
Thomas Avatar answered Nov 21 '22 17:11

Thomas