Why i can't write the depth info into image file

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();

}

your calculated value for ‘depth’ should vary
between 0.0f-256.0f. At the moment I am unsure why your are using such a strange equation. the contents of pDepthOut should vary between 0-1 so just multiple pDepthOut result by 256.

[This message has been edited by stim (edited 06-04-2001).]

I use Znear and Zfar to define my clip plane,so I don’t think i should multiply 256.0 directly, do you think so?

stim is right. glReadPixels help:

GL_DEPTH_COMPONENT
Depth values are read from the depth buffer. Each component is converted to floating point such that the minimum depth value maps to 0.0 and the maximum value maps to 1.0. Each component is then multiplied by GL_DEPTH_SCALE, added to GL_DEPTH_BIAS, and finally clamped to the range [0,1].

yes,thanks,i have resolved it, there are two main reason :
first, I use gluOrtho(-1,1,-1,1,0,1) to define my view volume, i did not consider when the Zfar is converted to negative how should i do,
the second, stim is right.
thanks very much