-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
270 lines (227 loc) · 7.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
** PixelMachine
** v 0.2.20071108
**
** A ray-traced 3D renderer by SuperJer
**
** Email = [email protected]
** Web = http://www.superjer.com/
**
**
**
*/
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <memory.h>
#include <time.h>
#define NICEME (0)
#ifdef unix
# include <sys/time.h>
# include <sys/resource.h>
# undef NICEME
# define NICEME (setpriority(PRIO_PROCESS,0,10))
#endif
#ifdef WIN32
# include <windows.h>
# undef NICEME
# define NICEME (SetPriorityClass(GetCurrentProcess(),BELOW_NORMAL_PRIORITY_CLASS))
#endif
#include "pixelmachine.h"
#include "sjui.h"
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define SWAP(t,a,b) {t SWAP_temp = a;a = b;b = SWAP_temp;}
#define W 800
#define H 600
#define MULTIS 2
#define THREADS 8
#define PHOTONS 0
Uint32 gutime = 0;
Uint32 gutimenow = 0;
SDL_Surface *sdlscreen = NULL;
SDL_Surface *sdlrender = NULL;
SDL_Thread *thread;
const SDL_VideoInfo *pinfo;
PIXELMACHINE *pixelmachine;
SJUI ui;
SJUI_HANDLE h_menu,h_render;
int run_pixel_machine( void *data );
void Render();
void SetVideo( const int nw, const int nh );
bool InRect( const SDL_Rect *porect, const int nx, const int ny );
void Cleanup();
int main( int argc, char* argv[] )
{
SDL_Event event;
Uint32 u;
Uint32 v;
int i;
int w = W;
int h = H;
int multis = MULTIS;
int threads = THREADS;
int photons = PHOTONS;
unsigned seed = (unsigned)-1;
bool preview = true;
NICEME;
// Process cmd line args
for(i=1; i<argc; i++)
{
if( argv[i][0]=='-' ) switch( argv[i][1] )
{
case 'w': w = atoi(argv[i]+2); break;
case 'h': h = atoi(argv[i]+2); break;
case 'm': multis = atoi(argv[i]+2); break;
case 't': threads = atoi(argv[i]+2); break;
case 'p': photons = atoi(argv[i]+2); break;
case '-': printf( "Usage: [OPTION]... [SEED]\nRender ray-traced 3D images generated randomly with seed number SEED.\n\n option default description\n -wNUM %7d Set image output width to NUM\n -hNUM %7d Set image output height to NUM\n -mNUM %7d Set multisampling level to NUM (level 2 recommended)\n -tNUM %7d Parallelize with NUM threads\n -pNUM %7d Simulate lighting with NUM million photons!\n",W,H,MULTIS,THREADS,PHOTONS ), exit(0);
default: fprintf( stderr, "Halt! -%c isn't one of my options!\nUse --help for help.\n", argv[i][1] ), exit(-1);
}
else if( seed==(unsigned)-1 )
{
seed = atoi(argv[i]);
if( !seed )
fprintf( stderr, "Halt! SEED ought to be a positive number, not %s\n", argv[i] ), exit(-1);
}
else
fprintf( stderr, "Halt! I'm confused by cmd line argument #%d: %s\n", i, argv[i] ), exit(-1);
}
if( w<1 ) w=W;
if( h<1 ) h=H;
if( multis<1 ) multis=MULTIS;
if( threads<1 ) threads=THREADS;
if( photons<1 ) photons=PHOTONS;
// Use time as seed if not otherwise specified
if( seed==(unsigned)-1 )
seed = (unsigned)time(NULL);
// Init SDL
if( SDL_Init( SDL_INIT_TIMER|SDL_INIT_AUDIO|SDL_INIT_VIDEO ) < 0 || !SDL_GetVideoInfo() )
return 0;
// Create Window
SetVideo( w, h );
SDL_WM_SetCaption("PixelMachine", NULL);
pinfo = SDL_GetVideoInfo();
// font init
SJF_Init(pinfo);
//ui setup
ui.init();
h_menu = ui.new_control(0,0,0,0);
h_render = ui.new_control(h_menu,80,15,SJUIF_EXTENDSV);
ui.set_caption(h_menu,"Click to render...");
ui.set_caption(h_render,"Loading");
//pm preview render setup
pixelmachine = new PIXELMACHINE;
pixelmachine->init(seed,100,75,1,threads,(photons?1:0));
thread = SDL_CreateThread(run_pixel_machine,NULL);
// MAIN LOOP
while( 1 )
{
while( SDL_PollEvent(&event) ) switch(event.type)
{
case SDL_VIDEOEXPOSE:
;
break;
case SDL_VIDEORESIZE:
SetVideo( event.resize.w, event.resize.h );
break;
case SDL_MOUSEBUTTONDOWN:
if( preview )
{
//pm full render go!
preview = false;
pixelmachine->cancel = true;
SDL_WaitThread(thread,NULL); //BUG: thread may not be valid?
delete pixelmachine;
pixelmachine = new PIXELMACHINE;
pixelmachine->init(seed,w,h,multis,threads,photons);
thread = SDL_CreateThread(run_pixel_machine,NULL);
}
break;
case SDL_KEYDOWN:
;
break;
case SDL_QUIT:
pixelmachine->cancel = true;
SDL_WaitThread(thread,NULL); //BUG: thread may not be valid?
delete pixelmachine;
Cleanup();
break;
}
gutimenow = SDL_GetTicks();
if( (Uint32)(gutimenow-gutime)>50 )
{
Render();
gutime = gutimenow;
}
// chill out for a bit
SDL_Delay(10);
}
return 0;
}
int run_pixel_machine( void *data )
{
pixelmachine->run();
return 0;
}
void Render()
{
int i,j,m,n;
const SDL_VideoInfo *info;
SDL_Rect rect;
Uint32 color;
int winw;
int winh;
double ratiox;
double ratioy;
info = SDL_GetVideoInfo();
winw = info->current_w;
winh = info->current_h;
if( sdlrender )
{
if( !SDL_LockSurface(sdlrender) )
{
while( pixelmachine->pop_region(&rect) )
for(i=rect.x; i<rect.x+rect.w; i++)
for(j=rect.y; j<rect.y+rect.h; j++)
{
Uint8 b = pixelmachine->img[(i+j*pixelmachine->w)*3+0];
Uint8 g = pixelmachine->img[(i+j*pixelmachine->w)*3+1];
Uint8 r = pixelmachine->img[(i+j*pixelmachine->w)*3+2];
ratiox = (double)winw/(double)pixelmachine->w;
ratioy = (double)winh/(double)pixelmachine->h;
for(m=(int)(i*ratiox);m<(int)((i+1)*ratiox);m++)
for(n=(int)(j*ratioy);n<(int)((j+1)*ratioy);n++)
SDL_SetPixel(sdlrender,m,n,r,g,b);
}
SDL_UnlockSurface(sdlrender);
}
SDL_BlitSurface(sdlrender,NULL,sdlscreen,NULL);
}
ui.set_caption(h_render,pixelmachine->statustext);
ui.paint(sdlscreen);
SDL_Flip(sdlscreen);
}
void SetVideo( int nw, int nh )
{
bool different = (!pinfo || nw!=pinfo->current_w || nh!=pinfo->current_h);
sdlscreen = SDL_SetVideoMode( nw, nh, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_RESIZABLE|SDL_DOUBLEBUF );
pinfo = SDL_GetVideoInfo();
if( different && sdlrender )
SDL_FreeSurface(sdlrender);
if( different )
sdlrender = SDL_CreateRGBSurface(0,nw,nh,pinfo->vfmt->BitsPerPixel,pinfo->vfmt->Rmask,pinfo->vfmt->Gmask,pinfo->vfmt->Bmask,pinfo->vfmt->Amask);
}
bool InRect( const SDL_Rect *porect, const int nx, const int ny )
{
return ( nx>=porect->x && nx<(porect->x+porect->w) &&
ny>=porect->y && ny<(porect->y+porect->h) );
}
void Cleanup()
{
ui.destroy();
SDL_Quit();
exit(0);
}