From fc2d9341fac6948716b328b75f3366313bd2545a Mon Sep 17 00:00:00 2001 From: don bright Date: Wed, 20 Feb 2013 19:56:30 -0600 Subject: begin process of unifying GLView and OffscreenView. renames, objrot, objtran diff --git a/openscad.pro b/openscad.pro index ead5d1f..c0995c6 100644 --- a/openscad.pro +++ b/openscad.pro @@ -179,7 +179,7 @@ HEADERS += src/version_check.h \ src/ThrownTogetherRenderer.h \ src/CGAL_renderer.h \ src/OGL_helper.h \ - src/GLView.h \ + src/QGLView.h \ src/MainWindow.h \ src/Preferences.h \ src/OpenCSGWarningDialog.h \ @@ -284,7 +284,7 @@ SOURCES += src/version_check.cc \ src/Preferences.cc \ src/OpenCSGWarningDialog.cc \ src/editor.cc \ - src/glview.cc \ + src/qglview.cc \ \ src/builtin.cc \ src/export.cc \ diff --git a/src/CsgInfo.h b/src/CsgInfo.h index 889eefe..6c3e352 100644 --- a/src/CsgInfo.h +++ b/src/CsgInfo.h @@ -6,8 +6,8 @@ class CsgInfo { public: - CsgInfo() { glview = NULL; } - OffscreenView *glview; + CsgInfo() { qglview = NULL; } + OffscreenView *qglview; }; @@ -26,7 +26,7 @@ public: root_chain = NULL; highlights_chain = NULL; background_chain = NULL; - glview = NULL; + qglview = NULL; } shared_ptr root_norm_term; // Normalized CSG products class CSGChain *root_chain; diff --git a/src/GLView.h b/src/GLView.h deleted file mode 100644 index 5552e4b..0000000 --- a/src/GLView.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef GLVIEW_H_ -#define GLVIEW_H_ - -#include "system-gl.h" -#include -#include - -class GLView : public QGLWidget -{ - Q_OBJECT - Q_PROPERTY(bool showFaces READ showFaces WRITE setShowFaces); - Q_PROPERTY(bool showEdges READ showEdges WRITE setShowEdges); - Q_PROPERTY(bool showAxes READ showAxes WRITE setShowAxes); - Q_PROPERTY(bool showCrosshairs READ showCrosshairs WRITE setShowCrosshairs); - Q_PROPERTY(bool orthoMode READ orthoMode WRITE setOrthoMode); - -public: - GLView(QWidget *parent = NULL); - GLView(const QGLFormat & format, QWidget *parent = NULL); - void setRenderer(class Renderer* r); -#ifdef ENABLE_OPENCSG - bool hasOpenCSGSupport() { return this->opencsg_support; } -#endif - // Properties - bool showFaces() const { return this->showfaces; } - void setShowFaces(bool enabled) { this->showfaces = enabled; } - bool showEdges() const { return this->showedges; } - void setShowEdges(bool enabled) { this->showedges = enabled; } - bool showAxes() const { return this->showaxes; } - void setShowAxes(bool enabled) { this->showaxes = enabled; } - bool showCrosshairs() const { return this->showcrosshairs; } - void setShowCrosshairs(bool enabled) { this->showcrosshairs = enabled; } - bool orthoMode() const { return this->orthomode; } - void setOrthoMode(bool enabled) { this->orthomode = enabled; } - const QString &getRendererInfo() const { return this->rendererInfo; } - -public: - QLabel *statusLabel; - double object_rot_x; - double object_rot_y; - double object_rot_z; - double object_trans_x; - double object_trans_y; - double object_trans_z; - GLint shaderinfo[11]; - -#ifdef ENABLE_OPENCSG - bool opencsg_support; - int opencsg_id; -#endif - -private: - void init(); - Renderer *renderer; - - QString rendererInfo; - - bool showfaces; - bool showedges; - bool showaxes; - bool showcrosshairs; - bool orthomode; - - double viewer_distance; - - double w_h_ratio; - - bool mouse_drag_active; - QPoint last_mouse; - - void keyPressEvent(QKeyEvent *event); - void wheelEvent(QWheelEvent *event); - void mousePressEvent(QMouseEvent *event); - void mouseMoveEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); - - void initializeGL(); - void resizeGL(int w, int h); - void setupPerspective(); - void setupOrtho(double distance,bool offset=false); - void paintGL(); - void normalizeAngle(GLdouble& angle); - -#ifdef ENABLE_OPENCSG - bool is_opencsg_capable; - bool has_shaders; -private slots: - void display_opencsg_warning(); -#endif - -signals: - void doAnimateUpdate(); -}; - -#endif diff --git a/src/MainWindow.ui b/src/MainWindow.ui index f71ac96..9dfc5dc 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -38,7 +38,7 @@ Qt::Vertical - + true @@ -679,9 +679,9 @@ - GLView + QGLView QWidget -
GLView.h
+
QGLView.h
1
diff --git a/src/OffscreenView.cc b/src/OffscreenView.cc index 12ebf3a..ee5e296 100644 --- a/src/OffscreenView.cc +++ b/src/OffscreenView.cc @@ -147,7 +147,7 @@ bool OffscreenView::save(std::ostream &output) return save_framebuffer(this->ctx, output); } -std::string OffscreenView::getInfo() +const std::string &OffscreenView::getRendererInfo() { std::stringstream out; GLint rbits, gbits, bbits, abits, dbits, sbits; diff --git a/src/OffscreenView.h b/src/OffscreenView.h index f401bef..c27277e 100644 --- a/src/OffscreenView.h +++ b/src/OffscreenView.h @@ -26,24 +26,25 @@ public: void paintGL(); bool save(const char *filename); bool save(std::ostream &output); - std::string getInfo(); + const std::string &getRendererInfo(); - GLint shaderinfo[11]; - OffscreenContext *ctx; - size_t width; - size_t height; + GLint shaderinfo[11]; // + + OffscreenContext *ctx; // not + size_t width; // not + size_t height; // not private: - Renderer *renderer; - double w_h_ratio; + Renderer *renderer;// + double w_h_ratio;// - bool orthomode; - bool showaxes; - bool showfaces; - bool showedges; + bool orthomode;// + bool showaxes;// + bool showfaces;// + bool showedges;// - Eigen::Vector3d object_rot; - Eigen::Vector3d camera_eye; - Eigen::Vector3d camera_center; + Eigen::Vector3d object_rot;// + Eigen::Vector3d camera_eye;// + Eigen::Vector3d camera_center;// }; #endif diff --git a/src/QGLView.h b/src/QGLView.h new file mode 100644 index 0000000..862d751 --- /dev/null +++ b/src/QGLView.h @@ -0,0 +1,98 @@ +#ifndef GLVIEW_H_ +#define GLVIEW_H_ + +#include "system-gl.h" +#include +#include + +#include +#include + +class QGLView : public QGLWidget +{ + Q_OBJECT + Q_PROPERTY(bool showFaces READ showFaces WRITE setShowFaces); + Q_PROPERTY(bool showEdges READ showEdges WRITE setShowEdges); + Q_PROPERTY(bool showAxes READ showAxes WRITE setShowAxes); + Q_PROPERTY(bool showCrosshairs READ showCrosshairs WRITE setShowCrosshairs); + Q_PROPERTY(bool orthoMode READ orthoMode WRITE setOrthoMode); + +public: + QGLView(QWidget *parent = NULL); + QGLView(const QGLFormat & format, QWidget *parent = NULL); + void setRenderer(class Renderer* r); +#ifdef ENABLE_OPENCSG + bool hasOpenCSGSupport() { return this->opencsg_support; } +#endif + // Properties + bool showFaces() const { return this->showfaces; } + void setShowFaces(bool enabled) { this->showfaces = enabled; } + bool showEdges() const { return this->showedges; } + void setShowEdges(bool enabled) { this->showedges = enabled; } + bool showAxes() const { return this->showaxes; } + void setShowAxes(bool enabled) { this->showaxes = enabled; } + bool showCrosshairs() const { return this->showcrosshairs; } + void setShowCrosshairs(bool enabled) { this->showcrosshairs = enabled; } + bool orthoMode() const { return this->orthomode; } + void setOrthoMode(bool enabled) { this->orthomode = enabled; } + const std::string &getRendererInfo() const { return this->rendererInfo; } + +public: + QLabel *statusLabel; + + Eigen::Vector3d object_rot; + Eigen::Vector3d object_trans; + Eigen::Vector3d camera_eye; + Eigen::Vector3d camera_center; + + GLint shaderinfo[11]; + +#ifdef ENABLE_OPENCSG + bool opencsg_support; + int opencsg_id; +#endif + +private: + void init(); + Renderer *renderer; + + std::string rendererInfo; + + bool showfaces; + bool showedges; + bool showaxes; + bool showcrosshairs; + bool orthomode; + + double viewer_distance; + + double w_h_ratio; + + bool mouse_drag_active; + QPoint last_mouse; + + void keyPressEvent(QKeyEvent *event); + void wheelEvent(QWheelEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + + void initializeGL(); + void resizeGL(int w, int h); + void setupPerspective(); + void setupOrtho(double distance,bool offset=false); + void paintGL(); + void normalizeAngle(GLdouble& angle); + +#ifdef ENABLE_OPENCSG + bool is_opencsg_capable; + bool has_shaders; +private slots: + void display_opencsg_warning(); +#endif + +signals: + void doAnimateUpdate(); +}; + +#endif diff --git a/src/export_png.cc b/src/export_png.cc index 2d6a4a2..1a92fc9 100644 --- a/src/export_png.cc +++ b/src/export_png.cc @@ -14,7 +14,7 @@ void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, std::ostream &output) { CsgInfo csgInfo; try { - csgInfo.glview = new OffscreenView(512,512); + csgInfo.qglview = new OffscreenView(512,512); } catch (int error) { fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i.\n", error); return; @@ -40,10 +40,10 @@ void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, std::ostream &output) //std::cerr << center << "\n"; //std::cerr << radius << "\n"; - csgInfo.glview->setCamera(camerapos, center); - csgInfo.glview->setRenderer(&cgalRenderer); - csgInfo.glview->paintGL(); - csgInfo.glview->save(output); + csgInfo.qglview->setCamera(camerapos, center); + csgInfo.qglview->setRenderer(&cgalRenderer); + csgInfo.qglview->paintGL(); + csgInfo.qglview->save(output); } void export_png_with_opencsg(CGAL_Nef_polyhedron *root_N, std::ostream &output) diff --git a/src/glview.cc b/src/glview.cc deleted file mode 100644 index 859bf82..0000000 --- a/src/glview.cc +++ /dev/null @@ -1,660 +0,0 @@ -/* - * OpenSCAD (www.openscad.org) - * Copyright (C) 2009-2011 Clifford Wolf and - * Marius Kintel - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * As a special exception, you have permission to link this program - * with the CGAL library and distribute executables, as long as you - * follow the requirements of the GNU GPL in regard to all of the - * software in the executable aside from CGAL. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#include "GLView.h" -#include "Preferences.h" -#include "renderer.h" -#include "rendersettings.h" -#include "linalg.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "OpenCSGWarningDialog.h" - -#include "mathc99.h" -#include - -#ifdef ENABLE_OPENCSG -# include -#endif - -#ifdef _WIN32 -#include -#elif !defined(__APPLE__) -#include -#endif - -#define FAR_FAR_AWAY 100000.0 - -GLView::GLView(QWidget *parent) : QGLWidget(parent), renderer(NULL) -{ - init(); -} - -GLView::GLView(const QGLFormat & format, QWidget *parent) : QGLWidget(format, parent) -{ - init(); -} - -static bool running_under_wine = false; - -void GLView::init() -{ - this->viewer_distance = 500; - this->object_rot_x = 35; - this->object_rot_y = 0; - this->object_rot_z = -25; - this->object_trans_x = 0; - this->object_trans_y = 0; - this->object_trans_z = 0; - - this->mouse_drag_active = false; - - this->showedges = false; - this->showfaces = true; - this->orthomode = false; - this->showaxes = false; - this->showcrosshairs = false; - - for (int i = 0; i < 10; i++) - this->shaderinfo[i] = 0; - - this->statusLabel = NULL; - - setMouseTracking(true); -#ifdef ENABLE_OPENCSG - this->is_opencsg_capable = false; - this->has_shaders = false; - this->opencsg_support = true; - static int sId = 0; - this->opencsg_id = sId++; -#endif - -// see paintGL() + issue160 + wine FAQ -#ifdef _WIN32 -#include - HMODULE hntdll = GetModuleHandle(L"ntdll.dll"); - if (hntdll) - if ( (void *)GetProcAddress(hntdll, "wine_get_version") ) - running_under_wine = true; -#endif -} - -void GLView::setRenderer(Renderer *r) -{ - this->renderer = r; - if (r) updateGL(); // Let the last image stay, e.g. to avoid animation flickering -} - -void GLView::initializeGL() -{ - glEnable(GL_DEPTH_TEST); - glDepthRange(-FAR_FAR_AWAY, +FAR_FAR_AWAY); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; - GLfloat light_position0[] = {-1.0, -1.0, +1.0, 0.0}; - GLfloat light_position1[] = {+1.0, +1.0, -1.0, 0.0}; - - glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); - glLightfv(GL_LIGHT0, GL_POSITION, light_position0); - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); - glLightfv(GL_LIGHT1, GL_POSITION, light_position1); - glEnable(GL_LIGHT1); - glEnable(GL_LIGHTING); - glEnable(GL_NORMALIZE); - - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - -#ifdef ENABLE_OPENCSG - GLenum err = glewInit(); - if (GLEW_OK != err) { - fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); - } - - GLint rbits, gbits, bbits, abits, dbits, sbits; - glGetIntegerv(GL_RED_BITS, &rbits); - glGetIntegerv(GL_GREEN_BITS, &gbits); - glGetIntegerv(GL_BLUE_BITS, &bbits); - glGetIntegerv(GL_ALPHA_BITS, &abits); - glGetIntegerv(GL_DEPTH_BITS, &dbits); - glGetIntegerv(GL_STENCIL_BITS, &sbits); - - - this->rendererInfo.sprintf("GLEW version %s\n" - "OpenGL version %s\n" - "%s (%s)\n\n" - "RGBA(%d%d%d%d), depth(%d), stencil(%d)\n" - "Extensions:\n" - "%s\n", - glewGetString(GLEW_VERSION), - glGetString(GL_VERSION), - glGetString(GL_RENDERER), - glGetString(GL_VENDOR), - rbits, gbits, bbits, abits, dbits, sbits, - glGetString(GL_EXTENSIONS)); -// FIXME: glGetString(GL_EXTENSIONS) is deprecated in OpenGL 3.0. -// Use: glGetIntegerv(GL_NUM_EXTENSIONS, &NumberOfExtensions) and -// glGetStringi(GL_EXTENSIONS, i) - - const char *openscad_disable_gl20_env = getenv("OPENSCAD_DISABLE_GL20"); - if (openscad_disable_gl20_env && !strcmp(openscad_disable_gl20_env, "0")) { - openscad_disable_gl20_env = NULL; - } - - // All OpenGL 2 contexts are OpenCSG capable - if (GLEW_VERSION_2_0) { - if (!openscad_disable_gl20_env) { - this->is_opencsg_capable = true; - this->has_shaders = true; - } - } - // If OpenGL < 2, check for extensions - else { - if (GLEW_ARB_framebuffer_object) this->is_opencsg_capable = true; - else if (GLEW_EXT_framebuffer_object && GLEW_EXT_packed_depth_stencil) { - this->is_opencsg_capable = true; - } -#ifdef WIN32 - else if (WGLEW_ARB_pbuffer && WGLEW_ARB_pixel_format) this->is_opencsg_capable = true; -#elif !defined(__APPLE__) - else if (GLXEW_SGIX_pbuffer && GLXEW_SGIX_fbconfig) this->is_opencsg_capable = true; -#endif - } - - if (!GLEW_VERSION_2_0 || !this->is_opencsg_capable) { - if (Preferences::inst()->getValue("advanced/opencsg_show_warning").toBool()) { - QTimer::singleShot(0, this, SLOT(display_opencsg_warning())); - } - } - 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" - "attribute vec3 trig, mask;\n" - "varying vec3 tp, tr;\n" - "varying float shading;\n" - "void main() {\n" - " vec4 p0 = gl_ModelViewProjectionMatrix * gl_Vertex;\n" - " vec4 p1 = gl_ModelViewProjectionMatrix * vec4(pos_b, 1.0);\n" - " vec4 p2 = gl_ModelViewProjectionMatrix * vec4(pos_c, 1.0);\n" - " float a = distance(vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" - " float b = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w));\n" - " float c = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" - " float s = (a + b + c) / 2.0;\n" - " float A = sqrt(s*(s-a)*(s-b)*(s-c));\n" - " float ha = 2.0*A/a;\n" - " gl_Position = p0;\n" - " tp = mask * ha;\n" - " tr = trig;\n" - " vec3 normal, lightDir;\n" - " normal = normalize(gl_NormalMatrix * gl_Normal);\n" - " lightDir = normalize(vec3(gl_LightSource[0].position));\n" - " 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" - "varying float shading;\n" - "void main() {\n" - " gl_FragColor = vec4(color1.r * shading, color1.g * shading, color1.b * shading, color1.a);\n" - " if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" - " gl_FragColor = color2;\n" - "}\n"; - - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vs, 1, (const GLchar**)&vs_source, NULL); - glCompileShader(vs); - - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL); - glCompileShader(fs); - - GLuint edgeshader_prog = glCreateProgram(); - glAttachShader(edgeshader_prog, vs); - glAttachShader(edgeshader_prog, fs); - glLinkProgram(edgeshader_prog); - - shaderinfo[0] = edgeshader_prog; - shaderinfo[1] = glGetUniformLocation(edgeshader_prog, "color1"); - shaderinfo[2] = glGetUniformLocation(edgeshader_prog, "color2"); - shaderinfo[3] = glGetAttribLocation(edgeshader_prog, "trig"); - shaderinfo[4] = glGetAttribLocation(edgeshader_prog, "pos_b"); - shaderinfo[5] = glGetAttribLocation(edgeshader_prog, "pos_c"); - shaderinfo[6] = glGetAttribLocation(edgeshader_prog, "mask"); - shaderinfo[7] = glGetUniformLocation(edgeshader_prog, "xscale"); - shaderinfo[8] = glGetUniformLocation(edgeshader_prog, "yscale"); - - GLenum err = glGetError(); - if (err != GL_NO_ERROR) { - fprintf(stderr, "OpenGL Error: %s\n", gluErrorString(err)); - } - - GLint status; - glGetProgramiv(edgeshader_prog, GL_LINK_STATUS, &status); - if (status == GL_FALSE) { - int loglen; - char logbuffer[1000]; - glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); - fprintf(stderr, "OpenGL Program Linker Error:\n%.*s", loglen, logbuffer); - } else { - int loglen; - char logbuffer[1000]; - glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); - if (loglen > 0) { - fprintf(stderr, "OpenGL Program Link OK:\n%.*s", loglen, logbuffer); - } - glValidateProgram(edgeshader_prog); - glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); - if (loglen > 0) { - fprintf(stderr, "OpenGL Program Validation results:\n%.*s", loglen, logbuffer); - } - } - } -#endif /* ENABLE_OPENCSG */ -} - -#ifdef ENABLE_OPENCSG -void GLView::display_opencsg_warning() -{ - OpenCSGWarningDialog *dialog = new OpenCSGWarningDialog(this); - - QString message; - if (this->is_opencsg_capable) { - message += "Warning: You may experience OpenCSG rendering errors.\n\n"; - } - else { - message += "Warning: Missing OpenGL capabilities for OpenCSG - OpenCSG has been disabled.\n\n"; - dialog->enableOpenCSGBox->hide(); - } - message += "It is highly recommended to use OpenSCAD on a system with " - "OpenGL 2.0 or later.\n" - "Your renderer information is as follows:\n"; - QString rendererinfo; - rendererinfo.sprintf("GLEW version %s\n" - "%s (%s)\n" - "OpenGL version %s\n", - glewGetString(GLEW_VERSION), - glGetString(GL_RENDERER), glGetString(GL_VENDOR), - glGetString(GL_VERSION)); - message += rendererinfo; - - dialog->setText(message); - dialog->enableOpenCSGBox->setChecked(Preferences::inst()->getValue("advanced/enable_opencsg_opengl1x").toBool()); - dialog->exec(); - - opencsg_support = this->is_opencsg_capable && Preferences::inst()->getValue("advanced/enable_opencsg_opengl1x").toBool(); -} -#endif - -void GLView::resizeGL(int w, int h) -{ -#ifdef ENABLE_OPENCSG - shaderinfo[9] = w; - shaderinfo[10] = h; -#endif - glViewport(0, 0, w, h); - w_h_ratio = sqrt((double)w / (double)h); - - setupPerspective(); -} - -void GLView::setupPerspective() -{ - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glFrustum(-w_h_ratio, +w_h_ratio, -(1/w_h_ratio), +(1/w_h_ratio), +10.0, +FAR_FAR_AWAY); - gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); -} - -void GLView::setupOrtho(double distance, bool offset) -{ - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - if(offset) - glTranslated(-0.8, -0.8, 0); - double l = distance/10; - glOrtho(-w_h_ratio*l, +w_h_ratio*l, - -(1/w_h_ratio)*l, +(1/w_h_ratio)*l, - -FAR_FAR_AWAY, +FAR_FAR_AWAY); - gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); -} - -void GLView::paintGL() -{ - glEnable(GL_LIGHTING); - - if (orthomode) setupOrtho(viewer_distance); - else setupPerspective(); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - Color4f bgcol = RenderSettings::inst()->color(RenderSettings::BACKGROUND_COLOR); - glClearColor(bgcol[0], bgcol[1], bgcol[2], 0.0); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - - glRotated(object_rot_x, 1.0, 0.0, 0.0); - glRotated(object_rot_y, 0.0, 1.0, 0.0); - glRotated(object_rot_z, 0.0, 0.0, 1.0); - - // FIXME: Crosshairs and axes are lighted, this doesn't make sense and causes them - // to change color based on view orientation. - if (showcrosshairs) - { - glLineWidth(3); - Color4f col = RenderSettings::inst()->color(RenderSettings::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 = viewer_distance/20; - glVertex3d(-xf*vd, -yf*vd, -vd); - glVertex3d(+xf*vd, +yf*vd, +vd); - } - glEnd(); - } - - glTranslated(object_trans_x, object_trans_y, object_trans_z); - - // Large gray axis cross inline with the model - // FIXME: This is always gray - adjust color to keep contrast with background - if (showaxes) - { - glLineWidth(1); - glColor3d(0.5, 0.5, 0.5); - glBegin(GL_LINES); - double l = viewer_distance/10; - glVertex3d(-l, 0, 0); - glVertex3d(+l, 0, 0); - glVertex3d(0, -l, 0); - glVertex3d(0, +l, 0); - glVertex3d(0, 0, -l); - glVertex3d(0, 0, +l); - glEnd(); - } - - glDepthFunc(GL_LESS); - glCullFace(GL_BACK); - glDisable(GL_CULL_FACE); - - glLineWidth(2); - glColor3d(1.0, 0.0, 0.0); - - if (this->renderer) { -#if defined(ENABLE_MDI) && defined(ENABLE_OPENCSG) - // FIXME: This belongs in the OpenCSG renderer, but it doesn't know about this ID yet - OpenCSG::setContext(this->opencsg_id); -#endif - this->renderer->draw(showfaces, showedges); - } - - // Small axis cross in the lower left corner - if (showaxes) - { - glDepthFunc(GL_ALWAYS); - - setupOrtho(1000,true); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glRotated(object_rot_x, 1.0, 0.0, 0.0); - glRotated(object_rot_y, 0.0, 1.0, 0.0); - glRotated(object_rot_z, 0.0, 0.0, 1.0); - - glLineWidth(1); - glBegin(GL_LINES); - glColor3d(1.0, 0.0, 0.0); - glVertex3d(0, 0, 0); glVertex3d(10, 0, 0); - glColor3d(0.0, 1.0, 0.0); - glVertex3d(0, 0, 0); glVertex3d(0, 10, 0); - glColor3d(0.0, 0.0, 1.0); - glVertex3d(0, 0, 0); glVertex3d(0, 0, 10); - glEnd(); - - GLdouble mat_model[16]; - glGetDoublev(GL_MODELVIEW_MATRIX, mat_model); - - GLdouble mat_proj[16]; - glGetDoublev(GL_PROJECTION_MATRIX, mat_proj); - - GLint viewport[4]; - glGetIntegerv(GL_VIEWPORT, viewport); - - GLdouble xlabel_x, xlabel_y, xlabel_z; - gluProject(12, 0, 0, mat_model, mat_proj, viewport, &xlabel_x, &xlabel_y, &xlabel_z); - xlabel_x = round(xlabel_x); xlabel_y = round(xlabel_y); - - GLdouble ylabel_x, ylabel_y, ylabel_z; - gluProject(0, 12, 0, mat_model, mat_proj, viewport, &ylabel_x, &ylabel_y, &ylabel_z); - ylabel_x = round(ylabel_x); ylabel_y = round(ylabel_y); - - GLdouble zlabel_x, zlabel_y, zlabel_z; - gluProject(0, 0, 12, mat_model, mat_proj, viewport, &zlabel_x, &zlabel_y, &zlabel_z); - zlabel_x = round(zlabel_x); zlabel_y = round(zlabel_y); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glTranslated(-1, -1, 0); - glScaled(2.0/viewport[2], 2.0/viewport[3], 1); - - 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); - glColor3f(0.0f, 0.0f, 0.0f); - glBegin(GL_LINES); - // X Label - glVertex3d(xlabel_x-3, xlabel_y-3, 0); glVertex3d(xlabel_x+3, xlabel_y+3, 0); - glVertex3d(xlabel_x-3, xlabel_y+3, 0); glVertex3d(xlabel_x+3, xlabel_y-3, 0); - // Y Label - glVertex3d(ylabel_x-3, ylabel_y-3, 0); glVertex3d(ylabel_x+3, ylabel_y+3, 0); - glVertex3d(ylabel_x-3, ylabel_y+3, 0); glVertex3d(ylabel_x, ylabel_y, 0); - // Z Label - glVertex3d(zlabel_x-3, zlabel_y-3, 0); glVertex3d(zlabel_x+3, zlabel_y-3, 0); - glVertex3d(zlabel_x-3, zlabel_y+3, 0); glVertex3d(zlabel_x+3, zlabel_y+3, 0); - glVertex3d(zlabel_x-3, zlabel_y-3, 0); glVertex3d(zlabel_x+3, zlabel_y+3, 0); - glEnd(); - - //Restore perspective for next paint - if(!orthomode) - setupPerspective(); - } - - if (statusLabel) { - QString msg; - msg.sprintf("Viewport: translate = [ %.2f %.2f %.2f ], rotate = [ %.2f %.2f %.2f ], distance = %.2f", - -object_trans_x, -object_trans_y, -object_trans_z, - fmodf(360 - object_rot_x + 90, 360), fmodf(360 - object_rot_y, 360), fmodf(360 - object_rot_z, 360), viewer_distance); - statusLabel->setText(msg); - } - - if (running_under_wine) swapBuffers(); -} - -void GLView::keyPressEvent(QKeyEvent *event) -{ - if (event->key() == Qt::Key_Plus) { - viewer_distance *= 0.9; - updateGL(); - return; - } - if (event->key() == Qt::Key_Minus) { - viewer_distance /= 0.9; - updateGL(); - return; - } -} - -void GLView::wheelEvent(QWheelEvent *event) -{ - viewer_distance *= pow(0.9, event->delta() / 120.0); - updateGL(); -} - -void GLView::mousePressEvent(QMouseEvent *event) -{ - mouse_drag_active = true; - last_mouse = event->globalPos(); -} - -void GLView::normalizeAngle(GLdouble& angle) -{ - while(angle < 0) - angle += 360; - while(angle > 360) - angle -= 360; -} - -void GLView::mouseMoveEvent(QMouseEvent *event) -{ - QPoint this_mouse = event->globalPos(); - double dx = (this_mouse.x()-last_mouse.x()) * 0.7; - double dy = (this_mouse.y()-last_mouse.y()) * 0.7; - if (mouse_drag_active) { - if (event->buttons() & Qt::LeftButton -#ifdef Q_WS_MAC - && !(event->modifiers() & Qt::MetaModifier) -#endif - ) { - // Left button rotates in xz, Shift-left rotates in xy - // On Mac, Ctrl-Left is handled as right button on other platforms - object_rot_x += dy; - if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) - object_rot_y += dx; - else - object_rot_z += dx; - - normalizeAngle(object_rot_x); - normalizeAngle(object_rot_y); - normalizeAngle(object_rot_z); - } else { - // Right button pans in the xz plane - // Middle button pans in the xy plane - // Shift-right and Shift-middle zooms - if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) { - viewer_distance += (GLdouble)dy; - } else { - - double mx = +(dx) * viewer_distance/1000; - double mz = -(dy) * viewer_distance/1000; - - double my = 0; -#if (QT_VERSION < QT_VERSION_CHECK(4, 7, 0)) - if (event->buttons() & Qt::MidButton) { -#else - if (event->buttons() & Qt::MiddleButton) { -#endif - my = mz; - mz = 0; - // actually lock the x-position - // (turns out to be easier to use than xy panning) - mx = 0; - } - - Matrix3d aax, aay, aaz, tm3; - aax = Eigen::AngleAxisd(-(object_rot_x/180) * M_PI, Vector3d::UnitX()); - aay = Eigen::AngleAxisd(-(object_rot_y/180) * M_PI, Vector3d::UnitY()); - aaz = Eigen::AngleAxisd(-(object_rot_z/180) * M_PI, Vector3d::UnitZ()); - tm3 = Matrix3d::Identity(); - tm3 = aaz * (aay * (aax * tm3)); - - Matrix4d tm; - tm = Matrix4d::Identity(); - for (int i=0;i<3;i++) for (int j=0;j<3;j++) tm(j,i)=tm3(j,i); - - Matrix4d vec; - vec << - 0, 0, 0, mx, - 0, 0, 0, my, - 0, 0, 0, mz, - 0, 0, 0, 1 - ; - tm = tm * vec; - object_trans_x += tm(0,3); - object_trans_y += tm(1,3); - object_trans_z += tm(2,3); - } - } - updateGL(); - emit doAnimateUpdate(); - } - last_mouse = this_mouse; -} - -void GLView::mouseReleaseEvent(QMouseEvent*) -{ - mouse_drag_active = false; - releaseMouse(); -} diff --git a/src/mainwin.cc b/src/mainwin.cc index d5af643..bd17402 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -190,8 +190,8 @@ MainWindow::MainWindow(const QString &filename) editor->setTabStopWidth(30); editor->setLineWrapping(true); // Not designable - this->glview->statusLabel = new QLabel(this); - statusBar()->addWidget(this->glview->statusLabel); + this->qglview->statusLabel = new QLabel(this); + statusBar()->addWidget(this->qglview->statusLabel); animate_timer = new QTimer(this); connect(animate_timer, SIGNAL(timeout()), this, SLOT(updateTVal())); @@ -291,7 +291,7 @@ MainWindow::MainWindow(const QString &filename) this->viewActionOpenCSG->setVisible(false); #else connect(this->viewActionOpenCSG, SIGNAL(triggered()), this, SLOT(viewModeOpenCSG())); - if (!this->glview->hasOpenCSGSupport()) { + if (!this->qglview->hasOpenCSGSupport()) { this->viewActionOpenCSG->setEnabled(false); } #endif @@ -343,9 +343,9 @@ MainWindow::MainWindow(const QString &filename) connect(editor->document(), SIGNAL(contentsChanged()), this, SLOT(animateUpdateDocChanged())); connect(editor->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool))); connect(editor->document(), SIGNAL(modificationChanged(bool)), fileActionSave, SLOT(setEnabled(bool))); - connect(this->glview, SIGNAL(doAnimateUpdate()), this, SLOT(animateUpdate())); + connect(this->qglview, SIGNAL(doAnimateUpdate()), this, SLOT(animateUpdate())); - connect(Preferences::inst(), SIGNAL(requestRedraw()), this->glview, SLOT(updateGL())); + connect(Preferences::inst(), SIGNAL(requestRedraw()), this->qglview, SLOT(updateGL())); connect(Preferences::inst(), SIGNAL(fontChanged(const QString&,uint)), this, SLOT(setFont(const QString&,uint))); connect(Preferences::inst(), SIGNAL(openCSGSettingsChanged()), @@ -595,7 +595,7 @@ bool MainWindow::compile(bool reload, bool procevents) if (!compileTopLevelDocument(reload)) return false; // Invalidate renderers before we kill the CSG tree - this->glview->setRenderer(NULL); + this->qglview->setRenderer(NULL); delete this->opencsgRenderer; this->opencsgRenderer = NULL; delete this->thrownTogetherRenderer; @@ -759,7 +759,7 @@ void MainWindow::compileCSG(bool procevents) this->opencsgRenderer = new OpenCSGRenderer(this->root_chain, this->highlights_chain, this->background_chain, - this->glview->shaderinfo); + this->qglview->shaderinfo); } this->thrownTogetherRenderer = new ThrownTogetherRenderer(this->root_chain, this->highlights_chain, @@ -942,7 +942,7 @@ void MainWindow::pasteViewportTranslation() { QTextCursor cursor = editor->textCursor(); QString txt; - txt.sprintf("[ %.2f, %.2f, %.2f ]", -this->glview->object_trans_x, -this->glview->object_trans_y, -this->glview->object_trans_z); + txt.sprintf("[ %.2f, %.2f, %.2f ]", -this->qglview->object_trans_x, -this->qglview->object_trans_y, -this->qglview->object_trans_z); cursor.insertText(txt); } @@ -951,7 +951,7 @@ void MainWindow::pasteViewportRotation() QTextCursor cursor = editor->textCursor(); QString txt; txt.sprintf("[ %.2f, %.2f, %.2f ]", - fmodf(360 - this->glview->object_rot_x + 90, 360), fmodf(360 - this->glview->object_rot_y, 360), fmodf(360 - this->glview->object_rot_z, 360)); + fmodf(360 - this->qglview->object_rot_x + 90, 360), fmodf(360 - this->qglview->object_rot_y, 360), fmodf(360 - this->qglview->object_rot_z, 360)); cursor.insertText(txt); } @@ -960,15 +960,15 @@ void MainWindow::updateTemporalVariables() this->root_ctx.set_variable("$t", Value(this->e_tval->text().toDouble())); Value::VectorType vpt; - vpt.push_back(Value(-this->glview->object_trans_x)); - vpt.push_back(Value(-this->glview->object_trans_y)); - vpt.push_back(Value(-this->glview->object_trans_z)); + vpt.push_back(Value(-this->qglview->object_trans_x)); + vpt.push_back(Value(-this->qglview->object_trans_y)); + vpt.push_back(Value(-this->qglview->object_trans_z)); this->root_ctx.set_variable("$vpt", Value(vpt)); Value::VectorType vpr; - vpr.push_back(Value(fmodf(360 - this->glview->object_rot_x + 90, 360))); - vpr.push_back(Value(fmodf(360 - this->glview->object_rot_y, 360))); - vpr.push_back(Value(fmodf(360 - this->glview->object_rot_z, 360))); + vpr.push_back(Value(fmodf(360 - this->qglview->object_rot_x + 90, 360))); + vpr.push_back(Value(fmodf(360 - this->qglview->object_rot_y, 360))); + vpr.push_back(Value(fmodf(360 - this->qglview->object_rot_z, 360))); root_ctx.set_variable("$vpr", Value(vpr)); } @@ -1147,7 +1147,7 @@ void MainWindow::actionCompile() } if (viewActionAnimate->isChecked() && e_dump->isChecked()) { - QImage img = this->glview->grabFrameBuffer(); + QImage img = this->qglview->grabFrameBuffer(); QString filename; double s = this->e_fsteps->text().toDouble(); double t = this->e_tval->text().toDouble(); @@ -1176,7 +1176,7 @@ void MainWindow::actionRenderCGAL() return; } - this->glview->setRenderer(NULL); + this->qglview->setRenderer(NULL); delete this->cgalRenderer; this->cgalRenderer = NULL; if (this->root_N) { @@ -1452,7 +1452,7 @@ void MainWindow::actionExportCSG() void MainWindow::actionExportImage() { - QImage img = this->glview->grabFrameBuffer(); + QImage img = this->qglview->grabFrameBuffer(); setCurrentOutput(); QString img_filename = QFileDialog::getSaveFileName(this, @@ -1497,10 +1497,10 @@ void MainWindow::viewModeActionsUncheck() */ void MainWindow::viewModeOpenCSG() { - if (this->glview->hasOpenCSGSupport()) { + if (this->qglview->hasOpenCSGSupport()) { viewModeActionsUncheck(); viewActionOpenCSG->setChecked(true); - this->glview->setRenderer(this->opencsgRenderer ? (Renderer *)this->opencsgRenderer : (Renderer *)this->thrownTogetherRenderer); + this->qglview->setRenderer(this->opencsgRenderer ? (Renderer *)this->opencsgRenderer : (Renderer *)this->thrownTogetherRenderer); } else { viewModeThrownTogether(); } @@ -1514,17 +1514,17 @@ void MainWindow::viewModeCGALSurface() { viewModeActionsUncheck(); viewActionCGALSurfaces->setChecked(true); - this->glview->setShowFaces(true); - this->glview->setRenderer(this->cgalRenderer); - this->glview->updateGL(); + this->qglview->setShowFaces(true); + this->qglview->setRenderer(this->cgalRenderer); + this->qglview->updateGL(); } void MainWindow::viewModeCGALGrid() { viewModeActionsUncheck(); viewActionCGALGrid->setChecked(true); - this->glview->setShowFaces(false); - this->glview->setRenderer(this->cgalRenderer); + this->qglview->setShowFaces(false); + this->qglview->setRenderer(this->cgalRenderer); } #endif /* ENABLE_CGAL */ @@ -1533,31 +1533,31 @@ void MainWindow::viewModeThrownTogether() { viewModeActionsUncheck(); viewActionThrownTogether->setChecked(true); - this->glview->setRenderer(this->thrownTogetherRenderer); + this->qglview->setRenderer(this->thrownTogetherRenderer); } void MainWindow::viewModeShowEdges() { QSettings settings; settings.setValue("view/showEdges",viewActionShowEdges->isChecked()); - this->glview->setShowEdges(viewActionShowEdges->isChecked()); - this->glview->updateGL(); + this->qglview->setShowEdges(viewActionShowEdges->isChecked()); + this->qglview->updateGL(); } void MainWindow::viewModeShowAxes() { QSettings settings; settings.setValue("view/showAxes",viewActionShowAxes->isChecked()); - this->glview->setShowAxes(viewActionShowAxes->isChecked()); - this->glview->updateGL(); + this->qglview->setShowAxes(viewActionShowAxes->isChecked()); + this->qglview->updateGL(); } void MainWindow::viewModeShowCrosshairs() { QSettings settings; settings.setValue("view/showCrosshairs",viewActionShowCrosshairs->isChecked()); - this->glview->setShowCrosshairs(viewActionShowCrosshairs->isChecked()); - this->glview->updateGL(); + this->qglview->setShowCrosshairs(viewActionShowCrosshairs->isChecked()); + this->qglview->updateGL(); } void MainWindow::viewModeAnimate() @@ -1595,66 +1595,66 @@ void MainWindow::animateUpdate() void MainWindow::viewAngleTop() { - this->glview->object_rot_x = 90; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = 0; - this->glview->updateGL(); + this->qglview->object_rot_x = 90; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = 0; + this->qglview->updateGL(); } void MainWindow::viewAngleBottom() { - this->glview->object_rot_x = 270; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = 0; - this->glview->updateGL(); + this->qglview->object_rot_x = 270; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = 0; + this->qglview->updateGL(); } void MainWindow::viewAngleLeft() { - this->glview->object_rot_x = 0; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = 90; - this->glview->updateGL(); + this->qglview->object_rot_x = 0; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = 90; + this->qglview->updateGL(); } void MainWindow::viewAngleRight() { - this->glview->object_rot_x = 0; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = 270; - this->glview->updateGL(); + this->qglview->object_rot_x = 0; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = 270; + this->qglview->updateGL(); } void MainWindow::viewAngleFront() { - this->glview->object_rot_x = 0; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = 0; - this->glview->updateGL(); + this->qglview->object_rot_x = 0; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = 0; + this->qglview->updateGL(); } void MainWindow::viewAngleBack() { - this->glview->object_rot_x = 0; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = 180; - this->glview->updateGL(); + this->qglview->object_rot_x = 0; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = 180; + this->qglview->updateGL(); } void MainWindow::viewAngleDiagonal() { - this->glview->object_rot_x = 35; - this->glview->object_rot_y = 0; - this->glview->object_rot_z = -25; - this->glview->updateGL(); + this->qglview->object_rot_x = 35; + this->qglview->object_rot_y = 0; + this->qglview->object_rot_z = -25; + this->qglview->updateGL(); } void MainWindow::viewCenter() { - this->glview->object_trans_x = 0; - this->glview->object_trans_y = 0; - this->glview->object_trans_z = 0; - this->glview->updateGL(); + this->qglview->object_trans_x = 0; + this->qglview->object_trans_y = 0; + this->qglview->object_trans_z = 0; + this->qglview->updateGL(); } void MainWindow::viewPerspective() @@ -1663,8 +1663,8 @@ void MainWindow::viewPerspective() settings.setValue("view/orthogonalProjection",false); viewActionPerspective->setChecked(true); viewActionOrthogonal->setChecked(false); - this->glview->setOrthoMode(false); - this->glview->updateGL(); + this->qglview->setOrthoMode(false); + this->qglview->updateGL(); } void MainWindow::viewOrthogonal() @@ -1673,8 +1673,8 @@ void MainWindow::viewOrthogonal() settings.setValue("view/orthogonalProjection",true); viewActionPerspective->setChecked(false); viewActionOrthogonal->setChecked(true); - this->glview->setOrthoMode(true); - this->glview->updateGL(); + this->qglview->setOrthoMode(true); + this->qglview->updateGL(); } void MainWindow::hideConsole() @@ -1751,7 +1751,7 @@ void MainWindow::helpLibrary() } - this->openglbox->setDetailedText(libinfo + this->glview->getRendererInfo()); + this->openglbox->setDetailedText(libinfo + QString(this->qglview->getRendererInfo())); this->openglbox->show(); } diff --git a/src/qglview.cc b/src/qglview.cc new file mode 100644 index 0000000..a359a68 --- /dev/null +++ b/src/qglview.cc @@ -0,0 +1,660 @@ +/* + * OpenSCAD (www.openscad.org) + * Copyright (C) 2009-2011 Clifford Wolf and + * Marius Kintel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * As a special exception, you have permission to link this program + * with the CGAL library and distribute executables, as long as you + * follow the requirements of the GNU GPL in regard to all of the + * software in the executable aside from CGAL. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "QGLView.h" +#include "Preferences.h" +#include "renderer.h" +#include "rendersettings.h" +#include "linalg.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "OpenCSGWarningDialog.h" + +#include "mathc99.h" +#include + +#ifdef ENABLE_OPENCSG +# include +#endif + +#ifdef _WIN32 +#include +#elif !defined(__APPLE__) +#include +#endif + +#define FAR_FAR_AWAY 100000.0 + +QGLView::QGLView(QWidget *parent) : QGLWidget(parent), renderer(NULL) +{ + init(); +} + +QGLView::QGLView(const QGLFormat & format, QWidget *parent) : QGLWidget(format, parent) +{ + init(); +} + +static bool running_under_wine = false; + +void QGLView::init() +{ + this->viewer_distance = 500; + this->object_rot.x() = 35; + this->object_rot.y() = 0; + this->object_rot.z() = -25; + this->object_trans.x() = 0; + this->object_trans.y() = 0; + this->object_trans.z() = 0; + + this->mouse_drag_active = false; + + this->showedges = false; + this->showfaces = true; + this->orthomode = false; + this->showaxes = false; + this->showcrosshairs = false; + + for (int i = 0; i < 10; i++) + this->shaderinfo[i] = 0; + + this->statusLabel = NULL; + + setMouseTracking(true); +#ifdef ENABLE_OPENCSG + this->is_opencsg_capable = false; + this->has_shaders = false; + this->opencsg_support = true; + static int sId = 0; + this->opencsg_id = sId++; +#endif + +// see paintGL() + issue160 + wine FAQ +#ifdef _WIN32 +#include + HMODULE hntdll = GetModuleHandle(L"ntdll.dll"); + if (hntdll) + if ( (void *)GetProcAddress(hntdll, "wine_get_version") ) + running_under_wine = true; +#endif +} + +void QGLView::setRenderer(Renderer *r) +{ + this->renderer = r; + if (r) updateGL(); // Let the last image stay, e.g. to avoid animation flickering +} + +void QGLView::initializeGL() +{ + glEnable(GL_DEPTH_TEST); + glDepthRange(-FAR_FAR_AWAY, +FAR_FAR_AWAY); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; + GLfloat light_position0[] = {-1.0, -1.0, +1.0, 0.0}; + GLfloat light_position1[] = {+1.0, +1.0, -1.0, 0.0}; + + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_POSITION, light_position0); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT1, GL_POSITION, light_position1); + glEnable(GL_LIGHT1); + glEnable(GL_LIGHTING); + glEnable(GL_NORMALIZE); + + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + glEnable(GL_COLOR_MATERIAL); + +#ifdef ENABLE_OPENCSG + GLenum err = glewInit(); + if (GLEW_OK != err) { + fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); + } + + GLint rbits, gbits, bbits, abits, dbits, sbits; + glGetIntegerv(GL_RED_BITS, &rbits); + glGetIntegerv(GL_GREEN_BITS, &gbits); + glGetIntegerv(GL_BLUE_BITS, &bbits); + glGetIntegerv(GL_ALPHA_BITS, &abits); + glGetIntegerv(GL_DEPTH_BITS, &dbits); + glGetIntegerv(GL_STENCIL_BITS, &sbits); + + + this->rendererInfo.sprintf("GLEW version %s\n" + "OpenGL version %s\n" + "%s (%s)\n\n" + "RGBA(%d%d%d%d), depth(%d), stencil(%d)\n" + "Extensions:\n" + "%s\n", + glewGetString(GLEW_VERSION), + glGetString(GL_VERSION), + glGetString(GL_RENDERER), + glGetString(GL_VENDOR), + rbits, gbits, bbits, abits, dbits, sbits, + glGetString(GL_EXTENSIONS)); +// FIXME: glGetString(GL_EXTENSIONS) is deprecated in OpenGL 3.0. +// Use: glGetIntegerv(GL_NUM_EXTENSIONS, &NumberOfExtensions) and +// glGetStringi(GL_EXTENSIONS, i) + + const char *openscad_disable_gl20_env = getenv("OPENSCAD_DISABLE_GL20"); + if (openscad_disable_gl20_env && !strcmp(openscad_disable_gl20_env, "0")) { + openscad_disable_gl20_env = NULL; + } + + // All OpenGL 2 contexts are OpenCSG capable + if (GLEW_VERSION_2_0) { + if (!openscad_disable_gl20_env) { + this->is_opencsg_capable = true; + this->has_shaders = true; + } + } + // If OpenGL < 2, check for extensions + else { + if (GLEW_ARB_framebuffer_object) this->is_opencsg_capable = true; + else if (GLEW_EXT_framebuffer_object && GLEW_EXT_packed_depth_stencil) { + this->is_opencsg_capable = true; + } +#ifdef WIN32 + else if (WGLEW_ARB_pbuffer && WGLEW_ARB_pixel_format) this->is_opencsg_capable = true; +#elif !defined(__APPLE__) + else if (GLXEW_SGIX_pbuffer && GLXEW_SGIX_fbconfig) this->is_opencsg_capable = true; +#endif + } + + if (!GLEW_VERSION_2_0 || !this->is_opencsg_capable) { + if (Preferences::inst()->getValue("advanced/opencsg_show_warning").toBool()) { + QTimer::singleShot(0, this, SLOT(display_opencsg_warning())); + } + } + 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" + "attribute vec3 trig, mask;\n" + "varying vec3 tp, tr;\n" + "varying float shading;\n" + "void main() {\n" + " vec4 p0 = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + " vec4 p1 = gl_ModelViewProjectionMatrix * vec4(pos_b, 1.0);\n" + " vec4 p2 = gl_ModelViewProjectionMatrix * vec4(pos_c, 1.0);\n" + " float a = distance(vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" + " float b = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w));\n" + " float c = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" + " float s = (a + b + c) / 2.0;\n" + " float A = sqrt(s*(s-a)*(s-b)*(s-c));\n" + " float ha = 2.0*A/a;\n" + " gl_Position = p0;\n" + " tp = mask * ha;\n" + " tr = trig;\n" + " vec3 normal, lightDir;\n" + " normal = normalize(gl_NormalMatrix * gl_Normal);\n" + " lightDir = normalize(vec3(gl_LightSource[0].position));\n" + " 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" + "varying float shading;\n" + "void main() {\n" + " gl_FragColor = vec4(color1.r * shading, color1.g * shading, color1.b * shading, color1.a);\n" + " if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" + " gl_FragColor = color2;\n" + "}\n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, (const GLchar**)&vs_source, NULL); + glCompileShader(vs); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL); + glCompileShader(fs); + + GLuint edgeshader_prog = glCreateProgram(); + glAttachShader(edgeshader_prog, vs); + glAttachShader(edgeshader_prog, fs); + glLinkProgram(edgeshader_prog); + + shaderinfo[0] = edgeshader_prog; + shaderinfo[1] = glGetUniformLocation(edgeshader_prog, "color1"); + shaderinfo[2] = glGetUniformLocation(edgeshader_prog, "color2"); + shaderinfo[3] = glGetAttribLocation(edgeshader_prog, "trig"); + shaderinfo[4] = glGetAttribLocation(edgeshader_prog, "pos_b"); + shaderinfo[5] = glGetAttribLocation(edgeshader_prog, "pos_c"); + shaderinfo[6] = glGetAttribLocation(edgeshader_prog, "mask"); + shaderinfo[7] = glGetUniformLocation(edgeshader_prog, "xscale"); + shaderinfo[8] = glGetUniformLocation(edgeshader_prog, "yscale"); + + GLenum err = glGetError(); + if (err != GL_NO_ERROR) { + fprintf(stderr, "OpenGL Error: %s\n", gluErrorString(err)); + } + + GLint status; + glGetProgramiv(edgeshader_prog, GL_LINK_STATUS, &status); + if (status == GL_FALSE) { + int loglen; + char logbuffer[1000]; + glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); + fprintf(stderr, "OpenGL Program Linker Error:\n%.*s", loglen, logbuffer); + } else { + int loglen; + char logbuffer[1000]; + glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); + if (loglen > 0) { + fprintf(stderr, "OpenGL Program Link OK:\n%.*s", loglen, logbuffer); + } + glValidateProgram(edgeshader_prog); + glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); + if (loglen > 0) { + fprintf(stderr, "OpenGL Program Validation results:\n%.*s", loglen, logbuffer); + } + } + } +#endif /* ENABLE_OPENCSG */ +} + +#ifdef ENABLE_OPENCSG +void QGLView::display_opencsg_warning() +{ + OpenCSGWarningDialog *dialog = new OpenCSGWarningDialog(this); + + QString message; + if (this->is_opencsg_capable) { + message += "Warning: You may experience OpenCSG rendering errors.\n\n"; + } + else { + message += "Warning: Missing OpenGL capabilities for OpenCSG - OpenCSG has been disabled.\n\n"; + dialog->enableOpenCSGBox->hide(); + } + message += "It is highly recommended to use OpenSCAD on a system with " + "OpenGL 2.0 or later.\n" + "Your renderer information is as follows:\n"; + QString rendererinfo; + rendererinfo.sprintf("GLEW version %s\n" + "%s (%s)\n" + "OpenGL version %s\n", + glewGetString(GLEW_VERSION), + glGetString(GL_RENDERER), glGetString(GL_VENDOR), + glGetString(GL_VERSION)); + message += rendererinfo; + + dialog->setText(message); + dialog->enableOpenCSGBox->setChecked(Preferences::inst()->getValue("advanced/enable_opencsg_opengl1x").toBool()); + dialog->exec(); + + opencsg_support = this->is_opencsg_capable && Preferences::inst()->getValue("advanced/enable_opencsg_opengl1x").toBool(); +} +#endif + +void QGLView::resizeGL(int w, int h) +{ +#ifdef ENABLE_OPENCSG + shaderinfo[9] = w; + shaderinfo[10] = h; +#endif + glViewport(0, 0, w, h); + w_h_ratio = sqrt((double)w / (double)h); + + setupPerspective(); +} + +void QGLView::setupPerspective() +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-w_h_ratio, +w_h_ratio, -(1/w_h_ratio), +(1/w_h_ratio), +10.0, +FAR_FAR_AWAY); + gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); +} + +void QGLView::setupOrtho(double distance, bool offset) +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + if(offset) + glTranslated(-0.8, -0.8, 0); + double l = distance/10; + glOrtho(-w_h_ratio*l, +w_h_ratio*l, + -(1/w_h_ratio)*l, +(1/w_h_ratio)*l, + -FAR_FAR_AWAY, +FAR_FAR_AWAY); + gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); +} + +void QGLView::paintGL() +{ + glEnable(GL_LIGHTING); + + if (orthomode) setupOrtho(viewer_distance); + else setupPerspective(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + Color4f bgcol = RenderSettings::inst()->color(RenderSettings::BACKGROUND_COLOR); + glClearColor(bgcol[0], bgcol[1], bgcol[2], 0.0); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + glRotated(object_rot.x(), 1.0, 0.0, 0.0); + glRotated(object_rot.y(), 0.0, 1.0, 0.0); + glRotated(object_rot.z(), 0.0, 0.0, 1.0); + + // FIXME: Crosshairs and axes are lighted, this doesn't make sense and causes them + // to change color based on view orientation. + if (showcrosshairs) + { + glLineWidth(3); + Color4f col = RenderSettings::inst()->color(RenderSettings::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 = viewer_distance/20; + glVertex3d(-xf*vd, -yf*vd, -vd); + glVertex3d(+xf*vd, +yf*vd, +vd); + } + glEnd(); + } + + glTranslated(object_trans.x(), object_trans.y(), object_trans.z()); + + // Large gray axis cross inline with the model + // FIXME: This is always gray - adjust color to keep contrast with background + if (showaxes) + { + glLineWidth(1); + glColor3d(0.5, 0.5, 0.5); + glBegin(GL_LINES); + double l = viewer_distance/10; + glVertex3d(-l, 0, 0); + glVertex3d(+l, 0, 0); + glVertex3d(0, -l, 0); + glVertex3d(0, +l, 0); + glVertex3d(0, 0, -l); + glVertex3d(0, 0, +l); + glEnd(); + } + + glDepthFunc(GL_LESS); + glCullFace(GL_BACK); + glDisable(GL_CULL_FACE); + + glLineWidth(2); + glColor3d(1.0, 0.0, 0.0); + + if (this->renderer) { +#if defined(ENABLE_MDI) && defined(ENABLE_OPENCSG) + // FIXME: This belongs in the OpenCSG renderer, but it doesn't know about this ID yet + OpenCSG::setContext(this->opencsg_id); +#endif + this->renderer->draw(showfaces, showedges); + } + + // Small axis cross in the lower left corner + if (showaxes) + { + glDepthFunc(GL_ALWAYS); + + setupOrtho(1000,true); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glRotated(object_rot.x(), 1.0, 0.0, 0.0); + glRotated(object_rot.y(), 0.0, 1.0, 0.0); + glRotated(object_rot.z(), 0.0, 0.0, 1.0); + + glLineWidth(1); + glBegin(GL_LINES); + glColor3d(1.0, 0.0, 0.0); + glVertex3d(0, 0, 0); glVertex3d(10, 0, 0); + glColor3d(0.0, 1.0, 0.0); + glVertex3d(0, 0, 0); glVertex3d(0, 10, 0); + glColor3d(0.0, 0.0, 1.0); + glVertex3d(0, 0, 0); glVertex3d(0, 0, 10); + glEnd(); + + GLdouble mat_model[16]; + glGetDoublev(GL_MODELVIEW_MATRIX, mat_model); + + GLdouble mat_proj[16]; + glGetDoublev(GL_PROJECTION_MATRIX, mat_proj); + + GLint viewport[4]; + glGetIntegerv(GL_VIEWPORT, viewport); + + GLdouble xlabel_x, xlabel_y, xlabel_z; + gluProject(12, 0, 0, mat_model, mat_proj, viewport, &xlabel_x, &xlabel_y, &xlabel_z); + xlabel_x = round(xlabel_x); xlabel_y = round(xlabel_y); + + GLdouble ylabel_x, ylabel_y, ylabel_z; + gluProject(0, 12, 0, mat_model, mat_proj, viewport, &ylabel_x, &ylabel_y, &ylabel_z); + ylabel_x = round(ylabel_x); ylabel_y = round(ylabel_y); + + GLdouble zlabel_x, zlabel_y, zlabel_z; + gluProject(0, 0, 12, mat_model, mat_proj, viewport, &zlabel_x, &zlabel_y, &zlabel_z); + zlabel_x = round(zlabel_x); zlabel_y = round(zlabel_y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glTranslated(-1, -1, 0); + glScaled(2.0/viewport[2], 2.0/viewport[3], 1); + + 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); + glColor3f(0.0f, 0.0f, 0.0f); + glBegin(GL_LINES); + // X Label + glVertex3d(xlabel_x-3, xlabel_y-3, 0); glVertex3d(xlabel_x+3, xlabel_y+3, 0); + glVertex3d(xlabel_x-3, xlabel_y+3, 0); glVertex3d(xlabel_x+3, xlabel_y-3, 0); + // Y Label + glVertex3d(ylabel_x-3, ylabel_y-3, 0); glVertex3d(ylabel_x+3, ylabel_y+3, 0); + glVertex3d(ylabel_x-3, ylabel_y+3, 0); glVertex3d(ylabel_x, ylabel_y, 0); + // Z Label + glVertex3d(zlabel_x-3, zlabel_y-3, 0); glVertex3d(zlabel_x+3, zlabel_y-3, 0); + glVertex3d(zlabel_x-3, zlabel_y+3, 0); glVertex3d(zlabel_x+3, zlabel_y+3, 0); + glVertex3d(zlabel_x-3, zlabel_y-3, 0); glVertex3d(zlabel_x+3, zlabel_y+3, 0); + glEnd(); + + //Restore perspective for next paint + if(!orthomode) + setupPerspective(); + } + + if (statusLabel) { + QString msg; + msg.sprintf("Viewport: translate = [ %.2f %.2f %.2f ], rotate = [ %.2f %.2f %.2f ], distance = %.2f", + -object_trans.x(), -object_trans.y(), -object_trans.z(), + fmodf(360 - object_rot.x() + 90, 360), fmodf(360 - object_rot.y(), 360), fmodf(360 - object_rot.z(), 360), viewer_distance); + statusLabel->setText(msg); + } + + if (running_under_wine) swapBuffers(); +} + +void QGLView::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Plus) { + viewer_distance *= 0.9; + updateGL(); + return; + } + if (event->key() == Qt::Key_Minus) { + viewer_distance /= 0.9; + updateGL(); + return; + } +} + +void QGLView::wheelEvent(QWheelEvent *event) +{ + viewer_distance *= pow(0.9, event->delta() / 120.0); + updateGL(); +} + +void QGLView::mousePressEvent(QMouseEvent *event) +{ + mouse_drag_active = true; + last_mouse = event->globalPos(); +} + +void QGLView::normalizeAngle(GLdouble& angle) +{ + while(angle < 0) + angle += 360; + while(angle > 360) + angle -= 360; +} + +void QGLView::mouseMoveEvent(QMouseEvent *event) +{ + QPoint this_mouse = event->globalPos(); + double dx = (this_mouse.x()-last_mouse.x()) * 0.7; + double dy = (this_mouse.y()-last_mouse.y()) * 0.7; + if (mouse_drag_active) { + if (event->buttons() & Qt::LeftButton +#ifdef Q_WS_MAC + && !(event->modifiers() & Qt::MetaModifier) +#endif + ) { + // Left button rotates in xz, Shift-left rotates in xy + // On Mac, Ctrl-Left is handled as right button on other platforms + object_rot.x() += dy; + if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) + object_rot.y() += dx; + else + object_rot.z() += dx; + + normalizeAngle(object_rot.x()); + normalizeAngle(object_rot.y()); + normalizeAngle(object_rot.z()); + } else { + // Right button pans in the xz plane + // Middle button pans in the xy plane + // Shift-right and Shift-middle zooms + if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) { + viewer_distance += (GLdouble)dy; + } else { + + double mx = +(dx) * viewer_distance/1000; + double mz = -(dy) * viewer_distance/1000; + + double my = 0; +#if (QT_VERSION < QT_VERSION_CHECK(4, 7, 0)) + if (event->buttons() & Qt::MidButton) { +#else + if (event->buttons() & Qt::MiddleButton) { +#endif + my = mz; + mz = 0; + // actually lock the x-position + // (turns out to be easier to use than xy panning) + mx = 0; + } + + Matrix3d aax, aay, aaz, tm3; + aax = Eigen::AngleAxisd(-(object_rot.x()/180) * M_PI, Vector3d::UnitX()); + aay = Eigen::AngleAxisd(-(object_rot.y()/180) * M_PI, Vector3d::UnitY()); + aaz = Eigen::AngleAxisd(-(object_rot.z()/180) * M_PI, Vector3d::UnitZ()); + tm3 = Matrix3d::Identity(); + tm3 = aaz * (aay * (aax * tm3)); + + Matrix4d tm; + tm = Matrix4d::Identity(); + for (int i=0;i<3;i++) for (int j=0;j<3;j++) tm(j,i)=tm3(j,i); + + Matrix4d vec; + vec << + 0, 0, 0, mx, + 0, 0, 0, my, + 0, 0, 0, mz, + 0, 0, 0, 1 + ; + tm = tm * vec; + object_trans.x() += tm(0,3); + object_trans.y() += tm(1,3); + object_trans.z() += tm(2,3); + } + } + updateGL(); + emit doAnimateUpdate(); + } + last_mouse = this_mouse; +} + +void QGLView::mouseReleaseEvent(QMouseEvent*) +{ + mouse_drag_active = false; + releaseMouse(); +} diff --git a/tests/csgtestcore.cc b/tests/csgtestcore.cc index 36e94e7..6d00f09 100644 --- a/tests/csgtestcore.cc +++ b/tests/csgtestcore.cc @@ -92,7 +92,7 @@ string info_dump(OffscreenView *glview) << EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION << "\nCGAL version: " << TOSTRING(CGAL_VERSION) << "\nOpenCSG version: " << OPENCSG_VERSION_STRING - << "\n" << glview->getInfo() + << "\n" << glview->getRendererInfo() << "\n"; return out.str(); -- cgit v0.10.1