Samuel
Samuel

Reputation: 6237

what's the difference between image and texture in OpenGL

I just start to learn the OpenGL. I was confused by the Image and texture.

Upvotes: 10

Views: 7201

Answers (2)

nkint
nkint

Reputation: 11733

I am new to OpenGL too. And not really good with low level computation/GPU architecture. But this is a thing I have just learned and I'm explaining to you.

In OpenGL you don't have an "image" object like in some other frameworks (QImage in Qt, BufferedImage in Java Swing, Mat object in OpenCV, etc), but you have some buffers in which you can render through the OpenGL render pipeline OR store images.

If you open an opengl context you are rendering in the default frame buffer (on the screen). But you can do some off-screen render in some other frame buffers, like in render buffer. Then you have some other buffers that can store images in the GPU in some buffers that can be used as texture.

Those OpenGL buffers (renderbuffers or texture) are stored inside the GPU (and the transfer from GPU to CPU or viceversa has some costs). The transfer from the GPU to the CPU is an operation called pixel transfer.

Some examples:

  • You can load an image (a bitmap from a file for example) and use it as a texture. What are you doing here? Load on the CPU an image from the disk (a matrix of pixel), then pass it to the GPU and store it in a texture buffer. Then you can say to OpenGL to render a cube with that texture.

  • You can render some complex scene to a render buffer (off-screen render) in the GPU, load it in the CPU though a transfer pixel operation, and save it into a file.

  • You can render a scene into a texture (render to texture), then change the camera parameters, render to screen, and use the texture previously rendered as texture in a mirror object.

The operation on the GPU are faster then the ones on the CPU. I think (but I'm not sure) that operation on textures are a little bit slower that operation on render buffer (because for example mipmap scaling that a texture could have and a render buffer not).

I think that for a background of the scene you have to use a texture (or some shaders that loads pixels from a buffer).

So, in the end, you don't have image object in OpenGL. You have buffers. In which you can store data (transfer from CPU to GPU), you can render inside of them (off screen render), you can read pixels (pixel transfer from GPU to CPU). Some of them can be used as a texture. That means some more things like that they can be scaled in different resolution with mipmap scaling and scaled with some gaussian operation, they can be antialiased in some different ways, clamped, etc..

Hope this helps a little bit. Sorry if there are some mistakes but I'm learning OpenGL too.

Upvotes: 4

Kevin
Kevin

Reputation: 2730

In terms of OpenGL an image is an array of pixel data in RAM. You can for instance load a smiley.tga in RAM using standard C functions, that would be an image. A texture is when the imagedata is loaded in video memory by OpenGL. This can be done like this:

GLuint *texID;
glGenTextures(1, (GLuint*)&texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imagedata);

After the image has been loaded into video memory, the original imagedata in RAM can be free()ed. The texture can now be used by OpenGL.

Upvotes: 6

Related Questions