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

下面代码显示的时红色,请修改 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); program.bind(); vbo.bind(); program.enableAttributeArray("vertexIn"); program.enableAttributeArray("textureIn"); program.setAttributeBuffer("vertexIn",GL_FLOAT, 0, 2, 2*sizeof(GLfloat)); program.setAttributeBuffer("textureIn",GL_FLOAT,2 * 4 * sizeof(GLfloat),2,2*sizeof(GLfloat)); // glActiveTexture(GL_TEXTURE0 + 1); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,idY); glTexImage2D(GL_TEXTURE_2D,0,GL_RED,width,height,0,GL_RED,GL_UNSIGNED_BYTE,p); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // glActiveTexture(GL_TEXTURE0 + 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D,idU); // glTexImage2D(GL_TEXTURE_2D,0,GL_RG,width >> 1,height >> 1,0,GL_RG,GL_UNSIGNED_BYTE,p + width*height); glTexImage2D(GL_TEXTURE_2D,0,GL_RED,width >> 1,height >> 1,0,GL_RED,GL_UNSIGNED_BYTE,p + width*height); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // glActiveTexture(GL_TEXTURE0 + 2); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D,idV); // glTexImage2D(GL_TEXTURE_2D,0,GL_RG,width >> 1,height >> 1,0,GL_RG,GL_UNSIGNED_BYTE,p + width*height*5/4); glTexImage2D(GL_TEXTURE_2D,0,GL_RED,width >> 1,height >> 1,0,GL_RED,GL_UNSIGNED_BYTE,p + width*height*5/4); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // program.setUniformValue("textureUV",0); // program.setUniformValue("textureY",1); program.setUniformValue("textureY",0); program.setUniformValue("textureU",1); program.setUniformValue("textureV",2); glDrawArrays(GL_TRIANGLE_STRIP,0,4); program.disableAttributeArray("vertexIn"); program.disableAttributeArray("textureIn"); program.release();
最新发布
09-23
void QScreenWidget::paintEvent(QPaintEvent* event) { QPainter painter(this); AVFrame* pFrame = m_pVideoDecoder->GetFrame(); if (pFrame) { // 使用OpenGL绘制YUV帧数据 QOpenGLFunctions* gl = QOpenGLContext::currentContext()->functions(); painter.beginNativePainting(); gl->glEnable(GL_TEXTURE_2D); qDebug() << "视频帧信息 - 宽度:" << pFrame->width << "高度:" << pFrame->height << "时间戳:" << pFrame->pts << "数据指针:" << pFrame->data[0] << pFrame->data[1] << pFrame->data[2] << "行大小:" << pFrame->linesize[0] << pFrame->linesize[1] << pFrame->linesize[2]; // 创建YUV纹理 GLuint yTexture, uTexture, vTexture; gl->glGenTextures(1, &yTexture); gl->glGenTextures(1, &uTexture); gl->glGenTextures(1, &vTexture); // 上传Y分量 gl->glBindTexture(GL_TEXTURE_2D, yTexture); gl->glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, pFrame->width, pFrame->height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pFrame->data[0]); // 上传U分量 gl->glBindTexture(GL_TEXTURE_2D, uTexture); gl->glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, pFrame->width / 2, pFrame->height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pFrame->data[1]); // 上传V分量 gl->glBindTexture(GL_TEXTURE_2D, vTexture); gl->glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, pFrame->width / 2, pFrame->height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pFrame->data[2]); gl->glDisable(GL_TEXTURE_2D); painter.endNativePainting(); 以上是 QOpenGLWidget 组件绘制 h264 frame 数据, 经过测试画面一直没有正确显示, 哪里存在问题?
05-13
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
#include <GL/glut.h> #include <stdlib.h> #include <stdio.h> #include <SOIL.h> // 纹理ID数组 GLuint textures[3]; // 光照设置 void initLighting() { // 启用光照 glEnable(GL_LIGHTING); // 光源1 (点光源) GLfloat light0_ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat light0_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light0_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light0_position[] = {1.0, 1.0, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glEnable(GL_LIGHT0); // 光源2 (平行光) GLfloat light1_ambient[] = {0.1, 0.1, 0.1, 1.0}; GLfloat light1_diffuse[] = {0.8, 0.8, 0.8, 1.0}; GLfloat light1_specular[] = {0.5, 0.5, 0.5, 1.0}; GLfloat light1_position[] = {-1.0, -1.0, -1.0, 0.0}; // w=0表示平行光 glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular); glLightfv(GL_LIGHT1, GL_POSITION, light1_position); glEnable(GL_LIGHT1); } // 加载纹理 void loadTextures() { glGenTextures(3, textures); // 纹理1 glBindTexture(GL_TEXTURE_2D, textures[0]); int width, height; unsigned char* image = SOIL_load_image("texture1.jpg", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 纹理2 glBindTexture(GL_TEXTURE_2D, textures[1]); image = SOIL_load_image("texture2.jpg", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 纹理3 (用于另一个物体) glBindTexture(GL_TEXTURE_2D, textures[2]); image = SOIL_load_image("texture3.jpg", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } // 绘制立方体 void drawCube() { glBegin(GL_QUADS); // 前面 glNormal3f(0.0, 0.0, 1.0); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 1.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0); // 后面 glNormal3f(0.0, 0.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, -1.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, -1.0); // 上面 glNormal3f(0.0, 1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0); // 下面 glNormal3f(0.0, -1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, -1.0, -1.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 1.0); glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); // 右面 glNormal3f(1.0, 0.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, -1.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 1.0); // 左面 glNormal3f(-1.0, 0.0, 0.0); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); glEnd(); } // 绘制球体 void drawSphere() { glutSolidSphere(1.0, 50, 50); } // 显示函数 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // 设置材质1 (用于立方体) GLfloat mat_ambient1[] = {0.7, 0.7, 0.7, 1.0}; GLfloat mat_diffuse1[] = {0.8, 0.8, 0.8, 1.0}; GLfloat mat_specular1[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess1[] = {50.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse1); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular1); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess1); // 绘制带纹理的立方体 (使用两种纹理) glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // 前面后面使用纹理1 glBindTexture(GL_TEXTURE_2D, textures[0]); glPushMatrix(); glTranslatef(-2.0, 0.0, 0.0); glRotatef(30, 1.0, 1.0, 0.0); drawCube(); glPopMatrix(); // 设置材质2 (用于球体) GLfloat mat_ambient2[] = {0.2, 0.2, 0.5, 1.0}; GLfloat mat_diffuse2[] = {0.3, 0.3, 0.8, 1.0}; GLfloat mat_specular2[] = {0.8, 0.8, 0.8, 1.0}; GLfloat mat_shininess2[] = {30.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse2); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular2); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess2); // 绘制带纹理的球体 (使用不同纹理) glBindTexture(GL_TEXTURE_2D, textures[2]); glPushMatrix(); glTranslatef(2.0, 0.0, 0.0); drawSphere(); glPopMatrix(); glDisable(GL_TEXTURE_2D); glutSwapBuffers(); } // 键盘回调函数 void keyboard(unsigned char key, int x, int y) { switch (key) { case '1': glShadeModel(GL_FLAT); break; case '2': glShadeModel(GL_SMOOTH); break; case 27: // ESC键 exit(0); break; } glutPostRedisplay(); } // 窗口大小调整 void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // 初始化函数 void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); initLighting(); loadTextures(); // 默认使用平滑着色 glShadeModel(GL_SMOOTH); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600); glutInitWindowPosition(100, 100); glutCreateWindow("真实感图形生成实验"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值