本文介绍了glTexImage2D多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我从openCV全屏幕绘制图像,这是一个大的图像在60fps所以我需要一个比openCV gui更快的方式。I'm drawing an image from openCV full screen, this is a large image at 60fps so I needed a faster way than the openCV gui.使用OpenGL I do:Using OpenGL I do:void paintGL() { glClear (GL_COLOR_BUFFER_BIT); glClearColor (0.0,0.0,0.0,1.0); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,width,height,0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data ); glBegin(GL_QUADS); glTexCoord2i(0,0); glVertex2i(0,height); glTexCoord2i(0,1); glVertex2i(0,0); glTexCoord2i(1,1); glVertex2i(width,0); glTexCoord2i(1,0); glVertex2i(width,height); glEnd(); glDisable(GL_TEXTURE_2D);}现在我想画两个图片:side-使用openGL硬件 我可以通过改变四边形尺寸来缩小图像。我不明白如何使用glTexImage2()加载两个图像,因为没有与图像相关的句柄或ID。Now I want to draw two images side:side - using the openGL hardware to scale them.I can shrink the image by changing the quad size I don't understand how to load two images with glTexImage2() since there is no handle or id associated with the image.推荐答案你不能看到如何添加另一个纹理的原因是因为你在你发布的代码中缺少两个关键函数: glGenTextures 和 glBindTexture 。第一个将在OpenGL上下文中生成纹理对象(纹理存在于图形硬件上的位置)。第二个选择其中一个纹理对象用于后续调用(glTex ..)以影响它。The reason why you cannot see how to add another texture is because you are missing two critical functions in the code that you posted: glGenTextures and glBindTexture. The first will generate texture objects in the OpenGL context (places for textures to exist on the graphics hardware). The second "selects" one of those texture objects for subsequent calls (glTex..) to affect it.首先,像glTexParameteri和glTexImage2D这样的函数不需要在每个渲染循环被再次调用...但我猜在你的情况下,你应该这样做,因为图像总是在变化。默认情况下,在代码中,使用的纹理对象是第零个对象(默认保留的一个对象)。您应该创建两个纹理对象,并依次绑定它们以实现所需的结果:First of all, the functions like glTexParameteri and glTexImage2D do not need to be called again at every rendering loop... but I guess in your case, you should do that because the images are always changing. By default, in your code, the texture object used is the zeroth object (a reserved one for the default). You should create two texture objects and bind them one after the other to achieve the desired result:GLuint tex_obj[2]; //create two names for the texture (should not be global variables, but just for sake of this example).void initGL() { glClearColor (0.0,0.0,0.0,1.0); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,width,height,0); glEnable(GL_TEXTURE_2D); glGenTextures(2,tex_obj); //generate 2 texture objects with names tex_obj[0] and [1] glBindTexture(GL_TEXTURE_2D, tex_obj[0]); //bind the first texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //set its parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, tex_obj[1]); //bind the second texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //set its parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);}void paintGL() { glClear (GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBindTexture(GL_TEXTURE_2D,tex_obj[0]); //bind the first texture. //then load it into the graphics hardware: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width0, height0, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data0 ); glBegin(GL_QUADS); glTexCoord2i(0,0); glVertex2i(0,height); //you should probably change these vertices. glTexCoord2i(0,1); glVertex2i(0,0); glTexCoord2i(1,1); glVertex2i(width,0); glTexCoord2i(1,0); glVertex2i(width,height); glEnd(); glBindTexture(GL_TEXTURE_2D, tex_obj[1]); //bind the second texture. //then load it into the graphics hardware: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data1 ); glBegin(GL_QUADS); glTexCoord2i(0,0); glVertex2i(0,height); //you should probably change these vertices. glTexCoord2i(0,1); glVertex2i(0,0); glTexCoord2i(1,1); glVertex2i(width,0); glTexCoord2i(1,0); glVertex2i(width,height); glEnd();}这基本上是怎么做的。但我必须警告你,我对OpenGL的了解有点过时,所以可能有更有效的方法来做到这一点(我知道至少glBegin / glEnd在C ++中已被弃用,由VBOs取代)。That is basically how it is done. But I have to warn you that my knowledge of OpenGL is a bit outdated, so there might be more efficient ways to do this (I know at least that glBegin/glEnd is deprecated in C++, replaced by VBOs). 这篇关于glTexImage2D多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 13:22