diff options
author | don bright <hugh.m.bright@gmail.com> | 2013-02-25 03:07:37 (GMT) |
---|---|---|
committer | don bright <hugh.m.bright@gmail.com> | 2013-02-25 03:07:37 (GMT) |
commit | 9896b6f15eced6d05a131ece44d60745e200b8f5 (patch) | |
tree | 7d8a362003174ed6b150c439f2827d6a7252674d /src | |
parent | 8738cdc03451803014ca84f8614bdff1ff393ca9 (diff) |
first working version of opencsg png export from gui binary
also guiopencsgtest introduced. passes 100% of 'normal' tests
Diffstat (limited to 'src')
-rw-r--r-- | src/CsgInfo.h | 3 | ||||
-rw-r--r-- | src/GLView.cc | 10 | ||||
-rw-r--r-- | src/OpenCSGRenderer.cc | 57 | ||||
-rw-r--r-- | src/OpenCSGRenderer.h | 5 | ||||
-rw-r--r-- | src/export.h | 3 | ||||
-rw-r--r-- | src/export_png.cc | 39 | ||||
-rw-r--r-- | src/openscad.cc | 15 |
7 files changed, 110 insertions, 22 deletions
diff --git a/src/CsgInfo.h b/src/CsgInfo.h index 889eefe..4107b1a 100644 --- a/src/CsgInfo.h +++ b/src/CsgInfo.h @@ -13,10 +13,7 @@ public: #ifdef ENABLE_OPENCSG -#include <opencsg.h> -#include "OpenCSGRenderer.h" #include "csgterm.h" -#include "csgtermnormalizer.h" class CsgInfo_OpenCSG : public CsgInfo { diff --git a/src/GLView.cc b/src/GLView.cc index 0507492..bec3a31 100644 --- a/src/GLView.cc +++ b/src/GLView.cc @@ -393,13 +393,3 @@ void GLView::showCrosshairs() glEnd(); } -/* - - - void paintGL(); // - bool save(const char *filename); // - //bool save(std::ostream &output); // not implemented in qgl? - - GLint shaderinfo[11]; // - -*/ diff --git a/src/OpenCSGRenderer.cc b/src/OpenCSGRenderer.cc index eb66687..e4ed928 100644 --- a/src/OpenCSGRenderer.cc +++ b/src/OpenCSGRenderer.cc @@ -38,7 +38,7 @@ class OpenCSGPrim : public OpenCSG::Primitive public: OpenCSGPrim(OpenCSG::Operation operation, unsigned int convexity) : OpenCSG::Primitive(operation, convexity) { } - shared_ptr<PolySet> ps; + boost::shared_ptr<PolySet> ps; Transform3d m; PolySet::csgmode_e csgmode; virtual void render() { @@ -71,6 +71,10 @@ void OpenCSGRenderer::draw(bool /*showfaces*/, bool showedges) const } } +#include "CGALEvaluator.h" +#include "CSGTermEvaluator.h" +#include "csgtermnormalizer.h" + void OpenCSGRenderer::renderCSGChain(CSGChain *chain, GLint *shaderinfo, bool highlight, bool background) const { @@ -129,3 +133,54 @@ void OpenCSGRenderer::renderCSGChain(CSGChain *chain, GLint *shaderinfo, } std::for_each(primitives.begin(), primitives.end(), del_fun<OpenCSG::Primitive>()); } + +// miscellaneous code to prep OpenCSG render. +// returns 0 on success, 1 on failure +// csgInfo gets modified +int opencsg_prep( const Tree &tree, const AbstractNode *root_node, CsgInfo_OpenCSG &csgInfo ) +{ + CGALEvaluator cgalevaluator(tree); + CSGTermEvaluator evaluator(tree, &cgalevaluator.psevaluator); + boost::shared_ptr<CSGTerm> root_raw_term = evaluator.evaluateCSGTerm(*root_node, + csgInfo.highlight_terms, + csgInfo.background_terms); + + if (!root_raw_term) { + std::cerr << "Error: CSG generation failed! (no top level object found)\n"; + return 1; + } + + // CSG normalization + CSGTermNormalizer normalizer(5000); + csgInfo.root_norm_term = normalizer.normalize(root_raw_term); + if (csgInfo.root_norm_term) { + csgInfo.root_chain = new CSGChain(); + csgInfo.root_chain->import(csgInfo.root_norm_term); + fprintf(stderr, "Normalized CSG tree has %d elements\n", int(csgInfo.root_chain->polysets.size())); + } + else { + csgInfo.root_chain = NULL; + fprintf(stderr, "WARNING: CSG normalization resulted in an empty tree\n"); + } + + if (csgInfo.highlight_terms.size() > 0) { + std::cerr << "Compiling highlights (" << csgInfo.highlight_terms.size() << " CSG Trees)...\n"; + + csgInfo.highlights_chain = new CSGChain(); + for (unsigned int i = 0; i < csgInfo.highlight_terms.size(); i++) { + csgInfo.highlight_terms[i] = normalizer.normalize(csgInfo.highlight_terms[i]); + csgInfo.highlights_chain->import(csgInfo.highlight_terms[i]); + } + } + + if (csgInfo.background_terms.size() > 0) { + std::cerr << "Compiling background (" << csgInfo.background_terms.size() << " CSG Trees)...\n"; + + csgInfo.background_chain = new CSGChain(); + for (unsigned int i = 0; i < csgInfo.background_terms.size(); i++) { + csgInfo.background_terms[i] = normalizer.normalize(csgInfo.background_terms[i]); + csgInfo.background_chain->import(csgInfo.background_terms[i]); + } + } + return 0; +} diff --git a/src/OpenCSGRenderer.h b/src/OpenCSGRenderer.h index e6091aa..9a24c1c 100644 --- a/src/OpenCSGRenderer.h +++ b/src/OpenCSGRenderer.h @@ -20,4 +20,9 @@ private: GLint *shaderinfo; }; +#include "Tree.h" +#include "CsgInfo.h" +// should we refactor this into the renderer class? +int opencsg_prep( const Tree &tree, const AbstractNode *root_node, CsgInfo_OpenCSG &csgInfo ); + #endif diff --git a/src/export.h b/src/export.h index b140db6..77359c1 100644 --- a/src/export.h +++ b/src/export.h @@ -2,6 +2,7 @@ #define EXPORT_H_ #include <iostream> +#include "Tree.h" #ifdef ENABLE_CGAL @@ -9,7 +10,7 @@ void export_stl(class CGAL_Nef_polyhedron *root_N, std::ostream &output); void export_off(CGAL_Nef_polyhedron *root_N, std::ostream &output); void export_dxf(CGAL_Nef_polyhedron *root_N, std::ostream &output); void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, std::ostream &output); -void export_png_with_opencsg(CGAL_Nef_polyhedron *root_N, std::ostream &output); +void export_png_with_opencsg(Tree &tree, std::ostream &output); #endif diff --git a/src/export_png.cc b/src/export_png.cc index 179278a..0626c71 100644 --- a/src/export_png.cc +++ b/src/export_png.cc @@ -46,15 +46,50 @@ void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, std::ostream &output) csgInfo.glview->save(output); } -void export_png_with_opencsg(CGAL_Nef_polyhedron *root_N, std::ostream &output) +#ifdef ENABLE_OPENCSG +#include "OpenCSGRenderer.h" +#include <opencsg.h> +#endif + +void export_png_with_opencsg(Tree &tree, std::ostream &output) { - CsgInfo csgInfo; +#ifdef ENABLE_OPENCSG + CsgInfo_OpenCSG csgInfo = CsgInfo_OpenCSG(); + AbstractNode const *root_node = tree.root(); + int result = opencsg_prep( tree, root_node, csgInfo ); + if ( result == 1 ) { + return; + fprintf(stderr,"Couldn't init OpenCSG chainsn"); + } + try { csgInfo.glview = new OffscreenView(512,512); } catch (int error) { fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i.\n", error); return; } + + OpenCSGRenderer opencsgRenderer(csgInfo.root_chain, csgInfo.highlights_chain, csgInfo.background_chain, csgInfo.glview->shaderinfo); + + Vector3d center(0,0,0); + double radius = 1.0; + if (csgInfo.root_chain) { + BoundingBox bbox = csgInfo.root_chain->getBoundingBox(); + center = (bbox.min() + bbox.max()) / 2; + radius = (bbox.max() - bbox.min()).norm() / 2; + } + + Vector3d cameradir(1, 1, -0.5); + Vector3d camerapos = center - radius*1.8*cameradir; + csgInfo.glview->setCamera(camerapos, center); + csgInfo.glview->setRenderer(&opencsgRenderer); + OpenCSG::setContext(0); + OpenCSG::setOption(OpenCSG::OffscreenSetting, OpenCSG::FrameBufferObject); + csgInfo.glview->paintGL(); + csgInfo.glview->save(output); +#else + fprintf(stderr,"This openscad was built without OpenCSG support\n"); +#endif } diff --git a/src/openscad.cc b/src/openscad.cc index 0b5466e..1bbe049 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -133,7 +133,7 @@ int main(int argc, char **argv) desc.add_options() ("help,h", "help message") ("version,v", "print the version") - ("render", "if exporting an image, do a full CGAL render") + ("render", "if exporting a png image, do a full CGAL render") ("o,o", po::value<string>(), "out-file") ("s,s", po::value<string>(), "stl-file") ("x,x", po::value<string>(), "dxf-file") @@ -266,6 +266,7 @@ int main(int argc, char **argv) ModuleInstantiation root_inst; AbstractNode *root_node; AbstractNode *absolute_root_node; + CGAL_Nef_polyhedron root_N; handle_dep(filename); @@ -310,10 +311,14 @@ int main(int argc, char **argv) } else { #ifdef ENABLE_CGAL - CGAL_Nef_polyhedron root_N = cgalevaluator.evaluateCGALMesh(*tree.root()); - + if (png_output_file && !vm.count("render")) { + // OpenCSG png -> don't necessarily need CGALMesh evaluation + } else { + root_N = cgalevaluator.evaluateCGALMesh(*tree.root()); + } + fs::current_path(original_path); - + if (deps_output_file) { std::string deps_out( deps_output_file ); std::string geom_out; @@ -395,7 +400,7 @@ int main(int argc, char **argv) if (vm.count("render")) { export_png_with_cgal(&root_N, fstream); } else { - export_png_with_opencsg(&root_N, fstream); + export_png_with_opencsg(tree, fstream); } fstream.close(); } |