1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #define VIDEO_WINMAIN_ARGS
51 #include "types.h"
52 #include "api.h"       /* The ray tracing library API */
53 #include "parse.h"     /* Support for my own file format */
54 #include "ui.h"
55 #include "util.h"
56 #include "tachyon_video.h"
57 #include "../../../common/utility/utility.h"
58 
59 #if WIN8UI_EXAMPLE
60 #include "tbb/tbb.h"
61 volatile long global_startTime = 0;
62 volatile long global_elapsedTime = 0;
63 volatile bool global_isCancelled = false;
64 volatile int global_number_of_threads;
65 #endif
66 
67 SceneHandle global_scene;
68 int global_xsize;     /*  size of graphic image rendered in window (from hres, vres)  */
69 int global_ysize;
70 int global_xwinsize;  /*  size of window (may be larger than above)  */
71 int global_ywinsize;
72 char *global_window_title;
73 bool global_usegraphics;
74 
75 bool silent_mode = false; /* silent mode */
76 
77 class tachyon_video *video = 0;
78 
79 typedef struct {
80   int foundfilename;      /* was a model file name found in the args? */
81   char filename[1024];    /* model file to render */
82   int useoutfilename;     /* command line override of output filename */
83   char outfilename[1024]; /* name of output image file */
84   int verbosemode;        /* verbose flags */
85   int antialiasing;       /* antialiasing setting */
86   int displaymode;        /* display mode */
87   int boundmode;          /* bounding mode */
88   int boundthresh;        /* bounding threshold */
89   int usecamfile;         /* use camera file */
90   char camfilename[1024]; /* camera filename */
91 } argoptions;
92 
initoptions(argoptions * opt)93 void initoptions(argoptions * opt) {
94     memset(opt, 0, sizeof(argoptions));
95     opt->foundfilename = -1;
96     opt->useoutfilename = -1;
97     opt->verbosemode = -1;
98     opt->antialiasing = -1;
99     opt->displaymode = -1;
100     opt->boundmode = -1;
101     opt->boundthresh = -1;
102     opt->usecamfile = -1;
103 }
104 
105 #if WIN8UI_EXAMPLE
CreateScene()106 int CreateScene() {
107 
108    char* filename = "Assets/balls.dat";
109 
110     global_scene = rt_newscene();
111     rt_initialize();
112 
113     if ( readmodel(filename, global_scene) != 0 ) {
114         rt_finalize();
115         return -1;
116     }
117 
118     // need these early for create_graphics_window() so grab these here...
119     scenedef *scene = (scenedef *) global_scene;
120 
121     // scene->hres and scene->vres should be equal to screen resolution
122     scene->hres = global_xwinsize = global_xsize;
123     scene->vres = global_ywinsize = global_ysize;
124 
125     return 0;
126 }
127 
example_main(void *)128 unsigned int __stdcall example_main(void *)
129 {
130     try {
131 
132         if ( CreateScene() != 0 )
133             exit(-1);
134 
135         tachyon_video tachyon;
136         tachyon.threaded = true;
137         tachyon.init_console();
138 
139         // always using window even if(!global_usegraphics)
140         global_usegraphics =
141             tachyon.init_window(global_xwinsize, global_ywinsize);
142         if(!tachyon.running)
143             exit(-1);
144 
145         video = &tachyon;
146 
147         for(;;) {
148             global_elapsedTime = 0;
149             global_startTime=(long) time(NULL);
150             global_isCancelled=false;
151             if (video)video->running = true;
152             tbb::global_control c(tbb::global_control::max_allowed_parallelism, global_number_of_threads);
153             memset(g_pImg, 0, sizeof(unsigned int) * global_xsize * global_ysize);
154             tachyon.main_loop();
155             global_elapsedTime = (long)(time(NULL)-global_startTime);
156             video->running=false;
157             //The timer to restart drawing then it is complete.
158             int timer=50;
159             while( (  !global_isCancelled && (timer--)>0 ) ){
160                 rt_sleep( 100 );
161             }
162         }
163         return NULL;
164 
165     } catch ( std::exception& e ) {
166         std::cerr<<"error occurred. error text is :\"" <<e.what()<<"\"\n";
167         return 1;
168     }
169 }
170 
171 #elif __TBB_IOS
172 
173 #include "tbb/tbb.h"
174 #include "CoreFoundation/CoreFoundation.h"
175 extern "C" void get_screen_resolution(int *x, int *y);
176 
CreateScene()177 int CreateScene() {
178 
179     CFURLRef balls_dat_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("balls"), CFSTR("dat"),NULL);
180     char filename[1024];
181     CFURLGetFileSystemRepresentation(balls_dat_url, true, (UInt8*)filename, (CFIndex)sizeof(filename));
182     CFRelease(balls_dat_url);
183 
184     global_scene = rt_newscene();
185     rt_initialize();
186 
187     if ( readmodel(filename, global_scene) != 0 ) {
188         rt_finalize();
189         return -1;
190     }
191 
192     // need these early for create_graphics_window() so grab these here...
193     scenedef *scene = (scenedef *) global_scene;
194 
195     get_screen_resolution(&global_xsize, &global_ysize);
196 
197     // scene->hres and scene->vres should be equal to screen resolution
198     scene->hres = global_xwinsize = global_xsize;
199     scene->vres = global_ywinsize = global_ysize;
200     return 0;
201 }
202 
main(int argc,char * argv[])203 int main (int argc, char *argv[]) {
204     try {
205 
206         if ( CreateScene() != 0 ) return -1;
207 
208         tachyon_video tachyon;
209         tachyon.threaded = true;
210         tachyon.init_console();
211 
212         global_usegraphics = tachyon.init_window(global_xwinsize, global_ywinsize);
213         if(!tachyon.running) return -1;
214 
215         //TODO: add a demo loop.
216         video = &tachyon;
217         if (video)video->running = true;
218         memset(g_pImg, 0, sizeof(unsigned int) * global_xsize * global_ysize);
219         tachyon.main_loop();
220         video->running=false;
221         return NULL;
222 
223     } catch ( std::exception& e ) {
224         std::cerr<<"error occurred. error text is :\"" <<e.what()<<"\"\n";
225         return 1;
226     }
227 }
228 
229 #else
230 
window_title_string(int argc,const char ** argv)231 static char *window_title_string (int argc, const char **argv)
232 {
233     int i;
234     char *name;
235 
236     name = (char *) malloc (8192);
237     char *title = getenv ("TITLE");
238     if( title ) strcpy( name, title );
239     else {
240         if(strrchr(argv[0], '\\')) strcpy (name, strrchr(argv[0], '\\')+1);
241         else if(strrchr(argv[0], '/')) strcpy (name, strrchr(argv[0], '/')+1);
242         else strcpy (name, *argv[0]?argv[0]:"Tachyon");
243     }
244     for (i = 1; i < argc; i++) {
245         strcat (name, " ");
246         strcat (name, argv[i]);
247     }
248 #ifdef _DEBUG
249     strcat (name, " (DEBUG BUILD)");
250 #endif
251     return name;
252 }
253 
useoptions(argoptions * opt,SceneHandle scene)254 int useoptions(argoptions * opt, SceneHandle scene) {
255   if (opt->useoutfilename == 1) {
256     rt_outputfile(scene, opt->outfilename);
257   }
258 
259   if (opt->verbosemode == 1) {
260     rt_verbose(scene, 1);
261   }
262 
263   if (opt->antialiasing != -1) {
264     /* need new api code for this */
265   }
266 
267   if (opt->displaymode != -1) {
268     rt_displaymode(scene, opt->displaymode);
269   }
270 
271   if (opt->boundmode != -1) {
272     rt_boundmode(scene, opt->boundmode);
273   }
274 
275   if (opt->boundthresh != -1) {
276     rt_boundthresh(scene, opt->boundthresh);
277   }
278 
279   return 0;
280 }
281 
ParseCommandLine(int argc,const char * argv[])282 argoptions ParseCommandLine(int argc, const char *argv[]) {
283     argoptions opt;
284 
285     initoptions(&opt);
286 
287     bool nobounding = false;
288     bool nodisp = false;
289 
290     string filename;
291 
292     utility::parse_cli_arguments(argc,argv,
293         utility::cli_argument_pack()
294         .positional_arg(filename,"dataset", "Model file")
295         .positional_arg(opt.boundthresh,"boundthresh","bounding threshold value")
296         .arg(nodisp,"no-display-updating","disable run-time display updating")
297         .arg(nobounding,"no-bounding","disable bounding technique")
298         .arg(silent_mode,"silent","no output except elapsed time")
299     );
300 
301     strcpy(opt.filename, filename.c_str());
302 
303     opt.displaymode = nodisp ? RT_DISPLAY_DISABLED : RT_DISPLAY_ENABLED;
304     opt.boundmode = nobounding ? RT_BOUNDING_DISABLED : RT_BOUNDING_ENABLED;
305 
306     return opt;
307 }
308 
CreateScene(argoptions & opt)309 int CreateScene(argoptions &opt) {
310     char *filename;
311 
312     global_scene = rt_newscene();
313     rt_initialize();
314 
315     /* process command line overrides */
316     useoptions(&opt, global_scene);
317 
318 #ifdef DEFAULT_MODELFILE
319 #if  _WIN32||_WIN64
320 #define _GLUE_FILENAME(x) "..\\dat\\" #x
321 #else
322 #define _GLUE_FILENAME(x) #x
323 #endif
324 #define GLUE_FILENAME(x) _GLUE_FILENAME(x)
325     if(opt.foundfilename == -1)
326         filename = GLUE_FILENAME(DEFAULT_MODELFILE);
327     else
328 #endif//DEFAULT_MODELFILE
329         filename = opt.filename;
330 
331     if ( readmodel(filename, global_scene) != 0 ) {
332         fprintf(stderr, "Parser returned a non-zero error code reading %s\n", filename);
333         fprintf(stderr, "Aborting Render...\n");
334         rt_finalize();
335         return -1;
336     }
337 
338     // need these early for create_graphics_window() so grab these here...
339     scenedef *scene = (scenedef *) global_scene;
340     global_xsize = scene->hres;
341     global_ysize = scene->vres;
342     global_xwinsize = global_xsize;
343     global_ywinsize = global_ysize;  // add some here to leave extra blank space on bottom for status etc.
344 
345     return 0;
346 }
347 
main(int argc,char * argv[])348 int main (int argc, char *argv[]) {
349     try {
350         timer mainStartTime = gettimer();
351 
352         global_window_title = window_title_string (argc, (const char**)argv);
353 
354         argoptions opt = ParseCommandLine(argc, (const char**)argv);
355 
356         if ( CreateScene(opt) != 0 )
357             return -1;
358 
359         tachyon_video tachyon;
360         tachyon.threaded = true;
361         tachyon.init_console();
362 
363         tachyon.title = global_window_title;
364         // always using window even if(!global_usegraphics)
365         global_usegraphics =
366             tachyon.init_window(global_xwinsize, global_ywinsize);
367         if(!tachyon.running)
368             return -1;
369 
370         video = &tachyon;
371         tachyon.main_loop();
372 
373         utility::report_elapsed_time(timertime(mainStartTime, gettimer()));
374         return 0;
375     } catch ( std::exception& e ) {
376         std::cerr<<"error occurred. error text is :\"" <<e.what()<<"\"\n";
377         return 1;
378     }
379 }
380 #endif
381 
382