Hook up d2d offscreen rendering.

This commit is contained in:
jmsgrogan 2023-01-11 17:16:55 +00:00
parent 0c84a53643
commit 53a9f7bd15
9 changed files with 130 additions and 58 deletions

View file

@ -10,6 +10,16 @@ class AbstractGeometricItem
{ {
public: public:
enum class Type
{
LINE,
RECTANGLE,
TRIANGLE,
POINT,
PATH,
UNKNOWN
};
struct Bounds struct Bounds
{ {
Bounds(double width, double height) Bounds(double width, double height)
@ -30,4 +40,6 @@ public:
virtual const Point& getLocation() const = 0; virtual const Point& getLocation() const = 0;
virtual void sample(Grid<unsigned char>* grid) const = 0; virtual void sample(Grid<unsigned char>* grid) const = 0;
virtual Type getType() const { return Type::UNKNOWN; };
}; };

View file

@ -1,14 +1,14 @@
#include "Grid.h" #include "Grid.h"
template<typename T> template<typename T>
Grid<T>::Grid(const Rectangle& bounds) Grid<T>::Grid(const ntk::Rectangle& bounds)
: mBounds(bounds) : mBounds(bounds)
{ {
mValues = std::vector<T>(mNumX*mNumY, T()); mValues = std::vector<T>(mNumX*mNumY, T());
} }
template<typename T> template<typename T>
const Rectangle& Grid<T>::getBounds() const const ntk::Rectangle& Grid<T>::getBounds() const
{ {
return mBounds; return mBounds;
} }
@ -32,7 +32,7 @@ const std::vector<T>& Grid<T>::getValues() const
} }
template<typename T> template<typename T>
void Grid<T>::resetBounds(const Rectangle& bounds) void Grid<T>::resetBounds(const ntk::Rectangle& bounds)
{ {
mBounds = bounds; mBounds = bounds;
mValues = std::vector<T>(mNumX*mNumY, T()); mValues = std::vector<T>(mNumX*mNumY, T());

View file

@ -7,10 +7,9 @@ template<typename T>
class Grid class Grid
{ {
public: public:
Grid(const ntk::Rectangle& bounds);
Grid(const Rectangle& bounds); const ntk::Rectangle& getBounds() const;
const Rectangle& getBounds() const;
double getXSpacing() const; double getXSpacing() const;
@ -18,12 +17,12 @@ public:
const std::vector<T>& getValues() const; const std::vector<T>& getValues() const;
void resetBounds(const Rectangle& bounds); void resetBounds(const ntk::Rectangle& bounds);
void setValues(const std::vector<std::size_t>& indices, T value); void setValues(const std::vector<std::size_t>& indices, T value);
private: private:
Rectangle mBounds; ntk::Rectangle mBounds;
std::vector<T> mValues; std::vector<T> mValues;
unsigned mNumX{5}; unsigned mNumX{5};
unsigned mNumY{5}; unsigned mNumY{5};

View file

@ -1,5 +1,6 @@
#include "Rectangle.h" #include "Rectangle.h"
namespace ntk {
Rectangle::Rectangle(const Point& bottomLeft, const Point& topRight) Rectangle::Rectangle(const Point& bottomLeft, const Point& topRight)
: mBottomLeft(bottomLeft) : mBottomLeft(bottomLeft)
{ {
@ -44,3 +45,4 @@ Rectangle::Bounds Rectangle::getSize() const
{ {
return { mWidth, mHeight }; return { mWidth, mHeight };
} }
}

View file

@ -6,10 +6,10 @@
template<class T> template<class T>
class Grid; class Grid;
namespace ntk{
class Rectangle : public AbstractGeometricItem class Rectangle : public AbstractGeometricItem
{ {
public: public:
Rectangle(const Point& bottomLeft, const Point& topRight); Rectangle(const Point& bottomLeft, const Point& topRight);
Rectangle(const Point& bottomLeft, double width, double height); Rectangle(const Point& bottomLeft, double width, double height);
@ -23,6 +23,11 @@ public:
Rectangle getBounds() const; Rectangle getBounds() const;
Type getType() const
{
return AbstractGeometricItem::Type::RECTANGLE;
}
void sample(Grid<unsigned char>* grid) const override; void sample(Grid<unsigned char>* grid) const override;
private: private:
@ -30,3 +35,5 @@ private:
double mWidth{0}; double mWidth{0};
double mHeight{0}; double mHeight{0};
}; };
}

View file

@ -6,7 +6,7 @@
RasterPainter::RasterPainter(DrawingContext* context) RasterPainter::RasterPainter(DrawingContext* context)
: AbstractPainter(context), : AbstractPainter(context),
mGrid(std::make_unique<Grid<unsigned char> >(Rectangle(Point(0, 0), Point(100, 100)))) mGrid(std::make_unique<Grid<unsigned char> >(ntk::Rectangle(Point(0, 0), Point(100, 100))))
{ {
} }
@ -15,7 +15,7 @@ void RasterPainter::paint()
{ {
const auto width = mDrawingContext->getSurface()->getWidth(); const auto width = mDrawingContext->getSurface()->getWidth();
const auto height = mDrawingContext->getSurface()->getHeight(); const auto height = mDrawingContext->getSurface()->getHeight();
mGrid->resetBounds(Rectangle(Point(0, 0), Point(width, height))); mGrid->resetBounds(ntk::Rectangle(Point(0, 0), Point(width, height)));
/* /*
for (unsigned idx=0; idx< context->getNumItems(); idx++) for (unsigned idx=0; idx< context->getNumItems(); idx++)

View file

@ -1,10 +1,42 @@
#include "DirectX2dPainter.h" #include "DirectX2dPainter.h"
#include "DirectX2dInterface.h" #include "DirectX2dInterface.h"
#include "AbstractGeometricItem.h"
#include "Rectangle.h"
#include "SceneModel.h"
void DirectX2dPainter::paint(AbstractGeometricItem* item) void DirectX2dPainter::startDrawing()
{ {
mD2dInterface->getRenderTarget()->BeginDraw();
}
void DirectX2dPainter::finishDrawing()
{
mD2dInterface->getRenderTarget()->EndDraw();
}
void DirectX2dPainter::clearBackground()
{
mD2dInterface->getRenderTarget()->Clear(D2D1::ColorF(D2D1::ColorF::White));
}
void DirectX2dPainter::paint(SceneModel* model)
{
auto rt = mD2dInterface->getRenderTarget();
auto hr = rt->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mWorkingBrush);
if (model->getGeometry()->getType() == AbstractGeometricItem::Type::RECTANGLE)
{
auto rect = dynamic_cast<ntk::Rectangle*>(model->getGeometry());
const auto loc = rect->getLocation();
const auto width = rect->getWidth();
const auto height = rect->getHeight();
D2D1_RECT_F d2d_rect{ static_cast<float>(loc.getX()), static_cast<float>(loc.getY() + height), static_cast<float>(loc.getX() + width), static_cast<float>(loc.getY()) };
rt->FillRectangle(d2d_rect, mWorkingBrush.Get());
}
} }
void DirectX2dPainter::setD2dInterface(DirectX2dInterface* d2dIterface) void DirectX2dPainter::setD2dInterface(DirectX2dInterface* d2dIterface)

View file

@ -1,15 +1,29 @@
#pragma once #pragma once
class AbstractGeometricItem; #include <wrl.h>
class SceneModel;
class DirectX2dInterface; class DirectX2dInterface;
struct ID2D1SolidColorBrush;
class DirectX2dPainter class DirectX2dPainter
{ {
public: public:
void paint(AbstractGeometricItem* item); void startDrawing();
void clearBackground();
void finishDrawing();
void paint(SceneModel* model);
void setD2dInterface(DirectX2dInterface* d2dIterface); void setD2dInterface(DirectX2dInterface* d2dIterface);
private: private:
void createRectangle();
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mWorkingBrush;
DirectX2dInterface* mD2dInterface{ nullptr }; DirectX2dInterface* mD2dInterface{ nullptr };
}; };

View file

@ -109,6 +109,10 @@ void DirectXPainter::paint()
} }
auto scene = mDrawingContext->getSurface()->getScene(); auto scene = mDrawingContext->getSurface()->getScene();
m2dPainter->startDrawing();
m2dPainter->clearBackground();
for (const auto item : scene->getItems()) for (const auto item : scene->getItems())
{ {
if (item->getType() == SceneItem::Type::MODEL && item->isVisible()) if (item->getType() == SceneItem::Type::MODEL && item->isVisible())
@ -116,10 +120,12 @@ void DirectXPainter::paint()
auto model = dynamic_cast<SceneModel*>(item); auto model = dynamic_cast<SceneModel*>(item);
if (model->getGeometry()) if (model->getGeometry())
{ {
m2dPainter->paint(model->getGeometry()); m2dPainter->paint(model);
} }
} }
} }
m2dPainter->finishDrawing();
} }
void DirectXPainter::paintBackground(const D3D12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList) void DirectXPainter::paintBackground(const D3D12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList)