main.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:45k
源码类别:

多媒体

开发平台:

MultiPlatform

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