BGE VideoTexture: add depth buffer access to ImageViewport and ImageRender.

2 new attributes to ImageViewport and ImageRender object:
depth: set to True to retrieve the depth buffer as an array of float
       (not suitable for texture source).
zbuff: set to True to retrieve the depth buffer as a grey scale pixel array
       (suitable for texture source).

A new mode 'F' is added to VideoTexture.imageToArray() to allow returning the image
buffer as a one dimensional array of float. This mode should only be used to retrieve
the depth buffer of ImageViewport and ImageRender objects.

Example:

viewport = VideoTexture.ImageViewport()
viewport.depth = True
depth = VideoTexture.imageToArray(viewport,'F')
# show depth of bottom left pixel
# 1.0 = infinite, 0.0 = on near clip plane.
print(depth[0])
This commit is contained in:
2012-10-20 22:28:44 +00:00
parent 7deb8d8a26
commit 4213eca5fc
7 changed files with 204 additions and 5 deletions

View File

@@ -50,6 +50,8 @@ ImageViewport::ImageViewport (void) : m_alpha(false), m_texInit(false)
//glGetIntegerv(GL_VIEWPORT, m_viewport);
// create buffer for viewport image
// Warning: this buffer is also used to get the depth buffer as an array of
// float (1 float = 4 bytes per pixel)
m_viewportImage = new BYTE [4 * getViewportSize()[0] * getViewportSize()[1]];
// set attributes
setWhole(false);
@@ -57,7 +59,9 @@ ImageViewport::ImageViewport (void) : m_alpha(false), m_texInit(false)
// destructor
ImageViewport::~ImageViewport (void)
{ delete [] m_viewportImage; }
{
delete [] m_viewportImage;
}
// use whole viewport to capture image
@@ -131,7 +135,7 @@ void ImageViewport::calcImage (unsigned int texId, double ts)
}
// if texture can be directly created
if (texId != 0 && m_pyfilter == NULL && m_capSize[0] == calcSize(m_capSize[0])
&& m_capSize[1] == calcSize(m_capSize[1]) && !m_flip)
&& m_capSize[1] == calcSize(m_capSize[1]) && !m_flip && !m_zbuff && !m_depth)
{
// just copy current viewport to texture
glBindTexture(GL_TEXTURE_2D, texId);
@@ -142,6 +146,32 @@ void ImageViewport::calcImage (unsigned int texId, double ts)
// otherwise copy viewport to buffer, if image is not available
else if (!m_avail)
{
if (m_zbuff)
{
// Use read pixels with the depth buffer
// *** misusing m_viewportImage here, but since it has the correct size
// (4 bytes per pixel = size of float) and we just need it to apply
// the filter, it's ok
glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1],
GL_DEPTH_COMPONENT, GL_FLOAT, m_viewportImage);
// filter loaded data
FilterZZZA filt;
filterImage(filt, (float *)m_viewportImage, m_capSize);
}
else
if (m_depth)
{
// Use read pixels with the depth buffer
// See warning above about m_viewportImage.
glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1],
GL_DEPTH_COMPONENT, GL_FLOAT, m_viewportImage);
// filter loaded data
FilterDEPTH filt;
filterImage(filt, (float *)m_viewportImage, m_capSize);
}
else
// get frame buffer data
if (m_alpha)
{
@@ -310,6 +340,8 @@ static PyGetSetDef imageViewportGetSets[] =
{(char*)"size", (getter)Image_getSize, NULL, (char*)"image size", NULL},
{(char*)"scale", (getter)Image_getScale, (setter)Image_setScale, (char*)"fast scale of image (near neighbor)", NULL},
{(char*)"flip", (getter)Image_getFlip, (setter)Image_setFlip, (char*)"flip image vertically", NULL},
{(char*)"zbuff", (getter)Image_getZbuff, (setter)Image_setZbuff, (char*)"use depth buffer as texture", NULL},
{(char*)"depth", (getter)Image_getDepth, (setter)Image_setDepth, (char*)"get depth information from z-buffer as array of float", NULL},
{(char*)"filter", (getter)Image_getFilter, (setter)Image_setFilter, (char*)"pixel filter", NULL},
{NULL}
};