Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
5
Добавлен:
01.05.2014
Размер:
3.53 Кб
Скачать
// RenderWindow.cpp : implementation file
//

#include "stdafx.h"
#include <gl/GL.h>
#include <gl/glu.h>
#include "testMFC.h"
#include "RenderWindow.h"

#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "Glu32.lib" )

// CRenderWindow

IMPLEMENT_DYNAMIC(CRenderWindow, CStatic)

CRenderWindow::CRenderWindow() : mIsOpenGlInitialized(false)
{
}

CRenderWindow::~CRenderWindow()
{
	mSquares.clear();

	if (mIsOpenGlInitialized) {
		uninitializeOpenGL();
	}
}


BEGIN_MESSAGE_MAP(CRenderWindow, CStatic)
	ON_WM_PAINT()
//	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()



// CRenderWindow message handlers



void CRenderWindow::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CStatic::OnPaint() for painting messages
	if (!mIsOpenGlInitialized) {
		initializeOpenGl();
	}

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//glLoadIdentity();

	SquareList::iterator i;
	for (i = mSquares.begin(); i != mSquares.end(); ++i) {
		i->draw();
	}

	SwapBuffers(dc.m_hDC);
}

void CRenderWindow::OnDestroy()
{
	CStatic::OnDestroy();
	mSquares.clear();

	// TODO: Add your message handler code here
	if (mIsOpenGlInitialized) {
		uninitializeOpenGL();
	}
}

void CRenderWindow::generateBox( unsigned int deep, float x, float y, float a, unsigned int firstColor, unsigned int colorStep ){
	mSquares.clear();
	generate(1, deep, x, y, a, firstColor, colorStep);
	RedrawWindow();
}
bool CRenderWindow::initializeOpenGl(){
	// TODO:  Add your specialized creation code here

	PIXELFORMATDESCRIPTOR pfd;
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits	= 32;
	pfd.cRedBits	= 8;
	pfd.cRedShift	= 16;
	pfd.cGreenBits	= 8;
	pfd.cGreenShift = 8;
	pfd.cBlueBits	= 8;
	pfd.cBlueShift	= 0;
	pfd.cAlphaBits	= 8;
	pfd.cAlphaShift	= 0;
	pfd.cAccumBits		= 64;
	pfd.cAccumRedBits	= 16;
	pfd.cAccumGreenBits = 16;
	pfd.cAccumBlueBits	= 16;
	pfd.cAccumAlphaBits = 16;
	pfd.cDepthBits		= 32;
	pfd.cStencilBits	= 8;
	pfd.cAuxBuffers		= 0;
	pfd.iLayerType	= PFD_MAIN_PLANE;
	pfd.bReserved	= 0;
	pfd.dwLayerMask = 0;
	pfd.dwVisibleMask	= 0;
	pfd.dwDamageMask	= 0;

	CClientDC clientDC(this);
	int pixelFormat = ChoosePixelFormat( clientDC.m_hDC, &pfd );
	if ( !SetPixelFormat( clientDC.m_hDC, pixelFormat, &pfd) ) {
		throw std::runtime_error( "Can not SetPixelFormat!" );
	}

	mGlDc = wglCreateContext(clientDC.m_hDC);
	wglMakeCurrent( clientDC.m_hDC, mGlDc );

	mIsOpenGlInitialized = true;

	return true;
}

void CRenderWindow::uninitializeOpenGL(){
	CClientDC clientDC(this);
	wglMakeCurrent( clientDC.m_hDC, NULL );
	wglDeleteContext(mGlDc);

	mIsOpenGlInitialized = false;;
}

void CRenderWindow::generate(unsigned int currentLevel, unsigned int deep, float x, float y, float a, unsigned int firstColor, unsigned int colorStep){
	if (currentLevel >= deep){
		return;
	}

	Square square(x, y, a, firstColor );
	firstColor += colorStep;
	mSquares.push_back(square);

	generate(currentLevel+1, deep, x, y - 3*a/4, a/2, firstColor, colorStep );
	generate(currentLevel+1, deep, x + 3*a/4, y, a/2, firstColor, colorStep );
	generate(currentLevel+1, deep, x, y + 3*a/4, a/2, firstColor, colorStep );
	generate(currentLevel+1, deep, x - 3*a/4, y, a/2, firstColor, colorStep );
}

Соседние файлы в папке testMFC