I have image data and i want to get a sub image of that to use as an opengl texture.
glGenTextures(1, &m_name); glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldName); glBindTexture(GL_TEXTURE_2D, m_name); 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_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_data);
How can i get a sub image of that image loaded as a texture. I think it has something to do with using glTexSubImage2D, but i have no clue how to use it to create a new texture that i can load. Calling:
glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, xWidth, yHeight, GL_RGBA, GL_UNSIGNED_BYTE, m_data);
does nothing that i can see, and calling glCopyTexSubImage2D just takes part of my framebuffer. Thanks
Edit: Use glPixelStorei. You use it to set
GL_UNPACK_ROW_LENGTHto the width (in pixels) of the entire image. Then you call glTexImage2D (or whatever), passing it a pointer to the first pixel of the subimage and the width and height of the subimage.Don’t forget to restore
GL_UNPACK_ROW_LENGTHto 0 when you’re finished with it.Ie:
Or, if you’re allergic to pointer maths:
Edit2: For the sake of completeness, I should point out that if you’re using OpenGL-ES then you don’t get
GL_UNPACK_ROW_LENGTH. In which case, you could either (a) extract the subimage into a new buffer yourself, or (b)…