You directly evaluate the Bezier basis functions. While this works most of the time, it's not numerically stable, i.e. for some values the calculations can go wrong (caused mainly by the the binomial coefficients). Luckily, there is an easy to understand and numerically stable algorithm: De Casteljau's Algorithm.
While straightforward, the rendering algorithm is suboptimal. A better solution is recursive subdivision. Usually about 3 or 4 recursion steps produce good looking curves. You can also stop recursion if the current segment is "suffieciently smooth". You can check for that using the second forward differences.
Side note: LÖVE 0.9 will introduce a BezierCurve object.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.
I've implemented a new version using de Casteljau's Algorithm, which was very, very slow at first. Then I used the subdivision algorithm and it's still very slow. I will check again after getting some sleep - maybe I missed something.
The problem with the recursion seems to be that it very quickly grows in computation time when adding more points.
I think it would be no Problem if I only allow cubic Beziers (and automatically split everything above cubic into cubic-and-less Beziers)... I'll have to fiddle with it.
Roland, thanks a lot for the tests! 39 FPS is too low, but then again this is not optimized. Also, the final program will probably not need curves with more than 6 points...
I've reworked the rendering algorithm according to vrld's suggestions.
It's now much more configurable (dynamically chooses the number of segments according to a maximum-segment-length setting) and adding more points does not increase the number of rendering points as much any more. Works for me until up to 150 construction points!
I am finally getting somewhere. I'm not posting a .love for every change, if you're interested just get it from github.
Added:
Real time lighting (used this tutorial and shamelessly ripped most of the shader code from there)
Auto-generated normalmap, diffusemap and specular maps
Multiple materials (Feel free to add your own. If they're cool I might include them )*
Duplicate and flip shapes for easy symmetry
Move shapes up and down in the layers
* To make a material, simply copy and paste the metal.lua inside the Material subfolder. Then change it around. the profile function and specular functions give the normals and spec-shading at the edge of a shape. So returning 0,0,1 for the profile function just gives you a flat surface, while returning 1, 0, 0.1 gives you sharp edges all pointing left. Dir is the default normal of the edge, amount is the distance from the edge (ranging from 0 to 1).
Perfectly acceptable, you posted new, relevant information!
Real time lighting (used this tutorial and shamelessly ripped most of the shader code from there)
Thanks so much for this link.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Last edited by LoneArtisan on Mon May 26, 2014 8:36 pm, edited 1 time in total.
You know that moment when you finally do that thing you've been trying to accomplish for hours and you finally get it exactly right? No matter how small the task was, it's one of the greatest things that could happen when you're programming!