Cool BezierCurve demo (You can drag the endpoints around)
This commit is contained in:
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){}
|
||||
|
Reference in New Issue
Block a user