Skip to content

Commit

Permalink
colors and graph interactivity;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 25, 2024
1 parent 786ef7c commit 902623b
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 55 deletions.
5 changes: 4 additions & 1 deletion src/calculator/renderer/VulkanWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ namespace ui

void wheelEvent(QWheelEvent *ev) override
{
// this->scene->camera.resolution += (float)ev->angleDelta().y();
float angle = ev->angleDelta().y();
float direction = angle / std::abs(angle);

this->camera.TranslateWorld({0, 0, 5.f * direction});
}

void resizeEvent(QResizeEvent *ev) override
Expand Down
5 changes: 3 additions & 2 deletions src/calculator/resource/shaders/grid.frag
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ void main() {
float dist = camera.location.z;
float alpha = (180.f - camera.fov) / 2.f;
float base = (dist / tan(alpha * (M_PI / 180.f))) * 2;
float aspect = camera.window.x / camera.window.y;
vec2 window = camera.window * camera.dpi;
float aspect = window.x / window.y;

vec2 world = vec2(base * aspect, base);

vec2 st = (gl_FragCoord.xy / camera.window) * world;
vec2 st = (((gl_FragCoord.xy - window / 2) / window) * world) + camera.location.xy;
vec4 color = vec4(1.0, 1.0, 1.0, 1.0);

if (centerline(st, 0.2)) {
Expand Down
12 changes: 0 additions & 12 deletions src/calculator/resource/shaders/grid.vert
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#version 450

layout (binding = 0) uniform Camera {
float dpi;
float fov;

vec2 location;
vec2 window;

mat4 model;
mat4 view;
mat4 projection;
} camera;

layout (location = 0) in vec2 in_position;
layout (location = 1) in vec4 in_color;

Expand Down
4 changes: 2 additions & 2 deletions src/calculator/resource/shaders/plot.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ layout (binding = 0) uniform Camera {
float dpi;
float fov;

vec2 location;
vec3 location;
vec2 window;

mat4 model;
Expand All @@ -18,6 +18,6 @@ layout (location = 1) in vec4 in_color;
layout (location = 0) out vec4 out_color;

void main() {
gl_Position = camera.projection * camera.view * camera.model * vec4(in_position, 0.0, 1.0);
gl_Position = camera.projection * camera.view * camera.model * vec4(in_position.x, in_position.y * -1, 0.0, 1.0);
out_color = in_color;
}
23 changes: 13 additions & 10 deletions src/compiler/std/StandardLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ using namespace unlogic;

extern "C"
{
double unlogic_std_pow(double base, double exponent) { return std::pow(base, exponent); }
double unlogic_std_pow(double base, double exponent)
{
return std::pow(base, exponent);
}

void unlogic_std_log(char const *message)
{
Expand All @@ -20,25 +23,25 @@ extern "C"

Library unlogic::stdlib("stdlib");

LibrarySymbol std_pow(stdlib, "unlogic_std_pow", (void*)unlogic_std_pow, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
LibrarySymbol std_pow(stdlib, "pow", (void *)unlogic_std_pow, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
std::array args = {
llvm::Type::getDoubleTy(ctx),
llvm::Type::getDoubleTy(ctx),
llvm::Type::getDoubleTy(ctx),
llvm::Type::getDoubleTy(ctx),
};
llvm::Type *ret = llvm::Type::getDoubleTy(ctx);

llvm::FunctionType *fn = llvm::FunctionType::get(ret, args, false);
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_std_pow", mod);
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "pow", mod);
});

LibrarySymbol std_log(stdlib, "unlogic_std_log", (void*)unlogic_std_log, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
std::array<llvm::Type*, 1> args = {
llvm::PointerType::getInt8Ty(ctx),
LibrarySymbol std_log(stdlib, "log", (void *)unlogic_std_log, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
std::array<llvm::Type *, 1> args = {
llvm::PointerType::getInt8Ty(ctx),
};
llvm::Type *ret = llvm::Type::getVoidTy(ctx);

llvm::FunctionType *fn = llvm::FunctionType::get(ret, args, false);
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_std_log", mod);
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "log", mod);
});

/*
Expand All @@ -48,4 +51,4 @@ UNLOGIC_DEFINE_LIBFUNCTION(stdlib, cos, std::cos, UNLOGIC_ARGS(double));
UNLOGIC_DEFINE_LIBFUNCTION(stdlib, tan, std::tan, UNLOGIC_ARGS(double));
UNLOGIC_DEFINE_LIBFUNCTION(stdlib, sqrt, std::sqrt, UNLOGIC_ARGS(double));
UNLOGIC_DEFINE_LIBFUNCTION(stdlib, log, std::log, UNLOGIC_ARGS(double));
*/
*/
2 changes: 1 addition & 1 deletion src/compiler/transformer/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void unlogic::IRGenerator::Visit(unlogic::PotentiationNode const *node)
llvm::Value *rhs = this->values.top();
this->values.pop();

llvm::Function *std_pow = this->ctx.module->getFunction("unlogic_std_pow");
llvm::Function *std_pow = this->ctx.module->getFunction("pow");

llvm::Value *value = this->builder.CreateCall(std_pow, {lhs, rhs}, "powtmp");
this->values.push(value);
Expand Down
33 changes: 17 additions & 16 deletions src/graphic/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace unlogic
/**
* Location of camera in WORLD.
*/
alignas(16) glm::vec3 location = {0.f, 0.f, 10.0f};
alignas(16) glm::vec3 location = {0.f, 0.f, 20.0f};
alignas(8) glm::vec2 window = {0.f, 0.f};

alignas(16) glm::mat4 model = glm::mat4(1.f);
Expand All @@ -29,10 +29,12 @@ namespace unlogic
protected:
void CalculateMVP()
{
this->location.z = glm::clamp(this->location.z, 5.f, 100.f);

// Plots come out of function in world space already.
this->model = glm::mat4(1.f);

this->view = glm::lookAt(this->location, {0.f, 0.f, 0.f}, {0.f, -1.f, 0.f});
this->view = glm::lookAt(this->location, {this->location.x, this->location.y, 0.f}, {0.f, 1.f, 0.f});

if (this->window.y != 0)
{
Expand All @@ -58,11 +60,20 @@ namespace unlogic
return this->window;
}

void TranslatePixel(glm::vec2 const &distance) {}
void TranslatePixel(glm::vec2 const &distance, float d = 0.f)
{
auto world_translation = glm::vec3((distance / this->window) * this->WorldDimensionsAtDepth(d), 0.f) * -1.f;
this->location = glm::translate(glm::mat4(1.f), world_translation) * glm::vec4(this->location, 1.f);
this->CalculateMVP();
}

void TranslateWorld(glm::vec2 const &distance) {}
void TranslateWorld(glm::vec3 const &translation)
{
this->location = glm::translate(glm::mat4(1.f), translation) * glm::vec4(this->location, 1.f);
this->CalculateMVP();
}

[[nodiscard]] glm::vec2 WorldDimensionsAtDepth(float d = 0.f)
[[nodiscard]] glm::vec2 WorldDimensionsAtDepth(float d = 0.f) const
{
float distance = this->location.z + d;
float alpha = (180.f - this->fov) / 2;
Expand All @@ -74,17 +85,7 @@ namespace unlogic
};
}

[[nodiscard]] glm::vec3 ScreenToWorldCoordinate(glm::vec3 const &screen) const
{
glm::vec4 viewport = {
0.f,
0.f,
this->window.x,
this->window.y,
};

return glm::unProject(screen, this->view * this->model, this->projection, viewport);
}
[[nodiscard]] glm::vec3 ScreenToWorldCoordinate(glm::vec3 const &screen) const {}

[[nodiscard]] glm::vec3 WorldToScreenCoordinate(glm::vec3 const &world) const {}
};
Expand Down
15 changes: 10 additions & 5 deletions src/graphic/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

using namespace unlogic;

Color Color::White = { 1.0, 1.0, 1.0 };
Color Color::Black = { 0.0, 0.0, 0.0 };
Color Color::Red = { 1.0, 0.0, 0.0 };
Color Color::Green = { 0.0, 1.0, 0.0 };
Color Color::Blue = { 0.0, 0.0, 1.0 };
Color Color::White = {1.0, 1.0, 1.0};
Color Color::Black = {0.0, 0.0, 0.0};
Color Color::Red = {1.0, 0.0, 0.0};
Color Color::Orange = {1.0, 0.5, 0.0};
Color Color::Yellow = {1.0, 1.0, 0.0};
Color Color::Green = {0.0, 1.0, 0.0};
Color Color::Blue = {0.0, 0.0, 1.0};
Color Color::Cyan = {0.0, 1.0, 1.0};
Color Color::Purple = {0.5, 0.0, 1.0};
Color Color::Pink = {1.0, 0.0, 0.5};
9 changes: 7 additions & 2 deletions src/graphic/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ namespace unlogic
static Color White;
static Color Black;
static Color Red;
static Color Orange;
static Color Yellow;
static Color Green;
static Color Blue;
static Color Cyan;
static Color Purple;
static Color Pink;
};
}
} // namespace unlogic

#endif //COLOR_H
#endif // COLOR_H
7 changes: 5 additions & 2 deletions src/graphic/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <vector>
#include <memory>
#include <glm/glm.hpp>
#include "Camera.h"
#include "VertexBuffer.h"
#include "Color.h"
#include "shape/Plot.h"

Expand All @@ -27,6 +25,11 @@ namespace unlogic
Color::Red,
Color::Green,
Color::Blue,
Color::Orange,
Color::Yellow,
Color::Cyan,
Color::Pink,
Color::Purple,
};
Color color = colors[std::rand() % colors.size()];

Expand Down
2 changes: 1 addition & 1 deletion src/graphic/shape/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Plot2d::Plot2d(std::string name, Plot2dFunctionType fn, Color color) : name_(std

auto const &point = points[i];

this->vertices.push_back({glm::vec2(point.x - tx, point.y + ty), this->color_}); // a2
this->vertices.push_back({glm::vec2(point.x + tx, point.y - ty), this->color_}); // a1
this->vertices.push_back({glm::vec2(point.x - tx, point.y + ty), this->color_}); // a2
}
}
2 changes: 1 addition & 1 deletion src/graphic/shape/Plot.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace unlogic
glm::vec2 range_ = {-10.f, 10.f};

float precision_ = 0.1f;
float line_thickness_ = 0.05f;
float line_thickness_ = 0.2f;

public:
Plot2d(std::string name, Plot2dFunctionType fn, Color color);
Expand Down

0 comments on commit 902623b

Please sign in to comment.