Skip to content

Commit

Permalink
add big xyz scale handler
Browse files Browse the repository at this point in the history
  • Loading branch information
miyanyan committed Jul 25, 2023
1 parent de30b0a commit 6f56b48
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
8 changes: 4 additions & 4 deletions zenovis/include/zenovis/bate/GraphicHandlerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ void drawSquare(vec3f pos, vec3f a, vec3f b, vec3f color, float size, std::uniqu

void drawCircle(vec3f pos, vec3f a, vec3f b, vec3f color, float size, std::unique_ptr<Buffer> &vbo) {
constexpr int point_num = 100;
constexpr double step = 1.0 / point_num;
std::vector<vec3f> mem;
mem.reserve(point_num * 2);

double step = 1.0 / point_num;
for (double t = 0; t < 1.0; t += step) {
double theta = 2.0 * PI * t;
auto p = pos + size * cos(theta) * a + size * sin(theta) * b;
Expand All @@ -134,7 +134,7 @@ void drawCircle(vec3f pos, vec3f a, vec3f b, vec3f color, float size, std::uniqu
vbo->attribute(0, sizeof(float) * 0, sizeof(float) * 6, GL_FLOAT, 3);
vbo->attribute(1, sizeof(float) * 3, sizeof(float) * 6, GL_FLOAT, 3);

CHECK_GL(glDrawArrays(GL_LINE_STRIP, 0, vertex_count));
CHECK_GL(glDrawArrays(GL_LINE_LOOP, 0, vertex_count));

vbo->disable_attribute(0);
vbo->disable_attribute(1);
Expand All @@ -143,13 +143,13 @@ void drawCircle(vec3f pos, vec3f a, vec3f b, vec3f color, float size, std::uniqu

void drawCircle(vec3f pos, vec3f a, vec3f b, vec3f color, float size, float width, std::unique_ptr<Buffer> &vbo) {
constexpr int point_num = 100;
constexpr double step = 1.0 / point_num;
std::vector<vec3f> mem;
mem.reserve(point_num * 4);

float inner_size = size - width / 2.0;
float outer_size = size + width / 2.0;
double step = 1.0 / point_num;
for (double t = 0; t < 1.0; t += step) {
for (double t = 0; t < 1.0 + step; t += step) {
double theta = 2.0 * PI * t;
auto p1 = pos + inner_size * cos(theta) * a + inner_size * sin(theta) * b;
auto p2 = pos + outer_size * cos(theta) * a + outer_size * sin(theta) * b;
Expand Down
78 changes: 54 additions & 24 deletions zenovis/src/bate/GraphicScaleHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ struct ScaleHandler final : IGraphicHandler {
std::unique_ptr<Buffer> lines_ebo;
size_t lines_count;

static constexpr float cube_factor = 0.08f;
static constexpr float axis_factor = 0.8f;
static constexpr float square_factor = 0.1f;
static constexpr float icircle_factor = 0.2f; // inner circle
static constexpr float ocircle_factor = 1.0f; // outer circle

explicit ScaleHandler(Scene *scene_, vec3f &center_, float scale_)
: scene(scene_), center(center_), scale(scale_), mode(INTERACT_NONE) {
vbo = std::make_unique<Buffer>(GL_ARRAY_BUFFER);
Expand All @@ -88,6 +94,7 @@ struct ScaleHandler final : IGraphicHandler {
float theta = scene->camera->m_zxx.theta;
float phi = scene->camera->m_zxx.phi;
float radius = scene->camera->m_zxx.radius;
const auto& view = scene->camera->m_view;

auto camera_center = glm::vec3(cx, cy, cz);
float cos_t = glm::cos(theta), sin_t = glm::sin(theta);
Expand All @@ -113,53 +120,63 @@ struct ScaleHandler final : IGraphicHandler {
auto y_axis = vec3f(0, 1, 0);
auto z_axis = vec3f(0, 0, 1);

auto axis_size = bound * axis_factor;
auto cube_size = bound * cube_factor;
auto square_size = bound * square_factor;
auto icircle_size = bound * icircle_factor;
auto ocircle_size = bound * ocircle_factor;

// x axis
if (mode == INTERACT_NONE || mode == INTERACT_X) {
drawAxis(center, x_axis, r, bound, vbo);
drawCube(center + bound * x_axis, y_axis, z_axis, r, bound * 0.08f, vbo);
drawAxis(center, x_axis, r, axis_size, vbo);
drawCube(center + axis_size * x_axis, y_axis, z_axis, r, cube_size, vbo);
}

if (mode == INTERACT_NONE || mode == INTERACT_Y) {
drawAxis(center, y_axis, g, bound, vbo);
drawCube(center + bound * y_axis, z_axis, x_axis, g, bound * 0.08f, vbo);
drawAxis(center, y_axis, g, axis_size, vbo);
drawCube(center + axis_size * y_axis, z_axis, x_axis, g, cube_size, vbo);
}

if (mode == INTERACT_NONE || mode == INTERACT_Z) {
drawAxis(center, z_axis, b, bound, vbo);
drawCube(center + bound * z_axis, x_axis, y_axis, b, bound * 0.08f, vbo);
drawAxis(center, z_axis, b, axis_size, vbo);
drawCube(center + axis_size * z_axis, x_axis, y_axis, b, cube_size, vbo);
}

if (mode == INTERACT_NONE || mode == INTERACT_YZ)
drawSquare(center, y_axis, z_axis, {0.6, 0.2, 0.2}, bound * 0.1f, vbo);
drawSquare(center, y_axis, z_axis, {0.6, 0.2, 0.2}, square_size, vbo);

if (mode == INTERACT_NONE || mode == INTERACT_XZ)
drawSquare(center, z_axis, x_axis, {0.2, 0.6, 0.2}, bound * 0.1f, vbo);
drawSquare(center, z_axis, x_axis, {0.2, 0.6, 0.2}, square_size, vbo);

if (mode == INTERACT_NONE || mode == INTERACT_XY)
drawSquare(center, x_axis, y_axis, {0.2, 0.2, 0.6}, bound * 0.1f, vbo);

if (mode == INTERACT_NONE || mode == INTERACT_XYZ)
drawCube(center, y_axis, z_axis, {0.51, 0.17, 0.85}, bound * 0.08f, vbo);
drawSquare(center, x_axis, y_axis, {0.2, 0.2, 0.6}, square_size, vbo);

if (mode == INTERACT_NONE || mode == INTERACT_XYZ) {
// http://www.opengl-tutorial.org/cn/intermediate-tutorials/billboards-particles/billboards/
// always face camera
// This is equivalent to mlutiplying (1,0,0) and (0,1,0) by inverse(ViewMatrix).
// ViewMatrix is orthogonal (it was made this way),
// so its inverse is also its transpose,
// and transposing a matrix is "free" (inversing is slooow)
auto right_world = vec3f(view[0][0], view[1][0], view[2][0]);
auto up_world = vec3f(view[0][1], view[1][1], view[2][1]);
drawCircle(center, right_world, up_world, { 1.0, 1.0, 1.0 }, icircle_size, bound * 0.01f, vbo);
drawCircle(center, right_world, up_world, { 1.0, 1.0, 1.0 }, ocircle_size, bound * 0.01f, vbo);
}
}

virtual int collisionTest(glm::vec3 ori, glm::vec3 dir) override {
auto x_axis = glm::vec3(1, 0, 0);
auto y_axis = glm::vec3(0, 1, 0);
auto z_axis = glm::vec3(0, 0, 1);

auto model_matrix =glm::translate(zeno::vec_to_other<glm::vec3>(center));

// xyz handler
float cube_size = bound * 0.08f; float t;
auto xyz_handler_max = cube_size * glm::vec3(1.0f);
auto xyz_handler_min = -xyz_handler_max;
if (rayIntersectOBB(ori, dir, xyz_handler_min, xyz_handler_max, model_matrix, t)) {
mode = INTERACT_XYZ;
return INTERACT_XYZ;
}
auto model_matrix = glm::translate(zeno::vec_to_other<glm::vec3>(center));
const auto& view = scene->camera->m_view;

float t;

// axis handlers
float axis_handler_l = bound * 1.1f;
float axis_handler_l = bound * axis_factor;
float axis_handler_w = bound * 0.1f;

// x handler
Expand Down Expand Up @@ -188,7 +205,7 @@ struct ScaleHandler final : IGraphicHandler {

// plane handlers
float square_ctr_offset = bound;
float square_size = bound * 0.1f;
float square_size = bound * square_factor;

// xy handler
auto xy_base = glm::normalize(x_axis + y_axis);
Expand Down Expand Up @@ -217,6 +234,19 @@ struct ScaleHandler final : IGraphicHandler {
return INTERACT_XZ;
}

// xyz handler
// the other handlers are in the range of xyz handler, so check xyz at last
float i_radius = bound * icircle_factor;
float o_radius = bound * ocircle_factor;
float thickness = bound * 0.1f;
auto right_world = vec3f(view[0][0], view[1][0], view[2][0]);
auto up_world = vec3f(view[0][1], view[1][1], view[2][1]);
if (rayIntersectRing(ori, dir, glm::vec3(0), o_radius, i_radius, zeno::vec_to_other<glm::vec3>(right_world),
zeno::vec_to_other<glm::vec3>(up_world), thickness, model_matrix)) {
mode = INTERACT_XYZ;
return INTERACT_XYZ;
}

mode = INTERACT_NONE;
return INTERACT_NONE;
}
Expand Down

0 comments on commit 6f56b48

Please sign in to comment.