31 #if !defined(FRONTEND_DOES_SNAPSHOTS)
37 #include <libFreeWRL.h>
39 #include "../vrml_parser/Structs.h"
41 #include "../vrml_parser/CParseGeneral.h"
42 #include "../world_script/CScripts.h"
44 #include "../scenegraph/Collision.h"
45 #include "../scenegraph/quaternion.h"
46 #include "../scenegraph/Viewer.h"
47 #include "../input/SensInterps.h"
48 #include "../x3d_parser/Bindable.h"
80 #if defined(DOSNAPSEQUENCE)
89 const char *default_seqtmp;
96 void *Snapshot_constructor()
98 void *v = MALLOCV(
sizeof(
struct pSnapshot));
107 void Snapshot_init(
struct tSnapshot* t)
110 t->doSnapshot = FALSE;
112 t->prv = Snapshot_constructor();
119 #
if defined(DOSNAPSEQUENCE)
121 p->snapsequence=FALSE;
122 p->maxSnapImages=100;
128 p->default_seqtmp =
"freewrl_tmp";
130 p->doSnapshot = FALSE;
131 p->doPrintshot = FALSE;
132 p->savedSnapshot = FALSE;
133 p->modeTesting = FALSE;
141 void set_snapsequence(
int on)
143 #ifdef DOSNAPSEQUENCE
146 p->snapsequence = on;
149 #ifdef DOSNAPSEQUENCE
151 void saveSnapSequence();
153 void set_snapshotModeTesting(
int value)
156 p->modeTesting = value;
158 int isSnapshotModeTesting()
160 struct tSnapshot* t = &gglobal()->Snapshot;
162 return p->modeTesting;
165 void fwl_set_SeqFile(
const char* file)
167 #if defined(DOSNAPSEQUENCE)
171 p->snapseqB = STRDUP(file);
172 printf(
"snapseqB is %s\n", p->snapseqB);
174 WARN_MSG(
"Call to fwl_set_SeqFile when Snapshot Sequence not compiled in.\n");
178 void fwl_set_SnapFile(
const char* file)
182 p->snapsnapB = STRDUP(file);
183 TRACE_MSG(
"snapsnapB set to %s\n", p->snapsnapB);
184 printf(
"%s\n",p->snapsnapB);
187 void fwl_set_MaxImages(
int max)
189 #if defined(DOSNAPSEQUENCE)
196 p->maxSnapImages = max;
198 WARN_MSG(
"Call to fwl_set_MaxImages when Snapshot Sequence not compiled in.\n");
206 void fwl_set_SnapTmp(
const char* file)
210 tg->Snapshot.doSnapshot = FALSE;
216 p->seqtmp = STRDUP(file);
217 TRACE_MSG(
"seqtmp set to %s\n", p->seqtmp);
244 static char * grabScreen(
int bytesPerPixel,
int x,
int y,
int width,
int height)
249 if(bytesPerPixel == 3) pixelType = GL_RGB;
250 if(bytesPerPixel == 4) pixelType = GL_RGBA;
251 buffer = MALLOC (GLvoid *, bytesPerPixel*width*height*
sizeof(
char));
254 FW_GL_PIXELSTOREI (GL_UNPACK_ALIGNMENT, 1);
255 FW_GL_PIXELSTOREI (GL_PACK_ALIGNMENT, 1);
257 FW_GL_READPIXELS (x,y,width,height,pixelType,GL_UNSIGNED_BYTE, buffer);
262 #if defined( _MSC_VER) || defined (IPHONE)
264 void setSnapshot() {}
265 void fwl_toggleSnapshot(){}
266 void fwl_init_SnapGif(){}
267 void saveSnapSequence() {}
288 #define FDWORD unsigned long
290 #define FWORD unsigned short
291 #define FBYTE unsigned char
300 FDWORD biCompression;
302 FLONG biXPelsPerMeter;
303 FLONG biYPelsPerMeter;
305 FDWORD biClrImportant;
331 static void fromLong(
unsigned long myword,
char *buffer)
333 buffer[0] = (
unsigned char)((myword & 0x000000ff) >> 0);
334 buffer[1] = (
unsigned char)((myword & 0x0000ff00) >> 8);
335 buffer[2] = (
unsigned char)((myword & 0x00ff0000) >> 16);
336 buffer[3] = (
unsigned char)((myword & 0xff000000) >> 24);
340 static void fromShort(
unsigned short myword,
char *buffer)
342 buffer[0] = (myword & 0x00ff) >> 0;
343 buffer[1] = (myword & 0xff00) >> 8;
348 void saveSnapshotBMP(
char *pathname,
char *buffer,
int bytesPerPixel,
int width,
int height)
352 int rowlength, extra, alignedwidth, i;
357 char filler[3] = {
'\0',
'\0',
'\0'};
362 fout = fopen(pathname,
"w+b");
364 if(bytesPerPixel == 3) bi.biCompression = FBI_RGB;
365 bi.biHeight = height;
368 bi.biBitCount = 8 * bytesPerPixel;
369 bi.biXPelsPerMeter = 0;
370 bi.biYPelsPerMeter = 0;
374 rowlength = width*bytesPerPixel;
375 extra = 4 - (rowlength % 4);
376 if(extra == 4) extra = 0;
377 alignedwidth = rowlength + extra;
380 bi.biSizeImage = alignedwidth * height;
381 bi.biSize =
sizeof(bi);
383 bi.biClrImportant = 0;
389 memcpy(&bfType,
"BM",2);
390 bmph.bfReserved1 = 0;
391 bmph.bfReserved2 = 0;
394 fwrite(&bfType,
sizeof(bfType),1,fout);
402 for(i=0;i<rowlength*height;i+=3)
405 buffer[i] = buffer[i+1];
414 fwrite(
"BM",2,1,fout);
416 bmph.bfOffBits = 2 + 12 + bi.biSize;
417 bmph.bfSize = bmph.bfOffBits + bi.biSizeImage;
418 bmph.bfReserved1 = 0;
419 bmph.bfReserved2 = 0;
421 fromLong(bmph.bfSize, &buf[0]);
422 fromShort(bmph.bfReserved1, &buf[4]);
423 fromShort(bmph.bfReserved2, &buf[6]);
424 fromLong(bmph.bfOffBits, &buf[8]);
425 fromLong(bi.biSize, &buf[12]);
427 fromLong(bi.biWidth, &buf[16]);
428 fromLong(bi.biHeight, &buf[20]);
429 fromShort(bi.biPlanes, &buf[24]);
430 fromShort(bi.biBitCount, &buf[26]);
431 fromLong(bi.biCompression, &buf[28]);
432 fromLong(bi.biSizeImage, &buf[32]);
433 fromLong(bi.biXPelsPerMeter, &buf[36]);
434 fromLong(bi.biYPelsPerMeter, &buf[40]);
435 fromLong(bi.biClrUsed, &buf[44]);
436 fromLong(bi.biClrImportant, &buf[48]);
437 fwrite(buf,52,1,fout);
443 for(i=0;i<rowlength*height;i+=3)
446 buffer[i] = buffer[i+2];
452 for(i=0;i<height;i++)
455 fwrite(&buffer[j],rowlength,1,fout);
457 fwrite(filler,extra,1,fout);
464 int fw_mkdir(
const char* path);
476 char thisRawFile[2000];
477 char *mysnapb, *mytmp;
481 imgbuf = grabScreen(3,0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight);
482 if (p->snapsnapB == NULL)
483 mysnapb =
"freewrl.snap";
485 mysnapb = p->snapsnapB;
487 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
488 else mytmp = p->seqtmp;
492 snprintf(thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.bmp", mytmp, mysnapb, p->snapRawCount);
493 saveSnapshotBMP(thisRawFile, imgbuf, 3, gglobal()->display.screenWidth, gglobal()->display.screenHeight);
496 void Snapshot1(
char *fname){
498 imgbuf = grabScreen(3,0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight);
499 saveSnapshotBMP(fname, imgbuf, 3, gglobal()->display.screenWidth, gglobal()->display.screenHeight);
503 #if !(defined(_MSC_VER) || defined(IPHONE))
505 void fwl_init_SnapGif()
512 void fwl_init_PrintShot() {
515 p->doPrintshot = TRUE;
516 p->savedSnapshot = p->doSnapshot;
517 p->doSnapshot = TRUE;
518 printf(
"setting printshot/ snapshot\n");
522 void fwl_toggleSnapshot() {
523 struct tSnapshot* t = &gglobal()->Snapshot;
525 #ifdef DOSNAPSEQUENCE
529 if (!t->doSnapshot) {
530 t->doSnapshot = TRUE;
532 if (p->snapsequence) {
533 t->doSnapshot = FALSE;
538 t->doSnapshot = ! t->doSnapshot;
542 #ifdef DOSNAPSEQUENCE
546 void saveSnapSequence() {
547 char *mytmp, *myseqb;
549 char thisRawFile[2000];
550 char thisGoodFile[2000];
552 struct tSnapshot* t = (
struct tSnapshot*)gglobal()->Snapshot;
555 if (p->snapseqB == NULL) myseqb =
"freewrl.seq";
556 else myseqb = p->snapseqB;
557 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
558 else mytmp = p->seqtmp;
563 snprintf (thisGoodFile,
sizeof(thisGoodFile),
"%s/%s.%04d.gif",mytmp,myseqb,t->snapGoodCount);
565 snprintf (thisGoodFile,
sizeof(thisGoodFile),
"%s/%s.%04d.mpg",mytmp,myseqb,t->snapGoodCount);
572 snprintf(sysline,
sizeof(sysline),
"%s -size %dx%d -depth 24 -colorspace RGB +matte -flip %s/%s*rgb %s",
573 CONVERT, gglobal()->display.screenWidth, gglobal()->display.screenHeight,mytmp,myseqb,thisGoodFile);
577 if (system (sysline) != 0) {
578 printf (
"Freewrl: error running convert line %s\n",sysline);
580 printf (
"[1] snapshot is: %s\n",thisGoodFile);
582 for (xx=1; xx <= p->snapRawCount; xx++) {
583 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.rgb",mytmp,myseqb,xx);
584 UNLINK (thisRawFile);
645 char thisRawFile[2000];
649 char thisGoodFile[2000];
650 char *mytmp, *mysnapb;
661 struct tSnapshot* t = &gglobal()->Snapshot;
665 printf(
"do Snapshot ... \n");
668 #ifdef DOSNAPSEQUENCE
671 if (p->snapsequence) {
672 if (p->snapseqB == NULL)
673 mysnapb =
"freewrl.seq";
675 mysnapb = p->snapseqB;
678 if (p->snapsnapB == NULL)
679 mysnapb =
"freewrl.snap";
681 mysnapb = p->snapsnapB;
682 #ifdef DOSNAPSEQUENCE
689 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
690 else mytmp = p->seqtmp;
693 if ((mydir = opendir(mytmp)) == NULL) {
695 if ((mydir = opendir(mytmp)) == NULL) {
696 ConsoleMessage (
"error opening Snapshot directory %s\n",mytmp);
701 #ifdef DOSNAPSEQUENCE
705 if (!p->snapsequence) p->doSnapshot=FALSE;
722 buffer = MALLOC (GLvoid *, 3*gglobal()->display.screenWidth*gglobal()->display.screenHeight*
sizeof(
char));
725 FW_GL_PIXELSTOREI (GL_UNPACK_ALIGNMENT, 1);
726 FW_GL_PIXELSTOREI (GL_PACK_ALIGNMENT, 1);
727 FW_GL_READPIXELS (0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight,GL_RGB,GL_UNSIGNED_BYTE, buffer);
732 #ifdef DOSNAPSEQUENCE
735 if (p->snapRawCount > maxSnapImages) {
788 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.bmp",mytmp,mysnapb,p->snapRawCount);
789 saveSnapshotBMP(thisRawFile,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
792 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.rgb",mytmp,mysnapb,p->snapRawCount);
793 tmpfile = fopen(thisRawFile,
"w");
794 if (tmpfile == NULL) {
795 printf (
"cannot open temp file (%s) for writing\n",thisRawFile);
800 if (fwrite(buffer, 1, gglobal()->display.screenHeight*gglobal()->display.screenWidth*3, tmpfile) <= 0) {
801 printf (
"error writing snapshot to %s, aborting snapshot\n",thisRawFile);
811 #ifdef DOSNAPSEQUENCE
815 if (!p->snapsequence) {
819 snprintf (thisGoodFile,
sizeof(thisGoodFile),
"%s/%s.%04d.png",mytmp,mysnapb,t->snapGoodCount);
820 snprintf(sysline,
sizeof(sysline),
"%s -size %dx%d -depth 8 -flip %s %s",
821 IMAGECONVERT,gglobal()->display.screenWidth, gglobal()->display.screenHeight,thisRawFile,thisGoodFile);
823 if (system (sysline) != 0) {
824 printf (
"Freewrl: error running convert line %s\n",sysline);
826 printf (
"[2] snapshot is: %s\n",thisGoodFile);
827 UNLINK (thisRawFile);
829 #ifdef DOSNAPSEQUENCE