| 1 | /***** |
| 2 | * drawbezierpatch.cc |
| 3 | * Author: John C. Bowman |
| 4 | * |
| 5 | * Render a Bezier curve. |
| 6 | *****/ |
| 7 | |
| 8 | #include "bezierpatch.h" |
| 9 | #include "beziercurve.h" |
| 10 | |
| 11 | namespace camp { |
| 12 | |
| 13 | #ifdef HAVE_GL |
| 14 | |
| 15 | void BezierCurve::init(double res) |
| 16 | { |
| 17 | this->res=res; |
| 18 | res2=res*res; |
| 19 | |
| 20 | MaterialIndex=materialIndex; |
| 21 | } |
| 22 | |
| 23 | inline triple normal(triple bP, triple bPP) |
| 24 | { |
| 25 | return dot(bP,bP)*bPP-dot(bP,bPP)*bP; |
| 26 | } |
| 27 | |
| 28 | void BezierCurve::render(const triple *p, bool straight) |
| 29 | { |
| 30 | triple p0=p[0]; |
| 31 | triple p3=p[3]; |
| 32 | triple n0,n1; |
| 33 | |
| 34 | if(straight) { |
| 35 | n0=n1=triple(0.0,0.0,1.0); |
| 36 | } else { |
| 37 | triple p1=p[1]; |
| 38 | triple p2=p[2]; |
| 39 | |
| 40 | n0=normal(p1-p0,p0+p2-2.0*p1); |
| 41 | n1=normal(p3-p2,p3+p1-2.0*p2); |
| 42 | } |
| 43 | |
| 44 | GLuint i0=data.vertex(p0,n0); |
| 45 | GLuint i3=data.vertex(p3,n1); |
| 46 | |
| 47 | if(straight) { |
| 48 | std::vector<GLuint> &q=data.indices; |
| 49 | q.push_back(i0); |
| 50 | q.push_back(i3); |
| 51 | } else |
| 52 | render(p,i0,i3); |
| 53 | append(); |
| 54 | } |
| 55 | |
| 56 | // Use a uniform partition to draw a Bezier curve. |
| 57 | // p is an array of 4 triples representing the control points. |
| 58 | // Ii are the vertex indices. |
| 59 | void BezierCurve::render(const triple *p, GLuint I0, GLuint I1) |
| 60 | { |
| 61 | triple p0=p[0]; |
| 62 | triple p1=p[1]; |
| 63 | triple p2=p[2]; |
| 64 | triple p3=p[3]; |
| 65 | if(Straightness(p0,p1,p2,p3) < res2) { // Segment is flat |
| 66 | triple P[]={p0,p3}; |
| 67 | if(!offscreen(2,P)) { |
| 68 | std::vector<GLuint> &q=data.indices; |
| 69 | q.push_back(I0); |
| 70 | q.push_back(I1); |
| 71 | } |
| 72 | } else { // Segment is not flat |
| 73 | if(offscreen(4,p)) return; |
| 74 | triple m0=0.5*(p0+p1); |
| 75 | triple m1=0.5*(p1+p2); |
| 76 | triple m2=0.5*(p2+p3); |
| 77 | triple m3=0.5*(m0+m1); |
| 78 | triple m4=0.5*(m1+m2); |
| 79 | triple m5=0.5*(m3+m4); |
| 80 | |
| 81 | triple s0[]={p0,m0,m3,m5}; |
| 82 | triple s1[]={m5,m4,m2,p3}; |
| 83 | |
| 84 | triple n0=normal(bezierPh(p0,p1,p2,p3),bezierPPh(p0,p1,p2,p3)); |
| 85 | GLuint i0=data.vertex(m5,n0); |
| 86 | |
| 87 | render(s0,I0,i0); |
| 88 | render(s1,i0,I1); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #endif |
| 93 | |
| 94 | } //namespace camp |