Click here to Skip to main content
15,885,985 members
Articles / Multimedia / DirectX

Interactive 3D Spectrum Analyzer Visualization for Windows Media Player

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
17 May 2009CPOL12 min read 164.5K   6.5K   41  
Interactive 3D Spectrum Analyzer for Windows Media Player using DirectX 9 and some light GPGPU.
///////////////////////////////////////////////////////////////////////////////
//  Copyright (c) 2009 Carlo McWhirter. All Rights Reserved.	
//  Copyright (c) 2009 Hyteq Systems. All Rights Reserved.	
//  
//  http://www.hyteq.com
//
//	Hyteq Systems Educational License
//
//  This file is part of WM3DSpectrum, also known as 3D Spectrum Analyzer for
//  Windows Media Player. This file, the project that this file is part of, and
//  the resulting compiled program files are intended to be used for educational
//  purposes only. Use of this file or this project for any non-educational or
//  non-observatory purpose is strictly prohibited without the express written 
//  consent of all of the copyright holders listed above.
//
//  This file may only be modified and later redistributed by one or more of the
//  copyright holders listed above. Suggestions for bug fixes, enhancements,
//  and other modifications must be sent directly to one of the copyright holders.
//  
//  This file may be modified without being redistributed by any recipient of 
//  this file provided the modifications are NOT intentionaly or unintentionaly 
//  directed toward malicious or illegal purposes, but, instead, toward educational
//  purposes only.
//
//	THIS SOFTWARE IS PROVIDED 'AS-IS', WITHOUT ANY EXPRESS OR IMPLIED
//  WARRANTY. IN NO EVENT WILL THE AUTHORS BE HELD LIABLE FOR ANY DAMAGES
//  ARISING FROM THE USE OF THIS SOFTWARE.
//
//  This notice may not be removed from this file or altered in any manner.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

// gEffect is a global string that provides the shader code for use with DirectX.

const char* gEffect =
"uniform extern float4x4 gWVP;														\n"
"extern texture2D gTex;																\n"
"																					\n"
"struct OutputVS																	\n"
"{																					\n"
"    float4 posH	: POSITION0;													\n"
"    float4 color	: COLOR0;														\n"
"};																					\n"
"																					\n"
"sampler TexS = sampler_state														\n"
"{																					\n"
"	Texture = <gTex>;																\n"
"	MinFilter = LINEAR;																\n"
"	MagFilter = LINEAR;																\n"
"	MipFilter = POINT;																\n"
"	AddressU  = WRAP;																\n"
"   AddressV  = WRAP;																\n"
"};																					\n"
"																					\n"
"float4 GetColorFromHeight_RoseGarden(float y)										\n"
"{																					\n"
"	if(y < 0.001f)																	\n"
"		return float4( 0.0f, 0.0f, 0.0f, 1.0f);										\n"
"	else if(y < 0.501f)																\n"
"		return float4( 0.0f, y, 0.0f, 1.0f);										\n"
"	else if(y < 0.751f)																\n"
"		return float4( 0.0f, 1.0f - y, 1.0f, 1.0f) * -1.0f + 1.0f;					\n"
"	else																			\n"
"		return float4( y, 0.5f - y, 0.0f, 1.0f);									\n"
"}																					\n"
"																					\n"
"float4 GetColorFromHeight_CityLights(float y)										\n"
"{																					\n"
"	if(abs(y) <= 0.001f ) // black													\n"
"		return float4(0.0f, 0.0f, 0.0f, 1.0f);										\n"
"	else if(abs(y) <= 0.2f ) // blue												\n"
"		return float4(0.0f, 0.0f, 1.0f, 1.0f);										\n"
"	else if(abs(y) <= 0.5f ) // green												\n"
"		return float4(0.0f, 1.0f, 0.0f, 1.0f);										\n"
"	else if(abs(y) <= 1.0f ) // red													\n"
"		return float4(1.0f, 0.0f, 0.0f, 1.0f);										\n"
"	else // yellow																	\n"
"		return float4(1.0f, 1.0f, 0.0f, 1.0f);										\n"
"}																					\n"
"																					\n"
"OutputVS ColorVSRoseGarden(float3 posL : POSITION0)								\n"
"{																					\n"
"    // Zero out our output.														\n"
"	OutputVS outVS = (OutputVS)0;													\n"
"																					\n"
"	float2 texPos = float2( posL.x / 512.5f, posL.z / 512.5f );						\n"
"	texPos.y += 0.5f;																\n"
"	texPos.x = texPos.x * -1.0f + 1.0f;												\n"
"																					\n"
"	float4 HColor = tex2Dlod(TexS, float4(texPos.x, texPos.y, 0.0f, 0.0f));			\n"
"	float height = max(HColor.g, HColor.b);											\n"
"																					\n"
"	// Generate the vertex color based on its height.								\n"
"	outVS.color = GetColorFromHeight_RoseGarden(height);							\n"
"																					\n"
"	// Setup the texture coordinates												\n"
"	posL.y = height * 100;															\n"
"																					\n"
"	// Transform to homogeneous clip space.											\n"
"	outVS.posH = mul(float4(posL, 1.0f), gWVP);										\n"
"																					\n"
"	// Done--return the output.														\n"
"	return outVS;																	\n"
"}																					\n"
"																					\n"
"OutputVS ColorVSCityLights(float3 posL : POSITION0)								\n"
"{																					\n"
"    // Zero out our output.														\n"
"	OutputVS outVS = (OutputVS)0;													\n"
"																					\n"
"	float2 texPos = float2( posL.x / 512.5f, posL.z / 512.5f );						\n"
"	texPos.y += 0.5f;																\n"
"	texPos.x = texPos.x * -1.0f + 1.0f;												\n"
"																					\n"
"	float4 HColor = tex2Dlod(TexS, float4(texPos.x, texPos.y, 0.0f, 0.0f));			\n"
"	float height = max(HColor.g, HColor.b);											\n"
"																					\n"
"	// Generate the vertex color based on its height.								\n"
"	outVS.color = GetColorFromHeight_CityLights(height);							\n"
"																					\n"
"	// Setup the texture coordinates												\n"
"	posL.y = height * 100;															\n"
"																					\n"
"	// Transform to homogeneous clip space.											\n"
"	outVS.posH = mul(float4(posL, 1.0f), gWVP);										\n"
"																					\n"
"	// Done--return the output.														\n"
"	return outVS;																	\n"
"}																					\n"
"																					\n"
"float4 ColorPS(float4 c : COLOR0) : COLOR											\n"
"{																					\n"
"    return c;																		\n"
"}																					\n"
"																					\n"
"technique ColorGridRoseGardenTech													\n"
"{																					\n"
"    pass P0																		\n"
"    {																				\n"
"        // Specify the vertex and pixel shader associated with this pass.			\n"
"        vertexShader = compile vs_3_0 ColorVSRoseGarden();							\n"
"        pixelShader  = compile ps_3_0 ColorPS();									\n"
"    }																				\n"
"}																					\n"
"																					\n"
"technique ColorGridCityLightsTech													\n"
"{																					\n"
"    pass P0																		\n"
"    {																				\n"
"        // Specify the vertex and pixel shader associated with this pass.			\n"
"        vertexShader = compile vs_3_0 ColorVSCityLights();							\n"
"        pixelShader  = compile ps_3_0 ColorPS();									\n"
"    }																				\n"
"}																					\n"
"																					\n";

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I'm a Microsoft Certified Professional (MCP) in C++. I'm fluent in C/C++, C# and many other languages.

Comments and Discussions