forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gui] GGUI 3/n: Add dependencies, interfaces, and backend-independent…
… code (taichi-dev#2650) * backend independent code * fix convo * fix convo * remove redundant additions to TAICHI_CORE_SOURCE * add submodules .. * fix convo
- Loading branch information
1 parent
0fa4c67
commit 2d149e1
Showing
16 changed files
with
859 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include "taichi/ui/utils/utils.h" | ||
#include "taichi/program/arch.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
struct AppConfig { | ||
std::string name; | ||
int width{0}; | ||
int height{0}; | ||
bool vsync{false}; | ||
std::string package_path; | ||
taichi::lang::Arch ti_arch; | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include "taichi/ui/utils/utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
enum class ProjectionMode : int { Perspective = 0, Orthogonal = 1 }; | ||
|
||
struct Camera { | ||
glm::vec3 position; | ||
glm::vec3 lookat; | ||
glm::vec3 up; | ||
ProjectionMode projection_mode = ProjectionMode::Perspective; | ||
|
||
float fov{45}; | ||
|
||
float left{-1}; | ||
float right{1}; | ||
float top{-1}; | ||
float bottom{1}; | ||
float z_near{0.1}; | ||
float z_far{1000}; | ||
|
||
glm::mat4 get_view_matrix() { | ||
return glm::lookAt(position, lookat, up); | ||
} | ||
glm::mat4 get_projection_matrix(float aspect_ratio) { | ||
if (projection_mode == ProjectionMode::Perspective) { | ||
return glm::perspective(fov, aspect_ratio, z_far, z_near); | ||
} else if (projection_mode == ProjectionMode::Orthogonal) { | ||
return glm::ortho(left, right, top, bottom, z_far, z_near); | ||
} else { | ||
throw std::runtime_error("invalid camera projection mode"); | ||
} | ||
} | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
#include "taichi/ui/common/field_info.h" | ||
#include "taichi/ui/common/scene_base.h" | ||
#include "taichi/ui/common/renderable_info.h" | ||
#include "taichi/ui/utils/utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
struct SetImageInfo { | ||
FieldInfo img; | ||
}; | ||
|
||
struct TrianglesInfo { | ||
RenderableInfo renderable_info; | ||
glm::vec3 color; | ||
}; | ||
|
||
struct CirclesInfo { | ||
RenderableInfo renderable_info; | ||
glm::vec3 color; | ||
float radius; | ||
}; | ||
|
||
struct LinesInfo { | ||
RenderableInfo renderable_info; | ||
glm::vec3 color; | ||
float width; | ||
}; | ||
|
||
class CanvasBase { | ||
public: | ||
virtual void set_background_color(const glm::vec3 &color) = 0; | ||
virtual void set_image(const SetImageInfo &info) = 0; | ||
virtual void triangles(const TrianglesInfo &info) = 0; | ||
virtual void circles(const CirclesInfo &info) = 0; | ||
virtual void lines(const LinesInfo &info) = 0; | ||
virtual void scene(SceneBase *scene) = 0; | ||
virtual ~CanvasBase() = default; | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
#include "taichi/ui/utils/utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
enum class EventType : int { Any = 0, Press = 1, Release = 2 }; | ||
|
||
struct Event { | ||
EventType tag; | ||
|
||
DEFINE_PROPERTY(std::string, key); | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
#include "taichi/ui/utils/utils.h" | ||
|
||
#include "taichi/ir/type_utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
enum class FieldSource : int { | ||
TaichiCuda = 0, | ||
TaichiX64 = 1, | ||
TaichiVulkan = 2, | ||
TaichiOpenGL = 3 | ||
// support np array / torch tensor in the future? | ||
}; | ||
|
||
enum class FieldType : int { Scalar = 0, Matrix = 1 }; | ||
|
||
struct FieldInfo { | ||
DEFINE_PROPERTY(bool, valid) | ||
DEFINE_PROPERTY(FieldType, field_type); | ||
DEFINE_PROPERTY(int, matrix_rows); | ||
DEFINE_PROPERTY(int, matrix_cols); | ||
DEFINE_PROPERTY(std::vector<int>, shape); | ||
DEFINE_PROPERTY(FieldSource, field_source); | ||
DEFINE_PROPERTY(taichi::lang::DataType, dtype); | ||
DEFINE_PROPERTY(uint64_t, data); | ||
|
||
FieldInfo() { | ||
valid = false; | ||
} | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
#include <string> | ||
#include "taichi/ui/utils/utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
class GuiBase { | ||
public: | ||
virtual void begin(std::string name, | ||
float x, | ||
float y, | ||
float width, | ||
float height) = 0; | ||
virtual void end() = 0; | ||
virtual void text(std::string text) = 0; | ||
virtual bool checkbox(std::string name, bool old_value) = 0; | ||
virtual float slider_float(std::string name, | ||
float old_value, | ||
float minimum, | ||
float maximum) = 0; | ||
virtual glm::vec3 color_edit_3(std::string name, glm::vec3 old_value) = 0; | ||
virtual bool button(std::string text) = 0; | ||
virtual ~GuiBase() = default; | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#pragma once | ||
#include <memory> | ||
#include <functional> | ||
#include <vector> | ||
#include "taichi/ui/utils/utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
class InputHandler { | ||
public: | ||
void key_callback(GLFWwindow *window, | ||
int key, | ||
int scancode, | ||
int action, | ||
int mode) { | ||
if (action == GLFW_PRESS) { | ||
keys_[key] = true; | ||
} else if (action == GLFW_RELEASE) { | ||
keys_[key] = false; | ||
} | ||
for (auto f : user_key_callbacks_) { | ||
f(key, action); | ||
} | ||
} | ||
|
||
void mouse_pos_callback(GLFWwindow *window, double xpos, double ypos) { | ||
if (first_mouse_) { | ||
last_x_ = xpos; | ||
last_y_ = ypos; | ||
first_mouse_ = false; | ||
} | ||
|
||
last_x_ = xpos; | ||
last_y_ = ypos; | ||
|
||
for (auto f : user_mouse_pos_callbacks_) { | ||
f(xpos, ypos); | ||
} | ||
} | ||
|
||
void mouse_button_callback(GLFWwindow *window, | ||
int button, | ||
int action, | ||
int modifier) { | ||
if (button == GLFW_MOUSE_BUTTON_LEFT) { | ||
if (action == GLFW_PRESS) { | ||
left_mouse_down_ = true; | ||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); | ||
} | ||
if (action == GLFW_RELEASE) { | ||
left_mouse_down_ = false; | ||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); | ||
} | ||
} | ||
if (action == GLFW_PRESS) { | ||
keys_[button] = true; | ||
} else if (action == GLFW_RELEASE) { | ||
keys_[button] = false; | ||
} | ||
for (auto f : user_mouse_button_callbacks_) { | ||
f(button, action); | ||
} | ||
} | ||
|
||
bool is_pressed(int key) { | ||
return keys_[key]; | ||
} | ||
|
||
float last_x() { | ||
return last_x_; | ||
} | ||
|
||
float last_y() { | ||
return last_y_; | ||
} | ||
|
||
void add_key_callback(std::function<void(int, int)> f) { | ||
user_key_callbacks_.push_back(f); | ||
} | ||
void add_mouse_pos_callback(std::function<void(double, double)> f) { | ||
user_mouse_pos_callbacks_.push_back(f); | ||
} | ||
void add_mouse_button_callback(std::function<void(int, int)> f) { | ||
user_mouse_button_callbacks_.push_back(f); | ||
} | ||
|
||
InputHandler() : keys_(1024, false) { | ||
} | ||
|
||
private: | ||
bool first_mouse_ = true; | ||
|
||
bool left_mouse_down_ = false; | ||
|
||
std::vector<bool> keys_; | ||
float last_x_ = 0; | ||
float last_y_ = 0; | ||
|
||
std::vector<std::function<void(int, int)>> user_key_callbacks_; | ||
std::vector<std::function<void(double, double)>> user_mouse_pos_callbacks_; | ||
std::vector<std::function<void(int, int)>> user_mouse_button_callbacks_; | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
#include "taichi/ui/common/field_info.h" | ||
#include "taichi/ui/utils/utils.h" | ||
|
||
TI_UI_NAMESPACE_BEGIN | ||
|
||
struct RenderableInfo { | ||
FieldInfo vertices; | ||
FieldInfo normals; | ||
FieldInfo tex_coords; | ||
FieldInfo per_vertex_color; | ||
FieldInfo indices; | ||
}; | ||
|
||
TI_UI_NAMESPACE_END |
Oops, something went wrong.