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