]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/main.c
deea6f8d99eb0ec7470c4fae998424ea77f3c804
[vlc] / modules / visualization / galaktos / main.c
1 /*****************************************************************************
2  * main.c:
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          Adapted from projectM (http://xmms-projectm.sourceforge.net/)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "plugin.h"
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include "common.h"
33 #include "preset_types.h"
34 #include "preset.h"
35 #include "engine_vars.h"
36 #include "per_pixel_eqn_types.h"
37 #include "per_pixel_eqn.h"
38 #include "interface_types.h"
39 #include "video_init.h"             //Video Init Routines, resizing/fullscreen, creating pbuffers
40 #include "PCM.h"                    //Sound data handler (buffering, FFT, etc.)
41 #include "beat_detect.h"            //beat detection routines
42 #include "custom_wave_types.h"
43 #include "custom_wave.h"
44 #include "custom_shape_types.h"
45 #include "custom_shape.h"
46 //#include <dmalloc.h>
47
48 // Forward declarations
49
50 void read_cfg();
51
52 void modulate_opacity_by_volume();
53 void maximize_colors();
54 void do_per_pixel_math();
55 void do_per_frame();
56
57 void render_interpolation();
58 void render_texture_to_screen();
59 void render_texture_to_studio();
60 void draw_motion_vectors();
61 void draw_borders();
62 void draw_shapes();
63 void draw_waveform();
64 void draw_custom_waves();
65
66 void reset_per_pixel_matrices();
67 void init_per_pixel_matrices();
68 void free_per_pixel_matrices();
69
70 int noSwitch=0;
71 int pcmframes=1;
72 int freqframes=0;
73 int totalframes=1;
74
75 int studio=0;
76
77 extern preset_t * active_preset;
78
79 GLuint RenderTargetTextureID;
80
81 double wave_o;
82
83 //double gx=32;  //size of interpolation
84 //double gy=24;
85
86 int texsize=512;   //size of texture to do actual graphics
87 int vw=512;           //runtime dimensions
88 int vh=512;
89 int fullscreen=0;
90
91 int maxsamples=2048; //size of PCM buffer
92 int numsamples; //size of new PCM info
93 double *pcmdataL;     //holder for most recent pcm data
94 double *pcmdataR;     //holder for most recent pcm data
95
96 int avgtime=500;  //# frames per preset
97
98 char *title = NULL;
99 int drawtitle;
100 int title_font;
101 int other_font;
102
103 int correction=1;
104
105 double vol;
106
107 //per pixel equation variables
108
109
110 double **gridx;  //grid containing interpolated mesh
111 double **gridy;
112 double **origtheta;  //grid containing interpolated mesh reference values
113 double **origrad;
114 double **origx;  //original mesh
115 double **origy;
116
117 char *buffer; //XXX
118
119 int galaktos_init( galaktos_thread_t *p_thread )
120 {
121     init_per_pixel_matrices();
122     pcmdataL=(double *)malloc(maxsamples*sizeof(double));
123     pcmdataR=(double *)malloc(maxsamples*sizeof(double));
124
125     /* Preset loading function */
126     initPresetLoader();
127
128     /* Load default preset directory */
129 //    loadPresetDir("/home/cyril/.vlc/galaktos");
130     loadPresetDir("/etc/projectM/presets");
131
132     initPCM(maxsamples);
133     initBeatDetect();
134
135     // mutex = SDL_CreateMutex();
136     return 0;
137 }
138
139
140 void galaktos_done( galaktos_thread_t *p_thread )
141 {
142     free(pcmdataL);
143     free(pcmdataR);
144
145     freeBeatDetect();
146     freePCM();
147     free_per_pixel_matrices();
148     closePresetDir();
149 //    destroyPresetLoader(); XXX segfaults :(
150 }
151
152
153 int galaktos_update( galaktos_thread_t *p_thread )
154 {
155     static int nohard=0;
156     double vdataL[512];  //holders for FFT data (spectrum)
157     double vdataR[512];
158
159     avgtime=fps*18;
160     totalframes++; //total amount of frames since startup
161
162     Time=(double)(mdate()/1000000);
163
164     frame++;  //number of frames for current preset
165     progress= frame/(double)avgtime;
166     if (progress>1.0) progress=1.0;
167     // printf("start:%d at:%d min:%d stop:%d on:%d %d\n",startframe, frame frame-startframe,avgtime,  noSwitch,progress);
168
169     if (frame>avgtime)
170     {
171         if (noSwitch==0) switchPreset(RANDOM_NEXT,0);
172     }
173
174     evalInitConditions();
175     evalPerFrameEquations();
176
177     evalCustomWaveInitConditions();
178     evalCustomShapeInitConditions();
179
180     //     printf("%f %d\n",Time,frame);
181
182     reset_per_pixel_matrices();
183
184
185     numsamples = getPCMnew(pcmdataR,1,0,fWaveSmoothing,0,0);
186     getPCMnew(pcmdataL,0,0,fWaveSmoothing,0,1);
187     getPCM(vdataL,512,0,1,0,0);
188     getPCM(vdataR,512,1,1,0,0);
189
190     bass=0;mid=0;treb=0;
191
192     getBeatVals(vdataL,vdataR,&vol);
193
194     nohard--;
195     if(vol>8.0 && nohard<0 && noSwitch==0)
196     {
197
198         switchPreset(RANDOM_NEXT, HARD_CUT);
199         nohard=100;
200     }
201
202     //BEGIN PASS 1
203     //
204     //This pass is used to render our texture
205     //the texture is drawn to a subsection of the framebuffer
206     //and then we perform our manipulations on it
207     //in pass 2 we will copy the texture into texture memory
208
209   //  galaktos_glx_activate_pbuffer( p_thread );
210
211     glPushAttrib( GL_ALL_ATTRIB_BITS ); /* Overkill, but safe */
212
213     //   if (RenderTarget) glViewport( 0, 0, RenderTarget->w, RenderTarget->h );
214     if (0) {}
215     else glViewport( 0, 0, texsize, texsize );
216
217
218     glMatrixMode( GL_MODELVIEW );
219     glPushMatrix();
220     glLoadIdentity();
221
222     glMatrixMode( GL_PROJECTION );
223     glPushMatrix();
224     glLoadIdentity();
225
226     glOrtho(0.0, texsize, 0.0,texsize,10,40);
227
228     do_per_pixel_math();
229
230     do_per_frame();               //apply per-frame effects
231     render_interpolation();       //apply per-pixel effects
232     draw_motion_vectors();        //draw motion vectors
233     draw_borders();               //draw borders
234
235     draw_waveform();
236     draw_shapes();
237     draw_custom_waves();
238
239     glMatrixMode( GL_MODELVIEW );
240     glPopMatrix();
241
242     glMatrixMode( GL_PROJECTION );
243     glPopMatrix();
244
245     glPopAttrib();
246
247     //if ( RenderTarget )        SDL_GL_UnlockRenderTarget(RenderTarget);
248         /* Copy our rendering to the fake render target texture */
249     glBindTexture( GL_TEXTURE_2D, RenderTargetTextureID );
250     glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, texsize, texsize);
251 //    galaktos_glx_activate_window( p_thread );
252
253     //BEGIN PASS 2
254     //
255     //end of texture rendering
256     //now we copy the texture from the framebuffer to
257     //video texture memory and render fullscreen on a quad surface.
258     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
259     glMatrixMode(GL_PROJECTION);
260     glLoadIdentity();
261
262     glFrustum(-vw*.5, vw*.5, -vh*.5,vh*.5,10,40);
263
264     glLineWidth(texsize/512.0);
265     if(studio%2)render_texture_to_studio();
266     else render_texture_to_screen();
267
268     glFinish();
269     glFlush();
270     //  printf("Flush %d\n",(SDL_GetTicks()-timestart));
271
272     p_thread->p_opengl->pf_swap( p_thread->p_opengl );
273
274     /* Process events */
275     if( p_thread->p_opengl->pf_manage &&
276         p_thread->p_opengl->pf_manage( p_thread->p_opengl ) )
277     {
278         return 1;
279     }
280
281     return 0;
282 }
283
284
285 void free_per_pixel_matrices()
286 {
287     int x;
288
289     for(x = 0; x < gx; x++)
290     {
291         free(gridx[x]);
292         free(gridy[x]);
293         free(origtheta[x]);
294         free(origrad[x]);
295         free(origx[x]);
296         free(origy[x]);
297         free(x_mesh[x]);
298         free(y_mesh[x]);
299         free(rad_mesh[x]);
300         free(theta_mesh[x]);
301     }
302
303     free(origx);
304     free(origy);
305     free(gridx);
306     free(gridy);
307     free(x_mesh);
308     free(y_mesh);
309     free(rad_mesh);
310     free(theta_mesh);
311 }
312
313
314 void init_per_pixel_matrices()
315 {
316     int x,y;
317
318     gridx=(double **)malloc(gx * sizeof(double *));
319     gridy=(double **)malloc(gx * sizeof(double *));
320
321     origx=(double **)malloc(gx * sizeof(double *));
322     origy=(double **)malloc(gx * sizeof(double *));
323     origrad=(double **)malloc(gx * sizeof(double *));
324     origtheta=(double **)malloc(gx * sizeof(double *));
325
326     x_mesh=(double **)malloc(gx * sizeof(double *));
327     y_mesh=(double **)malloc(gx * sizeof(double *));
328     rad_mesh=(double **)malloc(gx * sizeof(double *));
329     theta_mesh=(double **)malloc(gx * sizeof(double *));
330
331     sx_mesh=(double **)malloc(gx * sizeof(double *));
332     sy_mesh=(double **)malloc(gx * sizeof(double *));
333     dx_mesh=(double **)malloc(gx * sizeof(double *));
334     dy_mesh=(double **)malloc(gx * sizeof(double *));
335     cx_mesh=(double **)malloc(gx * sizeof(double *));
336     cy_mesh=(double **)malloc(gx * sizeof(double *));
337     zoom_mesh=(double **)malloc(gx * sizeof(double *));
338     zoomexp_mesh=(double **)malloc(gx * sizeof(double *));
339     rot_mesh=(double **)malloc(gx * sizeof(double *));
340
341     for(x = 0; x < gx; x++)
342     {
343         gridx[x] = (double *)malloc(gy * sizeof(double));
344         gridy[x] = (double *)malloc(gy * sizeof(double));
345
346         origtheta[x] = (double *)malloc(gy * sizeof(double));
347         origrad[x] = (double *)malloc(gy * sizeof(double));
348         origx[x] = (double *)malloc(gy * sizeof(double));
349         origy[x] = (double *)malloc(gy * sizeof(double));
350
351         x_mesh[x] = (double *)malloc(gy * sizeof(double));
352         y_mesh[x] = (double *)malloc(gy * sizeof(double));
353
354         rad_mesh[x] = (double *)malloc(gy * sizeof(double));
355         theta_mesh[x] = (double *)malloc(gy * sizeof(double));
356
357         sx_mesh[x] = (double *)malloc(gy * sizeof(double));
358         sy_mesh[x] = (double *)malloc(gy * sizeof(double));
359         dx_mesh[x] = (double *)malloc(gy * sizeof(double));
360         dy_mesh[x] = (double *)malloc(gy * sizeof(double));
361         cx_mesh[x] = (double *)malloc(gy * sizeof(double));
362         cy_mesh[x] = (double *)malloc(gy * sizeof(double));
363
364         zoom_mesh[x] = (double *)malloc(gy * sizeof(double));
365         zoomexp_mesh[x] = (double *)malloc(gy * sizeof(double));
366
367         rot_mesh[x] = (double *)malloc(gy * sizeof(double));
368     }
369
370     //initialize reference grid values
371     for (x=0;x<gx;x++)
372     {
373         for(y=0;y<gy;y++)
374         {
375             origx[x][y]=x/(double)(gx-1);
376             origy[x][y]=-((y/(double)(gy-1))-1);
377             origrad[x][y]=hypot((origx[x][y]-.5)*2,(origy[x][y]-.5)*2) * .7071067;
378             origtheta[x][y]=atan2(((origy[x][y]-.5)*2),((origx[x][y]-.5)*2));
379             gridx[x][y]=origx[x][y]*texsize;
380             gridy[x][y]=origy[x][y]*texsize;
381         }
382     }
383 }
384
385
386
387 //calculate matrices for per_pixel
388 void do_per_pixel_math()
389 {
390     int x,y;
391
392     double rotx=0,roty=0;
393     evalPerPixelEqns();
394
395     if(!isPerPixelEqn(CX_OP))
396     {
397         for (x=0;x<gx;x++)
398         {
399             for(y=0;y<gy;y++){
400                 cx_mesh[x][y]=cx;
401             }
402         }
403     }
404
405     if(!isPerPixelEqn(CY_OP))
406     {
407         for (x=0;x<gx;x++)
408         {
409             for(y=0;y<gy;y++)
410             {
411                 cy_mesh[x][y]=cy;
412             }
413         }
414     }
415
416     if(isPerPixelEqn(ROT_OP))
417     {
418         for (x=0;x<gx;x++)
419         {
420             for(y=0;y<gy;y++)
421             {
422                 x_mesh[x][y]=x_mesh[x][y]-cx_mesh[x][y];
423                 y_mesh[x][y]=y_mesh[x][y]-cy_mesh[x][y];
424                 rotx=(x_mesh[x][y])*cos(rot_mesh[x][y])-(y_mesh[x][y])*sin(rot_mesh[x][y]);
425                 roty=(x_mesh[x][y])*sin(rot_mesh[x][y])+(y_mesh[x][y])*cos(rot_mesh[x][y]);
426                 x_mesh[x][y]=rotx+cx_mesh[x][y];
427                 y_mesh[x][y]=roty+cy_mesh[x][y];
428             }
429         }
430     }
431
432
433
434     if(!isPerPixelEqn(ZOOM_OP))
435     {
436         for (x=0;x<gx;x++)
437         {
438             for(y=0;y<gy;y++)
439             {
440                 zoom_mesh[x][y]=zoom;
441             }
442         }
443     }
444
445     if(!isPerPixelEqn(ZOOMEXP_OP))
446     {
447         for (x=0;x<gx;x++)
448         {
449             for(y=0;y<gy;y++)
450             {
451                 zoomexp_mesh[x][y]=zoomexp;
452             }
453         }
454     }
455
456
457     //DO ZOOM PER PIXEL
458     for (x=0;x<gx;x++)
459     {
460         for(y=0;y<gy;y++)
461         {
462             x_mesh[x][y]=(x_mesh[x][y]-.5)*2;
463             y_mesh[x][y]=(y_mesh[x][y]-.5)*2;
464             x_mesh[x][y]=x_mesh[x][y]/(((zoom_mesh[x][y]-1)*(pow(rad_mesh[x][y],zoomexp_mesh[x][y])/rad_mesh[x][y]))+1);
465             y_mesh[x][y]=y_mesh[x][y]/(((zoom_mesh[x][y]-1)*(pow(rad_mesh[x][y],zoomexp_mesh[x][y])/rad_mesh[x][y]))+1);
466             x_mesh[x][y]=(x_mesh[x][y]*.5)+.5;
467             y_mesh[x][y]=(y_mesh[x][y]*.5)+.5;
468         }
469     }
470
471     if(isPerPixelEqn(SX_OP))
472     {
473         for (x=0;x<gx;x++)
474         {
475             for(y=0;y<gy;y++)
476             {
477                 x_mesh[x][y]=((x_mesh[x][y]-cx_mesh[x][y])/sx_mesh[x][y])+cx_mesh[x][y];
478             }
479         }
480     }
481
482     if(isPerPixelEqn(SY_OP))
483     {
484         for (x=0;x<gx;x++)
485         {
486             for(y=0;y<gy;y++)
487             {
488                 y_mesh[x][y]=((y_mesh[x][y]-cy_mesh[x][y])/sy_mesh[x][y])+cy_mesh[x][y];
489             }
490         }
491     }
492
493     if(isPerPixelEqn(DX_OP))
494     {
495         for (x=0;x<gx;x++)
496         {
497             for(y=0;y<gy;y++)
498             {
499
500                 x_mesh[x][y]=x_mesh[x][y]-dx_mesh[x][y];
501
502             }
503         }
504     }
505
506     if(isPerPixelEqn(DY_OP))
507     {
508         for (x=0;x<gx;x++)
509         {
510             for(y=0;y<gy;y++)
511             {
512                 y_mesh[x][y]=y_mesh[x][y]-dy_mesh[x][y];
513
514             }
515         }
516     }
517
518
519 }
520
521 void reset_per_pixel_matrices()
522 {
523     int x,y;
524
525     for (x=0;x<gx;x++)
526     {
527         for(y=0;y<gy;y++)
528         {
529             x_mesh[x][y]=origx[x][y];
530             y_mesh[x][y]=origy[x][y];
531             rad_mesh[x][y]=origrad[x][y];
532             theta_mesh[x][y]=origtheta[x][y];
533         }
534     }
535 }
536
537
538
539 void draw_custom_waves()
540 {
541     int x;
542
543     custom_wave_t *wavecode;
544     glPointSize(texsize/512);
545     //printf("%d\n",wavecode);
546     //  more=isMoreCustomWave();
547     // printf("not inner loop\n");
548     while ((wavecode = nextCustomWave()) != NULL)
549     {
550         //printf("begin inner loop\n");
551         if(wavecode->enabled==1)
552         {
553             // nextCustomWave();
554
555             //glPushMatrix();
556
557             //if(wavecode->bUseDots==1) glEnable(GL_LINE_STIPPLE);
558             if (wavecode->bAdditive==0)  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
559             else    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
560             if (wavecode->bDrawThick==1)  glLineWidth(2*texsize/512);
561
562             //  xx= ((pcmdataL[x]-pcmdataL[x-1])*80*fWaveScale)*2;
563             //yy=pcmdataL[x]*80*fWaveScale,-1;
564             //glVertex3f( (wave_x*texsize)+(xx+yy)*cos(45), (wave_y*texsize)+(-yy+xx)*cos(45),-1);
565             // printf("samples: %d\n", wavecode->samples);
566
567             getPCM(wavecode->value1,wavecode->samples,0,wavecode->bSpectrum,wavecode->smoothing,0);
568             getPCM(wavecode->value2,wavecode->samples,1,wavecode->bSpectrum,wavecode->smoothing,0);
569             // printf("%f\n",pcmL[0]);
570             for(x=0;x<wavecode->samples;x++)
571             {wavecode->value1[x]=wavecode->value1[x]*wavecode->scaling;}
572
573             for(x=0;x<wavecode->samples;x++)
574             {wavecode->value2[x]=wavecode->value2[x]*wavecode->scaling;}
575
576             for(x=0;x<wavecode->samples;x++)
577             {wavecode->sample_mesh[x]=((double)x)/((double)(wavecode->samples-1));}
578
579             // printf("mid inner loop\n");
580             evalPerPointEqns();
581             /*
582                if(!isPerPointEquation("x"))
583                {for(x=0;x<wavecode->samples;x++)
584                {cw_x[x]=0;} }
585
586                if(!isPerPointEquation(Y_POINT_OP))
587                {for(x=0;x<wavecode->samples;x++)
588                {cw_y[x]=0;}}
589
590                if(!isPerPointEquation(R_POINT_OP))
591                {for(x=0;x<wavecode->samples;x++)
592                {cw_r[x]=wavecode->r;}}
593                if(!isPerPointEquation(G_POINT_OP))
594                {for(x=0;x<wavecode->samples;x++)
595                {cw_g[x]=wavecode->g;}}
596                if(!isPerPointEquation(B_POINT_OP))
597                {for(x=0;x<wavecode->samples;x++)
598                {cw_b[x]=wavecode->b;}}
599                if(!isPerPointEquation(A_POINT_OP))
600                {for(x=0;x<wavecode->samples;x++)
601                {cw_a[x]=wavecode->a;}}
602              */
603             //put drawing code here
604             if (wavecode->bUseDots==1)   glBegin(GL_POINTS);
605             else   glBegin(GL_LINE_STRIP);
606
607             for(x=0;x<wavecode->samples;x++)
608             {
609                 //          printf("x:%f y:%f a:%f g:%f %f\n", wavecode->x_mesh[x], wavecode->y_mesh[x], wavecode->a_mesh[x], wavecode->g_mesh[x], wavecode->sample_mesh[x]);
610                 glColor4f(wavecode->r_mesh[x],wavecode->g_mesh[x],wavecode->b_mesh[x],wavecode->a_mesh[x]);
611                 glVertex3f(wavecode->x_mesh[x]*texsize,-(wavecode->y_mesh[x]-1)*texsize,-1);
612             }
613             glEnd();
614             glPointSize(texsize/512);
615             glLineWidth(texsize/512);
616             glDisable(GL_LINE_STIPPLE);
617             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
618             //  glPopMatrix();
619
620         }
621
622     }
623 }
624
625
626
627 void draw_shapes()
628 {
629     int i;
630
631     double theta;
632     double rad2;
633
634     double pi = 3.14159265;
635     double start,inc,xval,yval;
636     custom_shape_t *shapecode;
637
638     while ((shapecode = nextCustomShape()) != NULL)
639     {
640         if(shapecode->enabled==1)
641         {
642             // printf("drawing shape %f\n",shapecode->ang);
643             shapecode->y=-((shapecode->y)-1);
644             rad2=.5;
645             shapecode->rad=shapecode->rad*(texsize*.707*.707*.707*1.04);
646             //Additive Drawing or Overwrite
647             if (shapecode->additive==0)  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
648             else    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
649
650             glMatrixMode(GL_MODELVIEW);
651             glPushMatrix();
652             if(correction)
653             {
654                 glTranslatef(texsize*.5,texsize*.5, 0);
655                 glScalef(1.0,vw/(double)vh,1.0);
656                 glTranslatef((-texsize*.5) ,(-texsize*.5),0);
657             }
658
659             start=.78539+shapecode->ang;
660             inc=(pi*2)/(double)shapecode->sides;
661             xval=shapecode->x*texsize;
662             yval=shapecode->y*texsize;
663
664             if (shapecode->textured)
665             {
666                 glMatrixMode(GL_TEXTURE);
667                 glPushMatrix();
668                 glLoadIdentity();
669
670                 glTranslatef(.5,.5, 0);
671                 if (correction) glScalef(1,vw/(double)vh,1);
672
673                 glRotatef((shapecode->tex_ang*360/6.280), 0, 0, 1);
674
675                 glScalef(1/(shapecode->tex_zoom),1/(shapecode->tex_zoom),1);
676
677                 // glScalef(1,vh/(double)vw,1);
678                 glTranslatef((-.5) ,(-.5),0);
679                 // glScalef(1,vw/(double)vh,1);
680                 glEnable(GL_TEXTURE_2D);
681
682
683                 glBegin(GL_TRIANGLE_FAN);
684
685                 glColor4f(shapecode->r,shapecode->g,shapecode->b,shapecode->a);
686                 theta=start;
687                 glTexCoord2f(.5,.5);
688                 glVertex3f(xval,yval,-1);
689                 glColor4f(shapecode->r2,shapecode->g2,shapecode->b2,shapecode->a2);
690
691                 for ( i=0;i<shapecode->sides+1;i++)
692                 {
693
694                     theta+=inc;
695                     //  glColor4f(shapecode->r2,shapecode->g2,shapecode->b2,shapecode->a2);
696                     glTexCoord2f(rad2*cos(theta)+.5 ,rad2*sin(theta)+.5 );
697                     glVertex3f(shapecode->rad*cos(theta)+xval,shapecode->rad*sin(theta)+yval,-1);
698                 }
699                 glEnd();
700
701
702
703
704                 glDisable(GL_TEXTURE_2D);
705                 glPopMatrix();
706                 glMatrixMode(GL_MODELVIEW);
707             }
708             else
709             {//Untextured (use color values)
710                 //printf("untextured %f %f %f @:%f,%f %f %f\n",shapecode->a2,shapecode->a,shapecode->border_a, shapecode->x,shapecode->y,shapecode->rad,shapecode->ang);
711                 //draw first n-1 triangular pieces
712                 glBegin(GL_TRIANGLE_FAN);
713
714                 glColor4f(shapecode->r,shapecode->g,shapecode->b,shapecode->a);
715                 theta=start;
716                 // glTexCoord2f(.5,.5);
717                 glVertex3f(xval,yval,-1);
718                 glColor4f(shapecode->r2,shapecode->g2,shapecode->b2,shapecode->a2);
719
720                 for ( i=0;i<shapecode->sides+1;i++)
721                 {
722
723                     theta+=inc;
724                     //  glColor4f(shapecode->r2,shapecode->g2,shapecode->b2,shapecode->a2);
725                     //  glTexCoord2f(rad2*cos(theta)+.5 ,rad2*sin(theta)+.5 );
726                     glVertex3f(shapecode->rad*cos(theta)+xval,shapecode->rad*sin(theta)+yval,-1);
727                 }
728                 glEnd();
729
730
731             }
732             if (bWaveThick==1)  glLineWidth(2*texsize/512);
733             glBegin(GL_LINE_LOOP);
734             glColor4f(shapecode->border_r,shapecode->border_g,shapecode->border_b,shapecode->border_a);
735             for ( i=0;i<shapecode->sides;i++)
736             {
737                 theta+=inc;
738                 glVertex3f(shapecode->rad*cos(theta)+xval,shapecode->rad*sin(theta)+yval,-1);
739             }
740             glEnd();
741             if (bWaveThick==1)  glLineWidth(texsize/512);
742
743             glPopMatrix();
744         }
745     }
746
747 }
748
749
750 void draw_waveform()
751 {
752
753     int x;
754
755     double r,theta;
756
757     double offset,scale,dy2_adj;
758
759     double co;
760
761     double wave_x_temp=0;
762     double wave_y_temp=0;
763
764     modulate_opacity_by_volume();
765     maximize_colors();
766
767     if(bWaveDots==1) glEnable(GL_LINE_STIPPLE);
768
769     offset=(wave_x-.5)*texsize;
770     scale=texsize/505.0;
771
772     //Thick wave drawing
773     if (bWaveThick==1)  glLineWidth(2*texsize/512);
774
775     //Additive wave drawing (vice overwrite)
776     if (bAdditiveWaves==0)  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
777     else    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
778
779     switch(nWaveMode)
780     {
781         case 8://monitor
782
783             glPushMatrix();
784
785             glTranslatef(texsize*.5,texsize*.5, 0);
786             glRotated(-wave_mystery*90,0,0,1);
787
788             glTranslatef(-texsize*.5,-texsize*.825, 0);
789
790             /*
791                for (x=0;x<16;x++)
792                {
793                glBegin(GL_LINE_STRIP);
794                glColor4f(1.0-(x/15.0),.5,x/15.0,1.0);
795                glVertex3f((totalframes%256)*2*scale, -beat_val[x]*fWaveScale+texsize*wave_y,-1);
796                glColor4f(.5,.5,.5,1.0);
797                glVertex3f((totalframes%256)*2*scale, texsize*wave_y,-1);
798                glColor4f(1.0,1.0,0,1.0);
799             //glVertex3f((totalframes%256)*scale*2, beat_val_att[x]*fWaveScale+texsize*wave_y,-1);
800             glEnd();
801
802             glTranslatef(0,texsize*(1/36.0), 0);
803             }
804              */
805
806             glTranslatef(0,texsize*(1/18.0), 0);
807
808
809             glBegin(GL_LINE_STRIP);
810             glColor4f(1.0,1.0,0.5,1.0);
811             glVertex3f((totalframes%256)*2*scale, treb_att*5*fWaveScale+texsize*wave_y,-1);
812             glColor4f(.2,.2,.2,1.0);
813             glVertex3f((totalframes%256)*2*scale, texsize*wave_y,-1);
814             glColor4f(1.0,1.0,0,1.0);
815             glVertex3f((totalframes%256)*scale*2, treb*-5*fWaveScale+texsize*wave_y,-1);
816             glEnd();
817
818             glTranslatef(0,texsize*.075, 0);
819             glBegin(GL_LINE_STRIP);
820             glColor4f(0,1.0,0.0,1.0);
821             glVertex3f((totalframes%256)*2*scale, mid_att*5*fWaveScale+texsize*wave_y,-1);
822             glColor4f(.2,.2,.2,1.0);
823             glVertex3f((totalframes%256)*2*scale, texsize*wave_y,-1);
824             glColor4f(.5,1.0,.5,1.0);
825             glVertex3f((totalframes%256)*scale*2, mid*-5*fWaveScale+texsize*wave_y,-1);
826             glEnd();
827
828
829             glTranslatef(0,texsize*.075, 0);
830             glBegin(GL_LINE_STRIP);
831             glColor4f(1.0,0,0,1.0);
832             glVertex3f((totalframes%256)*2*scale, bass_att*5*fWaveScale+texsize*wave_y,-1);
833             glColor4f(.2,.2,.2,1.0);
834             glVertex3f((totalframes%256)*2*scale, texsize*wave_y,-1);
835             glColor4f(1.0,.5,.5,1.0);
836             glVertex3f((totalframes%256)*scale*2, bass*-5*fWaveScale+texsize*wave_y,-1);
837             glEnd();
838
839
840             glPopMatrix();
841             break;
842
843         case 0://circular waveforms
844             //  double co;
845             glPushMatrix();
846
847             glTranslatef(texsize*.5,texsize*.5, 0);
848             glScalef(1.0,vw/(double)vh,1.0);
849             glTranslatef((-texsize*.5) ,(-texsize*.5),0);
850
851             wave_y=-1*(wave_y-1.0);
852
853             glBegin(GL_LINE_STRIP);
854
855             for ( x=0;x<numsamples;x++)
856             {
857                 co= -(abs(x-((numsamples*.5)-1))/numsamples)+1;
858                 // printf("%d %f\n",x,co);
859                 theta=x*(6.28/numsamples);
860                 r= ((1+2*wave_mystery)*(texsize/5.0)+
861                     ( co*pcmdataL[x]+ (1-co)*pcmdataL[-(x-(numsamples-1))])
862                     *25*fWaveScale);
863
864                 glVertex3f(r*cos(theta)+(wave_x*texsize),r*sin(theta)+(wave_y*texsize),-1);
865             }
866
867             r= ( (1+2*wave_mystery)*(texsize/5.0)+
868                  (0.5*pcmdataL[0]+ 0.5*pcmdataL[numsamples-1])
869                  *20*fWaveScale);
870
871             glVertex3f(r*cos(0)+(wave_x*texsize),r*sin(0)+(wave_y*texsize),-1);
872
873             glEnd();
874             /*
875                glBegin(GL_LINE_LOOP);
876
877                for ( x=0;x<(512/pcmbreak);x++)
878                {
879                theta=(blockstart+x)*((6.28*pcmbreak)/512.0);
880                r= ((1+2*wave_mystery)*(texsize/5.0)+fdata_buffer[fbuffer][0][blockstart+x]*.0025*fWaveScale);
881
882                glVertex3f(r*cos(theta)+(wave_x*texsize),r*sin(theta)+(wave_y*texsize),-1);
883                }
884                glEnd();
885              */
886             glPopMatrix();
887
888             break;
889
890         case 1://circularly moving waveform
891             //  double co;
892             glPushMatrix();
893
894             glTranslatef(texsize*.5,texsize*.5, 0);
895             glScalef(1.0,vw/(double)vh,1.0);
896             glTranslatef((-texsize*.5) ,(-texsize*.5),0);
897
898             wave_y=-1*(wave_y-1.0);
899
900             glBegin(GL_LINE_STRIP);
901             //theta=(frame%512)*(6.28/512.0);
902
903             for ( x=1;x<512;x++)
904             {
905                 co= -(abs(x-255)/512.0)+1;
906                 // printf("%d %f\n",x,co);
907                 theta=((frame%256)*(2*6.28/512.0))+pcmdataL[x]*.2*fWaveScale;
908                 r= ((1+2*wave_mystery)*(texsize/5.0)+
909                     (pcmdataL[x]-pcmdataL[x-1])*80*fWaveScale);
910
911                 glVertex3f(r*cos(theta)+(wave_x*texsize),r*sin(theta)+(wave_y*texsize),-1);
912             }
913
914             glEnd();
915
916             glPopMatrix();
917
918             break;
919
920         case 2://EXPERIMENTAL
921             wave_y=-1*(wave_y-1.0);
922             glPushMatrix();
923             glBegin(GL_LINE_STRIP);
924             double xx,yy;
925             // double xr= (wave_x*texsize), yr=(wave_y*texsize);
926             xx=0;
927             for ( x=1;x<512;x++)
928             {
929                 //xx = ((pcmdataL[x]-pcmdataL[x-1])*80*fWaveScale)*2;
930                 xx += (pcmdataL[x]*fWaveScale);
931                 yy= pcmdataL[x]*80*fWaveScale;
932                 //  glVertex3f( (wave_x*texsize)+(xx+yy)*2, (wave_y*texsize)+(xx-yy)*2,-1);
933                 glVertex3f( (wave_x*texsize)+(xx)*2, (wave_y*texsize)+(yy)*2,-1);
934
935
936                 //   xr+=fdata_buffer[fbuffer][0][x] *.0005* fWaveScale;
937                 //yr=(fdata_buffer[fbuffer][0][x]-fdata_buffer[fbuffer][0][x-1])*.05*fWaveScale+(wave_y*texsize);
938                 //glVertex3f(xr,yr,-1);
939
940             }
941             glEnd();
942             glPopMatrix();
943             break;
944
945         case 3://EXPERIMENTAL
946             glPushMatrix();
947             wave_y=-1*(wave_y-1.0);
948             glBegin(GL_LINE_STRIP);
949
950
951             for ( x=1;x<512;x++)
952             {
953                 xx= ((pcmdataL[x]-pcmdataL[x-1])*80*fWaveScale)*2;
954                 yy=pcmdataL[x]*80*fWaveScale,-1;
955                 glVertex3f( (wave_x*texsize)+(xx+yy)*cos(45), (wave_y*texsize)+(-yy+xx)*cos(45),-1);
956             }
957             glEnd();
958             glPopMatrix();
959             break;
960
961         case 4://single x-axis derivative waveform
962             glPushMatrix();
963             wave_y=-1*(wave_y-1.0);
964             glTranslatef(texsize*.5,texsize*.5, 0);
965             glRotated(-wave_mystery*90,0,0,1);
966             glTranslatef(-texsize*.5,-texsize*.5, 0);
967             wave_x=(wave_x*.75)+.125;      wave_x=-(wave_x-1);
968             glBegin(GL_LINE_STRIP);
969
970             double dy_adj;
971             for ( x=1;x<512;x++)
972             {
973                 dy_adj=  pcmdataL[x]*20*fWaveScale-pcmdataL[x-1]*20*fWaveScale;
974                 glVertex3f((x*scale)+dy_adj, pcmdataL[x]*20*fWaveScale+texsize*wave_x,-1);
975             }
976             glEnd();
977             glPopMatrix();
978             break;
979
980         case 5://EXPERIMENTAL
981             glPushMatrix();
982
983
984             wave_y=-1*(wave_y-1.0);
985             wave_x_temp=(wave_x*.75)+.125;
986             wave_x_temp=-(wave_x_temp-1);
987             glBegin(GL_LINE_STRIP);
988
989             for ( x=1;x<(512);x++)
990             {
991                 dy2_adj=  (pcmdataL[x]-pcmdataL[x-1])*20*fWaveScale;
992                 glVertex3f((wave_x_temp*texsize)+dy2_adj*2, pcmdataL[x]*20*fWaveScale+texsize*wave_y,-1);
993             }
994             glEnd();
995             glPopMatrix();
996             break;
997
998         case 6://single waveform
999
1000
1001
1002
1003             glTranslatef(0,0, -1);
1004
1005             //glMatrixMode(GL_MODELVIEW);
1006             glPushMatrix();
1007             //            glLoadIdentity();
1008
1009             glTranslatef(texsize*.5,texsize*.5, 0);
1010             glRotated(-wave_mystery*90,0,0,1);
1011
1012             wave_x_temp=-2*0.4142*(abs(abs(wave_mystery)-.5)-.5);
1013             glScalef(1.0+wave_x_temp,1.0,1.0);
1014             glTranslatef(-texsize*.5,-texsize*.5, 0);
1015             wave_x_temp=-1*(wave_x-1.0);
1016
1017             glBegin(GL_LINE_STRIP);
1018             //      wave_x_temp=(wave_x*.75)+.125;
1019             //      wave_x_temp=-(wave_x_temp-1);
1020             for ( x=0;x<numsamples;x++)
1021             {
1022
1023                 //glVertex3f(x*scale, fdata_buffer[fbuffer][0][blockstart+x]*.0012*fWaveScale+texsize*wave_x_temp,-1);
1024                 glVertex3f(x*texsize/(double)numsamples, pcmdataR[x]*20*fWaveScale+texsize*wave_x_temp,-1);
1025
1026                 //glVertex3f(x*scale, texsize*wave_y_temp,-1);
1027             }
1028             //      printf("%f %f\n",texsize*wave_y_temp,wave_y_temp);
1029             glEnd();
1030             glPopMatrix();
1031             break;
1032
1033         case 7://dual waveforms
1034
1035             glPushMatrix();
1036
1037             glTranslatef(texsize*.5,texsize*.5, 0);
1038             glRotated(-wave_mystery*90,0,0,1);
1039
1040             wave_x_temp=-2*0.4142*(abs(abs(wave_mystery)-.5)-.5);
1041             glScalef(1.0+wave_x_temp,1.0,1.0);
1042             glTranslatef(-texsize*.5,-texsize*.5, 0);
1043
1044             wave_y_temp=-1*(wave_x-1);
1045
1046             glBegin(GL_LINE_STRIP);
1047
1048             for ( x=0;x<numsamples;x++)
1049             {
1050
1051                 glVertex3f((x*texsize)/(double)numsamples, pcmdataL[x]*20*fWaveScale+texsize*(wave_y_temp+(wave_y*wave_y*.5)),-1);
1052             }
1053             glEnd();
1054
1055             glBegin(GL_LINE_STRIP);
1056
1057
1058             for ( x=0;x<numsamples;x++)
1059             {
1060
1061                 glVertex3f((x*texsize)/(double)numsamples, pcmdataR[x]*20*fWaveScale+texsize*(wave_y_temp-(wave_y*wave_y*.5)),-1);
1062             }
1063             glEnd();
1064             glPopMatrix();
1065             break;
1066
1067         default:
1068             glBegin(GL_LINE_LOOP);
1069
1070             for ( x=0;x<512;x++)
1071             {
1072                 theta=(x)*(6.28/512.0);
1073                 r= (texsize/5.0+pcmdataL[x]*.002);
1074
1075                 glVertex3f(r*cos(theta)+(wave_x*texsize),r*sin(theta)+(wave_y*texsize),-1);
1076             }
1077             glEnd();
1078
1079             glBegin(GL_LINE_STRIP);
1080
1081             for ( x=0;x<512;x++)
1082             {
1083                 glVertex3f(x*scale, pcmdataL[x]*20*fWaveScale+(texsize*(wave_x+.1)),-1);
1084             }
1085             glEnd();
1086
1087             glBegin(GL_LINE_STRIP);
1088
1089             for ( x=0;x<512;x++)
1090             {
1091                 glVertex3f(x*scale, pcmdataR[x]*20*fWaveScale+(texsize*(wave_x-.1)),-1);
1092
1093             }
1094             glEnd();
1095             break;
1096             if (bWaveThick==1)  glLineWidth(2*texsize/512);
1097     }
1098     glLineWidth(texsize/512);
1099     glDisable(GL_LINE_STIPPLE);
1100 }
1101
1102
1103 void maximize_colors()
1104 {
1105     float wave_r_switch=0,wave_g_switch=0,wave_b_switch=0;
1106     //wave color brightening
1107     //
1108     //forces max color value to 1.0 and scales
1109     // the rest accordingly
1110
1111     if (bMaximizeWaveColor==1)
1112     {
1113         if(wave_r>=wave_g && wave_r>=wave_b)   //red brightest
1114         {
1115             wave_b_switch=wave_b*(1/wave_r);
1116             wave_g_switch=wave_g*(1/wave_r);
1117             wave_r_switch=1.0;
1118         }
1119         else if   (wave_b>=wave_g && wave_b>=wave_r)         //blue brightest
1120         {
1121             wave_r_switch=wave_r*(1/wave_b);
1122             wave_g_switch=wave_g*(1/wave_b);
1123             wave_b_switch=1.0;
1124
1125         }
1126
1127         else  if (wave_g>=wave_b && wave_g>=wave_r)         //green brightest
1128         {
1129             wave_b_switch=wave_b*(1/wave_g);
1130             wave_r_switch=wave_r*(1/wave_g);
1131             wave_g_switch=1.0;
1132         }
1133         glColor4f(wave_r_switch, wave_g_switch, wave_b_switch, wave_o);
1134     }
1135     else
1136     {
1137         glColor4f(wave_r, wave_g, wave_b, wave_o);
1138     }
1139
1140 }
1141
1142
1143 void modulate_opacity_by_volume()
1144
1145 {
1146     //modulate volume by opacity
1147     //
1148     //set an upper and lower bound and linearly
1149     //calculate the opacity from 0=lower to 1=upper
1150     //based on current volume
1151
1152     if (bModWaveAlphaByVolume==1)
1153     {if (vol<=fModWaveAlphaStart)  wave_o=0.0;
1154         else if (vol>=fModWaveAlphaEnd) wave_o=fWaveAlpha;
1155         else wave_o=fWaveAlpha*((vol-fModWaveAlphaStart)/(fModWaveAlphaEnd-fModWaveAlphaStart));}
1156     else wave_o=fWaveAlpha;
1157 }
1158
1159
1160 void draw_motion_vectors()
1161 {
1162     int x,y;
1163
1164     double offsetx=mv_dx*texsize, intervalx=texsize/(double)mv_x;
1165     double offsety=mv_dy*texsize, intervaly=texsize/(double)mv_y;
1166
1167     glPointSize(mv_l);
1168     glColor4f(mv_r, mv_g, mv_b, mv_a);
1169     glBegin(GL_POINTS);
1170     for (x=0;x<mv_x;x++){
1171         for(y=0;y<mv_y;y++){
1172             glVertex3f(offsetx+x*intervalx,offsety+y*intervaly,-1);
1173         }}
1174
1175     glEnd();
1176 }
1177
1178
1179 void draw_borders()
1180 {
1181     //no additive drawing for borders
1182     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1183
1184     glTranslatef(0,0,-1);
1185     //Draw Borders
1186     double of=texsize*ob_size*.5;
1187     double iff=(texsize*ib_size*.5);
1188     double texof=texsize-of;
1189
1190     glColor4d(ob_r,ob_g,ob_b,ob_a);
1191
1192     glRectd(0,0,of,texsize);
1193     glRectd(of,0,texof,of);
1194     glRectd(texof,0,texsize,texsize);
1195     glRectd(of,texsize,texof,texof);
1196     glColor4d(ib_r,ib_g,ib_b,ib_a);
1197     glRectd(of,of,of+iff,texof);
1198     glRectd(of+iff,of,texof-iff,of+iff);
1199     glRectd(texof-iff,of,texof,texof);
1200     glRectd(of+iff,texof,texof-iff,texof-iff);
1201 }
1202
1203
1204 //Here we render the interpolated mesh, and then apply the texture to it.
1205 //Well, we actually do the inverse, but its all the same.
1206 void render_interpolation()
1207 {
1208
1209     int x,y;
1210
1211     glMatrixMode(GL_MODELVIEW);
1212     glLoadIdentity();
1213     glTranslated(0, 0, -9);
1214
1215     glColor4f(0.0, 0.0, 0.0,decay);
1216
1217     glEnable(GL_TEXTURE_2D);
1218
1219     for (x=0;x<gx-1;x++)
1220     {
1221         glBegin(GL_TRIANGLE_STRIP);
1222         for(y=0;y<gy;y++)
1223         {
1224             glTexCoord4f(x_mesh[x][y], y_mesh[x][y],-1,1); glVertex4f(gridx[x][y], gridy[x][y],-1,1);
1225             glTexCoord4f(x_mesh[x+1][y], y_mesh[x+1][y],-1,1); glVertex4f(gridx[x+1][y], gridy[x+1][y],-1,1);
1226         }
1227         glEnd();
1228     }
1229     glDisable(GL_TEXTURE_2D);
1230 }
1231
1232
1233 void do_per_frame()
1234 {
1235     //Texture wrapping( clamp vs. wrap)
1236     if (bTexWrap==0)
1237     {
1238         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1239         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1240     }
1241     else
1242     {
1243         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1244         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1245     }
1246
1247
1248     //      glRasterPos2i(0,0);
1249     //      glClear(GL_COLOR_BUFFER_BIT);
1250     //      glColor4d(0.0, 0.0, 0.0,1.0);
1251
1252     //      glMatrixMode(GL_TEXTURE);
1253     //  glLoadIdentity();
1254
1255     glRasterPos2i(0,0);
1256     glClear(GL_COLOR_BUFFER_BIT);
1257     glColor4d(0.0, 0.0, 0.0,1.0);
1258
1259     glMatrixMode(GL_TEXTURE);
1260     glLoadIdentity();
1261
1262     glTranslatef(cx,cy, 0);
1263     if(correction)  glScalef(1,vw/(double)vh,1);
1264
1265     if(!isPerPixelEqn(ROT_OP))
1266     {
1267         //    printf("ROTATING: rot = %f\n", rot);
1268         glRotatef(rot*90, 0, 0, 1);
1269     }
1270     if(!isPerPixelEqn(SX_OP)) glScalef(1/sx,1,1);
1271     if(!isPerPixelEqn(SY_OP)) glScalef(1,1/sy,1);
1272
1273     if(correction) glScalef(1,vh/(double)vw,1);
1274     glTranslatef((-cx) ,(-cy),0);
1275
1276     if(!isPerPixelEqn(DX_OP)) glTranslatef(-dx,0,0);
1277     if(!isPerPixelEqn(DY_OP)) glTranslatef(0 ,-dy,0);
1278
1279 }
1280
1281
1282 //Actually draws the texture to the screen
1283 //
1284 //The Video Echo effect is also applied here
1285 void render_texture_to_screen()
1286 {
1287     glMatrixMode(GL_TEXTURE);
1288     glLoadIdentity();
1289
1290     glMatrixMode(GL_MODELVIEW);
1291     glLoadIdentity();
1292     glTranslatef(0, 0, -9);
1293
1294     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1295     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1296
1297     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,  GL_DECAL);
1298     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1299
1300     //       glClear(GL_ACCUM_BUFFER_BIT);
1301     glColor4d(0.0, 0.0, 0.0,1.0f);
1302
1303     glBegin(GL_QUADS);
1304     glVertex4d(-vw*.5,-vh*.5,-1,1);
1305     glVertex4d(-vw*.5,  vh*.5,-1,1);
1306     glVertex4d(vw*.5,  vh*.5,-1,1);
1307     glVertex4d(vw*.5, -vh*.5,-1,1);
1308     glEnd();
1309
1310     //      glBindTexture( GL_TEXTURE_2D, tex2 );
1311     glEnable(GL_TEXTURE_2D);
1312
1313     // glAccum(GL_LOAD,0);
1314     // if (bDarken==1)  glBlendFunc(GL_SRC_COLOR,GL_ZERO);
1315
1316     //Draw giant rectangle and texture it with our texture!
1317     glBegin(GL_QUADS);
1318     glTexCoord4d(0, 1,0,1); glVertex4d(-vw*.5,-vh*.5,-1,1);
1319     glTexCoord4d(0, 0,0,1); glVertex4d(-vw*.5,  vh*.5,-1,1);
1320     glTexCoord4d(1, 0,0,1); glVertex4d(vw*.5,  vh*.5,-1,1);
1321     glTexCoord4d(1, 1,0,1); glVertex4d(vw*.5, -vh*.5,-1,1);
1322     glEnd();
1323
1324     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1325
1326     //  if (bDarken==1)  glBlendFunc(GL_SRC_COLOR,GL_ONE_MINUS_SRC_ALPHA);
1327
1328     // if (bDarken==1) { glAccum(GL_ACCUM,1-fVideoEchoAlpha); glBlendFunc(GL_SRC_COLOR,GL_ZERO); }
1329
1330     glMatrixMode(GL_TEXTURE);
1331
1332     //draw video echo
1333     glColor4f(0.0, 0.0, 0.0,fVideoEchoAlpha);
1334     glTranslated(.5,.5,0);
1335     glScaled(1/fVideoEchoZoom,1/fVideoEchoZoom,1);
1336     glTranslated(-.5,-.5,0);
1337
1338     int flipx=1,flipy=1;
1339     switch (((int)nVideoEchoOrientation))
1340     {
1341         case 0: flipx=1;flipy=1;break;
1342         case 1: flipx=-1;flipy=1;break;
1343         case 2: flipx=1;flipy=-1;break;
1344         case 3: flipx=-1;flipy=-1;break;
1345         default: flipx=1;flipy=1; break;
1346     }
1347     glBegin(GL_QUADS);
1348     glTexCoord4d(0, 1,0,1); glVertex4f(-vw*.5*flipx,-vh*.5*flipy,-1,1);
1349     glTexCoord4d(0, 0,0,1); glVertex4f(-vw*.5*flipx,  vh*.5*flipy,-1,1);
1350     glTexCoord4d(1, 0,0,1); glVertex4f(vw*.5*flipx,  vh*.5*flipy,-1,1);
1351     glTexCoord4d(1, 1,0,1); glVertex4f(vw*.5*flipx, -vh*.5*flipy,-1,1);
1352     glEnd();
1353
1354
1355     glDisable(GL_TEXTURE_2D);
1356     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1357
1358     // if (bDarken==1) { glAccum(GL_ACCUM,fVideoEchoAlpha); glAccum(GL_RETURN,1);}
1359
1360     if (bInvert==1)
1361     {
1362         glColor4f(1.0, 1.0, 1.0,1.0);
1363         glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1364         glBegin(GL_QUADS);
1365         glVertex4f(-vw*.5*flipx,-vh*.5*flipy,-1,1);
1366         glVertex4f(-vw*.5*flipx,  vh*.5*flipy,-1,1);
1367         glVertex4f(vw*.5*flipx,  vh*.5*flipy,-1,1);
1368         glVertex4f(vw*.5*flipx, -vh*.5*flipy,-1,1);
1369         glEnd();
1370         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1371     }
1372 }
1373
1374
1375 void render_texture_to_studio()
1376 {
1377     glMatrixMode(GL_TEXTURE);
1378     glLoadIdentity();
1379
1380     glMatrixMode(GL_MODELVIEW);
1381     glLoadIdentity();
1382     glTranslatef(0, 0, -9);
1383
1384     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1385     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1386
1387     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,  GL_DECAL);
1388     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1389
1390     //       glClear(GL_ACCUM_BUFFER_BIT);
1391     glColor4f(0.0, 0.0, 0.0,0.04);
1392
1393     glVertex4d(-vw*.5,-vh*.5,-1,1);
1394     glVertex4d(-vw*.5,  vh*.5,-1,1);
1395     glVertex4d(vw*.5,  vh*.5,-1,1);
1396     glVertex4d(vw*.5, -vh*.5,-1,1);
1397     glEnd();
1398
1399     glColor4f(0.0, 0.0, 0.0,1.0);
1400
1401     glBegin(GL_QUADS);
1402     glVertex4d(-vw*.5,0,-1,1);
1403     glVertex4d(-vw*.5,  vh*.5,-1,1);
1404     glVertex4d(vw*.5,  vh*.5,-1,1);
1405     glVertex4d(vw*.5, 0,-1,1);
1406     glEnd();
1407
1408     glBegin(GL_QUADS);
1409     glVertex4d(0,-vh*.5,-1,1);
1410     glVertex4d(0,  vh*.5,-1,1);
1411     glVertex4d(vw*.5,  vh*.5,-1,1);
1412     glVertex4d(vw*.5, -vh*.5,-1,1);
1413     glEnd();
1414
1415     glPushMatrix();
1416     glTranslatef(.25*vw, .25*vh, 0);
1417     glScalef(.5,.5,1);
1418
1419     //      glBindTexture( GL_TEXTURE_2D, tex2 );
1420     glEnable(GL_TEXTURE_2D);
1421
1422     // glAccum(GL_LOAD,0);
1423     // if (bDarken==1)  glBlendFunc(GL_SRC_COLOR,GL_ZERO);
1424
1425     //Draw giant rectangle and texture it with our texture!
1426     glBegin(GL_QUADS);
1427     glTexCoord4d(0, 1,0,1); glVertex4d(-vw*.5,-vh*.5,-1,1);
1428     glTexCoord4d(0, 0,0,1); glVertex4d(-vw*.5,  vh*.5,-1,1);
1429     glTexCoord4d(1, 0,0,1); glVertex4d(vw*.5,  vh*.5,-1,1);
1430     glTexCoord4d(1, 1,0,1); glVertex4d(vw*.5, -vh*.5,-1,1);
1431     glEnd();
1432
1433     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1434
1435     //  if (bDarken==1)  glBlendFunc(GL_SRC_COLOR,GL_ONE_MINUS_SRC_ALPHA);
1436
1437     // if (bDarken==1) { glAccum(GL_ACCUM,1-fVideoEchoAlpha); glBlendFunc(GL_SRC_COLOR,GL_ZERO); }
1438
1439     glMatrixMode(GL_TEXTURE);
1440
1441     //draw video echo
1442     glColor4f(0.0, 0.0, 0.0,fVideoEchoAlpha);
1443     glTranslated(.5,.5,0);
1444     glScaled(1/fVideoEchoZoom,1/fVideoEchoZoom,1);
1445     glTranslated(-.5,-.5,0);
1446
1447     int flipx=1,flipy=1;
1448     switch (((int)nVideoEchoOrientation))
1449     {
1450         case 0: flipx=1;flipy=1;break;
1451         case 1: flipx=-1;flipy=1;break;
1452         case 2: flipx=1;flipy=-1;break;
1453         case 3: flipx=-1;flipy=-1;break;
1454         default: flipx=1;flipy=1; break;
1455     }
1456     glBegin(GL_QUADS);
1457     glTexCoord4d(0, 1,0,1); glVertex4f(-vw*.5*flipx,-vh*.5*flipy,-1,1);
1458     glTexCoord4d(0, 0,0,1); glVertex4f(-vw*.5*flipx,  vh*.5*flipy,-1,1);
1459     glTexCoord4d(1, 0,0,1); glVertex4f(vw*.5*flipx,  vh*.5*flipy,-1,1);
1460     glTexCoord4d(1, 1,0,1); glVertex4f(vw*.5*flipx, -vh*.5*flipy,-1,1);
1461     glEnd();
1462
1463     glDisable(GL_TEXTURE_2D);
1464     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1465
1466     // if (bDarken==1) { glAccum(GL_ACCUM,fVideoEchoAlpha); glAccum(GL_RETURN,1);}
1467
1468
1469     if (bInvert==1)
1470     {
1471         glColor4f(1.0, 1.0, 1.0,1.0);
1472         glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1473         glBegin(GL_QUADS);
1474         glVertex4f(-vw*.5*flipx,-vh*.5*flipy,-1,1);
1475         glVertex4f(-vw*.5*flipx,  vh*.5*flipy,-1,1);
1476         glVertex4f(vw*.5*flipx,  vh*.5*flipy,-1,1);
1477         glVertex4f(vw*.5*flipx, -vh*.5*flipy,-1,1);
1478         glEnd();
1479         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1480     }
1481
1482     //  glTranslated(.5,.5,0);
1483     //  glScaled(1/fVideoEchoZoom,1/fVideoEchoZoom,1);
1484     //   glTranslated(-.5,-.5,0);
1485     //glTranslatef(0,.5*vh,0);
1486
1487     //per_pixel monitor
1488     //glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1489     int x,y;
1490     glMatrixMode(GL_MODELVIEW);
1491     glPopMatrix();
1492     glPushMatrix();
1493     glTranslatef(.25*vw, -.25*vh, 0);
1494     glScalef(.5,.5,1);
1495     glColor4f(1,1,1,.6);
1496
1497     for (x=0;x<gx;x++)
1498     {
1499         glBegin(GL_LINE_STRIP);
1500         for(y=0;y<gy;y++)
1501         {
1502             glVertex4f((x_mesh[x][y]-.5)* vw, (y_mesh[x][y]-.5)*vh,-1,1);
1503             //glVertex4f((origx[x+1][y]-.5) * vw, (origy[x+1][y]-.5) *vh ,-1,1);
1504         }
1505         glEnd();
1506     }
1507
1508     for (y=0;y<gy;y++)
1509     {
1510         glBegin(GL_LINE_STRIP);
1511         for(x=0;x<gx;x++)
1512         {
1513             glVertex4f((x_mesh[x][y]-.5)* vw, (y_mesh[x][y]-.5)*vh,-1,1);
1514             //glVertex4f((origx[x+1][y]-.5) * vw, (origy[x+1][y]-.5) *vh ,-1,1);
1515         }
1516         glEnd();
1517     }
1518
1519     /*
1520        for (x=0;x<gx-1;x++){
1521        glBegin(GL_POINTS);
1522        for(y=0;y<gy;y++){
1523        glVertex4f((origx[x][y]-.5)* vw, (origy[x][y]-.5)*vh,-1,1);
1524        glVertex4f((origx[x+1][y]-.5) * vw, (origy[x+1][y]-.5) *vh ,-1,1);
1525        }
1526        glEnd();
1527        }
1528      */
1529     // glTranslated(-.5,-.5,0);     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1530     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1531     glMatrixMode(GL_MODELVIEW);
1532     glPopMatrix();
1533     glPushMatrix();
1534     glTranslatef(-.5*vw,0, 0);
1535
1536
1537     glTranslatef(0,-vh*.10, 0);
1538     glBegin(GL_LINE_STRIP);
1539     glColor4f(0,1.0,1.0,1.0);
1540     glVertex3f((((totalframes%256)/551.0))*vw, treb_att*-7,-1);
1541     glColor4f(1.0,1.0,1.0,1.0);
1542     glVertex3f((((totalframes%256)/551.0))*vw,0 ,-1);
1543     glColor4f(.5,1.0,1.0,1.0);
1544     glVertex3f((((totalframes%256)/551.0))*vw, treb*7,-1);
1545     glEnd();
1546
1547     glTranslatef(0,-vh*.13, 0);
1548     glBegin(GL_LINE_STRIP);
1549     glColor4f(0,1.0,0.0,1.0);
1550     glVertex3f((((totalframes%256)/551.0))*vw, mid_att*-7,-1);
1551     glColor4f(1.0,1.0,1.0,1.0);
1552     glVertex3f((((totalframes%256)/551.0))*vw,0 ,-1);
1553     glColor4f(.5,1.0,0.0,0.5);
1554     glVertex3f((((totalframes%256)/551.0))*vw, mid*7,-1);
1555     glEnd();
1556
1557     glTranslatef(0,-vh*.13, 0);
1558     glBegin(GL_LINE_STRIP);
1559     glColor4f(1.0,0.0,0.0,1.0);
1560     glVertex3f((((totalframes%256)/551.0))*vw, bass_att*-7,-1);
1561     glColor4f(1.0,1.0,1.0,1.0);
1562     glVertex3f((((totalframes%256)/551.0))*vw,0 ,-1);
1563     glColor4f(.7,0.2,0.2,1.0);
1564     glVertex3f((((totalframes%256)/551.0))*vw, bass*7,-1);
1565     glEnd();
1566
1567     glTranslatef(0,-vh*.13, 0);
1568     glBegin(GL_LINES);
1569
1570     glColor4f(1.0,1.0,1.0,1.0);
1571     glVertex3f((((totalframes%256)/551.0))*vw,0 ,-1);
1572     glColor4f(1.0,0.6,1.0,1.0);
1573     glVertex3f((((totalframes%256)/551.0))*vw, vol*7,-1);
1574     glEnd();
1575
1576     glPopMatrix();
1577 }
1578
1579