问题描述
我有图像数据,我想获得一个子图像用作opengl纹理。
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);
如何获取作为纹理加载的图像的子图像。我认为它有关于使用glTexSubImage2D,但我没有线索如何使用它来创建一个新的纹理,我可以加载。调用:
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);
doe没有什么我可以看到,调用glCopyTexSubImage2D只是我的framebuffer的一部分。
感谢
doe nothing that i can see, and calling glCopyTexSubImage2D just takes part of my framebuffer.Thanks
推荐答案
编辑:使用glPixelStorei。您可以使用它将整个图像的宽度(以像素为单位)设置为 GL_UNPACK_ROW_LENGTH
。然后你调用glTexImage2D(或任何),传递一个指向子图像的第一个像素的指针和子图像的宽度和高度。
Use glPixelStorei. You use it to set GL_UNPACK_ROW_LENGTH
to 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.
不要忘记恢复 GL_UNPACK_ROW_LENGTH
更改为0。
Don't forget to restore GL_UNPACK_ROW_LENGTH
to 0 when you're finished with it.
Ie:
glPixelStorei( GL_UNPACK_ROW_LENGTH, img_width );
char *subimg = (char*)m_data + (sub_x + sub_y*img_width)*4;
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sub_width, sub_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, subimg );
glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
或者,如果您对指针数学过敏:
Or, if you're allergic to pointer maths:
glPixelStorei( GL_UNPACK_ROW_LENGTH, img_width );
glPixelStorei( GL_UNPACK_SKIP_PIXELS, sub_x );
glPixelStorei( GL_UNPACK_SKIP_ROWS, sub_y );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sub_width, sub_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_data );
glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
Edit2:为了完整起见,你使用OpenGL-ES,那么你不会得到 GL_UNPACK_ROW_LENGTH
。在这种情况下,您可以(a)自己将子图像提取到新缓冲区中,或者(b)...
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)...
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sub_width, sub_height, 0, GL_RGBA, GL_UNSIGNED_BYTES, NULL );
for( int y = 0; y < sub_height; y++ )
{
char *row = m_data + ((y + sub_y)*img_width + sub_x) * 4;
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, y, sub_width, 1, GL_RGBA, GL_UNSIGNED_BYTE, row );
}
这篇关于openGL SubTexturing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!