Skip to content

Commit

Permalink
Some small refactoring of color handling to support using the color()…
Browse files Browse the repository at this point in the history
… module to change only alpha
  • Loading branch information
kintel committed Dec 11, 2011
1 parent 647fdf5 commit 9ed8d9a
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 169 deletions.
3 changes: 3 additions & 0 deletions openscad.pro
Expand Up @@ -163,6 +163,7 @@ FORMS += src/MainWindow.ui \
src/OpenCSGWarningDialog.ui

HEADERS += src/renderer.h \
src/rendersettings.h \
src/ThrownTogetherRenderer.h \
src/CGAL_renderer.h \
src/OGL_helper.h \
Expand Down Expand Up @@ -217,6 +218,8 @@ HEADERS += src/renderer.h \
SOURCES += src/openscad.cc \
src/mainwin.cc \
src/handle_dep.cc \
src/renderer.cc \
src/rendersettings.cc \
src/ThrownTogetherRenderer.cc \
src/glview.cc \
src/export.cc \
Expand Down
35 changes: 17 additions & 18 deletions src/OpenCSGRenderer.cc
Expand Up @@ -40,11 +40,11 @@ class OpenCSGPrim : public OpenCSG::Primitive
OpenCSG::Primitive(operation, convexity) { }
shared_ptr<PolySet> ps;
Transform3d m;
int csgmode;
PolySet::csgmode_e csgmode;
virtual void render() {
glPushMatrix();
glMultMatrixd(m.data());
ps->render_surface(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode), m);
ps->render_surface(csgmode, m);
glPopMatrix();
}
};
Expand Down Expand Up @@ -89,24 +89,23 @@ void OpenCSGRenderer::renderCSGChain(CSGChain *chain, GLint *shaderinfo,
double *c = chain->colors[j];
glPushMatrix();
glMultMatrixd(m.data());
int csgmode = chain->types[j] == CSGTerm::TYPE_DIFFERENCE ? PolySet::CSGMODE_DIFFERENCE : PolySet::CSGMODE_NORMAL;
PolySet::csgmode_e csgmode = chain->types[j] == CSGTerm::TYPE_DIFFERENCE ? PolySet::CSGMODE_DIFFERENCE : PolySet::CSGMODE_NORMAL;
if (highlight) {
chain->polysets[j]->render_surface(PolySet::COLORMODE_HIGHLIGHT, PolySet::csgmode_e(csgmode + 20), m, shaderinfo);
} else if (background) {
chain->polysets[j]->render_surface(PolySet::COLORMODE_BACKGROUND, PolySet::csgmode_e(csgmode + 10), m, shaderinfo);
} else if (c[0] >= 0 || c[1] >= 0 || c[2] >= 0) {
// User-defined color from source
glColor4dv(c);
if (shaderinfo) {
glUniform4f(shaderinfo[1], c[0], c[1], c[2], c[3]);
glUniform4f(shaderinfo[2], (c[0]+1)/2, (c[1]+1)/2, (c[2]+1)/2, 1.0);
}
chain->polysets[j]->render_surface(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode), m, shaderinfo);
setColor(COLORMODE_HIGHLIGHT, shaderinfo);
csgmode = PolySet::csgmode_e(csgmode + 20);
}
else if (background) {
setColor(COLORMODE_BACKGROUND, shaderinfo);
csgmode = PolySet::csgmode_e(csgmode + 10);
} else if (c[0] >= 0 || c[1] >= 0 || c[2] >= 0 || c[3] >= 0) {
// User-defined color or alpha from source
setColor(c, shaderinfo);
} else if (chain->types[j] == CSGTerm::TYPE_DIFFERENCE) {
chain->polysets[j]->render_surface(PolySet::COLORMODE_CUTOUT, PolySet::csgmode_e(csgmode), m, shaderinfo);
setColor(COLORMODE_CUTOUT, shaderinfo);
} else {
chain->polysets[j]->render_surface(PolySet::COLORMODE_MATERIAL, PolySet::csgmode_e(csgmode), m, shaderinfo);
setColor(COLORMODE_MATERIAL, shaderinfo);
}
chain->polysets[j]->render_surface(csgmode, m, shaderinfo);
glPopMatrix();
}
if (shaderinfo) glUseProgram(0);
Expand All @@ -124,8 +123,8 @@ void OpenCSGRenderer::renderCSGChain(CSGChain *chain, GLint *shaderinfo,
prim->ps = chain->polysets[i];
prim->m = chain->matrices[i];
prim->csgmode = chain->types[i] == CSGTerm::TYPE_DIFFERENCE ? PolySet::CSGMODE_DIFFERENCE : PolySet::CSGMODE_NORMAL;
if (highlight) prim->csgmode += 20;
else if (background) prim->csgmode += 10;
if (highlight) prim->csgmode = PolySet::csgmode_e(prim->csgmode + 20);
else if (background) prim->csgmode = PolySet::csgmode_e(prim->csgmode + 10);
primitives.push_back(prim);
}
std::for_each(primitives.begin(), primitives.end(), del_fun<OpenCSG::Primitive>());
Expand Down
76 changes: 38 additions & 38 deletions src/Preferences.cc
Expand Up @@ -54,38 +54,38 @@ Preferences::Preferences(QWidget *parent) : QMainWindow(parent)
this->actionTriggered(this->prefsAction3DView);

// 3D View pane
this->colorschemes["Cornfield"][BACKGROUND_COLOR] = QColor(0xff, 0xff, 0xe5);
this->colorschemes["Cornfield"][OPENCSG_FACE_FRONT_COLOR] = QColor(0xf9, 0xd7, 0x2c);
this->colorschemes["Cornfield"][OPENCSG_FACE_BACK_COLOR] = QColor(0x9d, 0xcb, 0x51);
this->colorschemes["Cornfield"][CGAL_FACE_FRONT_COLOR] = QColor(0xf9, 0xd7, 0x2c);
this->colorschemes["Cornfield"][CGAL_FACE_BACK_COLOR] = QColor(0x9d, 0xcb, 0x51);
this->colorschemes["Cornfield"][CGAL_FACE_2D_COLOR] = QColor(0x00, 0xbf, 0x99);
this->colorschemes["Cornfield"][CGAL_EDGE_FRONT_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Cornfield"][CGAL_EDGE_BACK_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Cornfield"][CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Cornfield"][CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00);

this->colorschemes["Metallic"][BACKGROUND_COLOR] = QColor(0xaa, 0xaa, 0xff);
this->colorschemes["Metallic"][OPENCSG_FACE_FRONT_COLOR] = QColor(0xdd, 0xdd, 0xff);
this->colorschemes["Metallic"][OPENCSG_FACE_BACK_COLOR] = QColor(0xdd, 0x22, 0xdd);
this->colorschemes["Metallic"][CGAL_FACE_FRONT_COLOR] = QColor(0xdd, 0xdd, 0xff);
this->colorschemes["Metallic"][CGAL_FACE_BACK_COLOR] = QColor(0xdd, 0x22, 0xdd);
this->colorschemes["Metallic"][CGAL_FACE_2D_COLOR] = QColor(0x00, 0xbf, 0x99);
this->colorschemes["Metallic"][CGAL_EDGE_FRONT_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Metallic"][CGAL_EDGE_BACK_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Metallic"][CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Metallic"][CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00);

this->colorschemes["Sunset"][BACKGROUND_COLOR] = QColor(0xaa, 0x44, 0x44);
this->colorschemes["Sunset"][OPENCSG_FACE_FRONT_COLOR] = QColor(0xff, 0xaa, 0xaa);
this->colorschemes["Sunset"][OPENCSG_FACE_BACK_COLOR] = QColor(0x88, 0x22, 0x33);
this->colorschemes["Sunset"][CGAL_FACE_FRONT_COLOR] = QColor(0xff, 0xaa, 0xaa);
this->colorschemes["Sunset"][CGAL_FACE_BACK_COLOR] = QColor(0x88, 0x22, 0x33);
this->colorschemes["Sunset"][CGAL_FACE_2D_COLOR] = QColor(0x00, 0xbf, 0x99);
this->colorschemes["Sunset"][CGAL_EDGE_FRONT_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Sunset"][CGAL_EDGE_BACK_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Sunset"][CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Sunset"][CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00);
this->colorschemes["Cornfield"][RenderSettings::BACKGROUND_COLOR] = QColor(0xff, 0xff, 0xe5);
this->colorschemes["Cornfield"][RenderSettings::OPENCSG_FACE_FRONT_COLOR] = QColor(0xf9, 0xd7, 0x2c);
this->colorschemes["Cornfield"][RenderSettings::OPENCSG_FACE_BACK_COLOR] = QColor(0x9d, 0xcb, 0x51);
this->colorschemes["Cornfield"][RenderSettings::CGAL_FACE_FRONT_COLOR] = QColor(0xf9, 0xd7, 0x2c);
this->colorschemes["Cornfield"][RenderSettings::CGAL_FACE_BACK_COLOR] = QColor(0x9d, 0xcb, 0x51);
this->colorschemes["Cornfield"][RenderSettings::CGAL_FACE_2D_COLOR] = QColor(0x00, 0xbf, 0x99);
this->colorschemes["Cornfield"][RenderSettings::CGAL_EDGE_FRONT_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Cornfield"][RenderSettings::CGAL_EDGE_BACK_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Cornfield"][RenderSettings::CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Cornfield"][RenderSettings::CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00);

this->colorschemes["Metallic"][RenderSettings::BACKGROUND_COLOR] = QColor(0xaa, 0xaa, 0xff);
this->colorschemes["Metallic"][RenderSettings::OPENCSG_FACE_FRONT_COLOR] = QColor(0xdd, 0xdd, 0xff);
this->colorschemes["Metallic"][RenderSettings::OPENCSG_FACE_BACK_COLOR] = QColor(0xdd, 0x22, 0xdd);
this->colorschemes["Metallic"][RenderSettings::CGAL_FACE_FRONT_COLOR] = QColor(0xdd, 0xdd, 0xff);
this->colorschemes["Metallic"][RenderSettings::CGAL_FACE_BACK_COLOR] = QColor(0xdd, 0x22, 0xdd);
this->colorschemes["Metallic"][RenderSettings::CGAL_FACE_2D_COLOR] = QColor(0x00, 0xbf, 0x99);
this->colorschemes["Metallic"][RenderSettings::CGAL_EDGE_FRONT_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Metallic"][RenderSettings::CGAL_EDGE_BACK_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Metallic"][RenderSettings::CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Metallic"][RenderSettings::CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00);

this->colorschemes["Sunset"][RenderSettings::BACKGROUND_COLOR] = QColor(0xaa, 0x44, 0x44);
this->colorschemes["Sunset"][RenderSettings::OPENCSG_FACE_FRONT_COLOR] = QColor(0xff, 0xaa, 0xaa);
this->colorschemes["Sunset"][RenderSettings::OPENCSG_FACE_BACK_COLOR] = QColor(0x88, 0x22, 0x33);
this->colorschemes["Sunset"][RenderSettings::CGAL_FACE_FRONT_COLOR] = QColor(0xff, 0xaa, 0xaa);
this->colorschemes["Sunset"][RenderSettings::CGAL_FACE_BACK_COLOR] = QColor(0x88, 0x22, 0x33);
this->colorschemes["Sunset"][RenderSettings::CGAL_FACE_2D_COLOR] = QColor(0x00, 0xbf, 0x99);
this->colorschemes["Sunset"][RenderSettings::CGAL_EDGE_FRONT_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Sunset"][RenderSettings::CGAL_EDGE_BACK_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Sunset"][RenderSettings::CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00);
this->colorschemes["Sunset"][RenderSettings::CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00);

// Editor pane
QFontDatabase db;
Expand All @@ -102,6 +102,8 @@ Preferences::Preferences(QWidget *parent) : QMainWindow(parent)
connect(this->openCSGWarningBox, SIGNAL(toggled(bool)),
this, SLOT(openCSGWarningChanged(bool)));
updateGUI();

RenderSettings::inst()->setColors(this->colorschemes[getValue("3dview/colorscheme").toString()]);
}

Preferences::~Preferences()
Expand All @@ -125,15 +127,13 @@ Preferences::actionTriggered(QAction *action)

void Preferences::colorSchemeChanged()
{
QString scheme = this->colorSchemeChooser->currentItem()->text();
QSettings settings;
settings.setValue("3dview/colorscheme", this->colorSchemeChooser->currentItem()->text());
settings.setValue("3dview/colorscheme", scheme);

emit requestRedraw();
}
RenderSettings::inst()->setColors(this->colorschemes[scheme]);

const QColor &Preferences::color(RenderColor idx)
{
return this->colorschemes[getValue("3dview/colorscheme").toString()][idx];
emit requestRedraw();
}

void Preferences::fontFamilyChanged(const QString &family)
Expand Down
16 changes: 2 additions & 14 deletions src/Preferences.h
Expand Up @@ -4,6 +4,7 @@
#include <QMainWindow>
#include <QSettings>
#include "ui_Preferences.h"
#include "rendersettings.h"

class Preferences : public QMainWindow, public Ui::Preferences
{
Expand All @@ -13,19 +14,6 @@ class Preferences : public QMainWindow, public Ui::Preferences
~Preferences();
static Preferences *inst() { if (!instance) instance = new Preferences(); return instance; }

enum RenderColor {
BACKGROUND_COLOR,
OPENCSG_FACE_FRONT_COLOR,
OPENCSG_FACE_BACK_COLOR,
CGAL_FACE_FRONT_COLOR,
CGAL_FACE_2D_COLOR,
CGAL_FACE_BACK_COLOR,
CGAL_EDGE_FRONT_COLOR,
CGAL_EDGE_BACK_COLOR,
CGAL_EDGE_2D_COLOR,
CROSSHAIR_COLOR
};
const QColor &color(RenderColor idx);
QVariant getValue(const QString &key) const;
void apply() const;

Expand All @@ -48,7 +36,7 @@ public slots:
void removeDefaultSettings();

QSettings::SettingsMap defaultmap;
QHash<QString, QMap<RenderColor, QColor> > colorschemes;
QHash<QString, QMap<RenderSettings::RenderColor, QColor> > colorschemes;

static Preferences *instance;
};
Expand Down
57 changes: 27 additions & 30 deletions src/ThrownTogetherRenderer.cc
Expand Up @@ -70,51 +70,48 @@ void ThrownTogetherRenderer::renderCSGChain(CSGChain *chain, bool highlight,
double *c = chain->colors[i];
glPushMatrix();
glMultMatrixd(m.data());
int csgmode = chain->types[i] == CSGTerm::TYPE_DIFFERENCE ? PolySet::CSGMODE_DIFFERENCE : PolySet::CSGMODE_NORMAL;
PolySet::csgmode_e csgmode = chain->types[i] == CSGTerm::TYPE_DIFFERENCE ? PolySet::CSGMODE_DIFFERENCE : PolySet::CSGMODE_NORMAL;
if (highlight) {
chain->polysets[i]->render_surface(PolySet::COLORMODE_HIGHLIGHT, PolySet::csgmode_e(csgmode + 20), m);
csgmode = PolySet::csgmode_e(csgmode + 20);
setColor(COLORMODE_HIGHLIGHT);
chain->polysets[i]->render_surface(csgmode, m);
if (showedges) {
glDisable(GL_LIGHTING);
chain->polysets[i]->render_edges(PolySet::COLORMODE_HIGHLIGHT, PolySet::csgmode_e(csgmode + 20));
glEnable(GL_LIGHTING);
setColor(COLORMODE_HIGHLIGHT_EDGES);
chain->polysets[i]->render_edges(csgmode);
}
} else if (background) {
chain->polysets[i]->render_surface(PolySet::COLORMODE_BACKGROUND, PolySet::csgmode_e(csgmode + 10), m);
csgmode = PolySet::csgmode_e(csgmode + 10);
setColor(COLORMODE_BACKGROUND);
chain->polysets[i]->render_surface(csgmode, m);
if (showedges) {
glDisable(GL_LIGHTING);
chain->polysets[i]->render_edges(PolySet::COLORMODE_BACKGROUND, PolySet::csgmode_e(csgmode + 10));
glEnable(GL_LIGHTING);
setColor(COLORMODE_BACKGROUND_EDGES);
chain->polysets[i]->render_edges(csgmode);
}
} else if (fberror) {
if (highlight) {
chain->polysets[i]->render_surface(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode + 20), m);
} else if (background) {
chain->polysets[i]->render_surface(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode + 10), m);
} else {
chain->polysets[i]->render_surface(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode), m);
}
} else if (c[0] >= 0 || c[1] >= 0 || c[2] >= 0) {
glColor4dv(c);
chain->polysets[i]->render_surface(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode), m);
if (highlight) csgmode = PolySet::csgmode_e(csgmode + 20);
else if (background) csgmode = PolySet::csgmode_e(csgmode + 10);
else csgmode = PolySet::csgmode_e(csgmode);
chain->polysets[i]->render_surface(csgmode, m);
} else if (c[0] >= 0 || c[1] >= 0 || c[2] >= 0 || c[3] >= 0) {
setColor(c);
chain->polysets[i]->render_surface(csgmode, m);
if (showedges) {
glDisable(GL_LIGHTING);
glColor4d((c[0]+1)/2, (c[1]+1)/2, (c[2]+1)/2, 1.0);
chain->polysets[i]->render_edges(PolySet::COLORMODE_NONE, PolySet::csgmode_e(csgmode));
glEnable(GL_LIGHTING);
chain->polysets[i]->render_edges(csgmode);
}
} else if (chain->types[i] == CSGTerm::TYPE_DIFFERENCE) {
chain->polysets[i]->render_surface(PolySet::COLORMODE_CUTOUT, PolySet::csgmode_e(csgmode), m);
setColor(COLORMODE_CUTOUT);
chain->polysets[i]->render_surface(csgmode, m);
if (showedges) {
glDisable(GL_LIGHTING);
chain->polysets[i]->render_edges(PolySet::COLORMODE_CUTOUT, PolySet::csgmode_e(csgmode));
glEnable(GL_LIGHTING);
setColor(COLORMODE_CUTOUT_EDGES);
chain->polysets[i]->render_edges(csgmode);
}
} else {
chain->polysets[i]->render_surface(PolySet::COLORMODE_MATERIAL, PolySet::csgmode_e(csgmode), m);
setColor(COLORMODE_MATERIAL);
chain->polysets[i]->render_surface(csgmode, m);
if (showedges) {
glDisable(GL_LIGHTING);
chain->polysets[i]->render_edges(PolySet::COLORMODE_MATERIAL, PolySet::csgmode_e(csgmode));
glEnable(GL_LIGHTING);
setColor(COLORMODE_MATERIAL_EDGES);
chain->polysets[i]->render_edges(csgmode);
}
}
glPopMatrix();
Expand Down
34 changes: 31 additions & 3 deletions src/glview.cc
Expand Up @@ -27,6 +27,7 @@
#include "GLView.h"
#include "Preferences.h"
#include "renderer.h"
#include "rendersettings.h"

#include <QApplication>
#include <QWheelEvent>
Expand Down Expand Up @@ -190,6 +191,28 @@ void GLView::initializeGL()
}
}
if (opencsg_support && this->has_shaders) {
/*
Uniforms:
1 color1 - face color
2 color2 - edge color
7 xscale
8 yscale
Attributes:
3 trig
4 pos_b
5 pos_c
6 mask
Other:
9 width
10 height
Outputs:
tp
tr
shading
*/
const char *vs_source =
"uniform float xscale, yscale;\n"
"attribute vec3 pos_b, pos_c;\n"
Expand All @@ -215,6 +238,11 @@ void GLView::initializeGL()
" shading = abs(dot(normal, lightDir));\n"
"}\n";

/*
Inputs:
tp && tr - if any components of tp < tr, use color2 (edge color)
shading - multiplied by color1. color2 is is without lighting
*/
const char *fs_source =
"uniform vec4 color1, color2;\n"
"varying vec3 tp, tr, tmp;\n"
Expand Down Expand Up @@ -351,7 +379,7 @@ void GLView::paintGL()
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

const QColor &bgcol = Preferences::inst()->color(Preferences::BACKGROUND_COLOR);
const QColor &bgcol = RenderSettings::inst()->color(RenderSettings::BACKGROUND_COLOR);
glClearColor(bgcol.redF(), bgcol.greenF(), bgcol.blueF(), 0.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Expand All @@ -369,7 +397,7 @@ void GLView::paintGL()
if (showcrosshairs)
{
glLineWidth(3);
const QColor &col = Preferences::inst()->color(Preferences::CROSSHAIR_COLOR);
const QColor &col = RenderSettings::inst()->color(RenderSettings::CROSSHAIR_COLOR);
glColor3f(col.redF(), col.greenF(), col.blueF());
glBegin(GL_LINES);
for (double xf = -1; xf <= +1; xf += 2)
Expand Down Expand Up @@ -470,7 +498,7 @@ void GLView::paintGL()
// FIXME: This was an attempt to keep contrast with background, but is suboptimal
// (e.g. nearly invisible against a gray background).
int r,g,b;
bgcol.getRgb(&r, &g, &b);
// bgcol.getRgb(&r, &g, &b);
glColor3d((255.0-r)/255.0, (255.0-g)/255.0, (255.0-b)/255.0);
glBegin(GL_LINES);
// X Label
Expand Down

0 comments on commit 9ed8d9a

Please sign in to comment.