GlTexImage2D multiple images

I am drawing an image from a full-screen image of openCV, this is a large image at 60 frames per second, so I needed a faster path than openCV gui.

Using OpenGL, I:

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

Now I want to draw two sides of the image: side - using openGL hardware to scale them.
I can compress the image by resizing the square. I don’t understand how to load two images using glTexImage2 (), since no descriptor or identifier is associated with the image.

+4
source share
2 answers

The reason you can't see how to add another texture is because the code you sent glGenTextures n't have two critical functions: glGenTextures and glBindTexture . The first will generate texture objects in the context of OpenGL (space for textures on graphics hardware). The second one selects one of these texture objects for subsequent calls (glTex ..) to affect it.

First of all, functions like glTexParameteri and glTexImage2D do not need to be called again in each rendering cycle ... but I think in your case you should do this because the images always change. By default, your code uses the used texture object β€” it is a null object (reserved for the default value). You must create two texture objects and link them one by one 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(); } 

This is basically how it is done. But I have to warn you that my OpenGL knowledge is a bit outdated, so there may be more efficient ways to do this (I know, at least, that glBegin / glEnd is deprecated in C ++, replaced by VBOs).

+11
source

Remember that openGL is a state machine - you put it in a state, give it a command, and it repeats the states later.

The best part about textures is that you can do something outside of the paint call - so if you create image 1 at your image processing stage, you can load it into the card at that point.

 glBindTexture(GL_TEXTURE_2D,tex_obj[1]); // select image 1 slot glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data1 ); // load it into the graphics card memory 

And then remember it in the paint call

 glBindTexture(GL_TEXTURE_2D,tex_obj[1]); // select pre loaded image 1 glBegin(GL_QUADS); // draw it 
+2
source

Source: https://habr.com/ru/post/1337826/


All Articles