PDA

View Full Version : about render 32bit alpha tga format image!



leeriver
08-25-2005, 11:57 PM
hi all!
I write opengl demo, save tga images, my demo save is 24bit no alpha tga images, i want to my demo render 32bit alpha tga images, who can help me,how!
thanks!

environment: ms vc + mfc + opengl

my demo a part source:
//////////////////////////////////

void CMainFrame::OnRendTga()
{
int nFrames=100; /*帧数*/
GLubyte *FBuffer = NULL; /* Memory for the framebuffer */
static GLubyte magic[12]= {0,0,2,0,0,0,0,0,0,0,0,0}; /* Targa header */
GLubyte info[6]; /* Width and Height Targa header info */
int WinW, WinH; /* Width and Height of the current window */
FILE *Outfile; /* The file to be written to disk */
char FileName[13]; /* Name of file: SCR#####.TGA */
static int FileNum = 1; /* Number to fill in ##### in FileName above */

#if (!defined(GL_BGR)) /* For compatibility with both NT and Suns */
#define GL_BGR 0x80E0
#endif
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;
////////////////////////////////////////////////
// glReadBuffer(GL_FRONT); /* Be sure to read from the displayed window */
glPixelStorei(GL_PACK_ALIGNMENT,1);
WinW =cx; // glutGet((GLenum)GLUT_WINDOW_WIDTH); /* Get dimensions of window */
WinH =cy; //glutGet((GLenum)GLUT_WINDOW_HEIGHT);
/* Allocate space for the current window's framebuffer */
if ((FBuffer = (GLubyte *)malloc(WinW * WinH * 3)) != NULL)
{
glReadBuffer(GL_BACK); //读背景缓冲
for(int i=0;i<nFrames;i++)
{
pView->DrawGL();
/* Set the name for the file and open it for writing*/
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_RGB,GL_UNSIG NED_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);
}

////////////////end
E-MAIL:ying7970@hotmail.com

Hlz
08-26-2005, 07:57 AM
http://www.wotsit.org/search.asp?page=9&s=graphics

You can find TGA file format stuff with a quick search.

jasso
08-27-2005, 11:51 AM
Make sure you're reading and writing 32-bit data. GL_RGB will only give you 24 bits -- try GL_RGBA with glReadPixels() to get the alpha values. Then you'll have to play around with pixel formats to determine if you need to use ABGR or BGRA for writing your image.

leeriver
08-30-2005, 06:12 PM
thanks two guy!

I try modification under source succeed save 32bit alpha tga images. ~_~

////////////////////
SetupPixelFormat()
32 //Select Our Color Depth
1 //No Alpha Buffer
32 //Depth Buffe

// Allocate space for the current window's framebuffer
FBuffer = (GLubyte *)malloc(4*weidth*height);

FBuffer = (GLubyte *)malloc(4*weidth*height);
// Read the image data from the framebuffer
glReadPixels(0,0,weidth,height,(GLenum)GL_RGBA,GL_ UNSIGNED_BYTE,FBuffer);

fwrite(FBuffer,1,weidth*height*4,Outfile); // Data