summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordon bright <hugh.m.bright@gmail.com>2013-02-27 06:37:37 (GMT)
committerdon bright <hugh.m.bright@gmail.com>2013-02-27 06:37:37 (GMT)
commit13b0fe4b3f8e0367f02a982deb5be695ccd00c80 (patch)
treef834a15d8ed4d37f23882ff60a2e2c08ea4f53f2
parentc84b0d21842cbd09a46c80e23a2f1a3fc2a04592 (diff)
rework Camera using boost::variant. first working --gimbalcam version
-rw-r--r--openscad.pro1
-rw-r--r--src/GLView.cc110
-rw-r--r--src/GLView.h10
-rw-r--r--src/OffscreenView.cc39
-rw-r--r--src/OffscreenView.h2
-rw-r--r--src/QGLView.cc44
-rw-r--r--src/export.h2
-rw-r--r--src/export_png.cc14
-rw-r--r--src/linalg.h53
-rw-r--r--src/openscad.cc37
10 files changed, 146 insertions, 166 deletions
diff --git a/openscad.pro b/openscad.pro
index 3947c65..c7a3e43 100644
--- a/openscad.pro
+++ b/openscad.pro
@@ -230,6 +230,7 @@ HEADERS += src/version_check.h \
src/mathc99.h \
src/memory.h \
src/linalg.h \
+ src/Camera.h \
src/system-gl.h \
src/stl-utils.h \
src/svg.h \
diff --git a/src/GLView.cc b/src/GLView.cc
index 4880c40..130df29 100644
--- a/src/GLView.cc
+++ b/src/GLView.cc
@@ -20,6 +20,7 @@ GLView::GLView()
showaxes = false;
showcrosshairs = false;
renderer = NULL;
+ camtype = Camera::NONE;
#ifdef ENABLE_OPENCSG
is_opencsg_capable = false;
has_shaders = false;
@@ -49,10 +50,10 @@ void GLView::resizeGL(int w, int h)
void GLView::setGimbalCamera(const Eigen::Vector3d &pos, const Eigen::Vector3d &rot, double distance)
{
- (void) pos;
- (void) rot;
- (void) distance;
- PRINT("set gimbal camera not implemented");
+ gcam.object_trans = pos;
+ gcam.object_rot = rot;
+ gcam.viewer_distance = distance;
+ camtype = Camera::GIMBAL;
}
void GLView::setupGimbalCamPerspective()
@@ -80,6 +81,7 @@ void GLView::setVectorCamera(const Eigen::Vector3d &pos, const Eigen::Vector3d &
{
vcam.eye = pos;
vcam.center = center;
+ camtype = Camera::VECTOR;
// FIXME kludge for showAxes to work in VectorCamera mode
gcam.viewer_distance = 10*3*(vcam.center - vcam.eye).norm();
}
@@ -105,18 +107,24 @@ void GLView::setupVectorCamOrtho(bool offset)
void GLView::setCamera( Camera &cam )
{
- // FIXME : use boost::variant, like value.cc does
- if (cam.camtype == Camera::NULL_CAMERA) {
+ if ( cam.type() == Camera::NONE ) {
return;
- } else if (cam.camtype == Camera::GIMBAL_CAMERA) {
- GimbalCamera *gc = dynamic_cast<GimbalCamera *>( &cam );
- setGimbalCamera( gc->object_trans, gc->object_rot, gc->viewer_distance );
- } else if (cam.camtype == Camera::VECTOR_CAMERA) {
- VectorCamera *vc = dynamic_cast<VectorCamera *>( &cam );
- setVectorCamera( vc->eye, vc->center );
+ } else if ( cam.type() == Camera::GIMBAL ) {
+ GimbalCamera gc = boost::get<GimbalCamera>(cam.value);
+ setGimbalCamera( gc.object_trans, gc.object_rot, gc.viewer_distance );
+ } else if ( cam.type() == Camera::VECTOR ) {
+ VectorCamera vc = boost::get<VectorCamera>(cam.value);
+ setVectorCamera( vc.eye, vc.center );
}
}
+void GLView::paintGL()
+{
+ if (camtype == Camera::NONE) return;
+ else if (camtype == Camera::GIMBAL) gimbalCamPaintGL();
+ else if (camtype == Camera::VECTOR) vectorCamPaintGL();
+}
+
#ifdef ENABLE_OPENCSG
void GLView::enable_opencsg_shaders()
{
@@ -293,6 +301,84 @@ void GLView::initializeGL()
#endif
}
+void GLView::vectorCamPaintGL()
+{
+ glEnable(GL_LIGHTING);
+
+ if (orthomode) setupVectorCamOrtho();
+ else setupVectorCamPerspective();
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glClearColor(1.0f, 1.0f, 0.92f, 1.0f);
+
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+
+ gluLookAt(vcam.eye[0], vcam.eye[1], vcam.eye[2],
+ vcam.center[0], vcam.center[1], vcam.center[2],
+ 0.0, 0.0, 1.0);
+
+ // fixme - showcrosshairs doesnt work with vector camera
+ // if (showcrosshairs) GLView::showCrosshairs();
+
+ if (showaxes) GLView::showAxes();
+
+ glDepthFunc(GL_LESS);
+ glCullFace(GL_BACK);
+ glDisable(GL_CULL_FACE);
+
+ glLineWidth(2);
+ glColor3d(1.0, 0.0, 0.0);
+ //FIXME showSmallAxes wont work with vector camera
+ //if (showaxes) GLView::showSmallaxes();
+
+ if (this->renderer) {
+ this->renderer->draw(showfaces, showedges);
+ }
+}
+
+void GLView::gimbalCamPaintGL()
+{
+ glEnable(GL_LIGHTING);
+
+ if (orthomode) GLView::setupGimbalCamOrtho(gcam.viewer_distance);
+ else GLView::setupGimbalCamPerspective();
+
+ 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(gcam.object_rot.x(), 1.0, 0.0, 0.0);
+ glRotated(gcam.object_rot.y(), 0.0, 1.0, 0.0);
+ glRotated(gcam.object_rot.z(), 0.0, 0.0, 1.0);
+
+ if (showcrosshairs) GLView::showCrosshairs();
+
+ glTranslated(gcam.object_trans.x(), gcam.object_trans.y(), gcam.object_trans.z() );
+
+ if (showaxes) GLView::showAxes();
+
+ 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) GLView::showSmallaxes();
+}
+
void GLView::showSmallaxes()
{
// Fixme - this modifies the camera and doesnt work in 'non-gimbal' camera mode
diff --git a/src/GLView.h b/src/GLView.h
index c7803c9..6dadbf8 100644
--- a/src/GLView.h
+++ b/src/GLView.h
@@ -29,6 +29,7 @@ OffscreenView uses VectorCamera.
#include <iostream>
#include "renderer.h"
#include "linalg.h"
+#include "Camera.h"
#define FAR_FAR_AWAY 100000.0
@@ -41,16 +42,21 @@ public:
void initializeGL();
void resizeGL(int w, int h);
- virtual void paintGL() = 0;
+ virtual void paintGL();
void setGimbalCamera(const Eigen::Vector3d &pos, const Eigen::Vector3d &rot, double distance);
void setupGimbalCamPerspective();
void setupGimbalCamOrtho(double distance, bool offset=false);
+ void gimbalCamPaintGL();
void setVectorCamera(const Eigen::Vector3d &pos, const Eigen::Vector3d &center);
void setupVectorCamPerspective();
void setupVectorCamOrtho(bool offset=false);
+ void vectorCamPaintGL();
+ void setCamera( NullCamera &nc );
+ void setCamera( GimbalCamera &gc );
+ void setCamera( VectorCamera &vc );
void setCamera( Camera &cam );
void showCrosshairs();
@@ -69,7 +75,7 @@ public:
bool showedges;
bool showcrosshairs;
- // fixme -- use boost::variant like in value.cc ??
+ Camera::CameraType camtype;
VectorCamera vcam;
GimbalCamera gcam;
diff --git a/src/OffscreenView.cc b/src/OffscreenView.cc
index 9d7584e..b09d80e 100644
--- a/src/OffscreenView.cc
+++ b/src/OffscreenView.cc
@@ -27,43 +27,10 @@ void OffscreenView::display_opencsg_warning()
}
#endif
-void OffscreenView::paintGL()
+/*void OffscreenView::paintGL()
{
- glEnable(GL_LIGHTING);
-
- if (orthomode) setupVectorCamOrtho();
- else setupVectorCamPerspective();
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glClearColor(1.0f, 1.0f, 0.92f, 1.0f);
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-
- gluLookAt(vcam.eye[0], vcam.eye[1], vcam.eye[2],
- vcam.center[0], vcam.center[1], vcam.center[2],
- 0.0, 0.0, 1.0);
-
- // fixme - showcrosshairs doesnt work with vector camera
- // if (showcrosshairs) GLView::showCrosshairs();
-
- if (showaxes) GLView::showAxes();
-
- glDepthFunc(GL_LESS);
- glCullFace(GL_BACK);
- glDisable(GL_CULL_FACE);
-
- glLineWidth(2);
- glColor3d(1.0, 0.0, 0.0);
-
- //FIXME showSmallAxes wont work with vector camera
- //if (showaxes) GLView::showSmallaxes();
-
- if (this->renderer) {
- this->renderer->draw(showfaces, showedges);
- }
-}
+ GLView::vectorCamPaintGL();
+}*/
bool OffscreenView::save(const char *filename)
{
diff --git a/src/OffscreenView.h b/src/OffscreenView.h
index b073f89..bc700f8 100644
--- a/src/OffscreenView.h
+++ b/src/OffscreenView.h
@@ -17,7 +17,7 @@ class OffscreenView : public GLView
public:
OffscreenView(size_t width, size_t height); // not
~OffscreenView(); // not
- void paintGL(); //
+// void paintGL(); //
bool save(std::ostream &output);
OffscreenContext *ctx; // not
// overrides
diff --git a/src/QGLView.cc b/src/QGLView.cc
index 01c6ff0..59c96c0 100644
--- a/src/QGLView.cc
+++ b/src/QGLView.cc
@@ -65,7 +65,10 @@ static bool running_under_wine = false;
void QGLView::init()
{
+ camtype = Camera::GIMBAL;
gcam.object_rot << 35, 0, -25;
+ gcam.object_trans << 0, 0, 0;
+ gcam.viewer_distance = 500;
this->mouse_drag_active = false;
this->statusLabel = NULL;
@@ -146,46 +149,7 @@ void QGLView::resizeGL(int w, int h)
void QGLView::paintGL()
{
- glEnable(GL_LIGHTING);
-
- if (orthomode) GLView::setupGimbalCamOrtho(gcam.viewer_distance);
- else GLView::setupGimbalCamPerspective();
-
- 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(gcam.object_rot.x(), 1.0, 0.0, 0.0);
- glRotated(gcam.object_rot.y(), 0.0, 1.0, 0.0);
- glRotated(gcam.object_rot.z(), 0.0, 0.0, 1.0);
-
- if (showcrosshairs) GLView::showCrosshairs();
-
- glTranslated(gcam.object_trans.x(), gcam.object_trans.y(), gcam.object_trans.z());
-
- if (showaxes) GLView::showAxes();
-
- 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) GLView::showSmallaxes();
+ GLView::gimbalCamPaintGL();
if (statusLabel) {
QString msg;
diff --git a/src/export.h b/src/export.h
index 243d897..a565561 100644
--- a/src/export.h
+++ b/src/export.h
@@ -3,7 +3,7 @@
#include <iostream>
#include "Tree.h"
-#include "linalg.h"
+#include "Camera.h"
#ifdef ENABLE_CGAL
diff --git a/src/export_png.cc b/src/export_png.cc
index 1c4a268..3e618e5 100644
--- a/src/export_png.cc
+++ b/src/export_png.cc
@@ -32,20 +32,19 @@ void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, Camera &cam, std::ostream
bbox = cgalRenderer.polyset->getBoundingBox();
}
- if (cam.camtype == Camera::NULL_CAMERA) {
+ if (cam.type() == Camera::NONE) {
VectorCamera vcam;
vcam.center = getBoundingCenter(bbox);
double radius = getBoundingRadius(bbox);
Vector3d cameradir(1, 1, -0.5);
vcam.eye = vcam.center - radius*2*cameradir;
- csgInfo.glview->setCamera( vcam );
- } else {
- csgInfo.glview->setCamera( cam );
+ cam.value = vcam;
}
//std::cerr << center << "\n";
//std::cerr << radius << "\n";
+ csgInfo.glview->setCamera( cam );
csgInfo.glview->setRenderer(&cgalRenderer);
csgInfo.glview->paintGL();
csgInfo.glview->save(output);
@@ -76,7 +75,7 @@ void export_png_with_opencsg(Tree &tree, Camera &cam, std::ostream &output)
OpenCSGRenderer opencsgRenderer(csgInfo.root_chain, csgInfo.highlights_chain, csgInfo.background_chain, csgInfo.glview->shaderinfo);
- if (cam.camtype == Camera::NULL_CAMERA) {
+ if (cam.type() == Camera::NONE) {
VectorCamera vcam;
vcam.center << 0,0,0;
double radius = 1.0;
@@ -87,11 +86,10 @@ void export_png_with_opencsg(Tree &tree, Camera &cam, std::ostream &output)
}
Vector3d cameradir(1, 1, -0.5);
vcam.eye = vcam.center - radius*1.8*cameradir;
- csgInfo.glview->setCamera( vcam );
- } else {
- csgInfo.glview->setCamera( cam );
+ cam.value = vcam;
}
+ csgInfo.glview->setCamera( cam );
csgInfo.glview->setRenderer(&opencsgRenderer);
OpenCSG::setContext(0);
OpenCSG::setOption(OpenCSG::OffscreenSetting, OpenCSG::FrameBufferObject);
diff --git a/src/linalg.h b/src/linalg.h
index 31cd922..1f9ed30 100644
--- a/src/linalg.h
+++ b/src/linalg.h
@@ -40,57 +40,4 @@ public:
bool isValid() const { return this->minCoeff() >= 0.0f; }
};
-
-
-// FIXME - for camera, use boost::variant like in value.cc
-
-#include <vector>
-
-
-class Camera
-{
-public:
- enum camera_type_e { GIMBAL_CAMERA, VECTOR_CAMERA, NULL_CAMERA } camtype;
- Camera() { camtype = NULL_CAMERA; }
- virtual ~Camera() {} // only prevent 'not polymorphic' compile error
-};
-
-class GimbalCamera : public Camera
-{
-public:
- GimbalCamera()
- {
- camtype = GIMBAL_CAMERA;
- object_trans << 0,0,0;
- object_rot << 35,0,25;
- viewer_distance = 500;
- }
- void setup( std::vector<double> d )
- {
- assert( d.size() == 7 );
- object_trans << d[0], d[1], d[2];
- object_rot << d[3], d[4], d[5];
- viewer_distance = d[6];
- }
- Eigen::Vector3d object_trans;
- Eigen::Vector3d object_rot;
- double viewer_distance;
-};
-
-class VectorCamera : public Camera
-{
-public:
- VectorCamera()
- {
- camtype = VECTOR_CAMERA;
- center << 0,0,0;
- Eigen::Vector3d cameradir(1, 1, -0.5);
- eye = center - 500 * cameradir;
- // "up" not currently used
- }
- Eigen::Vector3d eye;
- Eigen::Vector3d center; // (aka 'target')
- Eigen::Vector3d up;
-};
-
#endif
diff --git a/src/openscad.cc b/src/openscad.cc
index a63bac0..92ddcb7 100644
--- a/src/openscad.cc
+++ b/src/openscad.cc
@@ -57,6 +57,8 @@
#include "SparkleAutoUpdater.h"
#endif
+#include "Camera.h"
+#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
@@ -71,10 +73,11 @@ namespace fs = boost::filesystem;
static void help(const char *progname)
{
+ int tab = int(strlen(progname))+8;
fprintf(stderr, "Usage: %s [ -o output_file [ -d deps_file ] ]\\\n"
"%*s[ -m make_command ] [ -D var=val [..] ] [ --render ] \\\n"
- "[ --viewport=x,y,z,xrot,yrot,zrot,dist ] filename\n",
- progname, int(strlen(progname))+8, "");
+ "%*s[ --viewport=x,y,z,xrot,yrot,zrot,dist ] filename\n",
+ progname, tab, "", tab, "");
exit(1);
}
@@ -92,6 +95,8 @@ QString examplesdir;
using std::string;
using std::vector;
+using boost::lexical_cast;
+using boost::is_any_of;
int main(int argc, char **argv)
{
@@ -129,15 +134,15 @@ int main(int argc, char **argv)
const char *filename = NULL;
const char *output_file = NULL;
const char *deps_output_file = NULL;
- GimbalCamera gimbalCamera;
- gimbalCamera.camtype = Camera::NULL_CAMERA;
+ Camera camera;
+ camera.value = NullCamera();
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "help message")
("version,v", "print the version")
("render", "if exporting a png image, do a full CGAL render")
- ("viewport", "=x,y,z,xrot,yrot,zrot,dist for exporting png")
+ ("gimbalcam", po::value<string>(), "=x,y,z,xrot,yrot,zrot,dist for exporting png")
("o,o", po::value<string>(), "out-file")
("s,s", po::value<string>(), "stl-file")
("x,x", po::value<string>(), "dxf-file")
@@ -212,12 +217,18 @@ int main(int argc, char **argv)
currentdir = boosty::stringy(fs::current_path());
- if (vm.count("viewport")) {
- vector<double> camnums = vm["viewport"].as< vector<double> >();
- if ( camnums.size() != 7 ) {
- fprintf(stderr,"Need 7 numbers for viewport");
- } else {
- gimbalCamera.setup( camnums );
+ if (vm.count("gimbalcam")) {
+ std::cout << "x:" << vm["gimbalcam"].as<string>().c_str() << "\n";
+ vector<string> strs;
+ vector<double> nums;
+ split(strs, vm["gimbalcam"].as<string>(), is_any_of(","));
+ if ( strs.size() != 7 ) {
+ fprintf(stderr,"Need 7 numbers for gimbalcam\n");
+ } else {
+ BOOST_FOREACH(string &s, strs) nums.push_back(lexical_cast<double>(s));
+ GimbalCamera gcam;
+ gcam.setup( nums );
+ camera.value = gcam;
}
}
@@ -411,9 +422,9 @@ int main(int argc, char **argv)
}
else {
if (vm.count("render")) {
- export_png_with_cgal(&root_N, gimbalCamera, fstream);
+ export_png_with_cgal(&root_N, camera, fstream);
} else {
- export_png_with_opencsg(tree, gimbalCamera, fstream);
+ export_png_with_opencsg(tree, camera, fstream);
}
fstream.close();
}
contact: Jan Huwald // Impressum