I work on a simple project just to write the depth and color information into image files,I programming under w2k platform with VC++ 6,when i use glrOrtho() instead of gluPerspective() to define viewing volume,i can not write the depth information correctly into image file,i can only see black and white region, moreover, the model’s color i displayed is blue, after i wrote into image file and displayed it again, it show like green, i don’t know why,
below is part of my source code of how to write:
void CWriteFileView::ReadWriteImgFile(CString strImgFile, CString strDepFile)
{
// glPixelStorei(GL_UNPACK_ALIGNMENT,1);
//IMG_WIDTH and IMG_HEIGHT has been deined with 256
glReadPixels((Width-IMG_WIDTH)/2,(Height-IMG_HEIGHT)/2,IMG_WIDTH,IMG_HEIGHT,GL_DEPTH_COMPONENT,GL_FLOAT,pDepthOut);
glReadPixels((Width-IMG_WIDTH)/2,(Height-IMG_HEIGHT)/2,IMG_WIDTH,IMG_HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,pImageOut);
for(int i=0;i<IMG_HEIGHT;i++)
for(int j=0;j<IMG_WIDTH;j++)
{
float depth;
if(pImageOut[3*(i*IMG_WIDTH+j)]==0)
depth = 255.0;
else
depth=Zfar*Znear/(Zfar-(Zfar-Znear)*pDepthOut[i*IMG_WIDTH+j]);
pDepImageOut[3*(i*IMG_WIDTH+j)]=(GLubyte)depth;
pDepImageOut[3*(i*IMG_WIDTH+j)+1]=(GLubyte)depth;
pDepImageOut[3*(i*IMG_WIDTH+j)+2]=(GLubyte)depth;
}
WriteToFile(strImgFile,pImageOut);
WriteToFile(strDepFile,pDepImageOut);
}
void CWriteFileView::WriteToFile(CString strFile, BYTE *pImgOut)
{
CFile rgbImage(strFile, CFile::modeCreate | CFile::modeWrite);
bfh.bfType='MB';
bfh.bfSize=sizeof(bfh)+WIDTHBYTES(24*IMG_WIDTH)*IMG_HEIGHT;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
bfh.bfOffBits=sizeof(bfh)+sizeof(bih);
bih.biSizeImage=WIDTHBYTES(24*IMG_WIDTH)*IMG_HEIGHT;
rgbImage.Write((BYTE* )&bfh,sizeof(bfh));
bih.biBitCount=24;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=IMG_HEIGHT;
bih.biPlanes=1;
bih.biSize=sizeof(bih);
bih.biWidth=IMG_WIDTH;
bih.biXPelsPerMeter=3780;
bih.biYPelsPerMeter=3780;
rgbImage.Write((GLubyte* )&bih,sizeof(bih));
rgbImage.Write((GLubyte* )pImgOut+sizeof(bih), bih.biSizeImage-sizeof(bih));
rgbImage.Close();
}