OpenGL: Copy PixelBuffers to GL Texture

NOTE: some source is omitted, but should be relatively easy to re-enter.

1. Create the pixel buffer

EGLint configAttributes[] = {
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
//EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
EGL_LUMINANCE_SIZE, EGL_DONT_CARE,
EGL_SURFACE_TYPE, SURFACE_TYPE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE,
EGL_NONE
};

EGLint pbufferAttributes[] = {
EGL_WIDTH, width,
EGL_HEIGHT, height,
EGL_COLORSPACE, COLOR_SPACE,
EGL_ALPHA_FORMAT, ALPHA_FORMAT,
EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
EGL_NONE
};

eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(eglDisplay, &major, &minor);
eglChooseConfig(eglDisplay, configAttributes,
&eglConfig, 1, &numConfigs);
eglContext = eglCreateContext(eglDisplay,
eglConfig, NULL, NULL);
eglSurface = eglGetCurrentSurface(EGL_DRAW);
eglPbuffer = eglCreatePbufferSurface(eglDisplay,
eglConfig, pbufferAttributes);

2. Create empty pbuffer texture (theSource) to render GL onto

glGenTextures(1, &theSource);
glBindTexture(GL_TEXTURE_2D, theSource);
glTexImage2D(GL_TEXTURE_2D, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

3. Drawing to the pixel buffer surface

eglMakeCurrent(eglDisplay, pbuffer, pbuffer, eglContext);
glBindTexture(GL_TEXTURE_2D, theSource);
// draw code, glDraw*, etc
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

4. Creating the target texture (theCopy)

glGenTextures(1, &theCopy);
glBindTexture(GL_TEXTURE_2D, theCopy);
glTexImage2D(GL_TEXTURE_2D, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

5. Copying the pbuffer to the tagret texture (theCopy)

eglBindTexImage(eglDisplay, pbuffer, EGL_BACK_BUFFER);
glActiveTexture(GL_TEXTURE0 + theCopy);
glBindTexture(GL_TEXTURE_2D, theCopy);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 256, 256, 0);

1 comment:

Indicator Veritatis said...

What version of OpenGL, OpenGL ES, and EGL is this supposed to work for?