Sunday, July 24, 2016

Gldrawpixels To Stencil Buffer

Gldrawpixels To Stencil Buffer

I'm creating a image using OpenCV which I would like to use as a stencil.  However, when I upload the image using glDrawPixels and then read it back using glReadPixels,  I only get a fraction of the image.   It is not obvious to me what is the problem since the alignment appears correct.  Help would be appreciated.  The code and images are below.

 

  

 
//---------------------------------------------------------------------
// createStencilBuffer
//---------------------------------------------------------------------
void RenderRadiance::createStencilBuffer(void)
{
cv::Mat stencilImg(_radDim.y,_radDim.x,CV_8UC1);
 
  stencilImg.setTo(0); 
 
  for (size_t i = 0;i < _hPntLst.size();i++)
  {
  HPnt  hPnt = _hPntLst[i];
 
    cv::circle(stencilImg,cv::Point(hPnt._hCen.x,hPnt._hCen.y),(int)_hogelDiameter/2,cv::Scalar(255),-1);
  }
  
  glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  glPixelStorei(GL_UNPACK_ROW_LENGTH,_radDim.x);
  glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
  glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
 
  glPixelStorei(GL_PACK_ALIGNMENT,1);
  glPixelStorei(GL_PACK_ROW_LENGTH,_radDim.x);
  glPixelStorei(GL_PACK_SKIP_ROWS,0);
  glPixelStorei(GL_PACK_SKIP_PIXELS,0);
 
  glGenRenderbuffers(1,&_renderStencilBuffer);
  glBindRenderbuffer(GL_RENDERBUFFER,_renderStencilBuffer);
  glRenderbufferStorage(GL_RENDERBUFFER,GL_STENCIL_INDEX,_radDim.x,_radDim.y);
 
  glDrawPixels(_radDim.x,_radDim.y,GL_STENCIL_INDEX,GL_UNSIGNED_BYTE,stencilImg.data);
 
  if (1)
  {
  cv::Mat stencilImgRead(_radDim.y,_radDim.x,CV_8UC1);
 
    glReadPixels(0,0,_radDim.x,_radDim.y,GL_STENCIL_INDEX,GL_UNSIGNED_BYTE,stencilImgRead.data);
 
    cv::imwrite("Stencil.png",stencilImg);
    cv::imwrite("StencilGPU.png",stencilImgRead);
  }
}
 

Attached Thumbnails

  • Stencil.png
  • StencilGPU.png

No comments:

Post a Comment