main.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:45k
源码类别:

midi

开发平台:

Unix_Linux

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