PageRenderTime 167ms CodeModel.GetById 19ms app.highlight 135ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/XBMCProjectM/libprojectM/Renderer.cpp

http://github.com/xbmc/xbmc
C++ | 2002 lines | 1189 code | 483 blank | 330 comment | 122 complexity | 61512b97320b3caf17fe879fa9eb5326 MD5 | raw file
   1#include "Renderer.hpp"
   2#include "wipemalloc.h"
   3#include "math.h"
   4#include "Common.hpp"
   5#include "CustomShape.hpp"
   6#include "CustomWave.hpp"
   7#include "KeyHandler.hpp"
   8#include "TextureManager.hpp"
   9#include <iostream>
  10#include <cassert>
  11#include "SectionLock.h"
  12
  13class Preset;
  14
  15Renderer::Renderer(int width, int height, int gx, int gy, int texsize, BeatDetect *beatDetect, std::string _presetURL, std::string _titlefontURL, std::string _menufontURL, int xpos, int ypos, bool usefbo): title_fontURL(_titlefontURL), menu_fontURL(_menufontURL), presetURL(_presetURL), m_presetName("None"), vw(width), vh(height), gx(gx), gy(gy), texsize(texsize), vx(xpos), vy(ypos), useFBO(usefbo)
  16{
  17	int x; int y;
  18	
  19	//  this->gx=gx;
  20	//  this->gy=gy;
  21#ifdef _USE_THREADS
  22    pthread_mutexattr_t attr;
  23    pthread_mutexattr_init(&attr);
  24    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  25    pthread_mutex_init(&_renderer_lock, &attr);
  26    pthread_mutexattr_destroy(&attr);
  27#endif
  28
  29	
  30	this->totalframes = 1;
  31	this->noSwitch = false;
  32	this->showfps = false;
  33	this->showtitle = false;
  34	this->showpreset = false;
  35	this->showhelp = false;
  36	this->showstats = false;
  37	this->studio = false;
  38	this->realfps=0;
  39	
  40	this->drawtitle=0;
  41	
  42	this->title = "Unknown";
  43	
  44	/** Other stuff... */
  45	this->correction = true;
  46	this->aspect=1.33333333;
  47	
  48	this->gridx=(float **)wipemalloc(gx * sizeof(float *));
  49	for(x = 0; x < gx; x++)
  50	{
  51		this->gridx[x] = (float *)wipemalloc(gy * sizeof(float));
  52	}
  53	this->gridy=(float **)wipemalloc(gx * sizeof(float *));
  54	for(x = 0; x < gx; x++)
  55	{
  56		this->gridy[x] = (float *)wipemalloc(gy * sizeof(float));
  57	}
  58	
  59	this->origx2=(float **)wipemalloc(gx * sizeof(float *));
  60	for(x = 0; x < gx; x++)
  61	{
  62		this->origx2[x] = (float *)wipemalloc(gy * sizeof(float));
  63	}
  64	this->origy2=(float **)wipemalloc(gx * sizeof(float *));
  65	for(x = 0; x < gx; x++)
  66	{
  67		this->origy2[x] = (float *)wipemalloc(gy * sizeof(float));
  68	}
  69	
  70	//initialize reference grid values
  71	for (x=0;x<gx;x++)
  72	{
  73		for(y=0;y<gy;y++)
  74		{
  75			
  76			float origx=x/(float)(gx-1);
  77			float origy=-((y/(float)(gy-1))-1);
  78			this->gridx[x][y]=origx;
  79			this->gridy[x][y]=origy;
  80			this->origx2[x][y]=( origx-.5)*2;
  81			this->origy2[x][y]=( origy-.5)*2;
  82			
  83		}
  84	}
  85	
  86	/// @bug put these on member init list
  87	this->renderTarget = new RenderTarget( texsize, width, height, useFBO );
  88	this->textureManager = new TextureManager(presetURL);
  89	this->beatDetect = beatDetect;
  90	
  91	
  92#ifdef USE_FTGL
  93	/**f Load the standard fonts */
  94	
  95	title_font = new FTGLPixmapFont(title_fontURL.c_str());
  96	other_font = new FTGLPixmapFont(menu_fontURL.c_str());
  97	other_font->UseDisplayList(true);
  98	title_font->UseDisplayList(true);
  99	
 100	
 101	poly_font = new FTGLExtrdFont(title_fontURL.c_str());
 102	
 103	poly_font->UseDisplayList(true);
 104	poly_font->Depth(20);
 105	poly_font->FaceSize(72);
 106	
 107	poly_font->UseDisplayList(true);
 108	
 109#endif /** USE_FTGL */
 110	
 111	
 112}
 113
 114void Renderer::ResetTextures()
 115{
 116        CSectionLock lock(&_renderer_lock);
 117	textureManager->Clear();
 118	
 119	delete(renderTarget);
 120	renderTarget = new RenderTarget(texsize, vw, vh, useFBO);
 121	reset(vw, vh);
 122	
 123	textureManager->Preload();
 124}
 125
 126void Renderer::RenderFrame(PresetOutputs *presetOutputs, PresetInputs *presetInputs)
 127{
 128	/** Save original view state */
 129	// TODO: check there is sufficient room on the stack
 130        CSectionLock lock(&_renderer_lock);
 131	glMatrixMode(GL_PROJECTION);
 132	glPushMatrix();
 133	glMatrixMode(GL_MODELVIEW);
 134	glPushMatrix();
 135	GLint viewport[4];
 136	glGetIntegerv(GL_VIEWPORT, viewport);
 137	
 138	totalframes++;		
 139	
 140	//BEGIN PASS 1
 141	//
 142	//This pass is used to render our texture
 143	//the texture is drawn to a FBO or a subsection of the framebuffer
 144	//and then we perform our manipulations on it in pass 2 we
 145	//will copy the image into texture memory and render the final image
 146	
 147	
 148	//Lock FBO
 149	renderTarget->lock();
 150	
 151	glViewport( 0, 0, renderTarget->texsize, renderTarget->texsize );
 152	
 153	glEnable( GL_TEXTURE_2D );
 154	
 155	//If using FBO, sitch to FBO texture
 156	if(this->renderTarget->useFBO)
 157	{
 158		glBindTexture( GL_TEXTURE_2D, renderTarget->textureID[1] );
 159	}
 160	else
 161	{
 162		glBindTexture( GL_TEXTURE_2D, renderTarget->textureID[0] );
 163	}
 164	
 165	glMatrixMode(GL_TEXTURE);
 166	glLoadIdentity();
 167	
 168	
 169	glMatrixMode( GL_PROJECTION );
 170	glLoadIdentity();
 171#ifdef USE_GLES1
 172	glOrthof(0.0, 1, 0.0, 1, -40, 40);
 173#else
 174	glOrtho(0.0, 1, 0.0, 1, -40, 40);
 175#endif
 176	glMatrixMode( GL_MODELVIEW );
 177	glLoadIdentity();
 178		
 179	
 180	if(this->renderTarget->useFBO)
 181	{
 182		//draw_motion_vectors();        //draw motion vectors
 183		//unlockPBuffer( this->renderTarget);
 184		//lockPBuffer( this->renderTarget, PBUFFER_PASS1 );
 185	}
 186	
 187	Interpolation(presetOutputs, presetInputs);
 188	
 189	//    if(!this->renderTarget->useFBO)
 190	{
 191		draw_motion_vectors(presetOutputs);
 192	}
 193	
 194	draw_shapes(presetOutputs);
 195	draw_custom_waves(presetOutputs);
 196	draw_waveform(presetOutputs);
 197	if(presetOutputs->bDarkenCenter)darken_center();
 198	draw_borders(presetOutputs);
 199	draw_title_to_texture();
 200	/** Restore original view state */
 201	glMatrixMode( GL_MODELVIEW );
 202	glPopMatrix();
 203	
 204	glMatrixMode( GL_PROJECTION );
 205	glPopMatrix();
 206	
 207	renderTarget->unlock();
 208	
 209	
 210#ifdef DEBUG
 211	GLint msd = 0, psd = 0;
 212	glGetIntegerv( GL_MODELVIEW_STACK_DEPTH, &msd );
 213	glGetIntegerv( GL_PROJECTION_STACK_DEPTH, &psd );
 214	DWRITE( "end pass1: modelview matrix depth: %d\tprojection matrix depth: %d\n", msd, psd );
 215	DWRITE( "begin pass2\n" );
 216#endif
 217	
 218	//BEGIN PASS 2
 219	//
 220	//end of texture rendering
 221	//now we copy the texture from the FBO or framebuffer to
 222	//video texture memory and render fullscreen.
 223	
 224	/** Reset the viewport size */
 225#ifdef USE_FBO
 226	if(renderTarget->renderToTexture)
 227	{
 228		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->renderTarget->fbuffer[1]);
 229		glViewport( 0, 0, this->renderTarget->texsize, this->renderTarget->texsize );
 230	}
 231	else 
 232#endif
 233		glViewport( viewport[0], viewport[1], viewport[2], viewport[3] );
 234	
 235	
 236	
 237	glBindTexture( GL_TEXTURE_2D, this->renderTarget->textureID[0] );
 238	
 239	glMatrixMode(GL_PROJECTION);
 240	glLoadIdentity();
 241#ifdef USE_GLES1
 242	glOrthof(-0.5, 0.5, -0.5, 0.5, -40, 40);	
 243#else
 244	glOrtho(-0.5, 0.5, -0.5, 0.5, -40, 40);	
 245#endif
 246	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 247	
 248	glLineWidth( this->renderTarget->texsize < 512 ? 1 : this->renderTarget->texsize/512.0);
 249	if(this->studio%2)render_texture_to_studio(presetOutputs, presetInputs);
 250	else render_texture_to_screen(presetOutputs);
 251	
 252	
 253	glMatrixMode(GL_MODELVIEW);
 254	glTranslatef(-0.5, -0.5, 0);
 255	
 256	// When console refreshes, there is a chance the preset has been changed by the user
 257	refreshConsole();
 258	draw_title_to_screen(false);
 259	if(this->showhelp%2) draw_help();
 260	if(this->showtitle%2) draw_title();
 261	if(this->showfps%2) draw_fps(this->realfps);
 262	if(this->showpreset%2) draw_preset();
 263	if(this->showstats%2) draw_stats(presetInputs);
 264	glTranslatef(0.5 , 0.5, 0);
 265	
 266#ifdef USE_FBO
 267	if(renderTarget->renderToTexture)
 268		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 269#endif
 270}
 271
 272
 273void Renderer::Interpolation(PresetOutputs *presetOutputs, PresetInputs *presetInputs)
 274{
 275        CSectionLock lock(&_renderer_lock);
 276	//Texture wrapping( clamp vs. wrap)
 277	if (presetOutputs->bTexWrap==0)
 278	{
 279#ifdef USE_GLES1
 280	  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 281	  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 282#else
 283	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 284	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 285#endif
 286	}
 287	else
 288	{ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 289	  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);}
 290	
 291	glMatrixMode(GL_TEXTURE);
 292	glLoadIdentity();
 293	
 294	glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
 295	
 296	glColor4f(1.0, 1.0, 1.0, presetOutputs->decay);
 297	
 298	glEnable(GL_TEXTURE_2D);
 299	
 300	int size = presetInputs->gy;
 301
 302#ifdef _WIN32PC
 303  // guessed value. solved in current projectM svn
 304  float p[30*2][2];
 305  float t[30*2][2];
 306#else
 307	float p[size*2][2];      
 308	float t[size*2][2];
 309#endif
 310
 311	glEnableClientState(GL_VERTEX_ARRAY);
 312	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 313	glDisableClientState(GL_COLOR_ARRAY);
 314	glVertexPointer(2,GL_FLOAT,0,p);
 315	glTexCoordPointer(2,GL_FLOAT,0,t);
 316    	
 317	for (int x=0;x<presetInputs->gx - 1;x++)
 318	{	       
 319		for(int y=0;y<presetInputs->gy;y++)
 320		{		
 321		  t[y*2][0] = presetOutputs->x_mesh[x][y];
 322		  t[y*2][1] = presetOutputs->y_mesh[x][y];
 323
 324		  p[y*2][0] = this->gridx[x][y];
 325		  p[y*2][1] = this->gridy[x][y];			    
 326
 327		  t[(y*2)+1][0] = presetOutputs->x_mesh[x+1][y];
 328		  t[(y*2)+1][1] = presetOutputs->y_mesh[x+1][y];
 329
 330		  p[(y*2)+1][0] = this->gridx[x+1][y];
 331		  p[(y*2)+1][1] = this->gridy[x+1][y];			    
 332
 333		}
 334	       	glDrawArrays(GL_TRIANGLE_STRIP,0,size*2);
 335	}
 336
 337
 338
 339	glDisable(GL_TEXTURE_2D);
 340	
 341	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 342	
 343}
 344
 345
 346Renderer::~Renderer()
 347{
 348	
 349	int x;
 350	
 351	
 352	if (renderTarget)
 353		delete(renderTarget);
 354	if (textureManager)
 355		delete(textureManager);
 356	
 357	assert(gx > 0);
 358	for(x = 0; x < this->gx; x++)
 359	{
 360		free(this->gridx[x]);
 361		free(this->gridy[x]);
 362		free(this->origx2[x]);
 363		free(this->origy2[x]);
 364	}
 365	
 366	
 367	//std::cerr << "freeing grids" << std::endl;
 368	free(this->origx2);
 369	free(this->origy2);
 370	free(this->gridx);
 371	free(this->gridy);
 372	
 373//std:cerr << "grid assign begin " << std::endl;
 374	this->origx2 = NULL;
 375	this->origy2 = NULL;
 376	this->gridx = NULL;
 377	this->gridy = NULL;
 378	
 379//std::cerr << "grid assign end" << std::endl;
 380	
 381#ifdef USE_FTGL
 382//	std::cerr << "freeing title fonts" << std::endl;
 383	if (title_font)
 384		delete title_font;
 385	if (poly_font)
 386		delete poly_font;
 387	if (other_font)
 388		delete other_font;
 389//	std::cerr << "freeing title fonts finished" << std::endl;
 390#endif
 391//	std::cerr << "exiting destructor" << std::endl;
 392#ifdef _USE_THREADS
 393        pthread_mutex_destroy(&_renderer_lock);
 394#endif
 395}
 396
 397
 398void Renderer::PerPixelMath(PresetOutputs * presetOutputs, PresetInputs * presetInputs)
 399{
 400	
 401	int x, y;
 402	float fZoom2, fZoom2Inv;
 403        CSectionLock lock(&_renderer_lock);
 404	
 405	
 406	for (x=0;x<this->gx;x++)
 407	{
 408		for(y=0;y<this->gy;y++)
 409		{
 410			fZoom2 = powf( presetOutputs->zoom_mesh[x][y], powf( presetOutputs->zoomexp_mesh[x][y], presetInputs->rad_mesh[x][y]*2.0f - 1.0f));
 411			fZoom2Inv = 1.0f/fZoom2;
 412			presetOutputs->x_mesh[x][y]= this->origx2[x][y]*0.5f*fZoom2Inv + 0.5f;
 413			presetOutputs->y_mesh[x][y]= this->origy2[x][y]*0.5f*fZoom2Inv + 0.5f;
 414		}
 415	}
 416	
 417	for (x=0;x<this->gx;x++)
 418	{
 419		for(y=0;y<this->gy;y++)
 420		{
 421			presetOutputs->x_mesh[x][y]  = ( presetOutputs->x_mesh[x][y] - presetOutputs->cx_mesh[x][y])/presetOutputs->sx_mesh[x][y] + presetOutputs->cx_mesh[x][y];
 422		}
 423	}
 424	
 425	for (x=0;x<this->gx;x++)
 426	{
 427		for(y=0;y<this->gy;y++)
 428		{
 429			presetOutputs->y_mesh[x][y] = ( presetOutputs->y_mesh[x][y] - presetOutputs->cy_mesh[x][y])/presetOutputs->sy_mesh[x][y] + presetOutputs->cy_mesh[x][y];
 430		}
 431	}
 432	
 433	float fWarpTime = presetInputs->time * presetOutputs->fWarpAnimSpeed;
 434	float fWarpScaleInv = 1.0f / presetOutputs->fWarpScale;
 435	float f[4];
 436	f[0] = 11.68f + 4.0f*cosf(fWarpTime*1.413f + 10);
 437	f[1] =  8.77f + 3.0f*cosf(fWarpTime*1.113f + 7);
 438	f[2] = 10.54f + 3.0f*cosf(fWarpTime*1.233f + 3);
 439	f[3] = 11.49f + 4.0f*cosf(fWarpTime*0.933f + 5);
 440	
 441	for (x=0;x<this->gx;x++)
 442	{
 443		for(y=0;y<this->gy;y++)
 444		{
 445			presetOutputs->x_mesh[x][y] += presetOutputs->warp_mesh[x][y]*0.0035f*sinf(fWarpTime*0.333f + fWarpScaleInv*(this->origx2[x][y]*f[0] - this->origy2[x][y]*f[3]));
 446			presetOutputs->y_mesh[x][y] += presetOutputs->warp_mesh[x][y]*0.0035f*cosf(fWarpTime*0.375f - fWarpScaleInv*(this->origx2[x][y]*f[2] + this->origy2[x][y]*f[1]));
 447			presetOutputs->x_mesh[x][y] += presetOutputs->warp_mesh[x][y]*0.0035f*cosf(fWarpTime*0.753f - fWarpScaleInv*(this->origx2[x][y]*f[1] - this->origy2[x][y]*f[2]));
 448			presetOutputs->y_mesh[x][y] += presetOutputs->warp_mesh[x][y]*0.0035f*sinf(fWarpTime*0.825f + fWarpScaleInv*(this->origx2[x][y]*f[0] + this->origy2[x][y]*f[3]));
 449		}
 450	}
 451	for (x=0;x<this->gx;x++)
 452	{
 453		for(y=0;y<this->gy;y++)
 454		{
 455			float u2 = presetOutputs->x_mesh[x][y] - presetOutputs->cx_mesh[x][y];
 456			float v2 = presetOutputs->y_mesh[x][y] - presetOutputs->cy_mesh[x][y];
 457			
 458			float cos_rot = cosf(presetOutputs->rot_mesh[x][y]);
 459			float sin_rot = sinf(presetOutputs->rot_mesh[x][y]);
 460			
 461			presetOutputs->x_mesh[x][y] = u2*cos_rot - v2*sin_rot + presetOutputs->cx_mesh[x][y];
 462			presetOutputs->y_mesh[x][y] = u2*sin_rot + v2*cos_rot + presetOutputs->cy_mesh[x][y];
 463			
 464		}
 465	}
 466	
 467	
 468	for (x=0;x<this->gx;x++)
 469	{
 470		for(y=0;y<this->gy;y++)
 471		{
 472			presetOutputs->x_mesh[x][y] -= presetOutputs->dx_mesh[x][y];
 473		}
 474	}
 475	
 476	
 477	
 478	for (x=0;x<this->gx;x++)
 479	{
 480		for(y=0;y<this->gy;y++)
 481		{
 482			presetOutputs->y_mesh[x][y] -= presetOutputs->dy_mesh[x][y];
 483		}
 484	}
 485	
 486}
 487
 488
 489
 490void Renderer::reset(int w, int h)
 491{
 492        CSectionLock lock(&_renderer_lock);
 493	this->aspect=(float)h / (float)w;
 494	this -> vw = w;
 495	this -> vh = h;
 496	
 497	glShadeModel( GL_SMOOTH);
 498	
 499	glCullFace( GL_BACK );
 500	//glFrontFace( GL_CCW );
 501	
 502	glClearColor( 0, 0, 0, 0 );
 503	
 504	glViewport( 0, 0, w, h );
 505	
 506	glMatrixMode(GL_TEXTURE);
 507	glLoadIdentity();
 508	
 509	glMatrixMode(GL_PROJECTION);
 510	glLoadIdentity();
 511	
 512	glMatrixMode(GL_MODELVIEW);
 513	glLoadIdentity();
 514	
 515#ifndef USE_GLES1
 516	glDrawBuffer(GL_BACK);
 517	glReadBuffer(GL_BACK);
 518#endif
 519	glEnable(GL_BLEND);
 520	
 521	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 522	
 523	glEnable( GL_LINE_SMOOTH );
 524	
 525	
 526//	glEnable(GL_POINT_SMOOTH);
 527	glClear(GL_COLOR_BUFFER_BIT);
 528	
 529#ifndef USE_GLES1
 530	glLineStipple(2, 0xAAAA);       
 531#endif
 532
 533	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 534	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 535	
 536	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,  GL_MODULATE);
 537	
 538	//glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
 539	//glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
 540	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 541	
 542	if (!this->renderTarget->useFBO)
 543	{
 544		this->renderTarget->fallbackRescale(w, h);
 545	}
 546}
 547
 548
 549void Renderer::draw_custom_waves(PresetOutputs *presetOutputs)
 550{
 551	
 552	int x;
 553	
 554        CSectionLock lock(&_renderer_lock);
 555	
 556	glPointSize(this->renderTarget->texsize < 512 ? 1 : this->renderTarget->texsize/512);
 557	
 558	for (PresetOutputs::cwave_container::const_iterator pos = presetOutputs->customWaves.begin();
 559	pos != presetOutputs->customWaves.end(); ++pos)
 560	{
 561		
 562		if( (*pos)->enabled==1)
 563		{
 564			
 565			if ( (*pos)->bAdditive==0)  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 566			else    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 567			if ( (*pos)->bDrawThick==1)
 568			{ glLineWidth(this->renderTarget->texsize < 512 ? 1 : 2*this->renderTarget->texsize/512);
 569			  glPointSize(this->renderTarget->texsize < 512 ? 1 : 2*this->renderTarget->texsize/512);
 570			  
 571			}
 572			beatDetect->pcm->getPCM( (*pos)->value1, (*pos)->samples, 0, (*pos)->bSpectrum, (*pos)->smoothing, 0);
 573			beatDetect->pcm->getPCM( (*pos)->value2, (*pos)->samples, 1, (*pos)->bSpectrum, (*pos)->smoothing, 0);
 574			// printf("%f\n",pcmL[0]);
 575			
 576			
 577			float mult= (*pos)->scaling*presetOutputs->fWaveScale*( (*pos)->bSpectrum ? 0.015f :1.0f);
 578			
 579			for(x=0;x< (*pos)->samples;x++)
 580			{ (*pos)->value1[x]*=mult;}
 581			
 582			for(x=0;x< (*pos)->samples;x++)
 583			{ (*pos)->value2[x]*=mult;}
 584			
 585			for(x=0;x< (*pos)->samples;x++)
 586			{ (*pos)->sample_mesh[x]=((float)x)/((float)( (*pos)->samples-1));}
 587			
 588			// printf("mid inner loop\n");
 589			(*pos)->evalPerPointEqns();
 590			
 591#ifdef _WIN32PC
 592      char buf[1024];
 593      sprintf(buf, "%s: samples = %d\n", __FUNCTION__,(*pos)->samples);
 594      OutputDebugString( buf );
 595      float colors[1024][4];
 596			float points[1024][2];
 597#else
 598			float colors[(*pos)->samples][4];
 599			float points[(*pos)->samples][2];
 600#endif
 601
 602			for(x=0;x< (*pos)->samples;x++)
 603			{
 604			  colors[x][0] = (*pos)->r_mesh[x];
 605			  colors[x][1] = (*pos)->g_mesh[x];
 606			  colors[x][2] = (*pos)->b_mesh[x];			  
 607			  colors[x][3] = (*pos)->a_mesh[x];			     
 608
 609			  points[x][0] = (*pos)->x_mesh[x];
 610			  points[x][1] =  -( (*pos)->y_mesh[x]-1);
 611		       
 612			}
 613			
 614			glEnableClientState(GL_VERTEX_ARRAY);
 615			glEnableClientState(GL_COLOR_ARRAY);	 
 616			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 617
 618			glVertexPointer(2,GL_FLOAT,0,points);
 619			glColorPointer(4,GL_FLOAT,0,colors);
 620		     		       
 621	
 622			if ( (*pos)->bUseDots==1)
 623		       	glDrawArrays(GL_POINTS,0,(*pos)->samples);
 624			else  	glDrawArrays(GL_LINE_STRIP,0,(*pos)->samples);
 625			
 626			glPointSize(this->renderTarget->texsize < 512 ? 1 : this->renderTarget->texsize/512);
 627			glLineWidth(this->renderTarget->texsize < 512 ? 1 : this->renderTarget->texsize/512);
 628#ifndef USE_GLES1
 629			glDisable(GL_LINE_STIPPLE);
 630#endif
 631			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 632			//  glPopMatrix();
 633			
 634		}
 635	}
 636	
 637	
 638}
 639
 640void Renderer::draw_shapes(PresetOutputs *presetOutputs)
 641{
 642	
 643	
 644        CSectionLock lock(&_renderer_lock);
 645	float radius;
 646	float xval, yval;
 647	float t;
 648	
 649	float aspect=this->aspect;	
 650	
 651	for (PresetOutputs::cshape_container::const_iterator pos = presetOutputs->customShapes.begin();
 652	pos != presetOutputs->customShapes.end(); ++pos)
 653	{
 654		
 655		if( (*pos)->enabled==1)
 656		{
 657			
 658			// printf("drawing shape %f\n", (*pos)->ang);
 659			(*pos)->y=-(( (*pos)->y)-1);
 660			radius=.5;
 661			(*pos)->radius= (*pos)->radius*(.707*.707*.707*1.04);
 662			//Additive Drawing or Overwrite
 663			if ( (*pos)->additive==0)  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 664			else    glBlendFunc(GL_SRC_ALPHA, GL_ONE);			
 665			
 666			xval= (*pos)->x;
 667			yval= (*pos)->y;
 668									
 669			if ( (*pos)->textured)
 670			{
 671				
 672				if ((*pos)->getImageUrl() !="")
 673				{
 674					GLuint tex = textureManager->getTexture((*pos)->getImageUrl());
 675					if (tex != 0)
 676					{
 677						glBindTexture(GL_TEXTURE_2D, tex);
 678						aspect=1.0;
 679					}
 680				}
 681
 682
 683				
 684				glMatrixMode(GL_TEXTURE);
 685				glPushMatrix();
 686				glLoadIdentity();
 687				
 688				glEnable(GL_TEXTURE_2D);
 689					
 690				glEnableClientState(GL_VERTEX_ARRAY);
 691				glEnableClientState(GL_COLOR_ARRAY);	 
 692				glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 693
 694#ifdef _WIN32PC
 695        char buf[1024];
 696        sprintf(buf, "%s: sides = %d\n", __FUNCTION__,(*pos)->sides);
 697        OutputDebugString( buf );
 698        float colors[1024][4];
 699				float tex[1024][2];
 700				float points[1024][2];
 701#else
 702				float colors[(*pos)->sides+2][4];
 703				float tex[(*pos)->sides+2][2];
 704				float points[(*pos)->sides+2][2];			
 705#endif
 706										
 707				//Define the center point of the shape
 708				colors[0][0] = (*pos)->r;
 709				colors[0][1] = (*pos)->g;
 710				colors[0][2] = (*pos)->b;
 711				colors[0][3] = (*pos)->a;
 712			  	   tex[0][0] = 0.5;
 713				   tex[0][1] = 0.5;
 714				points[0][0] = xval;
 715				points[0][1] = yval;     							     
 716				
 717				for ( int i=1;i< (*pos)->sides+2;i++)
 718				{
 719				  colors[i][0]= (*pos)->r2;
 720				  colors[i][1]=(*pos)->g2;
 721				  colors[i][2]=(*pos)->b2;
 722				  colors[i][3]=(*pos)->a2;
 723
 724				  t = (i-1)/(float) (*pos)->sides;
 725				  tex[i][0] =0.5f + 0.5f*cosf(t*3.1415927f*2 +  (*pos)->tex_ang + 3.1415927f*0.25f)*(this->correction ? aspect : 1.0)/ (*pos)->tex_zoom;
 726				  tex[i][1] =  0.5f + 0.5f*sinf(t*3.1415927f*2 +  (*pos)->tex_ang + 3.1415927f*0.25f)/ (*pos)->tex_zoom;
 727				  points[i][0]=(*pos)->radius*cosf(t*3.1415927f*2 +  (*pos)->ang + 3.1415927f*0.25f)*(this->correction ? aspect : 1.0)+xval;
 728				  points[i][1]=(*pos)->radius*sinf(t*3.1415927f*2 +  (*pos)->ang + 3.1415927f*0.25f)+yval;
 729				  
 730										
 731				
 732				}
 733					
 734				glVertexPointer(2,GL_FLOAT,0,points);
 735				glColorPointer(4,GL_FLOAT,0,colors);
 736				glTexCoordPointer(2,GL_FLOAT,0,tex);
 737
 738				glDrawArrays(GL_TRIANGLE_FAN,0,(*pos)->sides+2);
 739				
 740				glDisable(GL_TEXTURE_2D);
 741				glPopMatrix();
 742				glMatrixMode(GL_MODELVIEW);
 743				
 744				//Reset Texture state since we might have changed it
 745				if(this->renderTarget->useFBO)
 746				{
 747					glBindTexture( GL_TEXTURE_2D, renderTarget->textureID[1] );
 748				}
 749				else
 750				{
 751					glBindTexture( GL_TEXTURE_2D, renderTarget->textureID[0] );
 752				}
 753				
 754				
 755			}
 756			else
 757			{//Untextured (use color values)
 758				
 759
 760			  glEnableClientState(GL_VERTEX_ARRAY);
 761			  glEnableClientState(GL_COLOR_ARRAY);	 
 762			  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 763
 764#ifdef _WIN32PC
 765        char buf[1024];
 766        sprintf(buf, "%s: sides = %d\n", __FUNCTION__,(*pos)->sides+2);
 767        OutputDebugString( buf );
 768        float colors[1024][4];				
 769			  float points[1024][2];
 770#else
 771			  float colors[(*pos)->sides+2][4];				
 772			  float points[(*pos)->sides+2][2];			
 773#endif
 774			  
 775			  //Define the center point of the shape
 776			  colors[0][0]=(*pos)->r;
 777			  colors[0][1]=(*pos)->g;
 778			  colors[0][2]=(*pos)->b;
 779			  colors[0][3]=(*pos)->a;			       
 780			  points[0][0]=xval;
 781			  points[0][1]=yval;
 782			  
 783			  
 784			  
 785			  for ( int i=1;i< (*pos)->sides+2;i++)
 786			    {
 787			      colors[i][0]=(*pos)->r2;
 788			      colors[i][1]=(*pos)->g2;
 789			      colors[i][2]=(*pos)->b2;
 790			      colors[i][3]=(*pos)->a2;
 791			      t = (i-1)/(float) (*pos)->sides;
 792			      points[i][0]=(*pos)->radius*cosf(t*3.1415927f*2 +  (*pos)->ang + 3.1415927f*0.25f)*(this->correction ? aspect : 1.0)+xval;
 793			      points[i][1]=(*pos)->radius*sinf(t*3.1415927f*2 +  (*pos)->ang + 3.1415927f*0.25f)+yval;
 794			     			      			      
 795			    }
 796					
 797			  glVertexPointer(2,GL_FLOAT,0,points);
 798			  glColorPointer(4,GL_FLOAT,0,colors);
 799			  
 800			  
 801			  glDrawArrays(GL_TRIANGLE_FAN,0,(*pos)->sides+2);
 802			  //draw first n-1 triangular pieces
 803			  			  				
 804			}
 805			if (presetOutputs->bWaveThick==1)  glLineWidth(this->renderTarget->texsize < 512 ? 1 : 2*this->renderTarget->texsize/512);
 806
 807			glEnableClientState(GL_VERTEX_ARRAY);
 808			glDisableClientState(GL_COLOR_ARRAY);	 
 809
 810#ifdef _WIN32PC
 811      char buf[1024];
 812      sprintf(buf, "%s: sides = %d\n", __FUNCTION__,(*pos)->sides+1);
 813      OutputDebugString( buf );
 814      float points[1024][2];
 815#else
 816			float points[(*pos)->sides+1][2];
 817#endif
 818
 819			glColor4f( (*pos)->border_r, (*pos)->border_g, (*pos)->border_b, (*pos)->border_a);
 820			
 821			for ( int i=0;i< (*pos)->sides;i++)
 822			{
 823				t = (i-1)/(float) (*pos)->sides;
 824				points[i][0]= (*pos)->radius*cosf(t*3.1415927f*2 +  (*pos)->ang + 3.1415927f*0.25f)*(this->correction ? aspect : 1.0)+xval;
 825				points[i][1]=  (*pos)->radius*sinf(t*3.1415927f*2 +  (*pos)->ang + 3.1415927f*0.25f)+yval;
 826								
 827			}
 828
 829			glVertexPointer(2,GL_FLOAT,0,points);			 			  			  
 830			glDrawArrays(GL_LINE_LOOP,0,(*pos)->sides);
 831		      
 832			if (presetOutputs->bWaveThick==1)  glLineWidth(this->renderTarget->texsize < 512 ? 1 : this->renderTarget->texsize/512);
 833			
 834			
 835		}
 836	}
 837	
 838	
 839}
 840
 841void Renderer::WaveformMath(PresetOutputs *presetOutputs, PresetInputs *presetInputs, bool isSmoothing)
 842{
 843	
 844	int x;
 845	
 846	float r, theta;
 847	
 848	float offset, scale;
 849	
 850	float wave_x_temp=0;
 851	float wave_y_temp=0;
 852	
 853	float cos_rot;
 854	float sin_rot;
 855        CSectionLock lock(&_renderer_lock);
 856	
 857	offset=presetOutputs->wave_x-.5;
 858	scale=505.0/512.0;
 859	
 860	presetOutputs->two_waves = false;
 861	presetOutputs->draw_wave_as_loop = false;
 862
 863	switch(presetOutputs->nWaveMode)
 864	{
 865		
 866		case 0:
 867		  {
 868 		        presetOutputs->draw_wave_as_loop = true;
 869			presetOutputs->wave_rot =   0;
 870			presetOutputs->wave_scale =1.0;
 871			presetOutputs->wave_y=-1*(presetOutputs->wave_y-1.0);
 872			
 873			
 874			presetOutputs->wave_samples = isSmoothing ? 512-32 : beatDetect->pcm->numsamples;
 875
 876			float inv_nverts_minus_one = 1.0f/(float)(presetOutputs->wave_samples);
 877
 878	float last_value = beatDetect->pcm->pcmdataR[presetOutputs->wave_samples-1]+beatDetect->pcm->pcmdataL[presetOutputs->wave_samples-1];
 879			float first_value = beatDetect->pcm->pcmdataR[0]+beatDetect->pcm->pcmdataL[0];
 880			float offset = first_value-last_value;
 881		
 882			for ( x=0;x<presetOutputs->wave_samples;x++)
 883			{ 
 884			
 885
 886			  float value = beatDetect->pcm->pcmdataR[x]+beatDetect->pcm->pcmdataL[x];
 887			  value += offset * (x/(float)presetOutputs->wave_samples);
 888
 889			  r=(0.5 + 0.4f*.12*value*presetOutputs->fWaveScale + presetOutputs->wave_mystery)*.5;
 890			  theta=(x)*inv_nverts_minus_one*6.28f + presetInputs->time*0.2f;
 891			  
 892			  presetOutputs->wavearray[x][0]=(r*cos(theta)*(this->correction ? this->aspect : 1.0)+presetOutputs->wave_x);
 893			  presetOutputs->wavearray[x][1]=(r*sin(theta)+presetOutputs->wave_y);		       
 894			  
 895			}
 896		
 897		  }
 898			
 899			break;
 900			
 901		case 1://circularly moving waveform
 902			
 903			presetOutputs->wave_rot =   0;
 904			presetOutputs->wave_scale = this->vh/(float)this->vw;
 905			
 906			
 907			presetOutputs->wave_y=-1*(presetOutputs->wave_y-1.0);
 908			
 909			
 910			presetOutputs->wave_samples = 512-32;
 911			for ( x=0;x<(512-32);x++)
 912			{
 913				
 914				theta=beatDetect->pcm->pcmdataL[x+32]*0.06*presetOutputs->fWaveScale * 1.57 + presetInputs->time*2.3;
 915				r=(0.53 + 0.43*beatDetect->pcm->pcmdataR[x]*0.12*presetOutputs->fWaveScale+ presetOutputs->wave_mystery)*.5;
 916				
 917				presetOutputs->wavearray[x][0]=(r*cos(theta)*(this->correction ? this->aspect : 1.0)+presetOutputs->wave_x);
 918				presetOutputs->wavearray[x][1]=(r*sin(theta)+presetOutputs->wave_y);
 919			
 920			}
 921			
 922			
 923			
 924			break;
 925			
 926		case 2://EXPERIMENTAL
 927			
 928			
 929			presetOutputs->wave_y=-1*(presetOutputs->wave_y-1.0);
 930			presetOutputs->wave_rot =   0;
 931			presetOutputs->wave_scale =1.0;
 932			presetOutputs->wave_samples = 512-32;
 933			
 934			
 935			for (x=0; x<512-32; x++)
 936			{
 937				presetOutputs->wavearray[x][0]=(beatDetect->pcm->pcmdataR[x]*presetOutputs->fWaveScale*0.5*(this->correction ? this->aspect : 1.0) + presetOutputs->wave_x);
 938				
 939				presetOutputs->wavearray[x][1]=(beatDetect->pcm->pcmdataL[x+32]*presetOutputs->fWaveScale*0.5 + presetOutputs->wave_y);
 940			
 941			}
 942		       
 943			
 944			break;
 945			
 946		case 3://EXPERIMENTAL
 947			
 948			
 949			presetOutputs->wave_y=-1*(presetOutputs->wave_y-1.0);
 950			
 951			presetOutputs->wave_rot =   0;
 952			presetOutputs->wave_scale =1.0;
 953			
 954			
 955			presetOutputs->wave_samples = 512-32;
 956			
 957			for (x=0; x<512-32; x++)
 958			{
 959				presetOutputs->wavearray[x][0]=(beatDetect->pcm->pcmdataR[x] * presetOutputs->fWaveScale*0.5 + presetOutputs->wave_x);
 960				presetOutputs->wavearray[x][1]=( (beatDetect->pcm->pcmdataL[x+32]*presetOutputs->fWaveScale*0.5 + presetOutputs->wave_y));
 961			
 962			}
 963			
 964			
 965			break;
 966			
 967		case 4://single x-axis derivative waveform
 968		{
 969			
 970			presetOutputs->wave_rot =-presetOutputs->wave_mystery*90;
 971			presetOutputs->wave_scale=1.0;
 972			
 973			presetOutputs->wave_y=-1*(presetOutputs->wave_y-1.0);
 974			
 975			
 976			float w1 = 0.45f + 0.5f*(presetOutputs->wave_mystery*0.5f + 0.5f);
 977			float w2 = 1.0f - w1;
 978			float xx[512], yy[512];
 979			presetOutputs->wave_samples = 512-32;
 980			
 981			for (int i=0; i<512-32; i++)
 982			{
 983				xx[i] = -1.0f + 2.0f*(i/(512.0-32.0)) + presetOutputs->wave_x;
 984				yy[i] =0.4* beatDetect->pcm->pcmdataL[i]*0.47f*presetOutputs->fWaveScale + presetOutputs->wave_y;
 985				xx[i] += 0.4*beatDetect->pcm->pcmdataR[i]*0.44f*presetOutputs->fWaveScale;
 986				
 987				if (i>1)
 988				{
 989					xx[i] = xx[i]*w2 + w1*(xx[i-1]*2.0f - xx[i-2]);
 990					yy[i] = yy[i]*w2 + w1*(yy[i-1]*2.0f - yy[i-2]);
 991				}
 992				presetOutputs->wavearray[i][0]=xx[i];
 993				presetOutputs->wavearray[i][1]=yy[i];			    
 994			}											   		}
 995		break;
 996		
 997		case 5://EXPERIMENTAL
 998					       
 999			presetOutputs->wave_rot = 0;
1000			presetOutputs->wave_scale =1.0;
1001			
1002			presetOutputs->wave_y=-1*(presetOutputs->wave_y-1.0);
1003			
1004			cos_rot = cosf(presetInputs->time*0.3f);
1005			sin_rot = sinf(presetInputs->time*0.3f);
1006			presetOutputs->wave_samples = 512-32;		      
1007			
1008			for (x=0; x<512-32; x++)
1009			{
1010				float x0 = (beatDetect->pcm->pcmdataR[x]*beatDetect->pcm->pcmdataL[x+32] + beatDetect->pcm->pcmdataL[x+32]*beatDetect->pcm->pcmdataR[x]);
1011				float y0 = (beatDetect->pcm->pcmdataR[x]*beatDetect->pcm->pcmdataR[x] - beatDetect->pcm->pcmdataL[x+32]*beatDetect->pcm->pcmdataL[x+32]);
1012				presetOutputs->wavearray[x][0]=((x0*cos_rot - y0*sin_rot)*presetOutputs->fWaveScale*0.5*(this->correction ? this->aspect : 1.0) + presetOutputs->wave_x);
1013				presetOutputs->wavearray[x][1]=( (x0*sin_rot + y0*cos_rot)*presetOutputs->fWaveScale*0.5 + presetOutputs->wave_y);
1014			
1015			}
1016			
1017			
1018			
1019			break;
1020			
1021		case 6://single waveform
1022			
1023			
1024			
1025			wave_x_temp=-2*0.4142*(fabs(fabs(presetOutputs->wave_mystery)-.5)-.5);
1026			
1027			presetOutputs->wave_rot = -presetOutputs->wave_mystery*90;
1028			presetOutputs->wave_scale =1.0+wave_x_temp;					
1029			wave_x_temp=-1*(presetOutputs->wave_x-1.0);		
1030			presetOutputs->wave_samples = isSmoothing ? 512-32 : beatDetect->pcm->numsamples;
1031			
1032			for ( x=0;x<  presetOutputs->wave_samples;x++)
1033			{
1034				
1035				presetOutputs->wavearray[x][0]=x/(float)  presetOutputs->wave_samples;
1036				presetOutputs->wavearray[x][1]=beatDetect->pcm->pcmdataR[x]*.04*presetOutputs->fWaveScale+wave_x_temp;
1037				
1038			}
1039			//	  printf("%f %f\n",renderTarget->texsize*wave_y_temp,wave_y_temp);
1040			
1041			break;
1042			
1043		case 7://dual waveforms
1044			
1045		
1046			wave_x_temp=-2*0.4142*(fabs(fabs(presetOutputs->wave_mystery)-.5)-.5);
1047		    
1048			presetOutputs->wave_rot = -presetOutputs->wave_mystery*90;
1049			presetOutputs->wave_scale =1.0+wave_x_temp;
1050		     
1051			
1052			presetOutputs->wave_samples = isSmoothing ? 512-32 : beatDetect->pcm->numsamples;
1053			presetOutputs->two_waves = true;
1054		
1055			double y_adj = presetOutputs->wave_y*presetOutputs->wave_y*.5;
1056			
1057			wave_y_temp=-1*(presetOutputs->wave_x-1);
1058			
1059			for ( x=0;x<  presetOutputs->wave_samples ;x++)
1060			{
1061				presetOutputs->wavearray[x][0]=x/((float)  presetOutputs->wave_samples);
1062				presetOutputs->wavearray[x][1]= beatDetect->pcm->pcmdataL[x]*.04*presetOutputs->fWaveScale+(wave_y_temp+y_adj);
1063			
1064			}
1065			
1066			for ( x=0;x<  presetOutputs->wave_samples;x++)
1067			{
1068				
1069				presetOutputs->wavearray2[x][0]=x/((float)  presetOutputs->wave_samples);
1070				presetOutputs->wavearray2[x][1]=beatDetect->pcm->pcmdataR[x]*.04*presetOutputs->fWaveScale+(wave_y_temp-y_adj);
1071			
1072			}
1073			
1074			break;
1075			
1076		
1077	}
1078
1079	
1080}
1081
1082void Renderer::draw_waveform(PresetOutputs * presetOutputs)
1083{
1084	
1085        CSectionLock lock(&_renderer_lock);
1086	glMatrixMode( GL_MODELVIEW );
1087	glPushMatrix();
1088	glLoadIdentity();
1089	
1090	modulate_opacity_by_volume(presetOutputs);
1091	maximize_colors(presetOutputs);
1092	
1093#ifndef USE_GLES1
1094	if(presetOutputs->bWaveDots==1) glEnable(GL_LINE_STIPPLE);
1095#endif
1096	
1097	//Thick wave drawing
1098	if (presetOutputs->bWaveThick==1)  glLineWidth( (this->renderTarget->texsize < 512 ) ? 2 : 2*this->renderTarget->texsize/512);
1099	else glLineWidth( (this->renderTarget->texsize < 512 ) ? 1 : this->renderTarget->texsize/512);
1100	
1101	//Additive wave drawing (vice overwrite)
1102	if (presetOutputs->bAdditiveWaves==0)  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1103	else    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1104	
1105	glTranslatef(.5, .5, 0);
1106	glRotatef(presetOutputs->wave_rot, 0, 0, 1);
1107	glScalef(presetOutputs->wave_scale, 1.0, 1.0);
1108	glTranslatef(-.5, -.5, 0);
1109	
1110
1111	glEnableClientState(GL_VERTEX_ARRAY);
1112	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1113	glDisableClientState(GL_COLOR_ARRAY);
1114	glVertexPointer(2,GL_FLOAT,0,presetOutputs->wavearray);	
1115
1116	if (presetOutputs->draw_wave_as_loop) 
1117	  glDrawArrays(GL_LINE_LOOP,0,presetOutputs->wave_samples);
1118	else
1119	  glDrawArrays(GL_LINE_STRIP,0,presetOutputs->wave_samples);
1120
1121
1122	if (presetOutputs->two_waves)
1123	  {
1124	    glVertexPointer(2,GL_FLOAT,0,presetOutputs->wavearray2);
1125	    if (presetOutputs->draw_wave_as_loop) 
1126	      glDrawArrays(GL_LINE_LOOP,0,presetOutputs->wave_samples);
1127	    else
1128	      glDrawArrays(GL_LINE_STRIP,0,presetOutputs->wave_samples);
1129	  }
1130	
1131	
1132#ifndef USE_GLES1
1133	if(presetOutputs->bWaveDots==1) glDisable(GL_LINE_STIPPLE);
1134#endif	
1135
1136	glPopMatrix();
1137}
1138
1139void Renderer::maximize_colors(PresetOutputs *presetOutputs)
1140{
1141	
1142	float wave_r_switch=0, wave_g_switch=0, wave_b_switch=0;
1143        CSectionLock lock(&_renderer_lock);
1144	//wave color brightening
1145	//
1146	//forces max color value to 1.0 and scales
1147	// the rest accordingly
1148	if(presetOutputs->nWaveMode==2 || presetOutputs->nWaveMode==5)
1149	{
1150		switch(this->renderTarget->texsize)
1151		{
1152			case 256:  presetOutputs->wave_o *= 0.07f; break;
1153			case 512:  presetOutputs->wave_o *= 0.09f; break;
1154			case 1024: presetOutputs->wave_o *= 0.11f; break;
1155			case 2048: presetOutputs->wave_o *= 0.13f; break;
1156		}
1157	}
1158	
1159	else if(presetOutputs->nWaveMode==3)
1160	{
1161		switch(this->renderTarget->texsize)
1162		{
1163			case 256:  presetOutputs->wave_o *= 0.075f; break;
1164			case 512:  presetOutputs->wave_o *= 0.15f; break;
1165			case 1024: presetOutputs->wave_o *= 0.22f; break;
1166			case 2048: presetOutputs->wave_o *= 0.33f; break;
1167		}
1168		presetOutputs->wave_o*=1.3f;
1169		presetOutputs->wave_o*=powf(beatDetect->treb , 2.0f);
1170	}
1171	
1172	if (presetOutputs->bMaximizeWaveColor==1)
1173	{
1174		if(presetOutputs->wave_r>=presetOutputs->wave_g && presetOutputs->wave_r>=presetOutputs->wave_b)   //red brightest
1175		{
1176			wave_b_switch=presetOutputs->wave_b*(1/presetOutputs->wave_r);
1177			wave_g_switch=presetOutputs->wave_g*(1/presetOutputs->wave_r);
1178			wave_r_switch=1.0;
1179		}
1180		else if   (presetOutputs->wave_b>=presetOutputs->wave_g && presetOutputs->wave_b>=presetOutputs->wave_r)         //blue brightest
1181		{
1182			wave_r_switch=presetOutputs->wave_r*(1/presetOutputs->wave_b);
1183			wave_g_switch=presetOutputs->wave_g*(1/presetOutputs->wave_b);
1184			wave_b_switch=1.0;
1185			
1186		}
1187		
1188		else  if (presetOutputs->wave_g>=presetOutputs->wave_b && presetOutputs->wave_g>=presetOutputs->wave_r)         //green brightest
1189		{
1190			wave_b_switch=presetOutputs->wave_b*(1/presetOutputs->wave_g);
1191			wave_r_switch=presetOutputs->wave_r*(1/presetOutputs->wave_g);
1192			wave_g_switch=1.0;
1193		}
1194		
1195		
1196		glColor4f(wave_r_switch, wave_g_switch, wave_b_switch, presetOutputs->wave_o);
1197	}
1198	else
1199	{
1200		glColor4f(presetOutputs->wave_r, presetOutputs->wave_g, presetOutputs->wave_b, presetOutputs->wave_o);
1201	}
1202	
1203}
1204
1205void Renderer::darken_center()
1206{
1207	
1208        CSectionLock lock(&_renderer_lock);
1209	float unit=0.05f;
1210	
1211	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1212
1213	float colors[6][4] = {{0, 0, 0, 3.0f/32.0f},
1214			      {0, 0, 0, 0},
1215			      {0, 0, 0, 0},
1216			      {0, 0, 0, 0},
1217			      {0, 0, 0, 0},
1218			      {0, 0, 0, 0}};
1219
1220	float points[6][2] = {{ 0.5,  0.5},
1221			      { 0.45, 0.5},
1222			      { 0.5,  0.45},
1223			      { 0.55, 0.5},
1224			      { 0.5,  0.55},
1225			      { 0.45, 0.5}};
1226
1227	glEnableClientState(GL_VERTEX_ARRAY);
1228	glEnableClientState(GL_COLOR_ARRAY);	 
1229	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1230	
1231	glVertexPointer(2,GL_FLOAT,0,points);
1232	glColorPointer(4,GL_FLOAT,0,colors);
1233		       
1234	glDrawArrays(GL_TRIANGLE_FAN,0,6);
1235
1236}
1237
1238
1239void Renderer::modulate_opacity_by_volume(PresetOutputs *presetOutputs)
1240{
1241	
1242	//modulate volume by opacity
1243	//
1244	//set an upper and lower bound and linearly
1245	//calculate the opacity from 0=lower to 1=upper
1246	//based on current volume
1247	
1248        CSectionLock lock(&_renderer_lock);
1249	
1250	if (presetOutputs->bModWaveAlphaByVolume==1)
1251	{if (beatDetect->vol<=presetOutputs->fModWaveAlphaStart)  presetOutputs->wave_o=0.0;
1252	 else if (beatDetect->vol>=presetOutputs->fModWaveAlphaEnd) presetOutputs->wave_o=presetOutputs->fWaveAlpha;
1253	 else presetOutputs->wave_o=presetOutputs->fWaveAlpha*((beatDetect->vol-presetOutputs->fModWaveAlphaStart)/(presetOutputs->fModWaveAlphaEnd-presetOutputs->fModWaveAlphaStart));}
1254	else presetOutputs->wave_o=presetOutputs->fWaveAlpha;
1255}
1256
1257void Renderer::draw_motion_vectors(PresetOutputs *presetOutputs)
1258{  	
1259        CSectionLock lock(&_renderer_lock);
1260
1261	glEnableClientState(GL_VERTEX_ARRAY);
1262	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1263	glDisableClientState(GL_COLOR_ARRAY);
1264	
1265	float offsetx=presetOutputs->mv_dx, intervalx=1.0/(float)presetOutputs->mv_x;
1266	float offsety=presetOutputs->mv_dy, intervaly=1.0/(float)presetOutputs->mv_y;
1267	
1268	
1269	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1270	
1271	glPointSize(presetOutputs->mv_l);
1272	glColor4f(presetOutputs->mv_r, presetOutputs->mv_g, presetOutputs->mv_b, presetOutputs->mv_a);
1273	
1274	int numx = static_cast<int>(presetOutputs->mv_x);
1275	int numy = static_cast<int>(presetOutputs->mv_y);
1276
1277	if (numx + numy < 600)
1278	  {
1279	int size = numx * numy;
1280
1281#ifdef _WIN32PC
1282  // guessed value. solved in current projectM svn
1283  char buf[1024];
1284  sprintf(buf, "%s: size = %d\n", __FUNCTION__,size);
1285  OutputDebugString( buf );
1286  float points[1024][2];
1287#else
1288	float points[size][2];
1289#endif
1290	
1291
1292       
1293	for (int x=0;x<numx;x++)
1294	{
1295		for(int y=0;y<numy;y++)
1296		{
1297			float lx, ly, lz;
1298			lx = offsetx+x*intervalx;
1299			ly = offsety+y*intervaly;
1300 			
1301			points[(x * numy) + y][0] = lx;
1302			points[(x * numy) + y][1] = ly;
1303		}
1304	}
1305
1306	glVertexPointer(2,GL_FLOAT,0,points);
1307	glDrawArrays(GL_POINTS,0,size);
1308	  }
1309	
1310}
1311
1312GLuint Renderer::initRenderToTexture()
1313{
1314	return renderTarget->initRenderToTexture();
1315}
1316
1317void Renderer::draw_borders(PresetOutputs *presetOutputs)
1318{
1319        CSectionLock lock(&_renderer_lock);
1320  	glEnableClientState(GL_VERTEX_ARRAY);
1321	glDisableClientState(GL_COLOR_ARRAY);	 
1322	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1323	//Draw Borders
1324	float of=presetOutputs->ob_size*.5;
1325	float iff=presetOutputs->ib_size*.5;
1326	float texof=1.0-of;
1327	
1328	//no additive drawing for borders
1329	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1330	
1331	glColor4f(presetOutputs->ob_r, presetOutputs->ob_g, presetOutputs->ob_b, presetOutputs->ob_a);
1332
1333
1334  
1335	float pointsA[4][2] = {{0,0},{0,1},{of,0},{of,1}};	
1336	glVertexPointer(2,GL_FLOAT,0,pointsA);    
1337	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1338
1339	float pointsB[4][2] = {{of,0},{of,of},{texof,0},{texof,of}};
1340	glVertexPointer(2,GL_FLOAT,0,pointsB);    
1341	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1342
1343	float pointsC[4][2] = {{texof,0},{texof,1},{1,0},{1,1}};
1344	glVertexPointer(2,GL_FLOAT,0,pointsC);    
1345	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1346
1347	float pointsD[4][2] = {{of,1},{of,texof},{texof,1},{texof,texof}};
1348	glVertexPointer(2,GL_FLOAT,0,pointsD);    
1349	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1350
1351	glColor4f(presetOutputs->ib_r, presetOutputs->ib_g, presetOutputs->ib_b, presetOutputs->ib_a);
1352
1353	glRectd(of, of, of+iff, texof);
1354	glRectd(of+iff, of, texof-iff, of+iff);
1355	glRectd(texof-iff, of, texof, texof);
1356	glRectd(of+iff, texof, texof-iff, texof-iff);
1357  
1358	float pointsE[4][2] = {{of,of},{of,texof},{of+iff,of},{of+iff,texof}};	
1359	glVertexPointer(2,GL_FLOAT,0,pointsE);    
1360	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1361
1362	float pointsF[4][2] = {{of+iff,of},{of+iff,of+iff},{texof-iff,of},{texof-iff,of+iff}};
1363	glVertexPointer(2,GL_FLOAT,0,pointsF);    
1364	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1365
1366	float pointsG[4][2] = {{texof-iff,of},{texof-iff,texof},{texof,of},{texof,texof}};
1367	glVertexPointer(2,GL_FLOAT,0,pointsG);    
1368	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1369
1370	float pointsH[4][2] = {{of+iff,texof},{of+iff,texof-iff},{texof-iff,texof},{texof-iff,texof-iff}};
1371	glVertexPointer(2,GL_FLOAT,0,pointsH);    
1372	glDrawArrays(GL_TRIANGLE_STRIP,0,4);
1373
1374	
1375
1376}
1377
1378
1379
1380void Renderer::draw_title_to_texture()
1381{
1382        CSectionLock lock(&_renderer_lock);
1383#ifdef USE_FTGL
1384	if (this->drawtitle>100)
1385	{
1386		draw_title_to_screen(true);
1387		this->drawtitle=0;
1388	}
1389#endif /** USE_FTGL */
1390}
1391
1392/*
1393void setUpLighting()
1394{
1395        CSectionLock lock(&_renderer_lock);
1396	// Set up lighting.
1397	float light1_ambient[4]  = { 1.0, 1.0, 1.0, 1.0 };
1398	float light1_diffuse[4]  = { 1.0, 0.9, 0.9, 1.0 };
1399	float light1_specular[4] = { 1.0, 0.7, 0.7, 1.0 };
1400	float light1_position[4] = { -1.0, 1.0, 1.0, 0.0 };
1401	glLightfv(GL_LIGHT1, GL_AMBIENT,  light1_ambient);
1402	glLightfv(GL_LIGHT1, GL_DIFFUSE,  light1_diffuse);
1403	glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
1404	glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
1405	//glEnable(GL_LIGHT1);
1406	
1407	float light2_ambient[4]  = { 0.2, 0.2, 0.2, 1.0 };
1408	float light2_diffuse[4]  = { 0.9, 0.9, 0.9, 1.0 };
1409	float light2_specular[4] = { 0.7, 0.7, 0.7, 1.0 };
1410	float light2_position[4] = { 0.0, -1.0, 1.0, 0.0 };
1411	glLightfv(GL_LIGHT2, GL_AMBIENT,  light2_ambient);
1412	glLightfv(GL_LIGHT2, GL_DIFFUSE,  light2_diffuse);
1413	glLightfv(GL_LIGHT2, GL_SPECULAR, light2_specular);
1414	glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
1415	glEnable(GL_LIGHT2);
1416	
1417	float front_emission[4] = { 0.3, 0.2, 0.1, 0.0 };
1418	float front_ambient[4]  = { 0.2, 0.2, 0.2, 0.0 };
1419	float front_diffuse[4]  = { 0.95, 0.95, 0.8, 0.0 };
1420	float front_specular[4] = { 0.6, 0.6, 0.6, 0.0 };
1421	glMaterialfv(GL_FRONT, GL_EMISSION, front_emission);
1422	glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient);
1423	glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse);
1424	glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular);
1425	glMaterialf(GL_FRONT, GL_SHININESS, 16.0);
1426	glColor4fv(front_diffuse);
1427	
1428	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
1429	glColorMaterial(GL_FRONT, GL_DIFFUSE);
1430	glEnable(GL_COLOR_MATERIAL);
1431	
1432	glEnable(GL_LIGHTING);
1433}
1434*/
1435
1436float title_y;
1437
1438void Renderer::draw_title_to_screen(bool flip)
1439{
1440        CSectionLock lock(&_renderer_lock);
1441	
1442#ifdef USE_FTGL
1443	if(this->drawtitle>0)
1444	{
1445		
1446	  //setUpLighting();
1447		
1448		//glEnable(GL_POLYGON_SMOOTH);
1449		//glEnable( GL_CULL_FACE);
1450		glEnable(GL_DEPTH_TEST);
1451		glClear(GL_DEPTH_BUFFER_BIT);
1452		
1453		int draw;
1454		if (drawtitle>=80) draw = 80;
1455		else draw = drawtitle;
1456		
1457		float easein = ((80-draw)*.0125);
1458		float easein2 = easein * easein;
1459		
1460		if(drawtitle==1)
1461		{
1462			title_y = (float)rand()/RAND_MAX;
1463			title_y *= 2;
1464			title_y -= 1;
1465			title_y *= .6;
1466		}
1467		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1468		//glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE);
1469		glColor4f(1.0, 1.0, 1.0, 1.0);
1470		
1471		glMatrixMode(GL_PROJECTION);
1472		glPushMatrix();
1473		glLoadIdentity();
1474		
1475		glFrustum(-1, 1, -1 * (float)vh/(float)vw, 1 *(float)vh/(float)vw, 1, 1000);
1476		if (flip) glScalef(1, -1, 1);
1477		glMatrixMode(GL_MODELVIEW);
1478		glPushMatrix();
1479		glLoadIdentity();
1480		
1481		glTranslatef(-850, title_y * 850 *vh/vw  , easein2*900-900);
1482		
1483		glRotatef(easein2*360, 1, 0, 0);
1484		
1485		poly_font->Render(this->title.c_str() );
1486		
1487		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1488		
1489		this->drawtitle++;
1490		
1491		glPopMatrix();
1492		glMatrixMode(GL_PROJECTION);
1493		glPopMatrix();
1494		
1495		glMatrixMode(GL_MODELVIEW);
1496		
1497		glDisable( GL_CULL_FACE);
1498		glDisable( GL_DEPTH_TEST);
1499		
1500		glDisable(GL_COLOR_MATERIAL);
1501		
1502		glDisable(GL_LIGHTING);
1503		glDisable(GL_POLYGON_SMOOTH);
1504	}
1505#endif /** USE_FTGL */
1506}
1507
1508
1509
1510void Renderer::draw_title()
1511{
1512#ifdef USE_FTGL
1513        CSectionLock lock(&_renderer_lock);
1514	//glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1515	
1516	glColor4f(1.0, 1.0, 1.0, 1.0);
1517	//  glPushMatrix();
1518	//  glTranslatef(this->vw*.001,this->vh*.03, -1);
1519	//  glScalef(this->vw*.015,this->vh*.025,0);
1520	
1521	glRasterPos2f(0.01, 0.05);
1522	title_font->FaceSize( (unsigned)(20*(this->vh/512.0)));
1523	
1524	
1525	title_font->Render(this->title.c_str() );
1526	//  glPopMatrix();
1527	//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1528	
1529#endif /** USE_FTGL */
1530}
1531
1532void Renderer::draw_preset()
1533{
1534#ifdef USE_FTGL
1535        CSectionLock lock(&_renderer_lock);
1536	//glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1537	
1538	glColor4f(1.0, 1.0, 1.0, 1.0);
1539	//      glPushMatrix();
1540	//glTranslatef(this->vw*.001,this->vh*-.01, -1);
1541	//glScalef(this->vw*.003,this->vh*.004,0);
1542	
1543	
1544	glRasterPos2f(0.01, 0.01);
1545	
1546	title_font->FaceSize((unsigned)(12*(this->vh/512.0)));
1547	if(this->noSwitch) title_font->Render("[LOCKED]  " );
1548	title_font->FaceSize((unsigned)(20*(this->vh/512.0)));
1549	
1550	title_font->Render(this->presetName().c_str() );
1551	
1552	
1553	
1554	//glPopMatrix();
1555	// glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1556#endif /** USE_FTGL */
1557}
1558
1559void Renderer::draw_help( )
1560{
1561	
1562#ifdef USE_FTGL
1563        CSectionLock lock(&_renderer_lock);
1564//glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1565
1566	glColor4f(1.0, 1.0, 1.0, 1.0);
1567	glPushMatrix();
1568	glTranslatef(0, 1, 0);
1569	//glScalef(this->vw*.02,this->vh*.02 ,0);
1570	
1571	
1572	title_font->FaceSize((unsigned)( 18*(this->vh/512.0)));
1573	
1574	glRasterPos2f(0.01, -0.05);
1575	title_font->Render("Help");
1576	
1577	glRasterPos2f(0.01, -0.09);
1578	title_font->Render("----------------------------");
1579	
1580	glRasterPos2f(0.01, -0.13);
1581	title_font->Render("F1: This help menu");
1582	
1583	glRasterPos2f(0.01, -0.17);
1584	title_font->Render("F2: Show song title");
1585	
1586	glRasterPos2f(0.01, -0.21);
1587	title_font->Render("F3: Show preset name");
1588	
1589	glRasterPos2f(0.01, -0.25);
1590	title_font->Render("F4: Show Rendering Settings");
1591	
1592	glRasterPos2f(0.01, -0.29);
1593	title_font->Render("F5: Show FPS");
1594	
1595	glRasterPos2f(0.01, -0.35);
1596	title_font->Render("F: Fullscreen");
1597	
1598	glRasterPos2f(0.01, -0.39);
1599	title_font->Render("L: Lock/Unlock Preset");
1600	
1601	glRasterPos2f(0.01, -0.43);
1602	title_font->Render("M: Show Menu");
1603	
1604	glRasterPos2f(0.01, -0.49);
1605	title_font->Render("R: Random preset");
1606	glRasterPos2f(0.01, -0.53);
1607	title_font->Render("N: Next preset");
1608	
1609	glRasterPos2f(0.01, -0.57);
1610	title_font->Render("P: Previous preset");
1611	
1612	glPopMatrix();
1613	//         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1614
1615#endif /** USE_FTGL */
1616}
1617
1618void Renderer::draw_stats(PresetInputs *presetInputs)
1619{
1620	
1621#ifdef USE_FTGL
1622        CSectionLock lock(&_renderer_lock);
1623	char buffer[128];
1624	float offset= (this->showfps%2 ? -0.05 : 0.0);
1625	// glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1626	
1627	glColor4f(1.0, 1.0, 1.0, 1.0);
1628	glPushMatrix();
1629	glTranslatef(0.01, 1, 0);
1630	glRasterPos2f(0, -.05+offset);
1631	other_font->Render(this->correction ? "  aspect: corrected" : "  aspect: stretched");
1632	sprintf( buffer, " (%f)", this->aspect);
1633	other_font->Render(buffer);
1634	
1635	
1636	
1637	glRasterPos2f(0, -.09+offset);
1638	other_font->FaceSize((unsigned)(18*(this->vh/512.0)));
1639	
1640	sprintf( buffer, " texsize: %d", this->renderTarget->texsize);
1641	other_font->Render(buffer);
1642	
1643	glRasterPos2f(0, -.13+offset);
1644	sprintf( buffer, "viewport: %d x %d", this->vw, this->vh);
1645	
1646	other_font->Render(buffer);
1647	glRasterPos2f(0, -.17+offset);
1648	other_font->Render((this->renderTarget->useFBO ? "     FBO: on" : "     FBO: off"));
1649	
1650	glRasterPos2f(0, -.21+offset);
1651	sprintf( buffer, "    mesh: %d x %d", presetInputs->gx, presetInputs->gy);
1652	other_font->Render(buffer);
1653	
1654	glRasterPos2f(0, -.25+offset);
1655	sprintf( buffer, "textures: %.1fkB", textureManager->getTextureMemorySize() /1000.0f);
1656	other_font->Render(buffer);
1657	
1658	glPopMatrix();
1659	// glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1660	
1661	
1662	
1663#endif /** USE_FTGL */
1664}
1665void Renderer::draw_fps( float realfps )
1666{
1667#ifdef USE_FTGL
1668        CSectionLock lock(&_renderer_lock);
1669	char bufferfps[20];
1670	sprintf( bufferfps, "%.1f fps", realfps);
1671	// glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1672	
1673	glColor4f(1.0, 1.0, 1.0, 1.0);
1674	glPushMatrix();
1675	glTranslatef(0.01, 1, 0);
1676	glRasterPos2f(0, -0.05);
1677	title_font->FaceSize((unsigned)(20*(this->vh/512.0)));
1678	title_font->Render(bufferfps);
1679	
1680	glPopMatrix();
1681	// glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1682	
1683#endif /** USE_FTGL */
1684}
1685
1686
1687//Actually draws the texture to the screen
1688//
1689//The Video Echo effect is also applied here
1690void Renderer::render_texture_to_screen(PresetOutputs *presetOutputs)
1691{
1692	
1693        CSectionLock lock(&_renderer_lock);
1694	int flipx=1, flipy=1;
1695	
1696	glMatrixMode(GL_TEXTURE);
1697	glLoadIdentity();
1698	
1699	glMatrixMode(GL_MODELVIEW);
1700	glLoadIdentity();
1701	
1702	//Overwrite anything on the screen
1703	glBlendFunc(GL_ONE, GL_ZERO);
1704	glColor4f(1.0, 1.0, 1.0, 1.0f);
1705	
1706	glEnable(GL_TEXTURE_2D);
1707	
1708
1709
1710	float tex[4][2] = {{0, 1},
1711			   {0, 0},
1712			   {1, 0},
1713			   {1, 1}};
1714
1715	float points[4][2] = {{-0.5, -0.5},
1716			      {-0.5,  0.5},
1717			      { 0.5,  0.5},
1718			      { 0.5,  -0.5}};
1719
1720	glEnableClientState(GL_VERTEX_ARRAY);
1721	glDisableClientState(GL_COLOR_ARRAY);	 
1722	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1723	
1724	glVertexPointer(2,GL_FLOAT,0,points);
1725	glTexCoordPointer(2,GL_FLOAT,0,tex);
1726		       
1727	glDrawArrays(GL_TRIANGLE_FAN,0,4);
1728
1729	//Noe Blend the Video Echo
1730	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1731	
1732	glMatrixMode(GL_TEXTURE);
1733	
1734	//draw video echo
1735	glColor4f(1.0, 1.0, 1.0, presetOutputs->fVideoEchoAlpha);
1736	glTranslatef(.5, .5, 0);
1737	glScalef(1.0/presetOutputs->fVideoEchoZoom, 1.0/presetOutputs->fVideoEchoZoom, 1);
1738	glTranslatef(-.5, -.5, 0);
1739	
1740	switch (((int)presetOutputs->nVideoEchoOrientation))
1741	{
1742		case 0: flipx=1;flipy=1;break;
1743		case 1: flipx=-1;flipy=1;break;
1744		case 2: flipx=1;flipy=-1;break;
1745		case 3: flipx=-1;flipy=-1;break;
1746		default: flipx=1;flipy=1; break;
1747	}
1748
1749	float pointsFlip[4][2] = {{-0.5*flipx, -0.5*flipy},
1750				  {-0.5*flipx,  0.5*flipy},
1751				  { 0.5*flipx,  0.5*flipy},
1752				  { 0.5*flipx, -0.5*flipy}};
1753	
1754	glVertexPointer(2,GL_FLOAT,0,pointsFlip);
1755	glDrawArrays(GL_TRIANGLE_FAN,0,4);	
1756	
1757	glDisable(GL_TEXTURE_2D);
1758	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1759	
1760	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1761
1762	if (presetOutputs->bBrighten==1)
1763	{
1764		glColor4f(1.0, 1.0, 1.0, 1.0);
1765		glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1766		glDrawArrays(GL_TRIANGLE_FAN,0,4);		
1767		glBlendFunc(GL_ZERO, GL_DST_COLOR);
1768		glDrawArrays(GL_TRIANGLE_FAN,0,4);	
1769		glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1770		glDrawArrays(GL_TRIANGLE_FAN,0,4);	
1771		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1772		
1773	}
1774	
1775	if (presetOutputs->bDarken==1)
1776	{		
1777		glColor4f(1.0, 1.0, 1.0, 1.0);
1778		glBlendFunc(GL_ZERO, GL_DST_COLOR);
1779		glDrawArrays(GL_TRIANGLE_FAN,0,4);	
1780		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);		
1781	}
1782	
1783	
1784	if (presetOutputs->bSolarize)
1785	{		
1786		glColor4f(1.0, 1.0, 1.0, 1.0);
1787		glBlendFunc(GL_ZERO, GL_ONE_MINUS_DST_COLOR);
1788		glDrawArrays(GL_TRIANGLE_FAN,0,4);		
1789		glBlendFunc(GL_DST_COLOR, GL_ONE);
1790		glDrawArrays(GL_TRIANGLE_FAN,0,4);			
1791		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);		
1792	}
1793	
1794	if (presetOutputs->bInvert)
1795	{
1796		glColor4f(1.0, 1.0, 1.0, 1.0);
1797		glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1798		glDrawArrays(GL_TRIANGLE_FAN,0,4);	
1799		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1800	}
1801	
1802}
1803
1804void Renderer::render_texture_to_studio(PresetOutputs *presetOutputs, PresetInputs *presetInputs)
1805{
1806  /*
1807        CSectionLock lock(&_renderer_lock);
1808	int x, y;
1809	int flipx=1, flipy=1;
1810	
1811	glMatrixMode(GL_TEXTURE);
1812	glLoadIdentity();
1813	
1814	
1815	glMatrixMode(GL_MODELVIEW);
1816	glLoadIdentity();
1817	
1818	glColor4f(0.0, 0.0, 0.0, 0.04);
1819	
1820	
1821	glBegin(GL_QUADS);
1822	glVertex4d(-0.5, -0.5, -1, 1);
1823	glVertex4d(-0.5,  0.5, -1, 1);
1824	glVertex4d(0.5,  0.5, -1, 1);
1825	glVertex4d(0.5, -0.5, -1, 1);
1826	glEnd();
1827	
1828	
1829	glColor4f(0.0, 0.0, 0.0, 1.0);
1830	
1831	glBegin(GL_QUADS);
1832	glVertex4d(-0.5, 0, -1, 1);
1833	glVertex4d(-0.5,  0.5, -1, 1);
1834	glVertex4d(0.5,  0.5, -1, 1);
1835	glVertex4d(0.5, 0, -1, 1);
1836	glEnd();
1837	
1838	glBegin(GL_QUADS);
1839	glVertex4d(0, -0.5, -1, 1);
1840	glVertex4d(0,  0.5, -1, 1);
1841	glVertex4d(0.5,  0.5, -1, 1);
1842	glVertex4d(0.5, -0.5, -1, 1);
1843	glEnd();
1844	
1845	glPushMatrix();
1846	glTranslatef(.25, .25, 0);
1847	glScalef(.5, .5, 1);
1848	
1849	glEnable(GL_TEXTURE_2D);
1850	
1851	glBlendFunc(GL_ONE, GL_ZERO);
1852	glColor4f(1.0, 1.0, 1.0, 1.0);
1853	//Draw giant rectangle and texture it with our texture!
1854	glBegin(GL_QUADS);
1855	glTexCoord4d(0, 1, 0, 1); glVertex4d(-0.5, -0.5, -1, 1);
1856	glTexCoord4d(0, 0, 0, 1); glVertex4d(-0.5,  0.5, -1, 1);
1857	glTexCoord4d(1, 0, 0, 1); glVertex4d(0.5,  0.5, -1, 1);
1858	glTexCoord4d(1, 1, 0, 1); glVertex4d(0.5, -0.5, -1, 1);
1859	glEnd();
1860	
1861	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1862	
1863	
1864	glMatrixMode(GL_TEXTURE);
1865	
1866	//draw video echo
1867	glColor4f(1.0, 1.0, 1.0, presetOutputs->fVideoEchoAlpha);
1868	glTranslatef(.5, .5, 0);
1869	glScalef(1/presetOutputs->fVideoEchoZoom, 1/presetOutputs->fVideoEchoZoom, 1);
1870	glTranslatef(-.5, -.5, 0);
1871	
1872	switch (((int)presetOutputs->nVideoEchoOrientation))
1873	{
1874		case 0: flipx=1;flipy=1;break;
1875		case 1: flipx=-1;flipy=1;break;
1876		case 2: flipx=1;flipy=-1;break;
1877		case 3: flipx=-1;flipy=-1;break;
1878		default: flipx=1;flipy=1; break;
1879	}
1880	glBegin(GL_QUADS);
1881	glTexCoord4d(0, 1, 0, 1); glVertex4f(-0.5*flipx, -0.5*flipy, -1, 1);
1882	glTexCoord4d(0, 0, 0, 1); glVertex4f(-0.5*flipx,  0.5*flipy, -1, 1);
1883	glTexCoord4d(1, 0, 0, 1); glVertex4f(0.5*flipx,  0.5*flipy, -1, 1);
1884	glTexCoord4d(1, 1, 0, 1); glVertex4f(0.5*flipx, -0.5*flipy, -1, 1);
1885	glEnd();
1886	
1887	
1888	glDisable(GL_TEXTURE_2D);
1889	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1890	
1891	
1892	
1893	if (presetOutputs->bInvert)
1894	{
1895		glColor4f(1.0, 1.0, 1.0, 1.0);
1896		glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1897		glBegin(GL_QUADS);
1898		glVertex4f(-0.5*flipx, -0.5*flipy, -1, 1);
1899		glVertex4f(-0.5*flipx,  0.5*flipy, -1, 1);
1900		glVertex4f(0.5*flipx,  0.5*flipy, -1, 1);
1901		glVertex4f(0.5*flipx, -0.5*flipy, -1, 1);
1902		glEnd();
1903		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1904	}
1905	
1906	//  glTranslated(.5,.5,0);
1907	//  glScaled(1/fVideoEchoZoom,1/fVideoEchoZoom,1);
1908	//   glTranslated(-.5,-.5,0);
1909	//glTranslatef(0,.5*vh,0);
1910	
1911	
1912	//glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ZERO);
1913	
1914	glDisable(GL_TEXTURE_2D);
1915	glMatrixMode(GL_MODELVIEW);
1916	glPopMatrix();
1917	glPushMatrix();
1918	glTranslatef(.25, -.25, 0);
1919	glScalef(.5, .5, 1);
1920	glColor4f(1.0, 1.0, 1.0, 1.0);
1921	
1922	for (x=0;x<presetInputs->gx;x++)
1923	{
1924		glBegin(GL_LINE_STRIP);
1925		for(y=0;y<presetInputs->gy;y++)
1926		{
1927			glVertex4f((presetOutputs->x_mesh[x][y]-.5), (presetOutputs->y_mesh[x][y]-.5), -1, 1);
1928			//glVertex4f((origx[x+1][y]-.5) * vw, (origy[x+1][y]-.5) *vh ,-1,1);
1929		}
1930		glEnd();
1931	}
1932	
1933	for (y=0;y<presetInputs->gy;y++)
1934	{
1935		glBegin(GL_LINE_STRIP);
1936		for(x=0;x<presetInputs->gx;x++)
1937		{
1938			glVertex4f((presetOutputs->x_mesh[x][y]-.5), (presetOutputs->y_mesh[x][y]-.5), -1, 1);
1939			//glVertex4f((origx[x+1][y]-.5) * vw, (origy[x+1][y]-.5) *vh ,-1,1);
1940		}
1941		glEnd();
1942	}
1943	
1944	glEnable( GL_TEXTURE_2D );
1945	
1946	
1947	// glTranslated(-.5,-.5,0);     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1948	
1949	// Waveform display -- bottom-left
1950	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1951	glMatrixMode(GL_MODELVIEW);
1952	glPushMatrix();
1953	glTranslatef(-.5, 0, 0);
1954	
1955	glTranslatef(0, -0.10, 0);
1956	glBegin(GL_LINE_STRIP);
1957	glColor4f(0, 1.0, 1.0, 1.0);
1958	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->treb_att*-7, -1);
1959	glColor4f(1.0, 1.0, 1.0, 1.0);
1960	glVertex3f((((this->totalframes%256)/551.0)), 0 , -1);
1961	glColor4f(.5, 1.0, 1.0, 1.0);
1962	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->treb*7, -1);
1963	glEnd();
1964	
1965	glTranslatef(0, -0.13, 0);
1966	glBegin(GL_LINE_STRIP);
1967	glColor4f(0, 1.0, 0.0, 1.0);
1968	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->mid_att*-7, -1);
1969	glColor4f(1.0, 1.0, 1.0, 1.0);
1970	glVertex3f((((this->totalframes%256)/551.0)), 0 , -1);
1971	glColor4f(.5, 1.0, 0.0, 0.5);
1972	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->mid*7, -1);
1973	glEnd();
1974	
1975	
1976	glTranslatef(0, -0.13, 0);
1977	glBegin(GL_LINE_STRIP);
1978	glColor4f(1.0, 0.0, 0.0, 1.0);
1979	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->bass_att*-7, -1);
1980	glColor4f(1.0, 1.0, 1.0, 1.0);
1981	glVertex3f((((this->totalframes%256)/551.0)), 0 , -1);
1982	glColor4f(.7, 0.2, 0.2, 1.0);
1983	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->bass*7, -1);
1984	glEnd();
1985	
1986	glTranslatef(0, -0.13, 0);
1987	glBegin(GL_LINES);
1988	
1989	glColor4f(1.0, 1.0, 1.0, 1.0);
1990	glVertex3f((((this->totalframes%256)/551.0)), 0 , -1);
1991	glColor4f(1.0, 0.6, 1.0, 1.0);
1992	glVertex3f((((this->totalframes%256)/551.0)), beatDetect->vol*7, -1);
1993	glEnd();
1994	
1995	glPopMatrix();
1996	
1997	glDisable(GL_TEXTURE_2D);
1998  */
1999}
2000
2001
2002