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:
enum class Type
{
LINE,
RECTANGLE,
TRIANGLE,
POINT,
PATH,
UNKNOWN
};
struct Bounds
{
Bounds(double width, double height)
@ -30,4 +40,6 @@ public:
virtual const Point& getLocation() 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"
template<typename T>
Grid<T>::Grid(const Rectangle& bounds)
Grid<T>::Grid(const ntk::Rectangle& bounds)
: mBounds(bounds)
{
mValues = std::vector<T>(mNumX*mNumY, T());
}
template<typename T>
const Rectangle& Grid<T>::getBounds() const
const ntk::Rectangle& Grid<T>::getBounds() const
{
return mBounds;
}
@ -32,7 +32,7 @@ const std::vector<T>& Grid<T>::getValues() const
}
template<typename T>
void Grid<T>::resetBounds(const Rectangle& bounds)
void Grid<T>::resetBounds(const ntk::Rectangle& bounds)
{
mBounds = bounds;
mValues = std::vector<T>(mNumX*mNumY, T());

View file

@ -7,10 +7,9 @@ template<typename T>
class Grid
{
public:
Grid(const ntk::Rectangle& bounds);
Grid(const Rectangle& bounds);
const Rectangle& getBounds() const;
const ntk::Rectangle& getBounds() const;
double getXSpacing() const;
@ -18,12 +17,12 @@ public:
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);
private:
Rectangle mBounds;
ntk::Rectangle mBounds;
std::vector<T> mValues;
unsigned mNumX{5};
unsigned mNumY{5};

View file

@ -1,46 +1,48 @@
#include "Rectangle.h"
Rectangle::Rectangle(const Point& bottomLeft, const Point& topRight)
: mBottomLeft(bottomLeft)
{
mHeight = mBottomLeft.getDeltaY(topRight);
mWidth = mBottomLeft.getDeltaX(topRight);
}
Rectangle::Rectangle(const Point& bottomLeft, double width, double height)
: mBottomLeft(bottomLeft),
mHeight(height),
mWidth(width)
{
}
Rectangle Rectangle::getBounds() const
{
return Rectangle(mBottomLeft, Point(mBottomLeft, mWidth, mHeight));
}
void Rectangle::sample(Grid<unsigned char>* grid) const
{
}
double Rectangle::getHeight() const
{
return mHeight;
}
double Rectangle::getWidth() const
{
return mWidth;
}
const Point& Rectangle::getLocation() const
{
return mBottomLeft;
}
Rectangle::Bounds Rectangle::getSize() const
{
return {mWidth, mHeight};
namespace ntk {
Rectangle::Rectangle(const Point& bottomLeft, const Point& topRight)
: mBottomLeft(bottomLeft)
{
mHeight = mBottomLeft.getDeltaY(topRight);
mWidth = mBottomLeft.getDeltaX(topRight);
}
Rectangle::Rectangle(const Point& bottomLeft, double width, double height)
: mBottomLeft(bottomLeft),
mHeight(height),
mWidth(width)
{
}
Rectangle Rectangle::getBounds() const
{
return Rectangle(mBottomLeft, Point(mBottomLeft, mWidth, mHeight));
}
void Rectangle::sample(Grid<unsigned char>* grid) const
{
}
double Rectangle::getHeight() const
{
return mHeight;
}
double Rectangle::getWidth() const
{
return mWidth;
}
const Point& Rectangle::getLocation() const
{
return mBottomLeft;
}
Rectangle::Bounds Rectangle::getSize() const
{
return { mWidth, mHeight };
}
}

View file

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

View file

@ -6,7 +6,7 @@
RasterPainter::RasterPainter(DrawingContext* 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 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++)

View file

@ -1,10 +1,42 @@
#include "DirectX2dPainter.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)

View file

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

View file

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