about converting the content of the uiview to the texture, OpenGL es
I want to convert the content of the uiview, which is showing in the screen at present, to the OpenGL es textures. But, while i do this, i met some trouble. The size of the textures, processed by OpenGL es, is the size of the input image. While the texture is rendered to the screen, the size of the uiview, showing the content of the textures, has been changed.
So, if i directly read the content of the uiview, by following code, (_drawView belongs to the class of UIView)
//convert current uiview to texture, opengl es
GLubyte *pixelBuffer = (GLubyte *)malloc(4 * _drawView.bounds.size.width * _drawView.bounds.size.height);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context =
CGBitmapContextCreate(pixelBuffer,
_drawView.bounds.size.width, _drawView.bounds.size.height,
8, 4* _drawView.bounds.size.width,
colourSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colourSpace);
NSLog(@"_drawView.bounds.size.width: %f", _drawView.bounds.size.width);
NSLog(@"_drawView.bounds.size.height: %f", _drawView.bounds.size.height);
[_drawView.layer renderInContext:context];
glGenBuffers(1, &_maskTexture);
glBindTexture(GL_TEXTURE_2D, _maskTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0,
GL_RGBA,
_drawView.bounds.size.width, _drawView.bounds.size.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);
CGContextRelease(context);
free(pixelBuffer);
_drawView.hidden = YES;
Then, the size of _maskTexture is not the same with the input image. But i need the data saved in _maskTexture to do some computation in shader function, with the texture of the input image. But, they are not in the same size. How could i solve this?
One problem in your code is that you call glGenBuffers
, which generates an object name for a buffer. You then use this object name for a glBindTexture
call, which needs the name of a texture. You need to use glGenTextures
instead to get a valid object name for your texture.
上一篇: 在OpenGL ES中渲染“图层”