PDA

View Full Version : how to save rendering to BMP file



huangzl99
09-05-2001, 06:15 PM
Hi all,

DOse anybody know where I can download c/c++ source code that save the rendering scene to a disk BMP file?

anybody can give some hints on this topic?

many thanks

Jerry

Humus
09-05-2001, 06:27 PM
Here's a piece of code that I've used in an old project to do that:




bool screenshot(char *fileName){
static unsigned char header[54] = {
0x42, 0x4D, 0x36, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char *pixels = (unsigned char *) malloc(Xres * Yres * 3);
((unsigned __int16 *) header)[ 9] = Xres;
((unsigned __int16 *) header)[11] = Yres;

glReadPixels(0,0,Xres,Yres,GL_RGB,GL_UNSIGNED_BYTE ,pixels);

unsigned char temp;
for (unsigned int i = 0; i < Xres * Yres * 3; i += 3){
temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}

HANDLE FileHandle;
unsigned long Size;

if (fileName == NULL){
char file[256];
i = 0;
do {
sprintf(file,"Screenshot%d.bmp",i);
FileHandle = CreateFile(file,GENERIC_WRITE,0,NULL,CREATE_NEW,FI LE_ATTRIBUTE_NORMAL,NULL);
i++;
} while (FileHandle == INVALID_HANDLE_VALUE);
} else {
FileHandle = CreateFile(fileName,GENERIC_WRITE,0,NULL,CREATE_AL WAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (FileHandle == INVALID_HANDLE_VALUE) return false;
}

WriteFile(FileHandle,header,sizeof(header),&amp;Size,N ULL);
WriteFile(FileHandle,pixels,Xres * Yres * 3,&amp;Size,NULL);

CloseHandle(FileHandle);

free(pixels);
return true;
}

huangzl99
09-06-2001, 08:05 AM
Originally posted by Humus:
Here's a piece of code that I've used in an old project to do that:




bool screenshot(char *fileName){
static unsigned char header[54] = {
0x42, 0x4D, 0x36, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char *pixels = (unsigned char *) malloc(Xres * Yres * 3);
((unsigned __int16 *) header)[ 9] = Xres;
((unsigned __int16 *) header)[11] = Yres;

glReadPixels(0,0,Xres,Yres,GL_RGB,GL_UNSIGNED_BYTE ,pixels);

unsigned char temp;
for (unsigned int i = 0; i < Xres * Yres * 3; i += 3){
temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}

HANDLE FileHandle;
unsigned long Size;

if (fileName == NULL){
char file[256];
i = 0;
do {
sprintf(file,"Screenshot%d.bmp",i);
FileHandle = CreateFile(file,GENERIC_WRITE,0,NULL,CREATE_NEW,FI LE_ATTRIBUTE_NORMAL,NULL);
i++;
} while (FileHandle == INVALID_HANDLE_VALUE);
} else {
FileHandle = CreateFile(fileName,GENERIC_WRITE,0,NULL,CREATE_AL WAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (FileHandle == INVALID_HANDLE_VALUE) return false;
}

WriteFile(FileHandle,header,sizeof(header),&amp;Size,N ULL);
WriteFile(FileHandle,pixels,Xres * Yres * 3,&amp;Size,NULL);

CloseHandle(FileHandle);

free(pixels);
return true;
}


thank you very much. I think I will learn much from it.