0

I'm trying to make a cube with rounded corners in OpenGL, much like a die if you were to sand down the corners, or one of those novelty stuffed chairs.

Bezier curves seem to be the best way to go about this to my understanding, but I'm sure how to stitch them together to form a cube like I want. I've had some success putting two Bezier curves together to make a "flat fish-like" triangular solid (see code below), but I always get some weird non-convex abomination whenever I try to extend it to a cube.

//Draw left side of fish
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsLeft[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);

//Draw right side of fish
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsRight[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);

/* uSteps and vSteps are defined elsewhere as constants - the number of steps to use in drawing    the surface. Likewise, the control point arrays are similarly defined elsewhere.*/

For the cube, specifically I am wondering how many control point arrays I should need to fully define it (I'm assuming I need 6 surfaces and thus 6 arrays, though if there is a simplification I can make to use less, please let me know), and how can I map them to form a cube shape?

Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • Would http://stackoverflow.com/questions/4250889/best-way-to-draw-a-cube-with-smooth-edges-bezier-curve-load-a-3ds-or-other be useful? – Mike 'Pomax' Kamermans Nov 26 '14 at 02:03
  • It might be easier to draw the edges as 1/4 of a cylinder, and the corners as 1/8 of a sphere. Particularly if you want to do the calculation of the vertices yourself, instead of using deprecated OpenGL features. – Reto Koradi Nov 26 '14 at 03:49
  • Distance fields are a great way to create cubes with rounded corners. Read up here : http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm – Lloyd Crawley Nov 26 '14 at 08:02
  • No help as of yet! I think the Bezier patch way is best, I just really can't get the geometry of the control points for this complicated shape. I'd be happy if it was just unit distance, but my efforts either end up a plate tectonic looking model or some assortment of weird shapes. – Rome_Leader Nov 27 '14 at 00:23
  • let me see if I can make some drawings to help me answer this as a pure Bezier patch answer... – Mike 'Pomax' Kamermans Nov 27 '14 at 04:22

1 Answers1

4

Let's look at a single face: if we want a rounded rectangle for that, we need to create a face with sixteen vertices: eight mesh points, and eight control points, arranged on-off-off-on for each corner.

Let's assume we want rounded corners of radius d. That's silly and it should be r but I drew it with the letter d so let's roll with it:

ewfwefw

We can find out where vertices p1 and p2 for each corner should go almost trivially, because they'll be d away from the (virtual) corner vertex. The placement of the off-mesh control points are a little more tricky, but we can simply rely on other people having done the heavy maths lifting for us: For a visually rounded corner, and a radius d (in this case), if point p1 is distance d away from the corner vertex, then control point c1 is distance (1 - 0.55228) * d away from the corner. I could have written 0.44772 but that 0.55228 is a magical value when it comes to using cubic Bezier curves to approximate quarter circles, so I'd rather keep it explicit (In your code, you probably want to use 0.44772 of course).

The trick is now that all six faces on your cube share their edge vertices, so you end up with a 32 vertex object. I'm not all too familiar with the OpenGL way of doing this, but if you know how to construct a Bezier patch object from vertices, this is how to determine the vertices you need to get your rounded-corner cube!

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153