diff options
author | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-06-25 09:49:28 (GMT) |
---|---|---|
committer | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-06-25 09:49:28 (GMT) |
commit | 1ef41a174f64d8ae98d2f727eef9a7613f85934c (patch) | |
tree | a349d76ac072e3923399168d7564e98eb9b534ab | |
parent | 4f7d83c00fa2cd56d60cf399ccf848fdd63e1703 (diff) |
Clifford Wolf:
Some fixes, some progress (OpenCSG)
git-svn-id: http://svn.clifford.at/openscad/trunk@20 b57f626f-c46c-0410-a088-ec61d464b74c
-rw-r--r-- | example.scad | 31 | ||||
-rw-r--r-- | glview.cc | 22 | ||||
-rw-r--r-- | mainwin.cc | 100 | ||||
-rw-r--r-- | polyset.cc | 15 | ||||
-rw-r--r-- | primitive.cc | 16 |
5 files changed, 135 insertions, 49 deletions
diff --git a/example.scad b/example.scad index 65d20f4..c0ab7c2 100644 --- a/example.scad +++ b/example.scad @@ -48,5 +48,34 @@ module test003() } } -test003(); +module test004() +{ + intersection() { + difference() { + cylinder(h = 5, r1 = 2, r2 = 0.5, center = true); + cylinder(h = 6, r1 = 0.7, r2 = 0.7, center = true); + } + cube(3); + } +} + +module test005() +{ + difference() { + union() { + cube([3 3 3], center = true); + cube([4 1.5 1.5], center = true); + cube([1.5 4 1.5], center = true); + cube([1.5 1.5 4], center = true); + } + union() { + cube([5 1 1], center = true); + cube([1 5 1], center = true); + cube([1 1 5], center = true); + } + } +} + +test005(); + @@ -27,9 +27,9 @@ GLView::GLView(QWidget *parent) : QGLWidget(parent) { - viewer_distance = 10; - object_rot_y = 0; - object_rot_z = 0; + viewer_distance = 20; + object_rot_y = 35; + object_rot_z = 25; mouse_drag_active = false; last_mouse_x = 0; @@ -47,19 +47,6 @@ void GLView::initializeGL() glDepthRange(-FAR_FAR_AWAY, +FAR_FAR_AWAY); glClearColor(1.0, 1.0, 0.9, 0.0); - -#if 0 - GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; - GLfloat light_position[] = {1.0, 1.0, -1.0, 0.0}; - - glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); - glLightfv(GL_LIGHT0, GL_POSITION, light_position); - glEnable(GL_LIGHT0); - - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - glEnable(GL_LIGHTING); -#endif } void GLView::resizeGL(int w, int h) @@ -82,6 +69,7 @@ void GLView::paintGL() glRotated(object_rot_z, 0.0, 0.0, 1.0); glDepthFunc(GL_LESS); + glDisable(GL_LIGHTING); #if 0 glLineWidth(1); @@ -93,7 +81,7 @@ void GLView::paintGL() glEnd(); #endif - glLineWidth(5); + glLineWidth(2); glColor3d(1.0, 0.0, 0.0); if (renderfunc) @@ -60,9 +60,9 @@ MainWindow::MainWindow(const char *filename) { QMenu *menu = menuBar()->addMenu("&Design"); - menu->addAction("&Compile", this, SLOT(actionCompile())); + menu->addAction("&Compile", this, SLOT(actionCompile()), QKeySequence(Qt::Key_F5)); #ifdef ENABLE_CGAL - menu->addAction("Compile and &Render (CGAL)", this, SLOT(actionRenderCGAL())); + menu->addAction("Compile and &Render (CGAL)", this, SLOT(actionRenderCGAL()), QKeySequence(Qt::Key_F6)); #endif menu->addAction("Display &AST...", this, SLOT(actionDisplayAST())); menu->addAction("Display CSG &Tree...", this, SLOT(actionDisplayCSGTree())); @@ -291,6 +291,7 @@ void MainWindow::actionCompile() root_chain = new CSGChain(); root_chain->import(root_norm_term); + screen->updateGL(); console->append("Compilation finished."); } @@ -397,10 +398,14 @@ void MainWindow::viewModeActionsUncheck() class DLPrim : public OpenCSG::Primitive { public: - DLPrim(unsigned int displayListId, OpenCSG::Operation operation, unsigned int convexity) : - OpenCSG::Primitive(operation, convexity), id(displayListId) { } - virtual void render() { glCallList(id); } + DLPrim(OpenCSG::Operation operation, unsigned int convexity) : + OpenCSG::Primitive(operation, convexity), id(0), color(0) { } + virtual void render() { + if (id) + glCallList(id); + } unsigned int id; + unsigned int color; }; static void renderGLviaOpenCSG(void *vp) @@ -411,31 +416,76 @@ static void renderGLviaOpenCSG(void *vp) glew_initialized = 1; glewInit(); } + + glPushMatrix(); + glLoadIdentity(); + + GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; + GLfloat light_position[] = {-0.3, -1.0, 0.3, 0.0}; + + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + glEnable(GL_LIGHT0); + + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + glEnable(GL_COLOR_MATERIAL); + glEnable(GL_LIGHTING); + + glPopMatrix(); + if (m->root_chain) { - glDepthFunc(GL_LEQUAL); - /* FIXME */ - for (int i = 0; i < m->root_chain->polysets.size(); i++) { - if (m->root_chain->types[i] == CSGTerm::DIFFERENCE) { - m->root_chain->polysets[i]->render_surface(PolySet::COLOR_CUTOUT); - m->root_chain->polysets[i]->render_edges(PolySet::COLOR_CUTOUT); - } else { - m->root_chain->polysets[i]->render_surface(PolySet::COLOR_MATERIAL); - m->root_chain->polysets[i]->render_edges(PolySet::COLOR_MATERIAL); + std::vector<OpenCSG::Primitive*> primitives; + for (int i = 0;; i++) + { + bool last = i == m->root_chain->polysets.size(); + + if (last || m->root_chain->types[i] == CSGTerm::UNION) + { + OpenCSG::render(primitives, OpenCSG::Goldfeather, OpenCSG::NoDepthComplexitySampling); + glDepthFunc(GL_EQUAL); + for (unsigned int j = 0; j < primitives.size(); j++) { + DLPrim *p = (DLPrim*)primitives[j]; + if (p->color) + glColor3f(0.0, 0.8, 0); + else + glColor3f(0.8, 0, 0); + glCallList(p->id); + glDeleteLists(p->id, 1); + delete p; + } + glDepthFunc(GL_LESS); + + primitives.clear(); } + + if (last) + break; + + + DLPrim *prim = new DLPrim(m->root_chain->types[i] == CSGTerm::DIFFERENCE ? OpenCSG::Subtraction : OpenCSG::Intersection, 1); + + prim->id = glGenLists(1); + prim->color = m->root_chain->types[i] == CSGTerm::DIFFERENCE ? 1 : 0; + glNewList(prim->id, GL_COMPILE); + m->root_chain->polysets[i]->render_surface(PolySet::COLOR_NONE); + glEndList(); + + primitives.push_back(prim); } } else { - GLuint id1 = glGenLists(1); - glNewList(id1, GL_COMPILE); + DLPrim *box = new DLPrim(OpenCSG::Intersection, 1); + box->id = glGenLists(1); + glNewList(box->id, GL_COMPILE); glutSolidCube(1.8); glEndList(); - GLuint id2 = glGenLists(1); - glNewList(id2, GL_COMPILE); + DLPrim *sphere = new DLPrim(OpenCSG::Subtraction, 1); + sphere->id = glGenLists(1); + sphere->color = 1; + glNewList(sphere->id, GL_COMPILE); glutSolidSphere(1.2, 20, 20); glEndList(); - DLPrim* box = new DLPrim(id1, OpenCSG::Intersection, 1); - DLPrim* sphere = new DLPrim(id2, OpenCSG::Subtraction, 1); std::vector<OpenCSG::Primitive*> primitives; primitives.push_back(box); @@ -444,10 +494,14 @@ static void renderGLviaOpenCSG(void *vp) OpenCSG::render(primitives, OpenCSG::Goldfeather, OpenCSG::NoDepthComplexitySampling); glDepthFunc(GL_EQUAL); - glColor3f(1.0, 0, 0); for (unsigned int i = 0; i < primitives.size(); i++) { - primitives[i]->render(); - glDeleteLists(((DLPrim*)primitives[i])->id, 1); + DLPrim *p = (DLPrim*)primitives[i]; + if (p->color) + glColor3f(0.0, 0.8, 0); + else + glColor3f(0.8, 0, 0); + glCallList(p->id); + glDeleteLists(p->id, 1); delete primitives[i]; } glDepthFunc(GL_LESS); @@ -41,6 +41,18 @@ void PolySet::insert_vertex(double x, double y, double z) polygons.last().insert(0, Point(x, y, z)); } +static void set_opengl_normal(double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3) +{ + double ax = x1 - x2, ay = y1 - y2, az = z1 - z2; + double bx = x3 - x2, by = y3 - y2, bz = z3 - z2; + double nx = ay*bz - az*by; + double ny = az*bx - ax*bz; + double nz = ax*by - ay*bx; + double n_scale = 1.0 / sqrt(nx*nx + ny*ny + nz*nz); + nx /= n_scale; ny /= n_scale; nz /= n_scale; + glNormal3d(-nx, -ny, -nz); +} + void PolySet::render_surface(colormode_e colormode) const { if (colormode == COLOR_MATERIAL) @@ -50,6 +62,9 @@ void PolySet::render_surface(colormode_e colormode) const for (int i = 0; i < polygons.size(); i++) { const Polygon *poly = &polygons[i]; glBegin(GL_POLYGON); + set_opengl_normal(poly->at(0).x, poly->at(0).y, poly->at(0).z, + poly->at(1).x, poly->at(1).y, poly->at(1).z, + poly->at(2).x, poly->at(2).y, poly->at(2).z); for (int j = 0; j < poly->size(); j++) { const Point *p = &poly->at(j); glVertex3d(p->x, p->y, p->z); diff --git a/primitive.cc b/primitive.cc index a3846a4..2c1093e 100644 --- a/primitive.cc +++ b/primitive.cc @@ -227,25 +227,25 @@ PolySet *PrimitiveNode::render_polyset(render_mode_e mode) const for (int i=0; i<fragments; i++) { int j = (i+1) % fragments; p->append_poly(); - p->append_vertex(circle1[i].x, circle1[i].y, z1); - p->append_vertex(circle2[i].x, circle2[i].y, z2); - p->append_vertex(circle1[j].x, circle1[j].y, z1); + p->insert_vertex(circle1[i].x, circle1[i].y, z1); + p->insert_vertex(circle2[i].x, circle2[i].y, z2); + p->insert_vertex(circle1[j].x, circle1[j].y, z1); p->append_poly(); - p->append_vertex(circle2[i].x, circle2[i].y, z2); - p->append_vertex(circle2[j].x, circle2[j].y, z2); - p->append_vertex(circle1[j].x, circle1[j].y, z1); + p->insert_vertex(circle2[i].x, circle2[i].y, z2); + p->insert_vertex(circle2[j].x, circle2[j].y, z2); + p->insert_vertex(circle1[j].x, circle1[j].y, z1); } if (r1 > 0) { p->append_poly(); for (int i=0; i<fragments; i++) - p->append_vertex(circle1[i].x, circle1[i].y, z1); + p->insert_vertex(circle1[i].x, circle1[i].y, z1); } if (r2 > 0) { p->append_poly(); for (int i=0; i<fragments; i++) - p->insert_vertex(circle2[i].x, circle2[i].y, z2); + p->append_vertex(circle2[i].x, circle2[i].y, z2); } } |