Add outline rendering.

This commit is contained in:
James Grogan 2022-11-18 09:43:22 +00:00
parent f04d86e0ad
commit a20c0183df
20 changed files with 291 additions and 64 deletions

View file

@ -19,7 +19,6 @@ void ImageViewWidget::doPaint(const PaintEvent* event)
if (!mGridNode)
{
std::cout << "Setting up grid node" << std::endl;
mGridNode = std::make_unique<GridNode>(mLocation);
mGridNode->setName(mName + "_GridNode");
mGridNode->setNumX(mNumX);
@ -27,6 +26,21 @@ void ImageViewWidget::doPaint(const PaintEvent* event)
mGridNode->setWidth(mSize.mWidth);
mGridNode->setHeight(mSize.mHeight);
mGridNode->setStrokeColor(mBorderColor);
mRootNode->addChild(mGridNode.get());
}
if (mTransformDirty)
{
mGridNode->setLocation(mLocation);
mGridNode->setWidth(mSize.mWidth);
mGridNode->setHeight(mSize.mHeight);
}
if (mMaterialDirty)
{
mGridNode->setFillColor(mBorderColor);
}
}

View file

@ -17,7 +17,8 @@ list(APPEND geometry_LIB_INCLUDES
Rectangle.h
Rectangle.cpp
Triangle.h
Triangle.cpp)
Triangle.cpp
Transform.cpp)
# add the library

View file

@ -33,10 +33,18 @@ public:
double getDeltaY(const Point& point) const;
void scale(double x, double y)
void scale(double x, double y, double z = 1.0)
{
mX = x*mX;
mY = y*mY;
mZ = z*mZ;
}
void translate(double x, double y, double z = 0.0)
{
mX += x;
mY += y;
mZ += z;
}
bool operator==(const Point& rhs) const

View file

@ -3,7 +3,9 @@
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "SceneModel.h"
#include "TriMesh.h"
#include "LineMesh.h"
#include "OpenGlShaderProgram.h"
@ -43,7 +45,7 @@ void OpenGlMeshPainter::initializeBuffers()
glGenVertexArrays(1, &mVertexArray);
}
void OpenGlMeshPainter::paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color)
void OpenGlMeshPainter::paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color, bool lines)
{
glBindVertexArray(mVertexArray);
@ -66,7 +68,15 @@ void OpenGlMeshPainter::paint(const std::vector<float>& verts, const std::vector
glUniform4f(vertexColorLocation, float(color[0]), float(color[1]), float(color[2]), float(color[3]));
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
if (lines)
{
glDrawElements(GL_LINES, elements.size(), GL_UNSIGNED_INT, 0);
}
else
{
glDrawElements( GL_TRIANGLES, elements.size(), GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
}
@ -86,19 +96,14 @@ void OpenGlMeshPainter::paint(SceneModel* model, DrawingContext* context)
const auto width = float(surface->getWidth());
const auto height = float(surface->getHeight());
auto mesh = dynamic_cast<TriMesh*>(model->getMesh());
std::cout << "Paint mesh for model " << model->getName() << std::endl;
auto transform = model->getTransform();
auto vertices = mesh->getVerticesFlat<float>();
auto vertices = model->getMesh()->getVerticesFlat<float>();
for (std::size_t idx = 0; idx<vertices.size(); idx++)
{
if (idx % 3 == 0)
{
auto x = vertices[idx]*transform.getScaleX() + transform.getLocation().getX();
vertices[idx] = 2*x/width - 1.0;
std::cout << "Vert x is " << vertices[idx] << std::endl;
}
else if(idx%3 == 1)
{
@ -107,11 +112,19 @@ void OpenGlMeshPainter::paint(SceneModel* model, DrawingContext* context)
}
}
const auto indices = mesh->getFaceNodeIds();
std::vector<unsigned> indices;
const bool line_mesh = model->getMesh()->getType() == AbstractMesh::MeshType::LINE;
if (line_mesh)
{
indices = dynamic_cast<LineMesh*>(model->getMesh())->getEdgeNodeIds();
}
else
{
indices = dynamic_cast<TriMesh*>(model->getMesh())->getFaceNodeIds();
}
auto model_color = model->getColor().getAsVectorDouble();
std::cout << "Color is " << model_color[0] << "|" << model_color[0] << std::endl;
std::vector<float> color = {float(model_color[0]), float(model_color[1]), float(model_color[2]), float(model_color[3])};
paint(vertices, indices, color);
paint(vertices, indices, color, line_mesh);
}

View file

@ -18,7 +18,7 @@ private:
void initializeShader();
void initializeBuffers();
void paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color);
void paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color, bool lines = false);
unsigned mVertexBuffer{0};
unsigned mElementBuffer{0};

View file

@ -38,3 +38,26 @@ void AbstractMesh::scale(double scaleX, double scaleY)
node->scale(scaleX, scaleY);
}
}
void AbstractMesh::transform(const Transform& transform)
{
auto scaleX = transform.getScaleX();
auto scaleY = transform.getScaleY();
scale(scaleX, scaleY);
translate(transform.getLocation());
}
void AbstractMesh::translate(double offsetX, double offsetY, double offsetZ)
{
for (auto& node : mNodes)
{
node->translate(offsetX, offsetY, offsetZ);
}
}
void AbstractMesh::translate(const Point& offset)
{
translate(offset.getX(), offset.getY(), offset.getZ());
}

View file

@ -2,6 +2,7 @@
#include "Point.h"
#include "Node.h"
#include "Transform.h"
#include <memory>
#include <vector>
@ -49,6 +50,12 @@ public:
void scale(double scaleX, double scaleY);
void translate(const Point& offset);
void translate(double offsetX, double offsetY, double offsetZ = 0.0);
void transform(const Transform& transform);
protected:
std::unordered_map<std::string, std::vector<double> > mVectorAttributes;
VecNodes mNodes;

View file

@ -7,6 +7,7 @@ list(APPEND mesh_LIB_INCLUDES
Node.cpp
QuadMesh.cpp
TriMesh.cpp
LineMesh.cpp
MeshPrimitives.cpp
MeshBuilder.cpp)

25
src/mesh/LineMesh.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "LineMesh.h"
#include "Edge.h"
LineMesh::~LineMesh()
{
}
void LineMesh::populate(VecNodes& nodes, VecEdges& edges)
{
mNodes = std::move(nodes);
mEdges = std::move(edges);
}
std::vector<unsigned> LineMesh::getEdgeNodeIds() const
{
std::vector<unsigned> ids(2*mEdges.size());
for(std::size_t idx=0; idx<mEdges.size(); idx++)
{
ids[2*idx] = mEdges[idx]->getNode0Id();
ids[2*idx + 1] = mEdges[idx]->getNode1Id();
}
return ids;
}

30
src/mesh/LineMesh.h Normal file
View file

@ -0,0 +1,30 @@
#pragma once
#include "AbstractMesh.h"
#include <string>
#include <unordered_map>
class Edge;
using EdgePtr = std::unique_ptr<Edge>;
using VecEdges = std::vector<EdgePtr>;
class LineMesh : public AbstractMesh
{
public:
LineMesh() = default;
~LineMesh();
void populate(VecNodes& nodes, VecEdges& edges);
std::vector<unsigned> getEdgeNodeIds() const;
MeshType getType() const override
{
return MeshType::LINE;
}
private:
VecEdges mEdges;
};

View file

@ -33,3 +33,23 @@ std::unique_ptr<TriMesh> MeshBuilder::buildTriMesh(const VecPoints& locations, c
mesh->populate(nodes, edges, faces);
return mesh;
}
std::unique_ptr<LineMesh> MeshBuilder::buildLineMesh(const VecPoints& locations, const EdgeIds& edgeIds)
{
auto mesh = std::make_unique<LineMesh>();
VecNodes nodes(locations.size());
for (std::size_t idx=0; idx<locations.size(); idx++)
{
nodes[idx] = Node::Create(locations[idx], idx);
}
VecEdges edges(edgeIds.size());
for (std::size_t idx=0; idx<edgeIds.size(); idx++)
{
edges[idx] = Edge::Create(nodes[edgeIds[idx].first].get(), nodes[edgeIds[idx].second].get(), idx);
}
mesh->populate(nodes, edges);
return mesh;
}

View file

@ -1,8 +1,11 @@
#pragma once
#include "TriMesh.h"
#include "LineMesh.h"
#include "Point.h"
#include <memory>
using EdgeIds = std::vector<std::pair<unsigned, unsigned> >;
using FaceIds = std::vector<std::vector<unsigned> >;
using VecPoints = std::vector<Point>;
@ -11,4 +14,6 @@ class MeshBuilder
{
public:
static std::unique_ptr<TriMesh> buildTriMesh(const VecPoints& locations, const EdgeIds& edgeIds, const FaceIds& faceIds);
static std::unique_ptr<LineMesh> buildLineMesh(const VecPoints& locations, const EdgeIds& edgeIds);
};

View file

@ -1,18 +1,16 @@
#include "MeshPrimitives.h"
#include "MeshBuilder.h"
#include <iostream>
std::unique_ptr<TriMesh> MeshPrimitives::build(const Rectangle& rectangle)
std::unique_ptr<TriMesh> MeshPrimitives::buildRectangleAsTriMesh()
{
const auto bottom_left = rectangle.getLocation();
const auto width = rectangle.getWidth();
const auto height = rectangle.getHeight();
VecPoints locations = {
bottom_left,
Point(bottom_left, width, 0),
Point(bottom_left, width, height),
Point(bottom_left, 0, height)
{0, 0},
{1, 0},
{1, 1},
{0, 1}
};
EdgeIds edge_ids = {
@ -31,14 +29,33 @@ std::unique_ptr<TriMesh> MeshPrimitives::build(const Rectangle& rectangle)
return MeshBuilder::buildTriMesh(locations, edge_ids, face_ids);
}
std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGrid(unsigned numX, unsigned numY)
std::unique_ptr<LineMesh> MeshPrimitives::buildRectangleAsLineMesh()
{
VecPoints locations = {
{0, 0},
{1, 0},
{1, 1},
{0, 1}
};
EdgeIds edge_ids = {
{0, 1},
{1, 2},
{2, 3},
{3, 0}
};
return MeshBuilder::buildLineMesh(locations, edge_ids);
}
std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGridAsTriMesh(unsigned numX, unsigned numY)
{
double delta_x = 1.0/double(numX);
double delta_y = 1.0/double(numY);
VecPoints locations (4 * numX * numY);
double offset_x = delta_x;
double offset_y = delta_y;
double offset_x = delta_x/2.0;
double offset_y = delta_y/2.0;
for (unsigned idx=0; idx<numY; idx++)
{
for(unsigned jdx=0; jdx<numX; jdx++)
@ -53,9 +70,10 @@ std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGrid(unsigned numX, unsign
locations[id_offset + 1] = Point(locX1, locY0);
locations[id_offset + 2] = Point(locX1, locY1);
locations[id_offset + 3] = Point(locX0, locY1);
offset_x += delta_x;
}
offset_x = delta_x;
offset_x = delta_x/2.0;
offset_y += delta_y;
}
@ -88,3 +106,49 @@ std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGrid(unsigned numX, unsign
return MeshBuilder::buildTriMesh(locations, edge_ids, face_ids);
}
std::unique_ptr<LineMesh> MeshPrimitives::buildExplodedGridAsLineMesh(unsigned numX, unsigned numY)
{
double delta_x = 1.0/double(numX);
double delta_y = 1.0/double(numY);
VecPoints locations (4 * numX * numY);
double offset_x = delta_x/2.0;
double offset_y = delta_y/2.0;
for (unsigned idx=0; idx<numY; idx++)
{
for(unsigned jdx=0; jdx<numX; jdx++)
{
auto locX0 = offset_x - delta_x/2.0;
auto locX1 = offset_x + delta_x/2.0;
auto locY0 = offset_y - delta_y/2.0;
auto locY1 = offset_y + delta_y/2.0;
auto id_offset = 4* (jdx + numX*idx);
locations[id_offset] = Point(locX0, locY0);
locations[id_offset + 1] = Point(locX1, locY0);
locations[id_offset + 2] = Point(locX1, locY1);
locations[id_offset + 3] = Point(locX0, locY1);
offset_x += delta_x;
}
offset_x = delta_x/2.0;
offset_y += delta_y;
}
EdgeIds edge_ids(4 * numX * numY);
for (unsigned idx=0; idx<numY; idx++)
{
for(unsigned jdx=0; jdx<numX; jdx++)
{
unsigned node_offset = 4 * (jdx + numX * idx);
auto id_offset = 4* (jdx + numX*idx);
edge_ids[id_offset] = {node_offset, node_offset + 1};
edge_ids[id_offset + 1] = {node_offset + 1, node_offset + 2};
edge_ids[id_offset + 2] = {node_offset + 2, node_offset + 3};
edge_ids[id_offset + 3] = {node_offset + 3, node_offset};
}
}
return MeshBuilder::buildLineMesh(locations, edge_ids);
}

View file

@ -1,21 +1,17 @@
#pragma once
#include "Rectangle.h"
#include "TriMesh.h"
#include "Node.h"
#include "Edge.h"
#include "MeshBuilder.h"
#include "AbstractGeometricItem.h"
#include <tuple>
#include "LineMesh.h"
class MeshPrimitives
{
public:
static std::unique_ptr<TriMesh> build(const Rectangle& rectangle);
static std::unique_ptr<TriMesh> buildRectangleAsTriMesh();
static std::unique_ptr<TriMesh> buildExplodedGrid(unsigned numX, unsigned numY);
static std::unique_ptr<LineMesh> buildRectangleAsLineMesh();
static std::unique_ptr<TriMesh> buildExplodedGridAsTriMesh(unsigned numX, unsigned numY);
static std::unique_ptr<LineMesh> buildExplodedGridAsLineMesh(unsigned numX, unsigned numY);
};

View file

@ -34,6 +34,11 @@ public:
mPoint.scale(x, y);
}
void translate(double x, double y, double z = 0.0)
{
mPoint.translate(x, y, z);
}
private:
std::unordered_map<std::string, std::vector<double> > mVectorAttributes;
unsigned mIndex{0};

View file

@ -7,7 +7,6 @@ list(APPEND visual_elements_LIB_INCLUDES
SceneModel.cpp
SceneItem.cpp
SceneText.cpp
Transform.cpp
Texture.cpp
GridNode.cpp
AbstractVisualNode.cpp

View file

@ -67,56 +67,73 @@ SceneItem* GridNode::getSceneItem(std::size_t idx) const
unsigned GridNode::getNumSceneItems() const
{
return 1;
return 2;
}
void GridNode::update(FontsManager* fontsManager)
{
if (!mBackgroundModel || mDataDirty)
{
auto mesh = MeshPrimitives::buildExplodedGrid(mNumberX, mNumberY);
if (mHeight > mWidth)
{
mesh->scale(mWidth, mWidth);
}
else
{
mesh->scale(mHeight, mHeight);
}
auto mesh = MeshPrimitives::buildExplodedGridAsTriMesh(mNumberX, mNumberY);
if (!mBackgroundModel)
{
std::cout << "Setting up background model for grid node " << std::endl;
mBackgroundModel = std::make_unique<SceneModel>(std::move(mesh));
mBackgroundModel->setName(mName + "_Model");
mBackgroundModel->updateUniformColor({200, 200, 200, 1.0});
mBackgroundModel->setName(mName + "_BackgroundModel");
}
else
{
mBackgroundModel.get()->updateMesh(std::move(mesh));
mBackgroundModel->updateUniformColor({200, 200, 200, 1.0});
}
}
/*
if (!mOutlineModel || mDataDirty)
if (mOutlineModel || mDataDirty)
{
const auto rect = Rectangle(Point(), 1, 1);
auto mesh = MeshPrimitives::build(rect);
auto mesh = MeshPrimitives::buildExplodedGridAsLineMesh(mNumberX, mNumberY);
if (!mOutlineModel)
{
mOutlineModel = std::make_unique<SceneModel>(std::move(mesh));
mOutlineModel->setName(mName + "_Model");
mOutlineModel->setName(mName + "_OulineModel");
}
else
{
mOutlineModel.get()->updateMesh(std::move(mesh));
}
}
*/
if (mDataDirty)
{
auto difference = mNumberX*mNumberY - mData.size();
mData.resize(mNumberX*mNumberY, {});
auto node_data = std::vector<std::vector<double> >(4*mNumberX*mNumberY);
for (unsigned idx=0; idx< node_data.size(); idx++)
{
if (idx < mData.size())
{
node_data[idx] = mData[idx].getAsVectorDouble();
}
}
//mBackgroundModel->getMesh()->addVectorAttribute("Color", mData);
}
if (mTransformIsDirty)
{
auto size = mHeight > mWidth ? mWidth : mHeight;
mBackgroundModel->updateTransform({mLocation, static_cast<double>(size), static_cast<double>(size)});
mOutlineModel->updateTransform({mLocation, static_cast<double>(size), static_cast<double>(size)});
mTransformIsDirty = false;
}
if (mMaterialIsDirty)
{
mOutlineModel->updateUniformColor(mStrokeColor);
mMaterialIsDirty = false;
}
mDataDirty = false;
}

View file

@ -73,8 +73,7 @@ void RectangleNode::update(FontsManager* fontsManager)
{
if (!mBackgroundItem || mGeometryIsDirty)
{
const auto rect = Rectangle(Point(), 1, 1);
auto mesh = MeshPrimitives::build(rect);
auto mesh = MeshPrimitives::buildRectangleAsTriMesh();
if (!mBackgroundItem)
{