|
|
几乎所有的三维动画软件和二维合成软件都能渲染输出成TGA图片格式,TGA文件格式非常多且比较复杂,网上也有些零碎的相关代码,下面的我在自己的项目中的应用。
功能:在切分窗口中显示一些特效,如烟,火,水,等,然后能把这些交果渲染成TGA图片格式和AVI格式。下面只讲如何渲染成TGA文件格式,渲染出来的是24位不带通道的tga图片序列。我主要是通过下面几步实现,希望本文对大家有用。
题目:把OPENGL动画渲染成TGA图片格式
作者:李英江
环境:WIN2000 - VC - MFC - OpenGL
第一步: 获得视口的指针
GLView_Left_Up* pView=(GLView_Left_Up *)GetActiveView();
int cx=0, cy=0;
CRect rect;
pView->GetClientRect(&rect); //获得视口宽和高
CSize size(rect.Width(), rect.Height());
cx=(size.cx/2)*2;
cy=(size.cy/2)*2;
第二步: 设定渲染尺寸
WinW =cx; //宽
WinH =cy; //高
第三步: 分配内存
if ((FBuffer = (GLubyte *)malloc(WinW * WinH * 3)) != NULL)
第四步: 读背景缓冲
glReadBuffer(GL_BACK); //读背景缓冲 !这很重要
第五步: 渲染帧数
for(int i=0;i<nFrames;i++)
{
pView->DrawGL(); //调用Opengl画图函数
sprintf(FileName,"SCR%05d.TGA",FileNum++);
if ((Outfile = fopen(FileName,"wb")) == NULL)
{
fprintf(stderr,"Can't save to the file %s!\n",FileName);
fflush(stderr); /* Make sure output shows up */
}
else
{
/* Read the image data from the framebuffer */
glReadPixels(0,0,WinW,WinH,(GLenum)GL_BGR,GL_UNSIGNED_BYTE,FBuffer);
/* Write the new BGR framebuffer data to the Targa file */
fwrite(magic,1,sizeof(magic),Outfile); /* Header */
info[0] = (GLubyte)(WinW % 256);
info[1] = (GLubyte)(WinW / 256);
info[2] = (GLubyte)(WinH % 256);
info[3] = (GLubyte)(WinH / 256);
info[4] = 24;
info[5] = 0;
fwrite(info,1,sizeof(info),Outfile); /* Width/Height header */
fwrite(FBuffer,1,WinW*WinH*3,Outfile); /* Data */
fclose(Outfile);
}
} //end nFrames
free(FBuffer);
/**************完**************/
本人对OPENGL非常感兴趣,希望大家多多交流。
我的邮箱是:ying7970@hotmail.com QQ: 153865155
|
|