Cool BezierCurve demo (You can drag the endpoints around)
This commit is contained in:
38
src/JGL.cpp
38
src/JGL.cpp
@@ -6,6 +6,7 @@
|
||||
#include <glad/glad.h>
|
||||
#include <Color3.hpp>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include "J3ML/Algorithm/Bezier.h"
|
||||
|
||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||
GLfloat baseColor[4] = {1, 1, 1, 1};
|
||||
@@ -498,6 +499,43 @@ namespace JGL {
|
||||
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void J2D::DrawCubicBezierCurve(const Color4 &color, const J3ML::LinearAlgebra::Vector2 &controlA,
|
||||
const J3ML::LinearAlgebra::Vector2 &pointA,
|
||||
const J3ML::LinearAlgebra::Vector2 &pointB,
|
||||
const J3ML::LinearAlgebra::Vector2 &controlB, int subdivisions, float thickness) {
|
||||
|
||||
|
||||
|
||||
|
||||
for (int k = 2; k < subdivisions; ++k)
|
||||
{
|
||||
Vector2 last = controlA;
|
||||
Vector2 first = controlB;
|
||||
for (int i = 0; i < k; ++i)
|
||||
{
|
||||
float alpha = (float)i / (float)k;
|
||||
Vector2 step = J3ML::Algorithm::Bezier(alpha, controlA, pointA, pointB, controlB);
|
||||
DrawLine(color, last, step, thickness);
|
||||
last = step;
|
||||
}
|
||||
|
||||
// Manually draw the last segment of the curve
|
||||
DrawLine(color, last, first, thickness);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Display control points
|
||||
DrawPoint(Colors::Red, controlA, 2.f);
|
||||
DrawPoint(Colors::Red, controlB, 2.f);
|
||||
DrawPoint(Colors::Reds::Salmon, pointA, 2.f);
|
||||
DrawPoint(Colors::Reds::Salmon, pointB, 2.f);
|
||||
|
||||
}
|
||||
|
||||
void J3D::Begin() {
|
||||
|
||||
//Get what the draw color was before we did anything.
|
||||
|
Reference in New Issue
Block a user