Debian Codegraph @

src/CgUtil.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #include <GL/glew.h>
2 #include "CgUtil.h"
3
4 #if defined(_WIN32)
5 #include <windows.h>
6 #else
7 #include <string.h> // for strlen
8 #endif
9
10 //#include <GL/gl.h>
11 #include <stdio.h>
12
13 #include "HardSettings.h"
14
15 #include "MyCanvas.h"
16
17 extern int CSIZE;
18 extern int BSIZE;
19 //extern GeoSettings geoSettings;
20
21 void CgUtil::Set(){
22 }
23
24 bool CgUtil::UseHalo(){
25 return P_halo_size * P_halo_str >0;
26 }
27
28 bool CgUtil::can_use_doubleshadow(){
29 return ((P_light_base==0.0) && (hardSettings.doubleSM) );
30 }
31
32 bool CgUtil::do_use_doubleshadow(){
33 return ((P_double_shadows) && (can_use_doubleshadow()));
34 }
35
36 static char* FORMAT="void CgUtil::Set(int K){\nif (K==0){\n P_light_base = %f ;\n P_lighting = %f ;\n P_phong = %f ;\n P_phong_size = %f ;\n P_col_atoms_sat = %f ;\n P_col_atoms_bri = %f ;\n P_texture = %f ;\n P_border_inside = %f ;\n P_border_outside = %f ;\n P_depth_full = %f ;\n P_sem_effect = %d ;\n P_halo_size = %f ;\n P_halo_col = %f ;\n P_halo_str = %f ;\n P_halo_aware = %f ;\n P_fog = %f ;\n P_capping = %d ;\n P_shadowstrenght = %f ;\n P_bg_color_R = %f ;\n P_bg_color_G = %f ;\n P_bg_color_B = %f ;\n auto_normalize = %d ;\n P_double_shadows = %d ;\n P_border_fixed = %d ;\n}\n}";
37
38 bool CgUtil::Load(const char* filename){
39 FILE *f=fopen(filename, "rt");
40 if (!f) return false;
41 fscanf(f,FORMAT
42 ,&P_light_base,&P_lighting,&P_phong,&P_phong_size,&P_col_atoms_sat,&P_col_atoms_bri,&P_texture,&P_border_inside,&P_border_outside,&P_depth_full,&P_sem_effect,&P_halo_size,&P_halo_col,&P_halo_str,&P_halo_aware,&P_fog,&P_capping,&P_shadowstrenght,&P_bg_color_R,&P_bg_color_G,&P_bg_color_B,&auto_normalize,&P_double_shadows,&P_border_fixed);
43 fclose(f);
44 return true;
45 }
46
47 bool CgUtil::Save(const char* filename){
48 FILE *f=fopen(filename, "wt");
49 if (!f) return false;
50 fprintf(f,FORMAT
51 ,P_light_base,P_lighting,P_phong,P_phong_size,P_col_atoms_sat,P_col_atoms_bri,P_texture,P_border_inside,P_border_outside,P_depth_full,P_sem_effect,P_halo_size,P_halo_col,P_halo_str,P_halo_aware,P_fog,P_capping,P_shadowstrenght,P_bg_color_R,P_bg_color_G,P_bg_color_B,auto_normalize,P_double_shadows,P_border_fixed);
52 fclose(f);
53 return true;
54 }
55
56
57 float CgUtil::_border_outside(){
58 return P_border_outside*0.075;
59 }
60
61 float CgUtil::_border_inside(){
62 return P_border_inside*0.5;
63 }
64
65 void CgUtil::setGeoSettings(const GeoSettings &gs){
66 if (gs.mode==GeoSettings::BALL_N_STICKS) {
67 P_cyl_const_color=gs.use_stick_const_color;
68 P_cyl_smooth_color=gs.stick_smooth_color;
69 P_cyl_const_color_R = gs.stick_const_color_R;
70 P_cyl_const_color_G = gs.stick_const_color_G;
71 P_cyl_const_color_B = gs.stick_const_color_B;
72 }
73 if (gs.mode==GeoSettings::LICORICE) {
74 P_cyl_const_color=false;
75 P_cyl_smooth_color=false;
76 }
77 }
78
79 CgUtil::CgUtil() {
80 loaded=false; idf=666; idv=666; auto_normalize=false; norm=1;
81 loadedVertexHalo=false;
82 ResetHalo();
83 proj_figa=false;
84 idfStick=idvStick=666; loadedStick=false;
85 idvHalo=666;
86 cyl_radius=0.2;
87 shadowmapBuilding=false;
88 accurateShadowmapBuilding=false;
89 doingAlphaSnapshot=false;
90
91 shadersMade=false;
92 }
93
94 void CgUtil::ResetHalo(){
95
96 for (int i=0; i<MAXPOW; i++) {
97 loadedHalo[i]=false;
98 idfHalo[i]=666;
99 }
100 }
101
102 char CgUtil::lasterr[255];
103
104 bool CgUtil::BindHaloShader(int pow){
105 if (idfHalo[pow]==666) glGenProgramsARB(1, &(idfHalo[pow]));
106 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idfHalo[pow]);
107
108 if (!loadedHalo[pow]) MakeHaloShader(pow);
109
110 if (idvHalo==666) glGenProgramsARB(1, &idvHalo);
111 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, idvHalo);
112 if (!loadedVertexHalo) LoadVertexHaloShader();
113
114 loadedHalo[pow]=true;
115 loadedVertexHalo=true;
116
117 return true;
118 }
119
120 void CgUtil::LoadVertexHaloShader(){
121 char vp[10096];
122
123 sprintf(vp,"\
124 !!ARBvp1.0\n\
125 \n\
126 ATTRIB pos = vertex.position;\n\
127 ATTRIB dataA = vertex.texcoord[0];\n\
128 ATTRIB dataB = vertex.texcoord[1];\n\
129 \n\
130 ");
131
132
133 sprintf(vp,"%s\
134 \n\
135 PARAM mat[4] = { state.matrix.mvp };\n\
136 PARAM matP[4] = { state.matrix.projection };\n\
137 \n\
138 TEMP p,po, disp, dataout, tmp;\n\
139 \n\
140 # Transform by concatenation of the\n\
141 # MODELVIEW and PROJECTION matrices.\n\
142 DP4 p.x, mat[0], pos;\n\
143 DP4 p.y, mat[1], pos;\n\
144 DP4 p.z, mat[2], pos;\n\
145 DP4 p.w, mat[3], pos;\n\
146 #MOV p.w, 1; \n\
147 \n\
148 MOV dataout, dataA;\n\
149 MOV dataout.z, dataB.y;\n\
150 ", vp);
151
152 sprintf(vp,"%s\
153 \n\
154 MUL disp, dataA, dataB.x; \n\
155 MUL disp, disp, program.env[0].x; \n\
156 #MUL disp.x, disp.x, matP[0].x;\n\
157 #MUL disp.y, disp.y, matP[1].y;\n\
158 MAD p, {1,1,0,0}, disp, p;\n\
159 ", vp);
160
161
162 sprintf(vp,"%sMOV result.position, p;\n",vp);
163
164 sprintf(vp,"%sMOV result.texcoord, dataout;\n",vp);
165
166 sprintf(vp,"%s\nEND\n", vp);
167
168 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(vp), vp);
169
170 //printf("\n--------<vp ball>---------\n%s",vp);
171 //if(!checkProgramError(vp)) return -1;
172 //return true;
173 }
174
175
176 bool CgUtil::MakeHaloShader(int pow){
177
178 char fp[10096];
179
180 sprintf(fp,"\
181 !!ARBfp1.0\n\
182 \n\
183 ATTRIB data = fragment.texcoord; \n\
184 \n\
185 TEMP tmp,tmp2,tmp3, t,t0,t1,t2,nor,n,nabs,nsign,disp,res,depth,pos,\n\
186 lighting; \n\
187 \n\
188 MOV nor, data; \n\
189 MUL tmp, data, data; \n\
190 ADD tmp2.x, tmp.x, tmp.y;\n\
191 ADD tmp2.z, -tmp2.x, 1;\n\
192 KIL tmp2.z;\n\
193 \n\
194 MAD tmp2.x, -data.z, tmp2.x, data.z;\n\
195 \n\
196 #TEST!\n\
197 #ADD tmp2.x, tmp2.x, %10.8f;\n\
198 #CMP result.color, tmp2.x, {1,0,0,1}, {0,0,1,1};\n\
199 \n\
200 MUL tmp, fragment.position, {%14.12f, %14.12f, 0, 0};\n\
201 #MAD tmp, fragment.position, {0.5, 0.5, 0, 0}, {0.5, 0.5, 0, 0};\n\
202 TEX tmp.z, tmp, texture[1], 2D; # tmp.z = old depth \n\
203 ADD tmp.z, tmp.z, -fragment.position.z; \n\
204 MUL_SAT tmp.z, tmp.z, program.env[0].x; \n\
205 MUL tmp.z, tmp.z, tmp2.x; \n\
206 MUL tmp.z, tmp.z, tmp2.x; # again for smoother edges\n\
207 ", +P_halo_str-1, 1.0f/(1<<pow), 1.0f/(1<<pow) );
208
209 if (P_halo_str<1.0) {
210 sprintf(fp,"%sMUL tmp.z , tmp.z, %10.8f;\n",fp, P_halo_str );
211 }
212 if (!doingAlphaSnapshot)
213 sprintf(fp,"%sMAD result.color, {0,0,0,1}, tmp.z, {%5.4f,%5.4f,%5.4f,0.0} ;\n",fp,P_halo_col,P_halo_col,P_halo_col);
214 else {
215 if (P_halo_col==1.0) // white halo
216 sprintf(fp,"%sMOV result.color, tmp.z;\n",fp);
217 else
218 sprintf(fp,"%sMUL result.color, {0,0,0,1}, tmp.z;\n",fp);
219 }
220 sprintf(fp,"%sEND\n",fp);
221
222
223
224
225 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
226 // printf("\n-------<fp HALO>--------\n%s",fp);
227 if(!checkProgramError(fp)) return false;
228 return true;
229 }
230
231 static void addDrawAOShaderSnippet(char* fp) {
232 sprintf(fp,"%s\n\
233 # Find shading value \n\
234 DP3 l.x, nor, -param; \n\
235 #MUL_SAT l.x, l.x, param.w; \n\
236 MUL l.x, l.x, param.w; \n\
237 #KIL l.x; \n\
238 ",fp);
239
240 if ( (!hardSettings.doubleSM) && (!hardSettings.NVIDIA_PATCH) ) {
241 sprintf(fp,"%s\n\
242 KIL l.x; # early KILL of fragments on the dark side...\n\
243 ",fp);
244 }
245
246 sprintf(fp,"%s\n\
247 # Project! \n\
248 DP4 pos.x, Smat0, origpos; \n\
249 DP4 pos.y, Smat1, origpos; \n\
250 DP4 pos.z, Smat2, origpos; \n\
251 ",fp);
252
253 if (hardSettings.doubleSM) sprintf(fp,"%s\n\
254 CMP tmp, l.x, {0.75,0.5,0.5,1}, {0.25,0.5,0.5,1};\n\
255 MAD pos, pos, {0.25,0.5,0.5,0}, tmp; \n\
256 \n\
257 ",fp); else
258 sprintf(fp,"%s\n\
259 MAD pos, pos, {0.5,0.5,0.5,0}, {0.5,0.5,0.5,1}; \n\
260 ",fp);
261
262 sprintf(fp,"%s\n\
263 # Access shadow map! \n\
264 TEX tmp.x, pos, texture[1], 2D;\n\
265 SUB l.z, tmp.x, pos.z; \n\
266 ",fp);
267
268 if (hardSettings.doubleSM) sprintf(fp,"%s\n\
269 CMP l.z, l.x, -l.z, l.z; \n\
270 CMP l.x, l.x, -l.x, l.x; # DOUBLE SIDE\n\
271 \n\
272 ",fp);
273
274 sprintf(fp,"%s\n\
275 # NVIDIA BUUUUGUUGUGUGUGUGUUGUUGGUGUUGUG GUUGUGUG GGFUCKFUCKFUCKFUCKFUCKFUCKFUCK!!! \n\
276 %s\
277 CMP result.color, l.z, 0, l.x; # <-- (shadow & shading) \n\
278 #CMP result.color, l.z, 0, 1; # <-- (TEST: only shadow - works) \n\
279 #CMP result.color, 1, 0, l.x; # <-- (TEST: only shading - works) \n\
280 # NVIDIA BUUUUGUUGUGUGUGUGUUGUUGGUGUUGUG GUUGUGUG GGFUCKFUCKFUCKFUCKFUCKFUCKFUCK!!! \n\
281 \n\
282 # TEST1: MAD result.color, {0.5,0.5,0.5,0},nor, {0.5,0.5,0.5,1};\n\
283 # TEST2: MAD result.color, {0.5,0.5,0.5,0},origpos, {0.5,0.5,0.5,1};\n\
284 # TEST3: CMP result.color, l.z, {1,0,0,1}, {0,0,0.5,1};\n\
285 # TEST4: MOV result.color, tmp.x;\n\
286 \n\
287 \n\
288 END\n\
289 ",
290 fp,
291 hardSettings.NVIDIA_PATCH?
292 "MUL l.x, 0.5, param.w; # <-- patch! REMOVE ME when N-VIDIA wakes up \n"
293 :""
294 );
295 }
296
297 bool CgUtil::MakeDrawAOShader(){
298 char fp[10096];
299 sprintf(fp,"\
300 !!ARBfp1.0 \n\
301 PARAM Smat0 = program.env[0];\n\
302 PARAM Smat1 = program.env[1];\n\
303 PARAM Smat2 = program.env[2];\n\
304 PARAM param = program.env[3];\n\
305 ATTRIB tc = fragment.texcoord; \n\
306 ATTRIB data = fragment.texcoord[1]; \n\
307 TEMP tmp,nor, pos,origpos, abs,l;\n\
308 \n\
309 # Find shpere normal... \n\
310 CMP abs, tc, -tc, tc;\n\
311 MAD nor, -abs, {1,1,0,0}, +1;\n\
312 CMP tmp.x, tc.x, -nor.y, nor.y; # tmp_i = sign_i*( 1-abs_i) \n\
313 CMP tmp.y, tc.y, -nor.x, nor.x; # tmp_i = sign_i*( 1-abs_i) \n\
314 ADD nor.z, abs.x, abs.y; \n\
315 ADD nor.z, nor.z, -1; \n\
316 CMP nor.x, -nor.z, tmp.x, tc.x;\n\
317 CMP nor.y, -nor.z, tmp.y, tc.y;\n\
318 # Normalize \n\
319 DP3 tmp.x, nor, nor; \n\
320 RSQ tmp.x, tmp.x; \n\
321 MUL nor, nor, tmp.x; \n\
322 \n\
323 # Find pos \n\
324 MAD origpos, data.w, nor, data;\n\
325 MOV origpos.w, 1;\n");
326
327 addDrawAOShaderSnippet(fp);
328
329 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
330 // printf("\n-------<fp special>--------\n%s",fp);
331 if(!checkProgramError(fp)) return false;
332 //ShowShaderInfo(GL_FRAGMENT_PROGRAM_ARB);
333 return true;
334 };
335
336 bool CgUtil::MakeDrawAOShaderSticks(){
337 char fp[10096];
338 sprintf(fp,"\
339 !!ARBfp1.0 \n\
340 PARAM Smat0 = program.env[0];\n\
341 PARAM Smat1 = program.env[1];\n\
342 PARAM Smat2 = program.env[2];\n\
343 PARAM param = program.env[3];\n\
344 PARAM radius = program.env[4];\n\
345 ATTRIB axispos= fragment.texcoord[1]; \n\
346 ATTRIB data = fragment.texcoord; \n\
347 TEMP tmp,n,nor, pos,origpos, abs,l;\n\
348 \n\
349 \n\
350 # find norm in cyl space \n\
351 MAD n.y, data.y, -2, 0; \n\
352 CMP n.y, n.y, -n.y, n.y; \n\
353 ADD n.x, 2, -n.y; \n\
354 MIN n.x, n.x, n.y; \n\
355 CMP n.x, data.y, n.x, -n.x; \n\
356 MAD n, n, {1,1,0,0}, {0,-1,0,0};\n\
357 \n\
358 # normalize \n\
359 DP3 tmp.x, n, n;\n\
360 RSQ tmp.x, tmp.x;\n\
361 MUL n, tmp.x, n;\n\
362 \n\
363 # rotate \n\
364 MUL nor, -n.x, fragment.texcoord[2];\n\
365 MAD nor, n.y, fragment.texcoord[3], nor;\n\
366 \n\
367 # find position \n\
368 MAD origpos, nor, radius.y, axispos; \n\
369 MOV origpos.w, 1;\n\
370 ");
371
372 addDrawAOShaderSnippet(fp);
373
374 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
375 // printf("\n-------<fp special>--------\n%s",fp);
376 if(!checkProgramError(fp)) return false;
377 //ShowShaderInfo(GL_FRAGMENT_PROGRAM_ARB);
378 return true;
379 };
380
381 void CgUtil::Normalize(){
382 /* if (!auto_normalize) return;
383 float sum=P_light_base + P_lighting + P_texture;
384 if (sum>1.0) {
385 norm=sum;
386 P_light_base/=norm;
387 P_lighting/=norm;
388 P_texture/=norm;
389 }*/
390 }
391
392
393 void CgUtil::UndoNormalize(){
394 //P_light_base*=norm;
395 P_lighting*=norm;
396 P_texture*=norm;
397 }
398
399 void CgUtil::SetDefaults(){
400 P_light_base=0.0;
401 P_lighting=0.9;
402 P_phong=0.0;
403 P_phong_size=0.75;
404
405 P_col_atoms_sat=0.5;
406 P_col_atoms_bri=1.0;
407
408 P_texture=0.0;
409 P_border_inside=0.0;
410
411 P_border_outside=0.0;
412 P_depth_full=0.5;
413
414 P_sem_effect=false;
415 //P_use_shadowmap=false;
416 P_shadowstrenght=0.0;
417 P_double_shadows=true;
418 P_fog=0;
419
420 P_bg_color_R=P_bg_color_G=P_bg_color_B=0.5;
421
422 // textmode=USE_CUBE;
423 textmode=USE_OCTA;
424 projmode=ORTHO;
425
426 writeAlpha=false;
427 writeCol=true;
428 gap =0.5;
429
430 P_capping=false;
431
432 P_halo_size=0.0; // 1.0;
433 P_halo_col =0.0; // 1.0;
434 P_halo_str =1.0; // 1.0;
435 P_halo_aware=0.5;
436
437 }
438
439 void CgUtil::SetForShadowMap(bool accurate){
440
441 P_light_base=0.0;
442 P_lighting=0.0;
443 P_phong=0.0;
444
445 P_col_atoms_sat=0.0;
446 P_col_atoms_bri=0.0;
447 P_texture=0.0;
448 P_border_inside=0.0;
449 P_border_outside=0.0;
450 P_sem_effect=false;
451 //P_use_shadowmap=false;
452 P_shadowstrenght=0.0;
453 P_fog=0;
454
455 projmode=ORTHO;
456
457 textmode=USE_OCTA;
458 writeAlpha=false;
459 writeCol=false;
460
461 P_capping=false;
462
463 P_halo_size=0.0;
464
465 shadowmapBuilding=true;
466 accurateShadowmapBuilding=accurate;
467 }
468
469 void CgUtil::SetForOffLine(){
470
471 P_light_base=0.0;
472 P_lighting=0.0;
473 P_phong=0.0;
474
475 P_col_atoms_sat=0.0;
476 P_col_atoms_bri=0.0;
477 P_lighting=0.0;
478 P_texture=1.0;
479 P_border_inside=0.0;
480 P_border_outside=0.0;
481 P_sem_effect=false;
482 //P_use_shadowmap=false;
483 P_shadowstrenght=0.0;
484 P_fog=0;
485
486 projmode=ORTHO;
487
488 textmode=USE_OCTA;
489
490 writeAlpha=true;
491 writeCol=true;
492
493 P_capping=false;
494
495 gap =0.2;
496
497 P_halo_size=0.0;
498 }
499
500 void CgUtil::BindShaders(){
501 if (!loaded) MakeShaders();
502 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idf);
503 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, idv);
504 }
505
506 void CgUtil::MakeShaders(){
507
508 if (shadersMade) return;
509
510 shadersMade=true;
511
512 if (idf==666) glGenProgramsARB(1, &idf);
513 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idf);
514 setBallFragmentProgram();
515
516 if (idv==666) glGenProgramsARB(1, &idv);
517 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, idv);
518 setBallVertexProgram();
519
520 loaded=true;
521
522 MakeStickShaders();
523
524 }
525
526 void CgUtil::BindStickShaders(){
527 if (!loadedStick) MakeStickShaders();
528 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idfStick);
529 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, idvStick);
530 }
531
532 void CgUtil::MakeStickShaders(){
533
534 if (idfStick==666) glGenProgramsARB(1, &idfStick);
535 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idfStick);
536 setStickFragmentProgram();
537
538 if (idvStick==666) glGenProgramsARB(1, &idvStick);
539 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, idvStick);
540 setStickVertexProgram();
541
542 loadedStick=true;
543
544 }
545
546 bool CgUtil::BindDrawAOShader(){
547 if (idf==666) glGenProgramsARB(1, &idf);
548 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idf);
549 if (!loaded) MakeDrawAOShader();
550 loaded=true;
551 return true;
552 }
553
554 bool CgUtil::BindDrawAOShaderSticks(){
555 if (idf==666) glGenProgramsARB(1, &idf);
556 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, idf);
557 if (!loaded) MakeDrawAOShaderSticks();
558 loaded=true;
559 return true;
560 }
561
562
563
564 bool CgUtil::setBallVertexProgram(){
565 char vp[10096];
566
567 // #####################
568 // # #
569 // # VERTEX PROGRAM #
570 // # #
571 // #####################
572
573 sprintf(vp,"\
574 !!ARBvp1.0\n\
575 \n\
576 ATTRIB pos = vertex.position;\n\
577 ATTRIB data = vertex.normal;\n\
578 \n\
579 ");
580
581 // DataOut = ( +- OutradX, +- OutRadY, InRad )
582
583 if (P_texture>0) sprintf(vp,"%s\
584 ATTRIB offset = vertex.texcoord;\n\
585 ", vp);
586
587 sprintf(vp,"%s\
588 \n\
589 PARAM mat[4] = { state.matrix.mvp };\n\
590 PARAM matP[4] = { state.matrix.projection };\n\
591 \n\
592 TEMP p,po, disp, dataout, tmp;\n\
593 \n\
594 # Transform by concatenation of the\n\
595 # MODELVIEW and PROJECTION matrices.\n\
596 DP4 p.x, mat[0], pos;\n\
597 DP4 p.y, mat[1], pos;\n\
598 DP4 p.z, mat[2], pos;\n\
599 DP4 p.w, mat[3], pos;\n\
600 #MOV p.w, 1; \n\
601 \n\
602 MOV dataout, data;\n\
603 MUL dataout.z, dataout.z, program.env[0].x;\n\
604 ", vp);
605
606 // Enlarge impostors to include borders
607 if (_border_outside()!=0) // Compute 'almost' radius and scale indep. border
608 sprintf(vp,"%s\
609 RSQ tmp.y, dataout.z ;\n\
610 #MUL tmp.y, tmp.y , tmp.y; # Comment to 'almost'\n\
611 MUL tmp.x, %7.5f , tmp.y;\n\
612 ADD dataout.w, tmp.x , 1;\n\
613 MUL dataout.xy, dataout, dataout.w ;\n\
614 MAD dataout.w, dataout.w, dataout.w, -1;\n\
615 ",
616 vp, _border_outside() );
617
618 sprintf(vp,"%s\
619 \n\
620 MUL disp, dataout, dataout.z; \n\
621 #MUL disp.x, disp.x, matP[0].x;\n\
622 #MUL disp.y, disp.y, matP[1].y;\n\
623 MAD p, {1,1,0,0}, disp, p;\n\
624 ", vp);
625
626
627 sprintf(vp,"%s\
628 \n\
629 MOV result.position, p;\n\
630 \n\
631 #MOV dataout.w, p.w;\n"
632 ,vp);
633
634 sprintf(vp,"%sMOV result.texcoord, dataout;\n",vp);
635
636 if ((P_col_atoms_sat>0)&&(P_col_atoms_bri>0)) sprintf(vp,"%sMOV result.color, vertex.color;\n",vp);
637
638 if (P_texture>0) sprintf(vp,"%sMOV result.texcoord[2], offset;\n",vp);
639
640 if (P_use_shadowmap() ) sprintf(vp,"%sMOV result.texcoord[3], vertex.position;\n",vp);
641
642 sprintf(vp,"%s\nEND\n", vp);
643
644 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(vp), vp);
645
646 //printf("\n--------<vp ball>---------\n%s",vp);
647 if(!checkProgramError(vp)) return -1;
648 return true;
649 }
650
651
652
653 void CgUtil::addShadowMapComputationFP(char* fp) {
654 sprintf(fp,"%s\n\
655 #SHADOWMAP\n\
656 \n\
657 #compute orig pos from attributes... MODE 1\n\
658 #MUL t0.x, data.z, ratio.x;\n\
659 #MAD pos, n, t0.x, origpos;\n\
660 #\n\
661 # ...or MODE 2!!! \n\
662 MAD posScreen, fragment.position, {1,1,0,0}, {0,0,0,1} ;\n\
663 MOV posScreen.z, depth.x;\n\
664 \n\
665 DP4 t0.x, Smat0, posScreen; \n\
666 DP4 t0.y, Smat1, posScreen; \n\
667 DP4 t0.z, Smat2, posScreen; \n\
668 ",fp);
669
670 if (do_use_doubleshadow()) sprintf(fp,"%s\n\
671 CMP t1, lighting.z, {0.75,0.5,0.5,1}, {0.25,0.5,0.5,1};\n\
672 MAD t0, t0, {0.25,0.5,0.5,0}, t1; \n\
673 \n\
674 ",fp); else {
675 double tmp=(hardSettings.doubleSM)?0.25:0.5;
676 sprintf(fp,"%s\n\
677 MAD t0, t0, {%4.2f,0.5,0.5,0}, {%4.2f,0.5,0.5,1}; \n\
678 ",fp,tmp,tmp);
679 }
680 /*
681 sprintf(fp,"%s\n\
682 MAD t0, t0, {0.5,0.5,0.5,0}, {0.5,0.5,0.4999999,0}; \n\
683 ",fp);
684 */
685 sprintf(fp,"%s\n\
686 # Access shadow map! \n\
687 TEX t1, t0, texture[1], 2D;\n\
688 ",fp);
689
690 sprintf(fp,"%s\n\
691 ADD t.z, -t1.z, t0.z; \n\
692 ",fp);
693
694 if (do_use_doubleshadow()) sprintf(fp,"%s\n\
695 CMP t.z, lighting.z, -t.z, t.z; \n\
696 \n\
697 ",fp);
698
699
700 if ((!do_use_doubleshadow())&&(P_light_base>0)) {
701 sprintf(fp,"%s\n\
702 CMP t.z, lighting.z, 1, t.z; # if light<0, then in shadow \n\
703 " ,fp,1.0-P_shadowstrenght);
704 }
705
706 if (P_shadowstrenght<1) {
707 sprintf(fp,"%s\n\
708 MUL tmp, lighting, %5.4f; # compute attenuated light \n\
709 CMP lighting, t.z, lighting, tmp; # if in shadow, then use attenuated light \n\
710 " ,fp,1.0-P_shadowstrenght);
711 }
712 else
713 sprintf(fp,"%s\n\
714 CMP lighting, t.z, lighting, 0; # if in shadow, then no light \n\
715 #CMP result.color, t.z, {0,1,0,0}, {1,0,0,0}; \n\
716 #\n\
717 #MAD t0, t0, {0.5,0.5, 200.0,0}, {0.5,0.5,196.5,0}; \n\
718 #TEX t1, t0, texture[1], 2D;\n\
719 #MAD t1, t1, 400, -3.5;\n\
720 #MUL t1, t1.z, {1,0,1,0};\n\
721 ##ADD t.z, -t1.z, t0.z; \n\
722 #MAD t0, t0.z, {0,1,0,0}, {0,0,0,0};\n\
723 #CMP result.color, mat0.x, t1, t0; \n\
724 #MUL result.color, {0.002,0.002,0,0}, posScreen; \n\
725 ",fp);
726
727 };
728
729 void CgUtil::addTexturingSnippet(char* fp){
730 if (P_texture>0) {
731 sprintf(fp,"%s%s",fp,"\n\n\
732 # texture access \n\
733 MAD t, t, TNORM, offset; \n\
734 TEX t, t, texture[0], 2D; \n\n");
735
736 if (P_capping) {
737 // overwrite ambient occlusion for close fragments
738 sprintf(fp,"%s\n\
739 \n\
740 # lighten OC for close frags \n\
741 MAD tmp.x, depth.x, -250, 0.50; \n\
742 CMP tmp.x, tmp.x, 0, tmp.x; \n\
743 # overwrite OC for cut \n\
744 CMP tmp.x, depth.x, 0.70, tmp.x; \n\
745 LRP t, tmp.x, 1, t; \n\
746 ", fp);
747
748 }
749
750 // Add "future" AO prediction (AO not computed yet)
751
752 // Additive prediction:
753 //sprintf(fp,"%sADD t, t, program.env[6].x;\n", fp );
754
755 // multiplicative prediction:
756 sprintf(fp,"%sMUL t, t, program.env[6].x;\n", fp );
757
758 sprintf(fp,"%sMAD res, %5.2f, t, res;\n", fp, P_texture );
759 if (P_phong>0.0) {
760 // weigth phong with AO light.
761 sprintf(fp, "%s\nMUL lighting.y,lighting.y, t;\n", fp);
762 }
763
764 }
765
766 float lighting = (!P_sem_effect) ? P_lighting : (1- P_lighting);
767
768
769 // apply lighting
770 if ( lighting>0 ) {
771 if (P_sem_effect) {
772 sprintf(fp,"%sMAD lighting.x, lighting.x, -1, 1 ;\n",fp );
773 // sprintf(fp,"%sMUL lighting.x, lighting.x, lighting.x ;\n",fp );
774 sprintf(fp,"%s\nMAD lighting.x, %10.8f, lighting.x, %10.8f;\n",fp, lighting, 1-lighting);
775 sprintf(fp,"%sMUL res, lighting.x, res;\n",fp );
776 }
777 else
778 sprintf(fp,"%s\nMAD res, lighting.x, %f, res;\n",fp, lighting);
779 }
780
781 if (P_col_atoms_sat>0) {
782 if ((P_col_atoms_sat<1)||(P_col_atoms_bri<1)) {
783 sprintf(fp,"%sMAD tmp, %5.3f, basecol,%5.3f;\n",fp
784 ,P_col_atoms_sat*P_col_atoms_bri, (1.0-P_col_atoms_sat)*P_col_atoms_bri );
785 sprintf(fp,"%s%s",fp,"MUL res, res, tmp;\n");
786 } else {
787 sprintf(fp,"%s%s",fp,"MUL res, res, basecol;\n");
788 }
789 } else {
790 if (P_col_atoms_bri<1.0)
791 {
792 sprintf(fp,"%s%s",fp,"MUL res, res, %5.3f;\n", P_col_atoms_bri );
793 }
794 }
795
796 if (writeCol) {
797 if (P_phong>0) {
798 sprintf(fp,"%s%s",fp,"LRP res, lighting.y, 1, res;\n");
799 }
800
801 if (writeAlpha)
802 sprintf(fp,"%s%s",fp,"MOV res.w, nor.z;\n");
803 }
804
805
806 // UNUSED:
807 if ( _border_inside()>0 ) {
808 sprintf(fp, "%s \n\
809 MAD tmp2.z, border.x, %f, 1; \n\
810 LRP tmp3, tmp2.z, 0, res;\n\
811 CMP res, -tmp2.z, tmp3, res;\n\
812 \n",
813 fp,
814 (1.0/_border_inside())
815 );
816 };
817
818 if ( _border_outside()>0 ) {
819 // Blackens borders:
820
821 #if (1)
822 // no AA:
823 sprintf(fp,"%sCMP res, -border.x, {0,0,0,0}, res;\n",fp);
824 #else
825 // internal AA:
826 //sprintf(fp,"%sMAD_SAT border.y, -border.x , 10, 1 ;\n",fp);
827 //sprintf(fp,"%sMUL res, border.y, res;\n",fp);
828 #endif
829 }
830
831 if ( P_fog>0 ) {
832 sprintf(fp,"%sMAD_SAT tmp.x, depth.x, 50, 0;\n",fp);
833 sprintf(fp,"%sMUL tmp.x, tmp.x, %5.4f;\n",fp, P_fog);
834 sprintf(fp,"%sLRP res, tmp.x, {%10.9f,%10.9f,%10.9f,1}, res;\n",fp, P_bg_color_R,P_bg_color_G,P_bg_color_B);
835 }
836
837 }
838
839 void CgUtil::addDepthAdjustSnippet(char* fp) {
840
841 float depth_full=P_depth_full*120.0f;
842
843 // DEPTH AWARE
844 if ( _border_outside()>0 ) {
845 sprintf(fp, "%s \n\
846 MUL tmp3.z, -border.x, data.z;\n\
847 MAD tmp3.z, %8.7f , tmp3.z , fragment.position.z;\n\
848 CMP depth.x, -border.x, tmp3.z, depth.x;\n\
849 \n",fp,
850 -depth_full/_border_outside() / 20000.0 //-0.001
851 );
852 }
853
854 if (P_capping) {
855
856 //
857 //sprintf(fp,"%s%s",fp, "MAD tmp.x, depth.x, 0.001, 10;\n");
858 //sprintf(fp,"%s%s",fp, "CMP result.depth, depth.x, tmp.x, depth.x;\n");
859 //
860
861 sprintf(fp,"%s%s",fp, "ADD result.depth, depth.x, 0.001;\n");
862 }
863 else {
864 // sprintf(fp,"%s%s",fp, "CMP result.depth, depth.x, 0, depth.x;\n");
865 sprintf(fp,"%s%s",fp, "MOV result.depth, depth.x;\n");
866 }
867
868 sprintf(fp,"%s%s",fp,"MUL res, res, t;\n" );
869
870 float lighting = (!P_sem_effect) ? P_lighting : (1- P_lighting);
871
872 if ((P_capping) && ( (lighting>0) || (P_phong>0) ) ) {
873 sprintf(fp,"%s\n\
874 # Overwrite capped normal \n\
875 CMP nor, depth.x, {0,0,1,0}, nor;\n",
876 fp );
877 }
878 }
879
880
881 void CgUtil::addDirectLightingSnippet(char* fp) {
882
883 /*
884 NOTE:
885 lighting.x = lambert direct (halved if opposite side)
886 lighting.y = phong direct (zeroed if opposite side)
887 lighting.z = lamber original (negative if opposite side)
888 */
889
890 float lighting = (!P_sem_effect) ? P_lighting : (1- P_lighting);
891
892 if (lighting>0) {
893 sprintf(fp,"%s%s",fp, " \n\n\
894 ## LIGHTING of Normal \n\
895 DP3 lighting.z, nor, LIGHT; \n\
896 MUL tmp.y, -lighting.z, 0.35; \n\
897 CMP lighting.x, lighting.z, tmp.y, lighting.z; \n\n"
898 );
899 }
900 if (P_phong>0.0) {
901 // phong
902 sprintf(fp, "%s\
903 ## PHONG \n\
904 #ADD hwv, {0,0,+1,0}, LIGHT;\n\
905 #DP3 lighting.y, hwv, hwv;\n\
906 #RSQ lighting.y, lighting.y;\n\
907 #MUL hwv, hwv, lighting.y;\n\
908 DP3 lighting.y, nor, hwv;\n", fp);
909
910 // compute exponent (TODO: use some sort of EXP funtion)
911 for (int i=0; i<(1.0-P_phong_size)*6.0+3; i++)
912 sprintf(fp,"%sMUL lighting.y, lighting.y, lighting.y;\n", fp);
913
914 if (P_phong<1.0)
915 sprintf(fp,"%s\nMUL lighting.y,%5.4f,lighting.y;\n", fp, P_phong);
916
917 }
918
919 if(P_light_base>0) {
920 sprintf(fp,"%s\nLRP lighting.x,%5.4f, 1, lighting.x; # flatten light \n", fp, P_light_base );
921 }
922
923 sprintf(fp,"%s\nMOV res, %5.4f;\n", fp, 0.0 );
924
925 }
926
927 bool CgUtil::shaderHasChanged=false;
928
929 bool CgUtil::setStickFragmentProgram(){
930
931 char fp[10096];
932
933 if (shadowmapBuilding) {
934 if (!accurateShadowmapBuilding)
935
936 sprintf(fp,"\
937 !!ARBfp1.0 \n\
938 \n\
939 MOV result.color, 1;\n\
940 END\n"
941 );
942 else
943 sprintf(fp,"\
944 !!ARBfp1.0 \n\
945 \n\
946 ATTRIB data = fragment.texcoord; \n\
947 MOV result.color, 0.8;\n\
948 MAD_SAT tmp.x, data.x, -data.x, 1; # tmp.z=1-x^2 \n\
949 RSQ tmp.x, tmp.x;\n\
950 RCP n.y, tmp.x;\n\
951 MAD tmp.z, n.y, data.z, 0; \n\
952 MAD result.depth, -tmp.z, 0.005, fragment.position.z; \n\
953 END\n");
954
955 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
956
957 //printf("\n-------<fp stick>--------\n%s",fp);
958
959 if(!checkProgramError(fp)) return false;
960 return true;
961 }
962
963 sprintf(fp,"\
964 !!ARBfp1.0\n\
965 \n\
966 ATTRIB data = fragment.texcoord; \n\
967 ATTRIB normcenter = fragment.texcoord[1];\n\
968 ATTRIB ROT = fragment.texcoord[2];\n\
969 ATTRIB offset = fragment.texcoord[3];\n\
970 ATTRIB normside = fragment.texcoord[4];\n");
971
972 if (!P_cyl_const_color) {
973 sprintf(fp,"%s\
974 ATTRIB col1 = fragment.color.primary;\n\
975 ATTRIB col2 = fragment.color.secondary;\n",fp);
976 }
977
978 sprintf(fp,"%s\
979 PARAM TNORM={%10.9f,%10.9f,0,0}; \n\
980 PARAM mat0 = program.env[0];\n\
981 PARAM mat1 = program.env[1];\n\
982 PARAM mat2 = program.env[2];\n",fp,
983 (moltextureCanvas.GetHardRes()==0)?0:1.0f/moltextureCanvas.GetHardRes(),
984 (moltextureCanvas.GetHardRes()==0)?0:1.0f/moltextureCanvas.GetHardRes()
985 );
986 if (P_use_shadowmap())
987 sprintf(fp,"%s\
988 PARAM Smat0 = program.env[3]; \n\
989 PARAM Smat1 = program.env[4]; \n\
990 PARAM Smat2 = program.env[5]; \n\
991 #PARAM ratio = program.env[6]; \n\
992 ATTRIB origpos = fragment.texcoord[4]; \n",fp);
993
994 sprintf(fp,"%s\n\
995 PARAM LIGHT= state.light[0].position ; \n\
996 PARAM hwv = state.light[0].half;\n\
997 \n\
998 TEMP tmp, nor, n, lighting, res, t, t0, t1, abs, posScreen; \n\
999 TEMP basecol, border, depth, tmp2, tmp3; \n\
1000 \n\
1001 \n\
1002 MAD_SAT tmp.x, data.x, -data.x, 1; # tmp.z=1-x^2 \n\
1003 RSQ tmp.x, tmp.x;\n\
1004 RCP n.y, tmp.x;\n\
1005 MAD tmp.z, n.y, data.z, 0; \n\
1006 MAD depth.x, -tmp.z, 0.005, fragment.position.z; \n\
1007 \n\
1008 ", fp);
1009
1010 if ( (_border_outside()>0 ) ) {
1011 sprintf(fp,"%s\n\
1012 #CMP border.x, n.y, n.y, -n.y; \n\
1013 MAD border.x, data.x, data.x, -1; \n\
1014 #ADD border.x, 1, -border.x; \n\
1015 ", fp);
1016 }
1017
1018 addDepthAdjustSnippet(fp);
1019
1020 sprintf(fp,"%s\n\
1021 # Compute normal\n\
1022 MUL nor, n.y, normcenter;\n\
1023 MAD nor, data.x, normside, nor;\n\
1024 ", fp);
1025
1026 addDirectLightingSnippet(fp);
1027
1028 sprintf(fp,"%s\n\
1029 #TEXTURING ON STICKS \n\
1030 \n\
1031 # FIND X (along axis) \n\
1032 MOV t.x, data.y;\n\
1033 MAD t.x, data.w, n.y, t.x;\n\
1034 # FIND Y (project norm) \n\
1035 MOV n.x, data.x; \n\
1036 \n\
1037 \n\
1038 # rotate (n.x, n.y)\n\
1039 MUL tmp, n, {1,1,0,0};\n\
1040 DP3 n.x, tmp, ROT;\n\
1041 MUL n.y, tmp.x, ROT.y;\n\
1042 MAD n.y, tmp.y, -ROT.x , n.y;\n\
1043 \n\
1044 # find x=FindRoll ( n.x, n.y) \n\
1045 ABS abs, n; \n\
1046 ADD abs.z, abs.x, abs.y; \n\
1047 RCP abs.z, abs.z; \n\
1048 MUL n, n, abs.z;\n\
1049 MAD tmp.x, 0.25, n.y, 0.25;\n\
1050 MAD tmp.y, 0.25,-n.y, 0.75;\n\
1051 CMP t.y, n.x, tmp.x, tmp.y;\n\
1052 \n\
1053 \n\
1054 #KILL OUTLIER FRAGMENTS \n\
1055 CMP t.w, t.x, -t.x, t.x;\n\
1056 ADD t.w, t.w, -1; # w = 1-|x|\n\
1057 KIL -t.w;\n\
1058 ",fp);
1059
1060
1061 if (P_use_shadowmap()) {
1062 addShadowMapComputationFP(fp);
1063 };
1064
1065 if ((P_col_atoms_sat>0) && (P_col_atoms_bri>0)) {
1066 if (P_cyl_const_color) sprintf(fp,"%sMOV basecol, {%10.9f,%10.9f,%10.9f,1};\n",
1067 fp,P_cyl_const_color_R, P_cyl_const_color_G, P_cyl_const_color_B);
1068 else if (P_cyl_smooth_color) {
1069 sprintf(fp,"%sMAD t.z, t.x, 0.5,0.5;\n",fp);
1070 sprintf(fp,"%sLRP basecol, t.z, col1,col2;\n",fp);
1071 } else
1072 sprintf(fp,"%sCMP basecol, t.x, col2,col1;\n",fp);
1073 }
1074
1075 if (P_texture>0) {
1076 float sidex=BSIZE, sidey=CSIZE;
1077 sprintf(fp,"%s\n\
1078 MAD t, t, {%5.2f, %5.2f, 0,0}, \n\
1079 {%5.2f, %5.2f, 0,0}; \n\
1080 #FRC t, t; \n\
1081 ",
1082 fp,
1083 (sidex-1)/2.0 , sidey-1.0 , (sidex)/2.0, 0.5);
1084
1085 }
1086
1087 addTexturingSnippet(fp);
1088
1089 sprintf(fp,"%s%s",fp,"ADD result.color, res, {0,0,0,1};\n\n");
1090 sprintf(fp,"%s%s",fp,"END\n");
1091
1092 /*#MOV result.color, n.y;\n\
1093 #MUL tmp.y, data.y, 8;\n\
1094 #FRC result.color.y, tmp.y;\n\
1095 #MOV result.color, fragment.color;\n\
1096 END\n\
1097 ");*/
1098
1099 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
1100 // printf("\n-------<fp Sticks>--------\n%s",fp);
1101 if(!checkProgramError(fp)) return false;
1102 return true;
1103 }
1104
1105 bool CgUtil::setStickVertexProgram(){
1106 /*
1107 RESULT:
1108 TC0 = (corner, ... )
1109 TC1 = NORMAL (on central axis)
1110 TC2 = ROTATION of basic point
1111 TC3 = TEXTURE OFFSET
1112 no! -> TC4 = POSITION of proj. over axis (for shadowmap)
1113 TC4 = DIRECTION of normal of side point
1114 */
1115 char vp[10096];
1116
1117 sprintf(vp,"\
1118 !!ARBvp1.0\n\
1119 \n\
1120 ATTRIB pos = vertex.position;\n\
1121 \n\
1122 ATTRIB data = vertex.normal;\n\
1123 ATTRIB dire = vertex.texcoord;\n\
1124 ATTRIB startp = vertex.texcoord[1];\n\
1125 \n\
1126 PARAM mat[4] = { state.matrix.mvp };\n\
1127 #PARAM matP[4] = { state.matrix.projection };\n\
1128 PARAM matM[4] = { state.matrix.modelview };\n\
1129 \n\
1130 TEMP p,tmp,tmp2, d,dr, dataout, disp, disp2, norm, normside; \n\
1131 \n\
1132 ");
1133 if (P_texture>0)
1134 sprintf(vp,"%s\
1135 # pass down texture offest \n\
1136 MOV result.texcoord[3], vertex.texcoord[2];\n\
1137 \n\
1138 ", vp);
1139
1140 if ((P_col_atoms_sat>0) && (P_col_atoms_bri>0))
1141 sprintf(vp,"%s\
1142 MOV result.color.primary, vertex.color.primary;\n\
1143 MOV result.color.secondary, vertex.color.secondary;\n\
1144 ",vp);
1145
1146 sprintf(vp,"%s\
1147 # Project direction View frame.\n\
1148 DP3 d.x, matM[0], dire;\n\
1149 DP3 d.y, matM[1], dire;\n\
1150 DP3 d.z, matM[2], dire;\n\
1151 \n\
1152 ",vp);
1153
1154 sprintf(vp,"%s\
1155 # find k = 1/sqrt(d.x^2+d.y^2) (= tmp.w) \n\
1156 MUL tmp.w, d.x, d.x; \n\
1157 MAD norm.z, d.y, d.y, tmp.w; \n\
1158 RSQ tmp.w, norm.z; \n\
1159 # \n\
1160 # Using orthonormal base B: \n\
1161 # Bx= (+dx,+dy, 0)*k \n\
1162 # By= (-dy,+dx, 0)*k \n\
1163 # Bz= ( 0, 0, 1) \n\
1164 \n\
1165 # now dr = d in B = ( (dx^2+dy^2)*k, 0, dz ) \n\
1166 \n\
1167 ",vp);
1168
1169 if (!shadowmapBuilding)
1170 sprintf(vp,"%s\
1171 # find normal = ( dr.z*Bx - dr.x*Bz ) = k* (-dx*dz, -dy*dz, dx^2+dy^2)\n\
1172 MUL norm.x, -d.x, d.z;\n\
1173 MUL norm.y, -d.y, d.z;\n\
1174 MUL norm, norm, tmp.w;\n\
1175 \n\
1176 DP3 tmp.x, norm, norm; # normalization, TEMP TEST!\n\
1177 RSQ tmp.x, tmp.x;\n\
1178 MUL norm, norm, tmp.x;\n\
1179 \n\
1180 ",vp);
1181
1182 sprintf(vp,"%s\
1183 # radius r (=tmp.z) = Raduis*GlobalScaleFactor \n\
1184 MUL tmp.z, program.env[0].y, program.env[0].x; \n\
1185 \n\
1186 SWZ disp, d, y,-x,0,0; \n\
1187 MUL disp, disp, tmp.w; \n\
1188 MUL disp2, disp, tmp.z; \n\
1189 ### postponed MAD p, data.x, disp2, p; \n\
1190 \n\
1191 MOV dataout, data;\n\
1192 \n\
1193 ",vp);
1194
1195
1196 if (!shadowmapBuilding)
1197 sprintf(vp,"%s\
1198 # pre-compute Z offset \n\
1199 \n\
1200 # dataout.z = R*(sen*sen/cos+cos) = R*(z*z*k+1/k) \n\
1201 # dataout.z is the z-offset for the center of the cyl \n\
1202 RCP tmp.x , tmp.w;\n\
1203 MUL tmp.y, d.z, d.z;\n\
1204 MAD tmp.y, tmp.y, tmp.w, tmp.x;\n\
1205 MUL dataout.z, program.env[0].y, tmp.y;\n\
1206 # dataout.w = dz/dx \n\
1207 # dataout.w is the y-offset for the center of the cyl \n\
1208 MUL dataout.w, d.z, tmp.w;\n\
1209 MUL dataout.w, dataout.w, dire.w;\n\
1210 MUL dataout.w, dataout.w, program.env[0].z; # (i.e. rad*2)\n\
1211 \n\
1212 ",vp);
1213
1214 // Further enlarge impostors at sides to include borders
1215 if (_border_outside()!=0) // Compute 'almost' radius and scale indep. border
1216 sprintf(vp,"%s\
1217 #MUL tmp.z, program.env[0].y, program.env[0].x; \n\
1218 RSQ tmp.x, tmp.z ;\n\
1219 #MUL tmp.x, tmp.x, tmp.x; # Comment to 'almost'\n\
1220 MUL tmp.x, %7.5f , tmp.x;\n\
1221 ADD tmp.x, tmp.x , 1;\n\
1222 MUL dataout.x, dataout.x, tmp.x ;\n\
1223 ",
1224 vp, _border_outside() );
1225
1226 if (!shadowmapBuilding)
1227 sprintf(vp,"%s\
1228 # extend cylinder on bottom do deal with ends \n\
1229 MUL tmp.x, -data.y, dataout.w;\n\
1230 SGE tmp.x, tmp.x, 0;\n\
1231 MUL tmp.x, -dataout.w, tmp.x; \n\
1232 ADD dataout.y, dataout.y, tmp.x;\n\
1233 RCP tmp2.x, dire.w; \n\
1234 MUL tmp.x, tmp.x, tmp2.x; \n\
1235 MUL tmp.x, 0.5, tmp.x; \n\
1236 MAD tmp, dire, tmp.x, pos; \n\
1237 ",vp);
1238 else sprintf(vp,"%s\
1239 MOV tmp, pos; \n\
1240 ",vp);
1241
1242 sprintf(vp,"%s\
1243 \n\
1244 # Project point in View frame.\n\
1245 MOV tmp.w, 1; \n\
1246 DP4 p.x, mat[0], tmp;\n\
1247 DP4 p.y, mat[1], tmp;\n\
1248 DP4 p.z, mat[2], tmp;\n\
1249 DP4 p.w, mat[3], tmp; \n\
1250 \n\
1251 MAD result.position, dataout.x, disp2, p; \n\
1252 ",vp);
1253
1254
1255 // Compute normal at side points
1256 if (!shadowmapBuilding)
1257 sprintf(vp,"%s\
1258 MOV result.texcoord[0], dataout;\n\
1259 MOV result.texcoord[1], norm;\n\
1260 XPD normside, d, norm;\n\
1261 \n\
1262 DP3 tmp.x, normside, normside; # normalization, of normside \n\
1263 RSQ tmp.x, tmp.x;\n\
1264 MUL normside, normside, tmp.x;\n\
1265 \n\
1266 MOV result.texcoord[4], normside;\n\
1267 ",vp);
1268
1269 if (P_texture>0)
1270 sprintf(vp,"%s\
1271 #ROTATE start vector;\n\
1272 DP3 tmp.x, matM[0], startp;\n\
1273 DP3 tmp.y, matM[1], startp;\n\
1274 DP3 tmp.z, matM[2], startp;\n\
1275 DP3 tmp.w, tmp, tmp;\n\
1276 RSQ tmp.w, tmp.w;\n\
1277 MUL tmp, tmp.w, tmp;\n\
1278 MOV tmp.w, tmp.z;\n\
1279 #MOV tmp.z, 0;\n\
1280 DP3 tmp.x, disp, tmp;\n\
1281 \n\
1282 # cheap way: find an offset to add \n\
1283 # tz=2-tz if tw negative \n\
1284 #SGE tmp.w, 0, -tmp.w; \n\
1285 #MAD tmp.z, tmp.w, -2, 1; \n\
1286 #MUL tmp.x, tmp.z, tmp.x; \n\
1287 #MAD tmp.x, tmp.w, -2, tmp.x; \n\
1288 #MUL result.texcoord[2].x, tmp.x, 0.25;\n\
1289 #\n\
1290 # cool way: store 2D rotation via complex numbers \n\
1291 # tmp.y = sign(tw)*sqrt(1-tmp.x^2)\n\
1292 SGE tmp.w, 0, -tmp.w; \n\
1293 MAD tmp.z, tmp.w, -2, 1; \n\
1294 MAD tmp.w, tmp.x, -tmp.x, 1;\n\
1295 RSQ tmp.w, tmp.w;\n\
1296 RCP tmp.w, tmp.w;\n\
1297 MUL tmp.y, tmp.w, -tmp.z;\n\
1298 \n\
1299 MOV result.texcoord[2], tmp;\n\
1300 ",
1301 vp);
1302
1303 /*
1304 // Not needed
1305 if (P_use_shadowmap())
1306 sprintf(vp,"\%s\n\
1307 MOV result.texcoord[4], pos;\n\
1308 ",vp);
1309 */
1310
1311 sprintf(vp,"\%s\n\
1312 END\n\
1313 ",vp);
1314
1315 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(vp), vp);
1316
1317 //printf("\n--------<vp stick>---------\n%s",vp);
1318 if(!checkProgramError(vp)) return -1;
1319 return true;
1320 };
1321
1322
1323 bool CgUtil::setBallFragmentProgram(){
1324 CgUtil::shaderHasChanged=true;
1325 char fp[10096];
1326
1327 if (shadowmapBuilding) {
1328 sprintf(fp,"\
1329 !!ARBfp1.0 \n\
1330 \n\
1331 ATTRIB data = fragment.texcoord; \n\
1332 TEMP tmp; \n\
1333 \n\
1334 MAD tmp.x, data.x, data.x, -1; \n\
1335 MAD tmp.x, data.y, data.y, tmp.x; \n\
1336 KIL -tmp.x;");
1337 if (accurateShadowmapBuilding)
1338 sprintf(fp,"%s\
1339 RSQ tmp.x, tmp.x; \n\
1340 RCP tmp.x, tmp.x; \n\
1341 MUL tmp.x, tmp.x, data.z; \n\
1342 MAD result.depth, -tmp.x, 0.005, fragment.position.z;\n"
1343 ,fp);
1344
1345 sprintf(fp,"\
1346 %sMOV result.color, 1;\n\
1347 END\n",fp);
1348
1349 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
1350
1351 //printf("\n-------<fp>--------\n%s",fp);
1352
1353 if(!checkProgramError(fp)) return false;
1354 return true;
1355 }
1356 // ############################
1357 // # #
1358 // # FRAGMENT PROGRAM BALL #
1359 // # #
1360 // ############################
1361
1362 sprintf(fp,"\
1363 !!ARBfp1.0 \n\
1364 \n\
1365 ATTRIB data = fragment.texcoord; \n\
1366 ATTRIB offset = fragment.texcoord[2];\n\
1367 ATTRIB basecol = fragment.color; \n\
1368 ");
1369 if (P_use_shadowmap())
1370 sprintf(fp,"%s\
1371 PARAM Smat0 = program.env[3]; \n\
1372 PARAM Smat1 = program.env[4]; \n\
1373 PARAM Smat2 = program.env[5]; \n\
1374 #PARAM ratio = program.env[6]; \n\
1375 ATTRIB origpos = fragment.texcoord[3]; \n\
1376 ",fp);
1377
1378 sprintf(fp,"%s\
1379 \n\
1380 TEMP tmp,tmp2,tmp3, t,t0,t1,t2,nor,n,nabs,nsign,disp,res,depth,\n\
1381 lighting, border, posScreen; \n\
1382 PARAM TNORM={%10.9f,%10.9f,0,0}; \n\
1383 \n\
1384 PARAM LIGHT= state.light[0].position ; \n\
1385 PARAM hwv = state.light[0].half;\n\
1386 \n\
1387 PARAM mat[4] = { state.matrix.projection }; \n\
1388 PARAM mat0 = program.env[0];\n\
1389 PARAM mat1 = program.env[1];\n\
1390 PARAM mat2 = program.env[2];\n\
1391 \n",
1392 fp,
1393 (moltextureCanvas.GetHardRes()==0)?0:1.0f/moltextureCanvas.GetHardRes(),
1394 (moltextureCanvas.GetHardRes()==0)?0:1.0f/moltextureCanvas.GetHardRes() );
1395
1396 sprintf(fp,"%s\
1397 MOV nor, data; \n"
1398 ,fp);
1399
1400 // tentativo prospettiva figa
1401 if ((projmode==PERSPECTIVE)&&(proj_figa)) {
1402 /* sprintf(fp,"%s\
1403 MAD tmp, fragment.position, {0.00390625, 0.00390625, 0,0}, {-1,-1,0,0}; \n\
1404 DP4 tmp.x, tmp, data; \n\
1405 MUL tmp.x, tmp.x, 0.5; \n\
1406 MAD data.xy, data, tmp.x, data; \n\
1407 "
1408 ,fp);*/
1409
1410 }
1411
1412 sprintf(fp,"%s\
1413 MUL tmp, data, data; \n\
1414 MOV tmp.z, 1; \n\
1415 DP3 border.x, tmp, {1,1,-1,0}; \n" // border.x = ( (x^2+y^2)-1 )
1416 ,fp);
1417
1418 // 1.20*.120 -1 = 0.44
1419
1420 if (_border_outside()>0) {
1421 sprintf(fp,"%s\n\n\
1422 ADD tmp2.y, -border.x, data.w; # allow for border (part ii) \n\
1423 #MAD tmp2.y, data.z, -border.x, %7.5f;\n\
1424 #MAD tmp2.y, data.z, tmp2.y, %7.5f;\n\
1425 KIL tmp2.y; \n\
1426 ",fp, -2*_border_outside(), _border_outside()*_border_outside());
1427 }
1428 else sprintf(fp,"%s\n\nKIL -border.x; \n",fp);
1429
1430 sprintf(fp,"%s\
1431 \n\
1432 RSQ tmp2.y, border.x; \n\
1433 RCP tmp2.x, tmp2.y; \n\
1434 MOV nor.z, tmp2.x; \n\
1435 \n\
1436 MAD tmp2.y, tmp2.x, data.z, 0; # note: add an extra range of 0 \n\
1437 \n",
1438 fp); // nor = x, y, z=(1- x^2+y^2)
1439
1440
1441 /* if (projmode==PERSPECTIVE) // new_z = old_z / (1/W) + sph_z * W
1442 sprintf(fp,"%s%s",fp,"\
1443 MUL tmp2.y, tmp2.y, fragment.position.w; \n\
1444 MAD depth.x, fragment.position.z, data.w, -tmp2.y; # prospettiva: new_z = old_z + 0.005*sph_z\n\
1445 MUL depth.x, depth.x, 0.1; \n\
1446 ");
1447 else*/
1448
1449 // new_z = old_z + 0.005*sph_z
1450 sprintf(fp,"%s%s",fp, "\
1451 MAD depth.x, -tmp2.y, 0.005, fragment.position.z; # ortho \n\
1452 ");
1453
1454 addDepthAdjustSnippet(fp);
1455
1456 if ((P_texture>0)|| (P_use_shadowmap())) {
1457
1458 sprintf(fp,"%s%s",fp," \n\n\
1459 # rotate normal \n\
1460 DP3 n.x, mat0, nor; \n\
1461 DP3 n.y, mat1, nor; \n\
1462 DP3 n.z, mat2, nor; \n\
1463 MOV n.w, 0; \n\n");
1464 }
1465
1466 addDirectLightingSnippet(fp);
1467
1468 if (P_use_shadowmap()) {
1469 addShadowMapComputationFP(fp);
1470 };
1471
1472 if (P_texture>0) {
1473 if (textmode==USE_CUBE)
1474 sprintf(fp, "%s \n\n\
1475 ## TEXTURING CUBEMAP STYLE \n\
1476 # \n\
1477 # project on cube: find face \n\
1478 CMP nabs, n, -n, n; \n\
1479 MOV t, n; \n\
1480 SWZ t0, t, z,x,y,1; \n\
1481 SWZ t1, t0, z,x,y,-1; \n\
1482 # \n\
1483 SUB t2.x, nabs.z, nabs.y; \n\
1484 CMP t, t2.x, t0, t; \n\
1485 # \n\
1486 CMP t2.z, t.z, -t.z, t.z; \n\
1487 SUB t2.x, t2.z, nabs.x; \n\
1488 CMP t, t2.x, t1, t; \n\
1489 # \n\
1490 # find face \n\
1491 ADD disp.x, t.w, 1; \n\
1492 CMP disp.y, t.z, 0, 1; \n\
1493 # \n\
1494 # divide by max coord \n\
1495 RCP t.z, t.z; \n\
1496 MUL t, t, t.z; \n\
1497 # \n\
1498 # find coords on face \n\
1499 MAD t, t, 0.5, 0.5; \n\
1500 MAD t, t, {%5.2f,%5.2f,0,0}, {%5.2f, %5.2f, 0,0}; \n\
1501 MAD t, {%5.2f,%5.2f,0,0}, disp, t; \n\n",
1502 fp, CSIZE-gap*2.0, CSIZE-gap*2.0, gap, gap, CSIZE, CSIZE );
1503
1504 else sprintf(fp, "%s\n\n\
1505 ## TEXTURING OCTAMAP STYLE \n\
1506 # \n\
1507 CMP nabs, n, -n, n; \n\
1508 DP3 tmp.y, {1,1,1,0}, nabs; \n\
1509 RCP tmp.z, tmp.y; \n\
1510 MUL t0, n, tmp.z; \n\
1511 MAD t1, nabs, tmp.z, -1; \n\
1512 # t1= -(1-abs(t)) \n\
1513 CMP t2.x, n.x, t1.y, -t1.y; \n\
1514 CMP t2.y, n.y, t1.x, -t1.x; \n\
1515 CMP t0, n.z, t0, t2; \n\
1516 MAD t, t0, {%5.2f, %5.2f, 0,0}, \n\
1517 {%5.2f, %5.2f, 0,0}; \n\n",
1518 fp,
1519 CSIZE/2.0 - gap, CSIZE/2.0 - gap, CSIZE/2.0, CSIZE/2.0);
1520
1521 }
1522
1523 addTexturingSnippet(fp);
1524
1525 sprintf(fp,"%s%s",fp,"ADD result.color, res, {0,0,0,1};\n\n");
1526 sprintf(fp,"%s%s",fp,"END\n");
1527
1528 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fp), fp);
1529
1530 //printf("\n-------<fp>--------\n%s",fp);
1531
1532 if(!checkProgramError(fp)) return false;
1533 return true;
1534
1535
1536 }
1537
1538
1539 bool CgUtil::init() {
1540
1541 GLenum err=glewInit();
1542 if(err!=GLEW_OK) {
1543 sprintf(lasterr,"%s\n",glewGetErrorString(err));
1544 return false;
1545 }
1546 /*
1547 if(!GLEW_ARB_vertex_program || !GLEW_ARB_fragment_program )
1548 {
1549 printf("Extension: 'ARB_fragment_program' and/or 'ARB_vertex_program' not supported.\n");
1550 return false;
1551 }
1552 */
1553 return true;
1554 }
1555
1556 /*
1557 int CgUtil::loadVertexProgram(const char *str) {
1558 unsigned int program;
1559 checkError();
1560 glGenProgramsARB(1, &program);
1561 checkError();
1562 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program);
1563 checkError();
1564 char *text = readFile(str);
1565 if(!text) {
1566 printf("could not find file %s\n", str);
1567 exit(0);
1568 }
1569 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(text), text);
1570
1571 if(!checkProgramError(text)) {
1572 delete []text;
1573 return -1;
1574 }
1575 delete []text;
1576 return program;
1577 }
1578 int CgUtil::loadFragmentProgram(const char *str) {
1579 unsigned int program;
1580 glGenProgramsARB(1, &program);
1581 checkError();
1582 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program);
1583 checkError();
1584 char *text = readFile(str);
1585 if(!text) {
1586 printf("could not find file %s\n", str);
1587 exit(0);
1588 }
1589 char *c=text;
1590 while ((*c)!=0) {
1591 if ((*c == '(') || (*c == ')') ) *c=' ';
1592 if ( *c == '/') *c='#';
1593 c++;
1594 }
1595 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(text), text);
1596 if(!checkProgramError(text)) {
1597 delete []text;
1598 return -1;
1599 }
1600 return program;
1601 }
1602 */
1603
1604 bool CgUtil::checkError() {
1605 int error = glGetError();
1606 if(error == GL_NO_ERROR)
1607 return true;
1608 const char *str;
1609 switch(error) {
1610 case GL_INVALID_ENUM: str = "Invalid Enum"; break;
1611 case GL_INVALID_OPERATION: str = "Invalid Operation"; break;
1612 case GL_INVALID_VALUE: str = "Invalid Value"; break;
1613 case GL_STACK_OVERFLOW: str = "Stack Overflow"; break;
1614 case GL_STACK_UNDERFLOW: str = "Stack Underflow"; break;
1615 case GL_OUT_OF_MEMORY: str = "Out of memory"; break;
1616 default: str = "Unknown error"; break;
1617 }
1618 printf("Error %s (%d)\n", str, error);
1619 return false;
1620 }
1621
1622 bool CgUtil::checkProgramError(char *st) {
1623 bool res=true;
1624 while (1) {
1625 int error = glGetError();
1626 if(error == GL_NO_ERROR ) return res;
1627
1628 res=false;
1629 if(error == GL_INVALID_OPERATION) {
1630
1631 GLint errPos;
1632 glGetIntegerv( GL_PROGRAM_ERROR_POSITION_ARB, &errPos );
1633 const GLubyte *errString = glGetString( GL_PROGRAM_ERROR_STRING_ARB);
1634 fprintf( stderr, "error at position: %d\n[%s]\n", errPos, errString );
1635 fprintf(stderr,"\n\"...");
1636 for (int i=errPos-40; i<errPos+40; i++)
1637 if (i>=0) {
1638 if (!st[i]) break;
1639 if (i==errPos) fprintf(stderr,"(*)");
1640 fprintf(stderr,"%c", (st[i]=='\n')?'\\':st[i] );
1641 }
1642 fprintf(stderr,"...\"\n");
1643 } else {
1644 const GLubyte *errString = gluErrorString( error );
1645 fprintf( stderr, "error: [%s]\n", errString );
1646 }
1647 }
1648 }
1649
1650
1651 char *CgUtil::readFile(const char *file) {
1652 FILE *fp = fopen(file, "r");
1653 if(!fp) return NULL;
1654 char *pippo = new char[1<<18];
1655 int tot = 0;
1656 while(1) {
1657 int nread = fread(pippo + tot, 1, 1024, fp);
1658 tot += nread;
1659 if(nread < 1024) {
1660 pippo[tot] = '\0';
1661 break;
1662 }
1663 }
1664 fclose(fp);
1665 return pippo;
1666 }
1667
1668
1669 void CgUtil::ShowShaderInfo(int fp){
1670 static GLint i[10],j[10],k[10],h[10];
1671
1672 glGetProgramivARB(fp, GL_MAX_PROGRAM_INSTRUCTIONS_ARB, i);
1673 glGetProgramivARB(fp, GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB, i+1);
1674 glGetProgramivARB(fp, GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB, i+2);
1675 glGetProgramivARB(fp, GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB, i+3);
1676 glGetProgramivARB(fp, GL_MAX_PROGRAM_TEMPORARIES_ARB, i+4);
1677 glGetProgramivARB(fp, GL_MAX_PROGRAM_PARAMETERS_ARB, i+5);
1678 glGetProgramivARB(fp, GL_MAX_PROGRAM_ATTRIBS_ARB, i+6);
1679
1680 glGetProgramivARB(fp, GL_PROGRAM_INSTRUCTIONS_ARB, j);
1681 glGetProgramivARB(fp, GL_PROGRAM_ALU_INSTRUCTIONS_ARB, i+1);
1682 glGetProgramivARB(fp, GL_PROGRAM_TEX_INSTRUCTIONS_ARB, j+2);
1683 glGetProgramivARB(fp, GL_PROGRAM_TEX_INDIRECTIONS_ARB, j+3);
1684 glGetProgramivARB(fp, GL_PROGRAM_TEMPORARIES_ARB, j+4);
1685 glGetProgramivARB(fp, GL_PROGRAM_PARAMETERS_ARB, j+5);
1686 glGetProgramivARB(fp, GL_PROGRAM_ATTRIBS_ARB, j+6);
1687
1688 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB, k);
1689 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, k+1);
1690 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB, k+2);
1691 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB, k+3);
1692 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_TEMPORARIES_ARB, k+4);
1693 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_PARAMETERS_ARB, k+5);
1694 glGetProgramivARB(fp, GL_PROGRAM_NATIVE_ATTRIBS_ARB, k+6);
1695
1696 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, h);
1697 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, h+1);
1698 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB, h+2);
1699 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB, h+3);
1700 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB, h+4);
1701 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB, h+5);
1702 glGetProgramivARB(fp, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, h+6);
1703
1704
1705 char st[10][10]={"Instr","Alu Instr","Tex Instr","Tex Indir","Temp","Param","Attr"};
1706 printf(" %s PROGRAM STATS \n",
1707 (fp==GL_FRAGMENT_PROGRAM_ARB)?"FRAGMENT":"VERTEX");
1708 printf(" original | native \n");
1709 printf(" MAX current | MAX current\n");
1710 for (int c=0; c<7; c++) {
1711 printf( "%10s %5d %5d | %5d %5d\n",st[c],i[c],j[c],h[c],k[c]);
1712 }
1713 printf("\n\n");
1714 }