4

I have succeed in loading an image to OpenGL as a texture (I use Gdk::Pixbuf from GTKmm library), but I have no idea how to get modified image from OpenGL and load it to Gdk::Pixbuf...

I want to modify images in OpenGL and the save them on hard disk.

There is some code:

Glib::RefPtr<Gdk::Pixbuf> pixmap = Gdk::Pixbuf::create_from_file("image.jpg");
GLuint texture[1];
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap->get_width(), pixmap->get_height(), 0,      GL_RGB, GL_UNSIGNED_BYTE, pixmap->get_pixels() );
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Marco
  • 582
  • 1
  • 6
  • 17

3 Answers3

4

Render textured quad to the framebuffer and then glReadPixels().

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Since he hasn't tagged it OpenGL ES, drawing a textured quad is not a good alternative to just using `glGetTexImage`. – Christian Rau Oct 27 '11 at 18:39
  • 3
    @ChristianRau: But since the OP said "want to modify images in OpenGL", glGetTexImage is probably not what is wanted, since it will just read back the texture image that was previously uploaded (this is one of the functions of which I never understood why they exist). To read back something "modified", something like drawing a textured quad (with some kind of "operation", likely a shader) is needed, as genpfault suggested. – Damon Oct 28 '11 at 09:11
  • @Damon The texture data can be modified by using render to texture, i.e. rendering to a framebuffer with a texture attached. Reading back the texture data therefore does make sense in some cases. – Gigo Jul 21 '13 at 00:48
  • @Gigo: Maybe, it's hard to tell, since we don't know what _exactly_ the OP wants ("modify" is not very concrete). Sure, you could e.g. draw some geometry on top of an existing image, that's fine. But the usual idea of "modify an image" is something different, it normally involves reading the image and applying some kind of convolution or such. Reading and writing to the same image is undefined (except using atomic ops, which are slow, or using expicit sync with a vendor extension). Which usually means you will want to read back a _different_ texture, not the same one. – Damon Jul 21 '13 at 11:20
4

As long as you don't use OpenGL ES, but real desktop OpenGL, you can just use glGetTexImage.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
0

If you want to avoid the draw call, you can create a framebuffer and attach the texture to a color attachment, then use glReadPixels

Litherum
  • 22,564
  • 3
  • 23
  • 27