Merge pull request #1038 from openscad/issue1024+issue1035

Update drawing logic
master
Torsten Paul 2014-11-30 03:35:37 +01:00
commit 471ff7718e
4 changed files with 107 additions and 33 deletions

View File

@ -113,7 +113,6 @@ void GLView::setupCamera()
glRotated(cam.object_rot.x(), 1.0, 0.0, 0.0);
glRotated(cam.object_rot.y(), 0.0, 1.0, 0.0);
glRotated(cam.object_rot.z(), 0.0, 0.0, 1.0);
glTranslated(cam.object_trans.x(), cam.object_trans.y(), cam.object_trans.z() );
break;
case Camera::VECTOR: {
switch (this->cam.projection) {
@ -150,18 +149,24 @@ void GLView::setupCamera()
void GLView::paintGL()
{
glEnable(GL_LIGHTING);
setupCamera();
glDisable(GL_LIGHTING);
Color4f bgcol = ColorMap::getColor(*this->colorscheme, BACKGROUND_COLOR);
Color4f bgcontrast = ColorMap::getContrastColor(bgcol);
glClearColor(bgcol[0], bgcol[1], bgcol[2], 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Only for GIMBAL cam
if (showcrosshairs) GLView::showCrosshairs();
if (showaxes) GLView::showAxes();
setupCamera();
if (this->cam.type) {
// Only for GIMBAL cam
// The crosshair should be fixed at the center of the viewport...
if (showcrosshairs) GLView::showCrosshairs();
glTranslated(cam.object_trans.x(), cam.object_trans.y(), cam.object_trans.z());
// ...the axis lines need to follow the object translation.
if (showaxes) GLView::showAxes(bgcontrast);
}
glEnable(GL_LIGHTING);
glDepthFunc(GL_LESS);
glCullFace(GL_BACK);
glDisable(GL_CULL_FACE);
@ -174,10 +179,11 @@ void GLView::paintGL()
OpenCSG::setContext(this->opencsg_id);
#endif
this->renderer->draw(showfaces, showedges);
}
}
// Only for GIMBAL
if (showaxes) GLView::showSmallaxes();
glDisable(GL_LIGHTING);
if (showaxes) GLView::showSmallaxes(bgcontrast);
}
#ifdef ENABLE_OPENCSG
@ -348,6 +354,7 @@ void GLView::initializeGL()
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_LINE_STIPPLE);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
// The following line is reported to fix issue #71
@ -358,7 +365,7 @@ void GLView::initializeGL()
#endif
}
void GLView::showSmallaxes()
void GLView::showSmallaxes(const Color4f &col)
{
// Fixme - this doesnt work in Vector Camera mode
@ -423,14 +430,9 @@ void GLView::showSmallaxes()
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// 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;
// r=g=b=0;
// bgcol.getRgb(&r, &g, &b);
// glColor3f((255.0f-r)/255.0f, (255.0f-g)/255.0f, (255.0f-b)/255.0f);
float d = 3*dpi;
glColor3f(0.0f, 0.0f, 0.0f);
glColor3f(col[0], col[1], col[2]);
float d = 3*dpi;
glBegin(GL_LINES);
// X Label
glVertex3d(xlabel_x-d, xlabel_y-d, 0); glVertex3d(xlabel_x+d, xlabel_y+d, 0);
@ -447,36 +449,47 @@ void GLView::showSmallaxes()
glEnd();
}
void GLView::showAxes()
void GLView::showAxes(const Color4f &col)
{
double l = cam.projection == Camera::PERSPECTIVE ? cam.viewer_distance : cam.height;
// FIXME: doesn't work under Vector Camera
// Large gray axis cross inline with the model
// FIXME: This is always gray - adjust color to keep contrast with background
glLineWidth(this->getDPI());
glColor3d(0.5, 0.5, 0.5);
glColor3f(col[0], col[1], col[2]);
glBegin(GL_LINES);
double l = cam.projection == Camera::PERSPECTIVE ? cam.viewer_distance : cam.height;
glVertex3d(-l, 0, 0);
glVertex3d(0, 0, 0);
glVertex3d(+l, 0, 0);
glVertex3d(0, -l, 0);
glVertex3d(0, 0, 0);
glVertex3d(0, +l, 0);
glVertex3d(0, 0, -l);
glVertex3d(0, 0, 0);
glVertex3d(0, 0, +l);
glEnd();
glPushAttrib(GL_LINE_BIT);
glLineStipple(3, 0xAAAA);
glBegin(GL_LINES);
glVertex3d(0, 0, 0);
glVertex3d(-l, 0, 0);
glVertex3d(0, 0, 0);
glVertex3d(0, -l, 0);
glVertex3d(0, 0, 0);
glVertex3d(0, 0, -l);
glEnd();
glPopAttrib();
}
void GLView::showCrosshairs()
{
// FIXME: this might not work with Vector camera
// FIXME: Crosshairs and axes are lighted, this doesn't make sense and causes them
// to change color based on view orientation.
glLineWidth(3);
glLineWidth(this->getDPI());
Color4f col = ColorMap::getColor(*this->colorscheme, CROSSHAIR_COLOR);
glColor3f(col[0], col[1], col[2]);
glBegin(GL_LINES);
for (double xf = -1; xf <= +1; xf += 2)
for (double yf = -1; yf <= +1; yf += 2) {
double vd = cam.viewer_distance/20;
double vd = cam.viewer_distance/8;
glVertex3d(-xf*vd, -yf*vd, -vd);
glVertex3d(+xf*vd, +yf*vd, +vd);
}

View File

@ -42,10 +42,6 @@ public:
void setCamera(const Camera &cam);
void setupCamera();
void showCrosshairs();
void showAxes();
void showSmallaxes();
void setColorScheme(const ColorScheme &cs);
void setColorScheme(const std::string &cs);
void updateColorScheme();
@ -76,4 +72,8 @@ public:
bool opencsg_support;
int opencsg_id;
#endif
private:
void showCrosshairs();
void showAxes(const Color4f &col);
void showSmallaxes(const Color4f &col);
};

View File

@ -5,6 +5,29 @@
static const char *DEFAULT_COLOR_SCHEME_NAME = "Cornfield";
// See http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
static void rgbtohsv(float r, float g, float b, float &h, float &s, float &v)
{
float K = 0.f;
if (g < b)
{
std::swap(g, b);
K = -1.f;
}
if (r < g)
{
std::swap(r, g);
K = -2.f / 6.f - K;
}
float chroma = r - std::min(g, b);
h = fabs(K + (g - b) / (6.f * chroma + 1e-20f));
s = chroma / (r + 1e-20f);
v = r;
}
RenderColorScheme::RenderColorScheme() : _path("")
{
_name = DEFAULT_COLOR_SCHEME_NAME;
@ -196,6 +219,42 @@ Color4f ColorMap::getColor(const ColorScheme &cs, const RenderColor rc)
return Color4f(0, 0, 0, 127);
}
Color4f ColorMap::getColorHSV(const Color4f &col)
{
float h, s, v;
rgbtohsv(col[0], col[1], col[2], h, s, v);
return Color4f(h, s, v, col[3]);
}
/**
* Calculate contrast color. Based on the article
* http://gamedev.stackexchange.com/questions/38536/given-a-rgb-color-x-how-to-find-the-most-contrasting-color-y
*
* @param col the input color
* @return a color with high contrast to the input color
*/
Color4f ColorMap::getContrastColor(const Color4f &col)
{
Color4f hsv = ColorMap::getColorHSV(col);
float Y = 0.2126 * col[0] + 0.7152 * col[1] + 0.0722 * col[2];
float S = hsv[1];
if (S < 0.5) {
// low saturation, choose between black / white based on luminance Y
float val = Y > 0.5 ? 0.0f : 1.0f;
return Color4f(val, val, val, 1.0f);
} else {
float H = 360 * hsv[0];
if ((H < 60) || (H > 300)) {
return Color4f(0.0f, 1.0f, 1.0f, 1.0f); // red -> cyan
} else if (H < 180) {
return Color4f(1.0f, 0.0f, 1.0f, 1.0f); // green -> magenta
} else {
return Color4f(1.0f, 1.0f, 0.0f, 1.0f); // blue -> yellow
}
}
}
void ColorMap::enumerateColorSchemesInPath(colorscheme_set_t &result_set, const fs::path basePath)
{
const fs::path color_schemes = basePath / "color-schemes" / "render";

View File

@ -79,6 +79,8 @@ public:
std::list<std::string> colorSchemeNames(bool guiOnly = false) const;
static Color4f getColor(const ColorScheme &cs, const RenderColor rc);
static Color4f getContrastColor(const Color4f &col);
static Color4f getColorHSV(const Color4f &col);
private:
ColorMap();