Click here to Skip to main content
15,881,882 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.3K   6.5K   41  
Interactive 3D Spectrum Analyzer for Windows Media Player using DirectX 9 and some light GPGPU.
/*
 * Copyright (c) 2005-2009, Carlo McWhirter. All Rights Reserved.
 * Copyright (c) 2009, Hyteq Systems. All Rights Reserved.
 */

#include "stdafx.h"
#include <GL/gl.h>
#include <GL/glu.h>
//#include <GL/glaux.h>
#include "GLRenderContext.h"

BOOL GLRenderContext::m_bIsInitialized = FALSE;

/**
 * GLRenderContext constructor.
 * Creates an opengl rendering context. If multiple OpenGL rendering
 * contexts exist in an application, this class will make sure only
 * one rendering context is active at a time.
 */
GLRenderContext::GLRenderContext() :
	m_hRC( NULL )
{
}

/// Disassociates and frees the current device context
GLRenderContext::~GLRenderContext()
{
    if ( m_hRC )
    {
        wglMakeCurrent( NULL, NULL );
        wglDeleteContext( m_hRC );
    }
}

/// Called to prepare this class for rendering.
void GLRenderContext::Initialize(HDC hReferenceDC, BOOL bIsMemoryDC)
{
	if( m_bIsInitialized == FALSE )
	{
		m_bIsInitialized = TRUE;

		PIXELFORMATDESCRIPTOR pfd;
		ZeroMemory( &pfd, sizeof( pfd ) );
		pfd.nSize = sizeof( pfd );
		pfd.nVersion = 1;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.cColorBits = 32;
		pfd.cDepthBits = 16;
		pfd.iLayerType = PFD_MAIN_PLANE;

		if( bIsMemoryDC )
			pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA;
		else
			pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA;
		
		int format = ChoosePixelFormat( hReferenceDC, &pfd );
		SetPixelFormat( hReferenceDC, format, &pfd );

		m_hRC = wglCreateContext( hReferenceDC );
	}
}

/// Called to begin rendering.
void GLRenderContext::Begin(HDC hDC, BOOL bIsMemoryDC)
{
	if( m_bIsInitialized == FALSE )
		Initialize( hDC, bIsMemoryDC );

	wglMakeCurrent( hDC, m_hRC );

	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

/// Called to end rendering.
void GLRenderContext::End(HDC hDC)
{
	SwapBuffers( hDC );
}

/// Checks whether the context has been initialized at least once.
BOOL GLRenderContext::IsInitialized()
{
	return m_bIsInitialized;
}

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