1 | #include "dxstdafx.h"
|
---|
2 | #include "Parameters.h"
|
---|
3 |
|
---|
4 | float noconvert(float a) {
|
---|
5 | return a;
|
---|
6 | }
|
---|
7 |
|
---|
8 | float convertNSamples(float a)
|
---|
9 | {
|
---|
10 | return 8 * (int)(a * 256);
|
---|
11 | }
|
---|
12 |
|
---|
13 | float convertDepthResolution(float a)
|
---|
14 | {
|
---|
15 | return 2 << (int)(a * 8);
|
---|
16 | }
|
---|
17 |
|
---|
18 | float convert100(float a)
|
---|
19 | {
|
---|
20 | return a * a * a * a * a * 99.0f + 1.0f;
|
---|
21 | }
|
---|
22 |
|
---|
23 | float convert10(float a)
|
---|
24 | {
|
---|
25 | return a * 9.8f + 0.2f;
|
---|
26 | }
|
---|
27 |
|
---|
28 | float convert1(float a)
|
---|
29 | {
|
---|
30 | return a * 0.9f + 0.1f;
|
---|
31 | }
|
---|
32 |
|
---|
33 | void OnChange() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | void Parameters::Setup( CDXUTDialog* g_HUD ) {
|
---|
37 | Setup(g_HUD, OnChange, OnChange, OnChange);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void Parameters::Setup( CDXUTDialog* g_HUD, ONCHANGE_CALLBACK OnReset, ONCHANGE_CALLBACK OnSave, ONCHANGE_CALLBACK OnLoad )
|
---|
41 | {
|
---|
42 | assert( g_HUD != NULL );
|
---|
43 | this->g_HUD = g_HUD;
|
---|
44 | g_HUD->GetDefaultElement( DXUT_CONTROL_STATIC, 0 )->dwTextFormat = DT_VCENTER | DT_LEFT;
|
---|
45 |
|
---|
46 | for (int i=0; i<LAST_NUMBER; i++) {
|
---|
47 | ffunc[i] = noconvert;
|
---|
48 | chfunc[i] = OnChange;
|
---|
49 | }
|
---|
50 |
|
---|
51 | int posX = 380;
|
---|
52 | int posY = 50 + LAST_BOOL*16;
|
---|
53 | g_HUD->AddButton( IDC_RESET_BUTTON, L"[R] Reset", posX, posY += 50, 60, 16, 'R' );
|
---|
54 | g_HUD->AddButton( IDC_SAVE_BUTTON, L"[X] Save", posX + 70, posY, 60, 16, 'X' );
|
---|
55 | g_HUD->AddButton( IDC_LOAD_BUTTON, L"[L] Load", posX + 140, posY, 60, 16, 'L' );
|
---|
56 | // g_HUD->AddButton( IDC_GEN_BUTTON, L"[G] Regenerate", posX - 250, posY, 100, 16, 'L' );
|
---|
57 | chfunc[ LAST_NUMBER ] = OnReset;
|
---|
58 | chfunc[ LAST_NUMBER+1 ] = OnSave;
|
---|
59 | chfunc[ LAST_NUMBER+2 ] = OnLoad;
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool Parameters::Get( bool_t i ) {
|
---|
63 | return bparam[i];
|
---|
64 | }
|
---|
65 |
|
---|
66 | float Parameters::Get( number_t i ) {
|
---|
67 | //assert( ffunc[i] != NULL );
|
---|
68 | return ffunc[i]( (float)param[i] / numsteps[i] ); // 0..1
|
---|
69 | }
|
---|
70 |
|
---|
71 | int Parameters::GetInt( number_t i ) {
|
---|
72 | //assert( ffunc[i] != NULL );
|
---|
73 | float r = ffunc[i]( (float)param[i] / numsteps[i] );
|
---|
74 | return (int)(r+0.5); // 0..numsteps[i]
|
---|
75 | }
|
---|
76 |
|
---|
77 | void Parameters::Add( bool_t ID, char* label, char cHotKey ) {
|
---|
78 |
|
---|
79 | assert( cHotKey != 'R' && cHotKey != 'S' && cHotKey != 'L' );
|
---|
80 | mbstowcs( bname[ID], label, CHARBUFFER_SIZE );
|
---|
81 |
|
---|
82 | int posX = 380;
|
---|
83 | int posY = 50;
|
---|
84 | CDXUTCheckBox* c;
|
---|
85 | g_HUD->AddCheckBox( ID+checkboxID0, bname[ID], posX, posY + ID*16, 150, 16, false, cHotKey, 0 );
|
---|
86 | // checked=false
|
---|
87 | }
|
---|
88 |
|
---|
89 | void Parameters::SetBool( bool_t ID, bool b ) {
|
---|
90 | bparam[ID] = b;
|
---|
91 |
|
---|
92 | bSilent = true;
|
---|
93 | if( g_HUD->GetCheckBox( ID+checkboxID0 ) != NULL )
|
---|
94 | g_HUD->GetCheckBox( ID+checkboxID0 )->SetChecked( bparam[ID] );
|
---|
95 | bSilent = false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void Parameters::SetFloat( number_t ID, float v ) {
|
---|
99 | SetInt( ID, (int)( v*numsteps[ID] + 0.5 ));
|
---|
100 | }
|
---|
101 |
|
---|
102 | void Parameters::SetInt( number_t ID, int v ) {
|
---|
103 | param[ID] = v;
|
---|
104 | if ( param[ID] < 0 ) param[ID] = 0;
|
---|
105 | else if ( param[ID] > numsteps[ID] ) param[ID] = numsteps[ID];
|
---|
106 |
|
---|
107 | bSilent = true;
|
---|
108 |
|
---|
109 | // minden frissül
|
---|
110 | if( g_HUD->GetSlider( ID+sliderID0 ) != NULL )
|
---|
111 | g_HUD->GetSlider( ID+sliderID0 )->SetValue( v );
|
---|
112 |
|
---|
113 | wchar_t text[30];
|
---|
114 | _sntprintf( text, 30, L"%.2f", Get( (number_t)ID ) );
|
---|
115 |
|
---|
116 | if( g_HUD->GetStatic( ID+staticID0 ) != NULL )
|
---|
117 | g_HUD->GetStatic( ID+staticID0 )->SetText( text );
|
---|
118 |
|
---|
119 | // user-defined callback
|
---|
120 | //assert( chfunc[ID] != NULL );
|
---|
121 | chfunc[ID]();
|
---|
122 |
|
---|
123 | bSilent = false;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void Parameters::Add( number_t ID, char* label, int num_steps) {
|
---|
127 | Add( ID, label, num_steps, noconvert );
|
---|
128 | }
|
---|
129 |
|
---|
130 | void Parameters::Add( number_t ID, char* label, int num_steps, CONVERTER ff, ONCHANGE_CALLBACK chf ) {
|
---|
131 | char cKeyDown = 0;
|
---|
132 | char cKeyUp = 0;
|
---|
133 | Add( ID, label, num_steps, cKeyDown, cKeyUp, ff, chf);
|
---|
134 | }
|
---|
135 |
|
---|
136 | void Parameters::Add( number_t ID, char* label, int num_steps, char cKeyDecr, char cKeyIncr, CONVERTER ff, ONCHANGE_CALLBACK chf ) {
|
---|
137 |
|
---|
138 | assert( cKeyDecr != 'R' && cKeyDecr != 'S' && cKeyDecr != 'L' );
|
---|
139 | assert( cKeyIncr != 'R' && cKeyIncr != 'S' && cKeyIncr != 'L' );
|
---|
140 |
|
---|
141 | mbstowcs( name[ID], label, CHARBUFFER_SIZE );
|
---|
142 | /*wchar_t val[30];
|
---|
143 | _sntprintf( val, 30, L"%.2f", param[ID] / 100.0f ); // nem kell */
|
---|
144 |
|
---|
145 | //cHotKey
|
---|
146 | ffunc[ID] = ff;
|
---|
147 | chfunc[ID] = chf;
|
---|
148 | numsteps[ID] = num_steps;
|
---|
149 |
|
---|
150 | int posX = 210;
|
---|
151 | int posY = 50;
|
---|
152 | g_HUD->AddStatic( 0, name[ID], posX-180, posY + ID*16, 120, 14 );
|
---|
153 | g_HUD->AddStatic( ID+staticID0, L"", posX-50, posY + ID*16, 120, 14 );
|
---|
154 | g_HUD->AddSlider( ID+sliderID0, posX, posY + ID*16, 120, 14, 0, num_steps );
|
---|
155 | //UpdateFromHUD( ID+sliderID0 );
|
---|
156 |
|
---|
157 | // hogy ne kelljen azt eltárolni, hogy melyik hotkey melyik sliderhez tartozik,
|
---|
158 | // 2 láthatatlan checkboxot hozok létre ezekkel a hotkeyekkel.
|
---|
159 | // a checkbox id alapján azonosíthatom az érintett slidert.
|
---|
160 | CDXUTCheckBox* c;
|
---|
161 | g_HUD->AddCheckBox( ID+downID0, L"d", posX-20, posY + ID*16, 200, 16, 0, cKeyDecr, 0, &c );
|
---|
162 | c->m_bVisible = false;
|
---|
163 | g_HUD->AddCheckBox( ID+upID0, L"u", posX-20, posY + ID*16, 200, 16, 0, cKeyIncr, 0, &c );
|
---|
164 | c->m_bVisible = false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | void Parameters::UpdateFromHUD( int controlID ) {
|
---|
168 |
|
---|
169 | //if (bSilent) return;
|
---|
170 |
|
---|
171 | // Reset, Save & Load buttons
|
---|
172 | if (controlID == IDC_RESET_BUTTON ) {
|
---|
173 | LoadFromFile( ".params0" );
|
---|
174 | chfunc[ LAST_NUMBER ]();
|
---|
175 | }
|
---|
176 | else if (controlID == IDC_SAVE_BUTTON ) {
|
---|
177 | SaveToFile( ".params" );
|
---|
178 | chfunc[ LAST_NUMBER+1 ]();
|
---|
179 | }
|
---|
180 | else if (controlID == IDC_LOAD_BUTTON ) {
|
---|
181 | LoadFromFile( ".params" );
|
---|
182 | chfunc[ LAST_NUMBER+2 ]();
|
---|
183 | }
|
---|
184 |
|
---|
185 | // a checkbox
|
---|
186 | else if (controlID >= checkboxID0 && controlID < checkboxID0+LAST_BOOL) {
|
---|
187 | bool b = g_HUD->GetCheckBox( controlID )->GetChecked();
|
---|
188 | int ID = controlID - checkboxID0;
|
---|
189 | SetBool( (bool_t)ID, b);
|
---|
190 | }
|
---|
191 |
|
---|
192 | // a slider
|
---|
193 | else if (controlID >= sliderID0 && controlID < sliderID0+LAST_NUMBER) {
|
---|
194 | int v = g_HUD->GetSlider( controlID )->GetValue();
|
---|
195 | int ID = controlID - sliderID0;
|
---|
196 | SetInt( (number_t)ID, v);
|
---|
197 | }
|
---|
198 |
|
---|
199 | // a hotkey (bound to an invisible checkbox) that decrements a slider
|
---|
200 | else if (controlID >= downID0 && controlID < downID0+LAST_NUMBER) { // down
|
---|
201 | int ID = controlID-downID0;
|
---|
202 | SetInt( (number_t)ID, param[ID]-1 );
|
---|
203 | }
|
---|
204 |
|
---|
205 | // a hotkey (bound to an invisible checkbox) that increments a slider
|
---|
206 | else if (controlID >= upID0 && controlID < upID0+LAST_NUMBER) { // up
|
---|
207 | int ID = controlID-upID0;
|
---|
208 | SetInt( (number_t)ID, param[ID]+1 );
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | void Parameters::SaveToFile( char* fileName ) {
|
---|
213 | FILE* fileS;
|
---|
214 | if( ( fileS = fopen( fileName, "wt" ) ) == NULL )
|
---|
215 | {
|
---|
216 | wchar_t wbuf[ CHARBUF ];
|
---|
217 | mbstowcs( wbuf, fileName, CHARBUF );
|
---|
218 | MessageBox( NULL, wbuf, L"File creation failed!", MB_ICONERROR ); // show error message
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 | fwprintf( fileS, L"----- RayTraceEffects: Saved parameters of the algorithm -----\n" );
|
---|
223 |
|
---|
224 | fwprintf( fileS, L"Bool values:\n" );
|
---|
225 | for ( int i=0; i<LAST_BOOL; i++)
|
---|
226 | fwprintf( fileS, L"%i", (int)bparam[i] );
|
---|
227 | fwprintf( fileS, L"\n" );
|
---|
228 |
|
---|
229 | fwprintf( fileS, L"(" );
|
---|
230 | for ( int i=0; i<LAST_BOOL; i++)
|
---|
231 | fwprintf( fileS, L"%s; ", bname[i] );
|
---|
232 | fwprintf( fileS, L")\n" );
|
---|
233 |
|
---|
234 | fwprintf( fileS, L"Float values:\n" );
|
---|
235 |
|
---|
236 | for ( int i=0; i<LAST_NUMBER; i++)
|
---|
237 | fwprintf( fileS, L"%i \t(%s, 0..%i)\n", param[i], name[i], numsteps[i] );
|
---|
238 |
|
---|
239 | fclose(fileS);
|
---|
240 | }
|
---|
241 |
|
---|
242 | void Parameters::LoadFromFile( char* fileName ) {
|
---|
243 | FILE* fileL;
|
---|
244 | if( ( fileL = fopen( fileName, "rt" ) ) == NULL )
|
---|
245 | {
|
---|
246 | wchar_t wbuf[ CHARBUF ];
|
---|
247 | mbstowcs( wbuf, fileName, CHARBUF );
|
---|
248 | MessageBox( NULL, wbuf, L"File not found!", MB_ICONERROR ); // show error message
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | char buf[ CHARBUF ];
|
---|
253 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
254 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
255 |
|
---|
256 | for ( int i=0; i<LAST_BOOL; i++)
|
---|
257 | {
|
---|
258 | int num = fgetc( fileL ) - '0';
|
---|
259 | SetBool( (bool_t)i, num > 0 );
|
---|
260 | }
|
---|
261 |
|
---|
262 | fgets( buf, CHARBUF, fileL ); // skip line
|
---|
263 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
264 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
265 |
|
---|
266 | for ( int i=0; i<LAST_NUMBER; i++)
|
---|
267 | {
|
---|
268 | fgets( buf, CHARBUF, fileL );
|
---|
269 | int v;
|
---|
270 | sscanf( buf, "%i", &v );
|
---|
271 | SetInt( (number_t)i, v );
|
---|
272 | }
|
---|
273 |
|
---|
274 | fclose(fileL);
|
---|
275 | } |
---|