在OpenGL中动态更改纹理 [英] Dynamically changing a texture in OpenGL

查看:1477
本文介绍了在OpenGL中动态更改纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(OpenCV)代码可以生成图像.我正在使用OpenGL显示这些.创建新图像时,我每次都运行具有相同texture名称和新image的以下函数:

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);
}

我在 glutMainLoop()之前初始化了第一张图像,并正确显示.编号为1.当我再次更新图像时,图片不会改变. (我已经确认正在调用显示功能,并且图像有所不同.)

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.)

编辑:另一个提示,我有子窗口.如果我将其他窗口注释掉,则代码将按预期工作.

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

推荐答案

由于它可以在没有子窗口"的情况下正常运行,因此我猜测您的应用程序中将包含多个OpenGL上下文,并且纹理的更新也会发生激活了错误的上下文.

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.

这篇关于在OpenGL中动态更改纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆