Skip to content

Commit

Permalink
Split BezierCurver render and draw operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncbowman committed Jan 21, 2017
1 parent ee78ea2 commit 1d32440
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 20 deletions.
94 changes: 94 additions & 0 deletions beziercurve.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*****
* beziercurve.h
* Author: John C. Bowman
*
* Render a Bezier curve.
*****/

#include "drawsurface.h"

namespace camp {

#ifdef HAVE_GL

static const double pixel=0.5; // Adaptive rendering constant.

extern const double Fuzz;
extern const double Fuzz2;

// return the maximum perpendicular distance squared of points c0 and c1
// from z0--z1.
inline double Distance1(const triple& z0, const triple& c0,
const triple& c1, const triple& z1)
{
triple Z0=c0-z0;
triple Q=unit(z1-z0);
triple Z1=c1-z0;
return max(abs2(Z0-dot(Z0,Q)*Q),abs2(Z1-dot(Z1,Q)*Q));
}

struct BezierCurve
{
static std::vector<GLfloat> buffer;
static std::vector<GLint> indices;
triple u,v,w;
GLuint nvertices;
double epsilon;
double res,res2;
triple Min,Max;

BezierCurve() : nvertices(0) {}

void init(double res, const triple& Min, const triple& Max);

// Store the vertex v and its normal vector n in the buffer.
GLuint vertex(const triple &v) {
buffer.push_back(v.getx());
buffer.push_back(v.gety());
buffer.push_back(v.getz());
return nvertices++;
}

// Approximate bounds by bounding box of control polyhedron.
bool offscreen(size_t n, const triple *v) {
double x,y,z;
double X,Y,Z;

boundstriples(x,y,z,X,Y,Z,4,v);
return
X < Min.getx() || x > Max.getx() ||
Y < Min.gety() || y > Max.gety() ||
Z < Min.getz() || z > Max.getz();
}

void render(const triple *p, GLuint I0, GLuint I1);
void render(const triple *p, bool straight);

void clear() {
nvertices=0;
buffer.clear();
indices.clear();
}

~BezierCurve() {
clear();
}

void draw();

void render(const triple *g, bool straight, double ratio,
const triple& Min, const triple& Max) {
init(pixel*ratio,Min,Max);
render(g,straight);
}

void draw(const triple *g, bool straight, double ratio,
const triple& Min, const triple& Max) {
render(g,straight,ratio,Min,Max);
draw();
}
};

#endif

} //namespace camp
35 changes: 15 additions & 20 deletions drawpath3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ namespace camp {
using vm::array;
using namespace prc;

#ifdef HAVE_GL
void bezierCurve(const triple *g, bool straight, double ratio,
const triple& Min, const triple& Max);
#endif

bool drawPath3::write(prcfile *out, unsigned int *, double, groupsmap&)
{
Int n=g.length();
Expand Down Expand Up @@ -100,22 +95,22 @@ void drawPath3::render(GLUnurbs *nurb, double size2,
glMaterialfv(GL_FRONT,GL_SPECULAR,Black);
glMaterialf(GL_FRONT,GL_SHININESS,128.0);

if(billboard) BB.init(center);

for(Int i=0; i < n; ++i) {
triple controls[]={g.point((Int) i),g.postcontrol((Int) i),
g.precontrol((Int) i+1),g.point((Int) i+1)};
triple *Controls;
triple C[4];
if(billboard) {
Controls=C;
for(size_t i=0; i < 4; i++)
Controls[i]=BB.transform(controls[i]);
} else
Controls=controls;

bezierCurve(Controls,straight,size3.length()/size2,m,M);
if(billboard) {
for(Int i=0; i < n; ++i) {
triple controls[]={BB.transform(g.point(i)),BB.transform(g.postcontrol(i)),
BB.transform(g.precontrol(i+1)),
BB.transform(g.point(i+1))};
R.render(controls,straight,size3.length()/size2,m,M);
}
} else {
BB.init(center);
for(Int i=0; i < n; ++i) {
triple controls[]={g.point(i),g.postcontrol(i),g.precontrol(i+1),
g.point(i+1)};
R.render(controls,straight,size3.length()/size2,m,M);
}
}
R.draw();
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions drawpath3.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

#include "drawelement.h"
#include "path3.h"
#include "beziercurve.h"

namespace camp {

class drawPath3 : public drawElement {
protected:
BezierCurve R;
const path3 g;
triple center;
bool straight;
Expand Down

0 comments on commit 1d32440

Please sign in to comment.