OpenGL 的glDisable(GL_TEXTURE_2D)函数和glBindTexture(GL_TEXTURE_2D,0)有什么区别?

OpenGL glDisable(GL_TEXTURE_2D)函数和glBindTexture(GL_TEXTURE_2D,0)有什么区别?


OpenGL glDisable(GL_TEXTURE_2D) vs glBindTexture(GL_TEXTURE_2D,0)

These are two fundamentally different things.


glDisable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,0)

glDisable (GL_TEXTURE_2D)

  • Disables 2D textures for the active Texture Unit in the fixed-function OpenGL pipeline.
  • In programmable OpenGL (GLSL or the older ARB VP/FP assembly languages), enabling or disabling GL_TEXTURE_2D is meaningless.

glBindTexture (GL_TEXTURE_2D,0)

  • Binds the "default" texture to the active Texture Image Unit.
  • For all intents and purposes, there is always a texture bound to a Texture Image Unit; 0 is just a special-case with an initially incomplete set of states, thus complicating validation (see below).

There is a little bit of validation overhead associated with binding any OpenGL resource (GLSL programs and FBOs tend to have the most complicated validation). In general, changing an object binding tends to be more expensive than changing one or two states associated with a bound object.

Therefore, unnecessarily changing the bound texture is inadvisable.

If your intention by binding 0 was simply to "disable" a texture, you would be better off setting a uniform value to communicate this intent to a shader. Uniforms are extremely cheap to set (assuming you do not do something stupid like query their location everytime you set them). Changing a uniform is orders of magnitude quicker than binding a different texture or even worse a different GLSL program.


If you are using fixed-function OpenGL: OpenGL1.0

  • Disabling GL_TEXTURE_2D is a better way of approaching this problem.
  • Changing bound objects should be done as little as possible.

If you are using programmable OpenGL: OpenGL2.0

  • Neither of these things make much sense.
  • Work your desired behavior into your shaders.

https://gamedev.stackexchange.com/questions/74583/opengl-gldisablegl-texture-2d-vs-glbindtexturegl-texture-2d-0/74584

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions { Q_OBJECT public: MyGLWidget(QWidget *parent = nullptr) : QOpenGLWidget(parent) {} void setImage(cv::Mat image) { m_image = image; update(); } void stopImage(bool) {} protected: protected: void initializeGL() override { initializeOpenGLFunctions(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 创建并绑定纹理 glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } void resizeGL(int w, int h) override { glViewport(0, 0, w, h); } void paintGL() override { // 从VideoCapture对象中读取图像数据 makeCurrent(); // 设置当前OpenGL上下文 // 从VideoCapture对象中读取图像数据 // 将图像数据上传到纹理中 if (!m_image.empty()) { glBindTexture(GL_TEXTURE_2D, m_texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_image.cols, m_image.rows, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, m_image.data); } // 清除帧缓冲区 glClear(GL_COLOR_BUFFER_BIT); // 渲染纹理 if (!m_image.empty()) { glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); glDisable(GL_TEXTURE_2D); } } private: cv::Mat m_image; GLuint m_texture{}; }; glClear(GL_COLOR_BUFFER_BIT);此处崩溃,分析原因并修改
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值