First opengl/x11/window integration.

This commit is contained in:
James Grogan 2022-11-14 13:07:11 +00:00
parent 7c6a92f4ec
commit cea3d2c39f
30 changed files with 254 additions and 72 deletions

View file

@ -29,7 +29,7 @@ AbstractApp* GuiApplication::getMainApplication() const
void GuiApplication::initializeViews()
{
mDesktopManager->getWindowManager()->getMainWindow()->setSize(800, 600);
}
void GuiApplication::setUiInterfaceBackend(UiInterfaceFactory::Backend backend)

View file

@ -6,9 +6,16 @@ Point::Point(double x, double y)
{
}
Point::Point(const DiscretePoint& point)
: mX(point.GetX()),
mY(point.GetY())
{
}
Point::Point(const Point& reference, double offSetX, double offSetY)
: mX(reference.getX() + offSetX),
mY(reference.getX() + offSetY)
mY(reference.getY() + offSetY)
{
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "DiscretePoint.h"
#include <memory>
#include <cmath>
@ -9,6 +11,8 @@ public:
Point(double x, double y);
Point(const DiscretePoint& point);
Point(const Point& reference, double offSetX, double offSetY);
~Point();

View file

@ -7,6 +7,14 @@ Rectangle::Rectangle(const Point& bottomLeft, const Point& 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));

View file

@ -11,6 +11,7 @@ class Rectangle : public AbstractGeometricItem
public:
Rectangle(const Point& bottomLeft, const Point& topRight);
Rectangle(const Point& bottomLeft, double width, double height);
double getHeight() const;

View file

@ -1,16 +1,13 @@
#include "DrawingContext.h"
#include "AbstractGeometricItem.h"
#include "AbstractPainter.h"
#include "OpenGlPainter.h"
#include "RasterPainter.h"
#include "Grid.h"
#include "TriMesh.h"
#include "DrawingSurface.h"
#include "Scene.h"
#include "Grid.h"
#include "MeshBuilder.h"
#include "TriMesh.h"
DrawingContext::DrawingContext(DrawingSurface* surface, DrawingMode requestedDrawingMode)
: mSurface(surface),
@ -32,11 +29,6 @@ std::unique_ptr<DrawingContext> DrawingContext::Create(DrawingSurface* surface,
return std::make_unique<DrawingContext>(surface, requestedDrawingMode);
}
void DrawingContext::updateMesh()
{
}
Scene* DrawingContext::getScene() const
{
return mScene.get();
@ -49,5 +41,6 @@ DrawingSurface* DrawingContext::getSurface() const
void DrawingContext::paint()
{
mScene->update(mDrawingMode == DrawingMode::RASTER ? mSurface->getImage() : nullptr);
mPainter->paint(this);
}

View file

@ -6,7 +6,6 @@
class Scene;
class AbstractPainter;
class DrawingSurface;
class TriMesh;
enum class DrawingMode
{
@ -28,12 +27,10 @@ public:
void paint();
private:
void updateMesh();
DrawingMode mDrawingMode;
std::unique_ptr<TriMesh> mMesh;
std::unique_ptr<Scene> mScene;
DrawingSurface* mSurface{nullptr};
std::unique_ptr<Scene> mScene;
std::unique_ptr<AbstractPainter> mPainter;
};

View file

@ -1,6 +1,9 @@
#include "OpenGlPainter.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "Scene.h"
#include "TriMesh.h"
#ifdef _WIN32
#include <windows.h>
@ -8,19 +11,47 @@
#include <GL/gl.h>
#include <iostream>
void OpenGlPainter::paint(DrawingContext* context)
{
auto surface = context->getSurface();
const auto width = double(surface->getWidth());
const auto height = double(surface->getHeight());
const auto num_mesh = context->getScene()->getNumMeshes();
glClearColor(0.4, 0.4, 0.4, 0.4);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.7, 0.7, 0);
glVertex3f(0.7, 0.7, 0);
glVertex3f(0, -1, 0);
glEnd();
for (std::size_t idx=0; idx<num_mesh; idx++)
{
auto mesh = context->getScene()->getMesh(idx);
const auto faces = mesh->getFaceVertices();
glColor3f(1.0, 0.0, 1.0);
for (const auto& face : faces)
{
glBegin(GL_TRIANGLES);
double x0 = 2.0*face[0].getX() / width - 1.0;
double y0 = 2.0*face[0].getY() / height - 1.0;
double x1 = 2.0*face[1].getX() / width - 1.0;
double y1 = 2.0*face[1].getY() / height - 1.0;
double x2 = 2.0*face[2].getX() / width - 1.0;
double y2 = 2.0*face[2].getY() / height - 1.0;
glVertex3f(x0, y0, 0);
glVertex3f(x1, y1, 0);
glVertex3f(x2, y2, 0);
glEnd();
}
}
glFlush();
}

View file

@ -1,8 +1,7 @@
#pragma once
class AbstractGeometricItem;
class AbstractMesh
{
public:
virtual ~AbstractMesh() = default;
};

View file

@ -18,3 +18,13 @@ Edge::~Edge()
{
}
unsigned Edge::getNode0Id() const
{
return mNode0->getIndex();
}
unsigned Edge::getNode1Id() const
{
return mNode1->getIndex();
}

View file

@ -12,6 +12,10 @@ public:
static std::unique_ptr<Edge> Create(Node* node0, Node* node1, unsigned id = 0);
unsigned getNode0Id() const;
unsigned getNode1Id() const;
private:
unsigned mId{0};
Node* mNode0{nullptr};

View file

@ -16,16 +16,11 @@ class MeshPrimitives
{
public:
static std::unique_ptr<TriMesh> build(AbstractGeometricItem* item)
{
}
static std::unique_ptr<TriMesh> build(const Rectangle& rectangle)
{
const auto bottom_left = rectangle.getBottomLeft();
const auto width = rectangle.GetWidth();
const auto height = rectangle.GetHeight();
const auto bottom_left = rectangle.getLocation();
const auto width = rectangle.getWidth();
const auto height = rectangle.getHeight();
VecPoints locations = {
bottom_left,

View file

@ -15,6 +15,11 @@ public:
void updateIndex(unsigned index);
const Point& getPoint() const
{
return mPoint;
}
private:
unsigned mIndex{0};
Point mPoint;

View file

@ -1,5 +1,7 @@
#include "TriFace.h"
#include "Edge.h"
TriFace::TriFace(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id)
: mEdge0(edge0),
mEdge1(edge1),
@ -18,3 +20,8 @@ TriFace::~TriFace()
{
}
std::vector<unsigned> TriFace::getNodeIds() const
{
return {mEdge0->getNode0Id(), mEdge0->getNode1Id(), mEdge1->getNode1Id()};
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include <vector>
class Edge;
@ -11,6 +12,8 @@ public:
~TriFace();
static std::unique_ptr<TriFace> Create(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id=0);
std::vector<unsigned> getNodeIds() const;
private:
unsigned mId{0};
Edge* mEdge0{nullptr};

View file

@ -15,3 +15,15 @@ void TriMesh::populate(VecNodes& nodes, VecEdges& edges, VecFaces& faces)
mEdges = std::move(edges);
mFaces = std::move(faces);
}
std::vector<std::vector<Point> > TriMesh::getFaceVertices() const
{
std::vector<std::vector<Point> > verts(mFaces.size());
for(std::size_t idx=0; idx<mFaces.size(); idx++)
{
const auto nodeIds = mFaces[idx]->getNodeIds();
verts[idx] = {mNodes[nodeIds[0]]->getPoint(), mNodes[nodeIds[1]]->getPoint(), mNodes[nodeIds[2]]->getPoint()};
}
return verts;
}

View file

@ -1,5 +1,8 @@
#pragma once
#include "AbstractMesh.h"
#include "Point.h"
#include <vector>
#include <memory>
@ -15,7 +18,7 @@ using VecNodes = std::vector<NodePtr>;
using VecEdges = std::vector<EdgePtr>;
using VecFaces = std::vector<TriFacePtr>;
class TriMesh
class TriMesh : public AbstractMesh
{
public:
TriMesh() = default;
@ -24,6 +27,8 @@ public:
void populate(VecNodes& nodes, VecEdges& edges, VecFaces& faces);
std::vector<std::vector<Point> > getFaceVertices() const;
private:
VecNodes mNodes;
VecEdges mEdges;

View file

@ -22,9 +22,9 @@ Window::Window()
: DrawingSurface(),
mWidget(Widget::Create())
{
mWidget->setBounds(mWidth, mHeight);
mWidth = 800;
mHeight = 600;
mWidget->setBounds(mWidth, mHeight);
}
Window::~Window()
@ -104,7 +104,7 @@ void Window::doPaint(mt::Screen* screen)
{
mPlatformWindow->beforePaint(screen);
mDrawingContext->getScene()->setLayers(mWidget->getLayers());
mDrawingContext->getScene()->syncLayers(mWidget->getLayers());
mDrawingContext->paint();

View file

@ -74,10 +74,11 @@ void Button::onPaintEvent(const PaintEvent* event)
{
unsigned fontOffset = unsigned(mLabel.size()) * 4;
auto middle = DiscretePoint(mLocation.GetX() + mSize.mWidth/2 - fontOffset, mLocation.GetY() + mSize.mHeight/2 + 4);
auto textLayer = VisualLayer::Create();
auto textElement = TextNode::Create(mLabel, middle);
textElement->setFillColor(mBackgroundColor);
textLayer->setText(std::move(textElement));
auto node = TextNode::Create(mLabel, middle);
node->setFillColor(mBackgroundColor);
textLayer->setTextNode(std::move(node));
mMyLayers.push_back(std::move(textLayer));
}
mDirty = false;

View file

@ -48,7 +48,7 @@ void Label::onPaintEvent(const PaintEvent* event)
auto textLayer = VisualLayer::Create();
auto textElement = TextNode::Create(mLabel, middle);
textElement->setFillColor(mBackgroundColor);
textLayer->setText(std::move(textElement));
textLayer->setTextNode(std::move(textElement));
mMyLayers.push_back(std::move(textLayer));
}
addMyLayers();

View file

@ -94,7 +94,7 @@ void TextBox::onPaintEvent(const PaintEvent* event)
auto textLayer = VisualLayer::Create();
auto textElement = TextNode::Create(line, loc);
textElement->setFillColor(mBackgroundColor);
textLayer->setText(std::move(textElement));
textLayer->setTextNode(std::move(textElement));
mMyLayers.push_back(std::move(textLayer));
offset += 20;
}

View file

@ -10,6 +10,7 @@
#include <algorithm>
#include <iterator>
#include <iostream>
Widget::Widget()
: mLocation(DiscretePoint(0, 0)),
@ -99,9 +100,9 @@ void Widget::setBounds(unsigned width, unsigned height)
if (width != mSize.mWidth || height != mSize.mHeight)
{
mDirty = true;
mSize.mWidth = width;
mSize.mHeight = height;
}
mSize.mWidth = width;
mSize.mHeight = height;
}
void Widget::setBackgroundColor(const Color& color)
@ -273,9 +274,9 @@ void Widget::addBackground(const PaintEvent* event)
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
auto shape = RectangleNode::Create(DiscretePoint(locX, locY), deltaX, deltaY);
shape->setFillColor(mBackgroundColor);
auto shapeLayer = VisualLayer::Create();
shapeLayer->setShape(std::move(shape));
mMyLayers.push_back(std::move(shapeLayer));
auto node = RectangleNode::Create(DiscretePoint(locX, locY), deltaX, deltaY);
node->setFillColor(mBackgroundColor);
auto layer = VisualLayer::Create();
layer->setShapeNode(std::move(node));
mMyLayers.push_back(std::move(layer));
}

View file

@ -14,11 +14,18 @@ public:
}
virtual ~AbstractVisualNode() = default;
AbstractMesh* getMesh() const
{
return mMesh.get();
}
virtual void updateMesh()
{
}
Image<unsigned char>* getImage() const
{
return mImage.get();
@ -29,7 +36,7 @@ public:
return mLocation;
}
private:
protected:
DiscretePoint mLocation;
std::unique_ptr<AbstractMesh> mMesh;
std::unique_ptr<Image<unsigned char> > mImage;

View file

@ -1,5 +1,10 @@
#include "RectangleNode.h"
#include "Rectangle.h"
#include "MeshPrimitives.h"
#include <iostream>
RectangleNode::RectangleNode(const DiscretePoint& loc, unsigned width, unsigned height)
: GeometryNode(loc),
mWidth(width),
@ -27,3 +32,11 @@ unsigned RectangleNode::getHeight() const
{
return mHeight;
}
void RectangleNode::updateMesh()
{
const auto rect = Rectangle(mLocation, mWidth, mHeight);
auto mesh = MeshPrimitives::build(rect);
mMesh = std::move(mesh);
}

View file

@ -15,6 +15,8 @@ public:
unsigned getWidth() const;
unsigned getHeight() const;
void updateMesh() override;
private:
unsigned mWidth{1};
unsigned mHeight{1};

View file

@ -0,0 +1,54 @@
#include "Scene.h"
#include "VisualLayer.h"
#include "GeometryNode.h"
#include "RectangleNode.h"
#include "MeshBuilder.h"
#include "TriMesh.h"
void Scene::syncLayers(const std::vector<VisualLayer*>& layers)
{
mLayers = layers;
}
void Scene::update(Image<unsigned char>* image)
{
if (image)
{
//
}
else
{
mWorkingMeshs.clear();
for(auto layer : mLayers)
{
if (layer->hasShapeNode())
{
auto node = layer->getShapeNode();
if (layer->getIsDirty())
{
node->updateMesh();
layer->setIsDirty(false);
}
mWorkingMeshs.push_back(dynamic_cast<TriMesh*>(node->getMesh()));
}
}
}
}
void Scene::processRectangleNode(RectangleNode* node)
{
}
unsigned Scene::getNumMeshes() const
{
return mWorkingMeshs.size();
}
TriMesh* Scene::getMesh(std::size_t idx) const
{
return mWorkingMeshs[idx];
}

View file

@ -1,23 +1,33 @@
#pragma once
#include <vector>
#include <memory>
class VisualLayer;
class TriMesh;
class RectangleNode;
template <typename T>
class Image;
class Scene
{
public:
Scene() = default;
void setLayers(const std::vector<VisualLayer*>& layers)
{
mLayers = layers;
}
void syncLayers(const std::vector<VisualLayer*>& layers);
void update(Image<unsigned char>* image = nullptr);
unsigned getNumMeshes() const;
TriMesh* getMesh(std::size_t idx) const;
const std::vector<VisualLayer*>& getLayers() const
{
return mLayers;
}
private:
void processRectangleNode(RectangleNode* node);
std::vector<TriMesh*> mWorkingMeshs;
std::vector<VisualLayer*> mLayers;
};

View file

@ -15,32 +15,32 @@ std::unique_ptr<VisualLayer> VisualLayer::Create()
return std::make_unique<VisualLayer>();
}
bool VisualLayer::hasShape() const
bool VisualLayer::hasShapeNode() const
{
return bool(mShape);
}
bool VisualLayer::hasText() const
bool VisualLayer::hasTextNode() const
{
return bool(mText);
}
GeometryNode* VisualLayer::getShape() const
GeometryNode* VisualLayer::getShapeNode() const
{
return mShape.get();
}
TextNode* VisualLayer::getText() const
TextNode* VisualLayer::getTextNode() const
{
return mText.get();
}
void VisualLayer::setShape(GeometryNodePtr shape)
void VisualLayer::setShapeNode(GeometryNodePtr shape)
{
mShape = std::move(shape);
}
void VisualLayer::setText(std::unique_ptr<TextNode> text)
void VisualLayer::setTextNode(std::unique_ptr<TextNode> text)
{
mText = std::move(text);
}

View file

@ -12,14 +12,25 @@ public:
static std::unique_ptr<VisualLayer> Create();
GeometryNode* getShape() const;
TextNode* getText() const;
bool hasShape() const;
bool hasText() const;
void setShape(std::unique_ptr<GeometryNode> shape);
void setText(std::unique_ptr<TextNode> text);
GeometryNode* getShapeNode() const;
TextNode* getTextNode() const;
bool hasShapeNode() const;
bool hasTextNode() const;
void setShapeNode(std::unique_ptr<GeometryNode> shape);
void setTextNode(std::unique_ptr<TextNode> text);
bool getIsDirty() const
{
return mIsDirty;
}
void setIsDirty(bool isDirty)
{
mIsDirty = isDirty;
}
private:
bool mIsDirty{true};
std::unique_ptr<GeometryNode> mShape;
std::unique_ptr<TextNode> mText;
};

View file

@ -26,6 +26,7 @@ void XcbLayerInterface::modifyGcColor(xcb_connection_t* connection, xcb_gcontext
void XcbLayerInterface::addLayer(xcb_connection_t* connection, xcb_screen_t* screen, xcb_window_t window, xcb_gcontext_t gc, VisualLayer* layer)
{
/*
if(layer->hasText())
{
XcbTextInterface::AddTextElement(connection, screen, window, layer->getText());
@ -45,5 +46,6 @@ void XcbLayerInterface::addLayer(xcb_connection_t* connection, xcb_screen_t* scr
xcb_poly_fill_rectangle(connection, window, gc, 1, rectangles);
}
}
*/
}