diff options
-rw-r--r-- | example.scad | 6 | ||||
-rw-r--r-- | glview.cc | 47 | ||||
-rw-r--r-- | mainwin.cc | 12 | ||||
-rw-r--r-- | openscad.h | 4 | ||||
-rw-r--r-- | polyset.cc | 79 |
5 files changed, 114 insertions, 34 deletions
diff --git a/example.scad b/example.scad index 55fbd23..e37956c 100644 --- a/example.scad +++ b/example.scad @@ -84,4 +84,8 @@ module test006() } } -cylinder(h=5, r1=3, r2 = 10, center = true); +test006(); + +// cylinder(h=5, r1=3, r2 = 10, center = true); +// cube(10, center = true); +// sphere(5); @@ -22,7 +22,6 @@ #include <QWheelEvent> #include <QMouseEvent> -#include <GL/glew.h> #define FAR_FAR_AWAY 100000.0 @@ -49,32 +48,44 @@ extern GLint e1, e2, e3; void GLView::initializeGL() { - GLenum err = glewInit(); - if (GLEW_OK != err) { - fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); - } - glEnable(GL_DEPTH_TEST); glDepthRange(-FAR_FAR_AWAY, +FAR_FAR_AWAY); glClearColor(1.0, 1.0, 0.9, 0.0); +#ifdef ENABLE_OPENCSG + GLenum err = glewInit(); + if (GLEW_OK != err) { + fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); + } if (glewIsSupported("GL_VERSION_2_0")) { const char *vs_source = - "attribute vec3 tripos;\n" - "varying vec3 tp;\n" + "uniform float xscale, yscale;\n" + "attribute vec3 pos_b, pos_c;\n" + "attribute vec3 trig, mask;\n" + "varying vec3 tp, tr;\n" "void main() {\n" - " gl_Position = ftransform();\n" - " tp = tripos;\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" "}\n"; const char *fs_source = "uniform vec4 color1, color2;\n" - "varying vec3 tp;\n" + "varying vec3 tp, tr, tmp;\n" "void main() {\n" " gl_FragColor = color1;\n" - " if (tp.x > 0.95 || tp.y > 0.95 || tp.z > 0.95)\n" + " if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" " gl_FragColor = color2;\n" "}\n"; @@ -94,7 +105,12 @@ void GLView::initializeGL() shaderinfo[0] = edgeshader_prog; shaderinfo[1] = glGetUniformLocation(edgeshader_prog, "color1"); shaderinfo[2] = glGetUniformLocation(edgeshader_prog, "color2"); - shaderinfo[3] = glGetAttribLocation(edgeshader_prog, "tripos"); + 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) { @@ -124,10 +140,15 @@ void GLView::initializeGL() } else { fprintf(stdout, "GLEW: GL_VERSION_2_0 is not supported!\n"); } +#endif /* ENABLE_OPENCSG */ } 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); } @@ -511,6 +511,17 @@ static void renderGLThrownTogether(void *vp) MainWindow *m = (MainWindow*)vp; if (m->root_chain) { glDepthFunc(GL_LEQUAL); +#if 0 + glUseProgram(m->screen->shaderinfo[0]); + 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->screen->shaderinfo); + } else { + m->root_chain->polysets[i]->render_surface(PolySet::COLOR_MATERIAL, m->screen->shaderinfo); + } + } + glUseProgram(0); +#else 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); @@ -520,6 +531,7 @@ static void renderGLThrownTogether(void *vp) m->root_chain->polysets[i]->render_edges(PolySet::COLOR_MATERIAL); } } +#endif } } @@ -426,9 +426,9 @@ public: double viewer_distance; double object_rot_y; double object_rot_z; - double w_h_ratio; - GLint shaderinfo[10]; + double w_h_ratio; + GLint shaderinfo[11]; GLView(QWidget *parent = NULL); @@ -41,43 +41,86 @@ void PolySet::insert_vertex(double x, double y, double z) polygons.last().insert(0, Point(x, y, z)); } +static void gl_draw_triangle(GLint *shaderinfo, const PolySet::Point *p0, const PolySet::Point *p1, const PolySet::Point *p2, bool e0, bool e1, bool e2) +{ +#ifdef ENABLE_OPENCSG + if (shaderinfo) { + double e0f = e0 ? 2.0 : -1.0; + double e1f = e1 ? 2.0 : -1.0; + double e2f = e2 ? 2.0 : -1.0; + glVertexAttrib3d(shaderinfo[3], e0f, e1f, e2f); + glVertexAttrib3d(shaderinfo[4], p1->x, p1->y, p1->z); + glVertexAttrib3d(shaderinfo[5], p2->x, p2->y, p2->z); + glVertexAttrib3d(shaderinfo[6], 0.0, 1.0, 0.0); + glVertex3d(p0->x, p0->y, p0->z); + glVertexAttrib3d(shaderinfo[3], e0f, e1f, e2f); + glVertexAttrib3d(shaderinfo[4], p0->x, p0->y, p0->z); + glVertexAttrib3d(shaderinfo[5], p2->x, p2->y, p2->z); + glVertexAttrib3d(shaderinfo[6], 0.0, 0.0, 1.0); + glVertex3d(p1->x, p1->y, p1->z); + glVertexAttrib3d(shaderinfo[3], e0f, e1f, e2f); + glVertexAttrib3d(shaderinfo[4], p0->x, p0->y, p0->z); + glVertexAttrib3d(shaderinfo[5], p1->x, p1->y, p1->z); + glVertexAttrib3d(shaderinfo[6], 1.0, 0.0, 0.0); + glVertex3d(p2->x, p2->y, p2->z); + } + else +#endif + { + glVertex3d(p0->x, p0->y, p0->z); + glVertex3d(p1->x, p1->y, p1->z); + glVertex3d(p2->x, p2->y, p2->z); + } +} + void PolySet::render_surface(colormode_e colormode, GLint *shaderinfo) const { if (colormode == COLOR_MATERIAL) { glColor3ub(249, 215, 44); +#ifdef ENABLE_OPENCSG if (shaderinfo) { glUniform4f(shaderinfo[1], 249 / 255.0, 215 / 255.0, 44 / 255.0, 1.0); glUniform4f(shaderinfo[2], 255 / 255.0, 236 / 255.0, 94 / 255.0, 1.0); } +#endif /* ENABLE_OPENCSG */ } if (colormode == COLOR_CUTOUT) { glColor3ub(157, 203, 81); +#ifdef ENABLE_OPENCSG if (shaderinfo) { glUniform4f(shaderinfo[1], 157 / 255.0, 203 / 255.0, 81 / 255.0, 1.0); glUniform4f(shaderinfo[2], 171 / 255.0, 216 / 255.0, 86 / 255.0, 1.0); } +#endif /* ENABLE_OPENCSG */ + } +#ifdef ENABLE_OPENCSG + if (shaderinfo) { + glUniform1f(shaderinfo[7], shaderinfo[9]); + glUniform1f(shaderinfo[8], shaderinfo[10]); } +#endif /* ENABLE_OPENCSG */ for (int i = 0; i < polygons.size(); i++) { const Polygon *poly = &polygons[i]; - glBegin(GL_TRIANGLES); - for (int j = 2; j < poly->size(); j++) { - const Point *p0 = &poly->at(0); - const Point *p1 = &poly->at(j-1); - const Point *p2 = &poly->at(j); - if (shaderinfo) { - double e0 = j == 2 ? 1.0 : 0.0; - double e2 = j == poly->size()-1 ? 1.0 : 0.0; - glVertexAttrib3d(shaderinfo[3], e0, 0.0, e2); - glVertex3d(p0->x, p0->y, p0->z); - glVertexAttrib3d(shaderinfo[3], e0, 1.0, 0.0); - glVertex3d(p1->x, p1->y, p1->z); - glVertexAttrib3d(shaderinfo[3], 0.0, 1.0, e2); - glVertex3d(p2->x, p2->y, p2->z); - } else { - glVertex3d(p0->x, p0->y, p0->z); - glVertex3d(p1->x, p1->y, p1->z); - glVertex3d(p2->x, p2->y, p2->z); + if (poly->size() == 3) { + gl_draw_triangle(shaderinfo, &poly->at(0), &poly->at(1), &poly->at(2), true, true, true); + } + else if (poly->size() == 4) { + gl_draw_triangle(shaderinfo, &poly->at(0), &poly->at(1), &poly->at(3), true, false, true); + gl_draw_triangle(shaderinfo, &poly->at(2), &poly->at(3), &poly->at(1), true, false, true); + } + else { + Point center; + for (int j = 0; j < poly->size(); j++) { + center.x += poly->at(j).x; + center.y += poly->at(j).y; + center.z += poly->at(j).z; + } + center.x /= poly->size(); + center.y /= poly->size(); + center.z /= poly->size(); + for (int j = 1; j <= poly->size(); j++) { + gl_draw_triangle(shaderinfo, ¢er, &poly->at(j - 1), &poly->at(j % poly->size()), false, true, false); } } glEnd(); |