Skip to content

Commit

Permalink
Significantly improved text rendering, support font size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peterspackman committed Aug 28, 2024
1 parent df52e60 commit b8771ae
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 294 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Desktop*/
Debug*/
Release*/
Makefile
.cache/
.DS_Store
resources/basis_sets
resources/tonto
Expand Down
39 changes: 20 additions & 19 deletions shaders/billboard.frag
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#version 330

in vec2 v_mapping;
in vec3 v_cameraPos;
out vec4 outputColor;
in vec2 v_texcoord;
out vec4 fragColor;

uniform float u_screenGamma;
uniform mat4 u_projectionMat;
uniform sampler2D u_texture;
uniform float u_textSDFBuffer;
uniform float u_textSDFSmoothing;
uniform float u_textSDFOutline;
uniform vec3 u_textColor;
uniform vec3 u_textOutlineColor;

vec3 gammaCorrection(vec3 color, float gamma) {
return pow(color, vec3(1.0/gamma));
}
uniform float u_textSDFSmoothing;
uniform float u_textSDFBuffer;
uniform float u_textSDFOutline;

void main()
{
highp vec2 tx = clamp(v_mapping, 0, 1);
float smoothing = u_textSDFSmoothing;
float buf = u_textSDFBuffer;
float distance = texture(u_texture, tx).r;
float border = smoothstep(buf + u_textSDFOutline - smoothing, buf + u_textSDFOutline + smoothing, distance);
float alpha = smoothstep(buf - smoothing, buf + smoothing, distance);
outputColor = vec4(mix(u_textOutlineColor, u_textColor, border), 1.) * alpha;
float distance = texture(u_texture, v_texcoord).r;
distance -= u_textSDFBuffer;
float smoothing = u_textSDFSmoothing * fwidth(distance);
float textAlpha = smoothstep(0.5 + smoothing, 0.5 - smoothing, distance);
float outlineAlpha = smoothstep(0.5 + u_textSDFOutline + smoothing,
0.5 + u_textSDFOutline - smoothing,
distance);
vec3 color = mix(u_textOutlineColor, u_textColor, textAlpha);
float alpha = max(textAlpha, outlineAlpha);
fragColor = vec4(color, alpha);

// Discard nearly transparent fragments
if (alpha < 0.01) {
discard;
}
}
17 changes: 11 additions & 6 deletions shaders/billboard.vert
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec2 dimensions;
layout(location = 2) in float alpha;
Expand All @@ -7,16 +8,20 @@ layout(location = 3) in vec2 texcoord;
uniform mat4 u_projectionMat;
uniform mat4 u_viewMat;
uniform float u_scale;
out vec3 v_cameraPos;
out vec2 v_mapping;

out vec2 v_texcoord;
out float v_alpha;

void main()
{
v_mapping = texcoord;
v_cameraPos = vec3(u_viewMat * vec4(position, 1.0));
v_cameraPos.z += 0.5 * u_scale;
vec4 cameraCornerPos = vec4(v_cameraPos, 1.0);
v_texcoord = texcoord;
v_alpha = alpha;

vec3 cameraPos = vec3(u_viewMat * vec4(position, 1.0));
cameraPos.z += 0.5 * u_scale;

vec4 cameraCornerPos = vec4(cameraPos, 1.0);
cameraCornerPos.xy += (0.002 * texcoord * dimensions) * clamp(u_scale * u_scale, 0.5, 10.0);

gl_Position = u_projectionMat * cameraCornerPos;
}
26 changes: 17 additions & 9 deletions src/core/pair_energy_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ QStringList PairInteractions::interactionComponents(const QString &model) {
void PairInteractions::add(PairInteraction *result) {
if (!result)
return;
qDebug() << "Adding interaction" << result;
QString model = result->interactionModel();
m_pairInteractions[model].insert({result->pairIndex(), result});
double v = 0.0;
impl::ValueRange currentRange;
{
const auto kv = m_maxNearestDistance.find(model);
if (kv != m_maxNearestDistance.end()) {
v = kv->second;
const auto kv = m_distanceRange.find(model);
if (kv != m_distanceRange.end()) {
currentRange = kv->second;
}
}
m_maxNearestDistance[model] = qMax(v, result->nearestAtomDistance());
double d = result->nearestAtomDistance();
m_distanceRange[model] = currentRange.update(d);
emit interactionAdded();
}

Expand Down Expand Up @@ -159,9 +159,9 @@ PairInteractions::getInteractionsMatchingFragments(
QMap<QString, PairInteractions::PairInteractionList> result;
for (const auto &model : models) {
double maxDistance = 0.0;
const auto kv = m_maxNearestDistance.find(model);
if (kv != m_maxNearestDistance.end())
maxDistance = kv->second;
const auto kv = m_distanceRange.find(model);
if (kv != m_distanceRange.end())
maxDistance = kv->second.maxValue;

QList<PairInteraction *> l;
for (const auto &dimer : dimers) {
Expand Down Expand Up @@ -191,6 +191,14 @@ PairInteraction *PairInteractions::getInteraction(const QString &model,
return result->second;
}

void PairInteractions::resetCounts() {
const auto models = interactionModels();
for (auto &[model, interactions]: m_pairInteractions) {
for(auto &[idx, interaction]: interactions) {
interaction->setCount(0);
}
}
}

bool PairInteractions::haveInteractions(const QString &model) const {
return getCount(model) > 0;
Expand Down
23 changes: 22 additions & 1 deletion src/core/pair_energy_results.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,35 @@ class PairInteraction : public QObject {

inline const auto &color() const { return m_color; }
inline void setColor(QColor color) { m_color = color; }
inline int count() const { return m_count; }
inline void setCount(int c) { m_count = c; }

void setParameters(const pair_energy::Parameters &);
const pair_energy::Parameters &parameters() const;
inline const auto &pairIndex() const { return m_parameters.fragmentDimer.index; }

private:
int m_count{0};
QColor m_color{Qt::blue};
QString m_interactionModel;
EnergyComponents m_components;
pair_energy::Parameters m_parameters;
};

namespace impl {
struct ValueRange {
double minValue{std::numeric_limits<double>::max()};
double maxValue{std::numeric_limits<double>::min()};

inline ValueRange merge(ValueRange rhs) const {
return ValueRange{qMin(minValue, rhs.minValue), qMax(maxValue, rhs.maxValue)};
}
inline ValueRange update(double v) const {
return ValueRange{qMin(minValue, v), qMax(maxValue, v)};
}
};
}

class PairInteractions : public QObject {
Q_OBJECT
public:
Expand All @@ -51,6 +68,8 @@ class PairInteractions : public QObject {
void add(PairInteraction *result);
void remove(PairInteraction *result);

void resetCounts();

QMap<QString, PairInteractionList> getInteractionsMatchingFragments(const std::vector<FragmentDimer> &frag);

PairInteraction *getInteraction(const QString &model,
Expand All @@ -73,8 +92,10 @@ class PairInteractions : public QObject {
void interactionRemoved();

private:


ModelInteractions m_pairInteractions;
ankerl::unordered_dense::map<QString, double> m_maxNearestDistance;
ankerl::unordered_dense::map<QString, impl::ValueRange> m_distanceRange;
bool m_haveDimerMap{false};
occ::crystal::DimerMappingTable m_dimerMappingTable;
};
15 changes: 13 additions & 2 deletions src/dialogs/interactioninfodocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ void InteractionInfoDocument::updateContent() {
tabLayout->addWidget(textEdit);

QTextCursor cursor(textEdit->document());
cursor.beginEditBlock();
insertInteractionEnergiesForModel(interactions, cursor, model);
cursor.endEditBlock();

m_tabWidget->addTab(tab, model);
}
Expand Down Expand Up @@ -101,7 +103,7 @@ void InteractionInfoDocument::insertInteractionEnergiesForModel(
}

QList<QString> sortedComponents = getOrderedComponents(uniqueComponents);
QStringList tableHeader{"Color", "Distance", "Symmetry"};
QStringList tableHeader{"Color", "N", "Distance", "Symmetry"};
tableHeader.append(sortedComponents);

InfoTable infoTable(cursor, results->filterByModel(model).size() + 1,
Expand All @@ -114,6 +116,8 @@ void InteractionInfoDocument::insertInteractionEnergiesForModel(
int column = 0;

infoTable.insertColorBlock(row, column++, result->color());
infoTable.insertCellValue(row, column++, QString::number(result->count()),
Qt::AlignRight);
infoTable.insertCellValue(
row, column++, QString::number(result->centroidDistance(), 'f', 2),
Qt::AlignRight);
Expand Down Expand Up @@ -146,7 +150,7 @@ QList<QString> InteractionInfoDocument::getOrderedComponents(
const QSet<QString> &uniqueComponents) {
QList<QString> knownComponentsOrder;
knownComponentsOrder << "coulomb" << "repulsion" << "exchange"
<< "dispersion";
<< "polarization" << "dispersion";

QList<QString> sortedComponents;

Expand Down Expand Up @@ -189,6 +193,7 @@ void InteractionInfoDocument::updateTableColors() {

QTextDocument *doc = textEdit->document();
QTextCursor cursor(doc);
cursor.beginEditBlock();

QTextTable *table = cursor.currentTable();
if (!table)
Expand All @@ -202,7 +207,13 @@ void InteractionInfoDocument::updateTableColors() {
QTextCharFormat format = cell.format();
format.setBackground(result->color());
cell.setFormat(format);
QTextTableCell countCell = table->cellAt(row, 1);
QTextCursor countCursor = countCell.firstCursorPosition();
countCursor.select(QTextCursor::BlockUnderCursor);
countCursor.removeSelectedText();
countCursor.insertText(QString::number(result->count()));
++row;
}
cursor.endEditBlock();
}
}
1 change: 1 addition & 0 deletions src/graphics/billboardrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ void BillboardRenderer::clear() {
m_indices.clear();
m_vertices.clear();
m_labels.clear();
m_textures.clear();
m_numberOfIndices = 0;
updateBuffers();
}
Expand Down
1 change: 1 addition & 0 deletions src/graphics/chemicalstructurerenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void ChemicalStructureRenderer::forceUpdates() {
m_atomsNeedsUpdate = true;
m_bondsNeedsUpdate = true;
m_meshesNeedsUpdate = true;
m_frameworkRenderer->forceUpdates();
}

void ChemicalStructureRenderer::updateLabels() {
Expand Down
Loading

0 comments on commit b8771ae

Please sign in to comment.