Permalink
Browse files

Add mandelbrot generation function.

Implements viewer render area.
  • Loading branch information...
Ninputer committed Dec 26, 2011
1 parent b983d88 commit 251865b82f493b410ada533342b926f1d0aae557
@@ -82,17 +82,18 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="mandelbrot.h" />
<ClInclude Include="MandelbrotViewerApplication.h" />
<ClInclude Include="RenderArea.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="mandelbrot.cpp" />
<ClCompile Include="MandelbrotViewer.cpp" />
<ClCompile Include="MandelbrotViewerApplication.cpp" />
<ClCompile Include="RenderArea.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
@@ -14,9 +14,6 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
@@ -30,6 +27,12 @@
<ClInclude Include="MandelbrotViewerApplication.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="mandelbrot.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RenderArea.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@@ -41,6 +44,12 @@
<ClCompile Include="MandelbrotViewerApplication.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="mandelbrot.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RenderArea.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="MandelbrotViewer.rc">
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "MandelbrotViewerApplication.h"
#include "RenderArea.h"
#include "Resource.h"
MandelbrotViewerApplication::MandelbrotViewerApplication(void)
{
@@ -10,3 +11,98 @@ MandelbrotViewerApplication::MandelbrotViewerApplication(void)
MandelbrotViewerApplication::~MandelbrotViewerApplication(void)
{
}
HRESULT MandelbrotViewerApplication::Initialize()
{
HRESULT hr = WindowApplication::Initialize();
ComPtr<IWindow> applicationWindow;
if (SUCCEEDED(hr))
{
hr = GetMainWindow(&applicationWindow);
}
if (SUCCEEDED(hr))
{
hr = applicationWindow->SetTitle(L"Mandelbrot Set Viewer");
}
if (SUCCEEDED(hr))
{
hr = applicationWindow->SetSize(640, 640);
}
if (SUCCEEDED(hr))
{
// Set the large icon
hr = applicationWindow->SetLargeIcon(::LoadIcon(HINST_THISCOMPONENT, MAKEINTRESOURCE(IDI_MANDELBROTVIEWER)));
}
if (SUCCEEDED(hr))
{
// Set the small icon
hr = applicationWindow->SetSmallIcon(::LoadIcon(HINST_THISCOMPONENT, MAKEINTRESOURCE(IDI_SMALL)));
}
// Create render area...
ComPtr<IWindow> renderAreaWindow;
if (SUCCEEDED(hr))
{
hr = InitializeRenderArea(&renderAreaWindow);
}
return hr;
}
HRESULT MandelbrotViewerApplication::InitializeRenderArea(IWindow** window)
{
using namespace Hilo::Direct2DHelpers;
static POINT location = {0, 0};
static SIZE size =
{
static_cast<long>(Direct2DUtility::ScaleValueForCurrentDPI(640)),
static_cast<long>(Direct2DUtility::ScaleValueForCurrentDPI(640))
};
ComPtr<IWindowMessageHandler> renderAreaMessageHandler;
HRESULT hr = SharedObject<RenderAreaMessageHandler>::Create(&renderAreaMessageHandler);
ComPtr<IWindow> applicationWindow;
if (SUCCEEDED(hr))
{
hr = GetMainWindow(&applicationWindow);
}
ComPtr<IWindowFactory> windowFactory;
if (SUCCEEDED(hr))
{
hr = GetWindowFactory(&windowFactory);
}
ComPtr<IWindow> renderArea;
if (SUCCEEDED(hr))
{
hr = windowFactory->Create(
location,
size,
renderAreaMessageHandler,
applicationWindow,
&renderArea);
}
if (SUCCEEDED(hr))
{
hr = renderArea->SetMessageHandler(renderAreaMessageHandler);
}
if (SUCCEEDED(hr))
{
hr = AssignToOutputPointer(window, renderArea);
}
return hr;
}
@@ -5,8 +5,18 @@
class MandelbrotViewerApplication : public WindowApplication
{
private:
HRESULT InitializeRenderArea(Hilo::WindowApiHelpers::IWindow** window);
unsigned int m_renderAreaIndex;
protected:
ComPtr<IWindowLayout> m_WindowLayout;
public:
MandelbrotViewerApplication(void);
~MandelbrotViewerApplication(void);
HRESULT __stdcall Initialize();
};
@@ -0,0 +1,67 @@
#include "stdafx.h"
#include "RenderArea.h"
RenderAreaMessageHandler::RenderAreaMessageHandler(void)
{
}
RenderAreaMessageHandler::~RenderAreaMessageHandler(void)
{
}
HRESULT RenderAreaMessageHandler::OnCreate()
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnEraseBackground()
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnRender()
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnSize(unsigned int width, unsigned int height)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnLeftMouseButtonDown(D2D1_POINT_2F mousePosition)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnLeftMouseButtonUp(D2D1_POINT_2F mousePosition)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnMouseMove(D2D1_POINT_2F mousePosition)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnMouseEnter(D2D1_POINT_2F mousePosition)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnMouseWheel(D2D1_POINT_2F mousePosition, short delta, int keys)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::OnKeyDown(unsigned int vKey)
{
return S_OK;
}
HRESULT RenderAreaMessageHandler::Initialize()
{
return S_OK;
}
@@ -0,0 +1,43 @@
#pragma once
#include "WindowMessageHandlerImpl.h"
#include "WindowLayout.h"
#include "WindowLayoutChildInterface.h"
#include "WindowMessageHandlerImpl.h"
class RenderAreaMessageHandler :
public IInitializable,
public Hilo::WindowApiHelpers::WindowMessageHandler
{
public:
RenderAreaMessageHandler(void);
~RenderAreaMessageHandler(void);
protected:
// IInitiliazable
HRESULT __stdcall Initialize();
// WindowMessageHandler Events
HRESULT OnCreate();
HRESULT OnEraseBackground();
HRESULT OnRender();
HRESULT OnSize(unsigned int width, unsigned int height);
HRESULT OnLeftMouseButtonDown(D2D1_POINT_2F mousePosition);
HRESULT OnLeftMouseButtonUp(D2D1_POINT_2F mousePosition);
HRESULT OnMouseMove(D2D1_POINT_2F mousePosition);
HRESULT OnMouseEnter(D2D1_POINT_2F mousePosition);
HRESULT OnMouseWheel(D2D1_POINT_2F mousePosition, short delta, int keys);
HRESULT OnKeyDown(unsigned int vKey);
bool QueryInterfaceHelper(const IID &iid, void **object)
{
return
CastHelper<IInitializable>::CastTo(iid, this, object) ||
Hilo::WindowApiHelpers::WindowMessageHandler::QueryInterfaceHelper(iid, object);
}
private:
ComPtr<ID2D1Factory> m_d2dFactory;
ComPtr<ID2D1HwndRenderTarget> m_renderTarget;
};
@@ -0,0 +1,97 @@
#include "stdafx.h"
#include "mandelbrot.h"
using namespace Concurrency;
unsigned int set_hsb (float hue, float saturate, float bright) restrict (direct3d)
{
// when I wrote this, openGL only liked colors specified by RGB values. The
// mandelbrot routine generated an HSB color, so I wrote this routine to do
// the conversion. It sure isn't perfect, but it does a respectable job.
//
// I expect that part of this work (but the final openGL call) could be
// pushed back with the mandelbrot color generator for more speedup.
//
float red, green, blue;
float h = (hue * 256) / 60;
float p = bright * (1 - saturate);
float q = bright * (1 - saturate * (h - (int)h));
float t = bright * (1 - saturate * (1 - (h - (int)h)));
switch ((int)h) {
case 0:
red = bright, green = t, blue = p;
break;
case 1:
red = q, green = bright, blue = p;
break;
case 2:
red = p, green = bright, blue = t;
break;
case 3:
red = p, green = q, blue = bright;
break;
case 4:
red = t, green = p, blue = bright;
break;
case 5:
case 6:
red = bright, green = p, blue = q;
break;
}
unsigned int ired, igreen, iblue;
ired = (unsigned int)(red * 255.0f);
igreen = (unsigned int)(green * 255.0f);
iblue = (unsigned int)(blue * 255.0f);
return 0xff000000 | (ired << 16) | (igreen << 8) | iblue;
}
void generate_mandelbrot(
array_view<unsigned int, 2> result,
unsigned int max_iter,
float real_min,
float imag_min,
float real_max,
float imag_max )
{
int width = result.extent.get_x();
int height = result.extent.get_y();
parallel_for_each(result.grid, [=](index<2> i) restrict(direct3d)
{
int gx = i.get_x();
int gy = i.get_y();
float scale_real = (real_max - real_min) / width;
float scale_imag = (imag_max - imag_min) / height;
float cx = real_min + gx * scale_real;
float cy = imag_min + (height - gy) * scale_imag;
float zx = 0.0f;
float zy = 0.0f;
float temp;
float length_sqr;
unsigned int count = 0;
do
{
count++;
temp = zx * zx - zy * zy + cx;
zy = 2 * zx * zy + cy;
zx = temp;
length_sqr = zx * zx + zy * zy;
}
while((length_sqr < 4.0f) && (count < max_iter));
float h = sqrt((float)count / max_iter);
//float l = clamp((float)max_iter - count, 0, 1);
result[i] = set_hsb(h, 0.7f, (1.0f - h * h / 1.2f));
});
}
Oops, something went wrong.

0 comments on commit 251865b

Please sign in to comment.