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