Cool BezierCurve demo (You can drag the endpoints around)
This commit is contained in:
@@ -185,7 +185,14 @@ namespace JGL {
|
||||
void FillTexturedTriangle();
|
||||
void FillTexturedPolygon();
|
||||
void DrawPartialSprite();
|
||||
void DrawCubicBezierCurve();
|
||||
|
||||
|
||||
void DrawCubicBezierCurve(const Color4& color,
|
||||
const Vector2& controlA, const Vector2& pointA,
|
||||
const Vector2& pointB, const Vector2& controlB,
|
||||
int subdivisions = 10,
|
||||
float thickness = 1);
|
||||
|
||||
void OutlinePolygon (const Color4& color, std::vector<Vector2> points);
|
||||
void FillPolygon (const Color4& color, std::vector<Vector2> points, float thickness = 1);
|
||||
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1);
|
||||
@@ -207,7 +214,7 @@ namespace JGL {
|
||||
void FillTriangleMesh(const Color3& color, const TriangleMesh& mesh);
|
||||
void WireframeTriangleMesh(const Color3& color, const TriangleMesh& mesh, float thickness = 1);
|
||||
void DrawString(const Color4& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font);
|
||||
|
||||
void DrawSprite();
|
||||
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
|
||||
void DrawMatrixGizmo (const Matrix4x4&);
|
||||
void DrawAxisAngleGizmo (const AxisAngle&, const Vector3&);
|
||||
|
104
main.cpp
104
main.cpp
@@ -10,6 +10,66 @@
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL;
|
||||
|
||||
JGL::Font FreeSans;
|
||||
JGL::Font Jupiteroid;
|
||||
|
||||
class Gizmo
|
||||
{
|
||||
public:
|
||||
Gizmo() {}
|
||||
Gizmo(const Vector2& pos) : position(pos) {}
|
||||
bool dragging = false;
|
||||
bool hovered = false;
|
||||
Vector2 position;
|
||||
float range = 6.f;
|
||||
|
||||
void Grab() {
|
||||
if (hovered)
|
||||
{
|
||||
dragging = true;
|
||||
}
|
||||
}
|
||||
void Release() {
|
||||
dragging = false;
|
||||
}
|
||||
|
||||
void Update(const Vector2& mouse)
|
||||
{
|
||||
if (dragging)
|
||||
{
|
||||
position = position.Lerp(mouse, 0.25f);
|
||||
}
|
||||
|
||||
if (mouse.Distance(position) < range)
|
||||
{
|
||||
hovered = true;
|
||||
} else
|
||||
{
|
||||
hovered = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Draw()
|
||||
{
|
||||
if (dragging)
|
||||
{
|
||||
J2D::DrawPoint(Colors::White, position, 4.f);
|
||||
} else if (hovered)
|
||||
{
|
||||
J2D::DrawPoint(Colors::Reds::Crimson, position, 6.f);
|
||||
} else
|
||||
{
|
||||
J2D::DrawPoint(Colors::Reds::Salmon, position, 3.f);
|
||||
}
|
||||
|
||||
J2D::DrawString(Colors::White, std::format("{:.1f},{:.1f}", position.x, position.y), position.x, position.y, 1.f, 10, FreeSans);
|
||||
}
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
Texture* image;
|
||||
//The Re3D style base projection.
|
||||
std::vector<GLfloat> perspective(float fov, float aspect, float nearPlane, float farPlane) {
|
||||
@@ -61,8 +121,12 @@ struct point {
|
||||
GLfloat t;
|
||||
};
|
||||
|
||||
JGL::Font FreeSans;
|
||||
JGL::Font Jupiteroid;
|
||||
|
||||
|
||||
Gizmo a({250, 150});
|
||||
Gizmo b({200, 250});
|
||||
Gizmo c({350, 300});
|
||||
Gizmo d({450, 250});
|
||||
|
||||
class JGLDemoWindow : public ReWindow::RWindow
|
||||
{
|
||||
@@ -130,10 +194,27 @@ public:
|
||||
J2D::DrawString(Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid);
|
||||
J2D::DrawString(Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 16, 1,16, Jupiteroid);
|
||||
J2D::DrawString(Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 33, 1,16, Jupiteroid);
|
||||
|
||||
J2D::DrawCubicBezierCurve(Colors::Blues::CornflowerBlue,
|
||||
a.position,
|
||||
b.position,
|
||||
c.position,
|
||||
d.position
|
||||
, 20, 1.5f);
|
||||
|
||||
a.Draw();
|
||||
b.Draw();
|
||||
c.Draw();
|
||||
d.Draw();
|
||||
J2D::End();
|
||||
}
|
||||
|
||||
void OnRefresh(float elapsed) override {
|
||||
auto mouse = GetMouseCoordinates();
|
||||
a.Update(mouse);
|
||||
b.Update(mouse);
|
||||
c.Update(mouse);
|
||||
d.Update(mouse);
|
||||
display();
|
||||
int glError = glGetError();
|
||||
if (glError != GL_NO_ERROR)
|
||||
@@ -141,6 +222,25 @@ public:
|
||||
glSwapBuffers();
|
||||
}
|
||||
|
||||
|
||||
void OnMouseButtonDown(const ReWindow::WindowEvents::MouseButtonDownEvent & ev) override
|
||||
{
|
||||
RWindow::OnMouseButtonDown(ev);
|
||||
a.Grab();
|
||||
b.Grab();
|
||||
c.Grab();
|
||||
d.Grab();
|
||||
}
|
||||
|
||||
void OnMouseButtonUp(const ReWindow::WindowEvents::MouseButtonUpEvent & ev) override
|
||||
{
|
||||
RWindow::OnMouseButtonUp(ev);
|
||||
a.Release();
|
||||
b.Release();
|
||||
c.Release();
|
||||
d.Release();
|
||||
}
|
||||
|
||||
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {return true;}
|
||||
JGLDemoWindow() : ReWindow::RWindow() {}
|
||||
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height){}
|
||||
|
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