void motion(int x, int y) { if (!current_trackball_state->dragged) return; glm::ivec2 current_position(x, y); if (camera_zoom) { glm::vec2 v(current_trackball_state->prev_position.x - current_position.x, current_trackball_state->prev_position.y - current_position.y); float delta = glm::length(v); if (delta > 0.0f) { float direction = glm::sign(glm::dot(v, glm::vec2(0.0f, 1.0f))); float theta = direction * glm::clamp(delta, 0.1f, 0.5f); camera_fovy = glm::clamp(camera_fovy + theta, 5.0f, 60.0f); } } else { glm::vec3 v0 = map_to_sphere(*current_trackball_state, current_trackball_state->prev_position); glm::vec3 v1 = map_to_sphere(*current_trackball_state, current_position); glm::vec3 v2 = glm::cross(v0, v1); // calculate rotation axis float d = glm::dot(v0, v1); float s = std::sqrt((1.0f + d) * 2.0f); glm::quat q(0.5f * s, v2 / s); current_trackball_state->orientation = q * current_trackball_state->orientation; current_trackball_state->orientation /= glm::length(current_trackball_state->orientation); } current_trackball_state->prev_position.x = x; current_trackball_state->prev_position.y = y; }
void TrackballViewer:: rotation(int x, int y) { if (last_point_ok_) { Vector3 new_point_3D; bool new_point_ok; new_point_ok = map_to_sphere(x, y, new_point_3D); if (new_point_ok) { Vector3 axis = last_point_3D_.cross( new_point_3D ); float cos_angle =last_point_3D_.dot( new_point_3D ); if (fabs(cos_angle) < 1.0) { axis.normalize(); float angle = 2.0*acos(cos_angle); // rotate camera around point m_camera.rotateAroundAxisObject(Vector3(0,0,-m_camera_rotation_depth),axis,-angle); } } } }
void TrackballViewer:: motion(int x, int y) { // zoom if ((button_down_[0] && button_down_[1]) || (button_down_[0] && (modifiers_==GLUT_ACTIVE_SHIFT))) { zoom(x, y); } // translation else if (button_down_[1] || (button_down_[0] && (modifiers_==GLUT_ACTIVE_ALT))) { translation(x, y); } // rotation else if (button_down_[0]) { rotation(x, y); } // remeber points last_x_ = x; last_y_ = y; last_point_ok_ = map_to_sphere(x, y, last_point_3D_); glutPostRedisplay(); }
void TrackballViewer:: mouse(int button, int state, int x, int y) { // mouse press if (state == GLUT_DOWN) { last_x_ = x; last_y_ = y; last_point_ok_ = map_to_sphere( x, y, last_point_3D_ ); button_down_[button] = true; } // mouse release else { last_point_ok_ = false; button_down_[button] = false; // GLUT: button 3 or 4 -> mouse wheel clicked if (button == 3) zoom(0, (int)(y - 0.05*width_)); else if (button == 4) zoom(0, (int)(y + 0.05*width_)); } modifiers_ = glutGetModifiers(); glutPostRedisplay(); }
static void mikk_get_texture_coordinate(const SMikkTSpaceContext *context, float uv[2], const int face_num, const int vert_num) { const MikkUserData *userdata = (const MikkUserData *)context->m_pUserData; const Mesh *mesh = userdata->mesh; if (userdata->texface != NULL) { const int corner_index = mikk_corner_index(mesh, face_num, vert_num); float2 tfuv = userdata->texface[corner_index]; uv[0] = tfuv.x; uv[1] = tfuv.y; } else if (userdata->orco != NULL) { const int vertex_index = mikk_vertex_index(mesh, face_num, vert_num); const float3 orco_loc = userdata->orco_loc; const float3 orco_size = userdata->orco_size; const float3 orco = (userdata->orco[vertex_index] + orco_loc) / orco_size; const float2 tmp = map_to_sphere(orco); uv[0] = tmp.x; uv[1] = tmp.y; } else { uv[0] = 0.0f; uv[1] = 0.0f; } }
static void mikk_get_texture_coordinate(const SMikkTSpaceContext *context, float uv[2], const int face_num, const int vert_num) { MikkUserData *userdata = (MikkUserData*)context->m_pUserData; if(userdata->layer != NULL) { BL::MeshTextureFace tf = userdata->layer->data[face_num]; float3 tfuv; switch(vert_num) { case 0: tfuv = get_float3(tf.uv1()); break; case 1: tfuv = get_float3(tf.uv2()); break; case 2: tfuv = get_float3(tf.uv3()); break; default: tfuv = get_float3(tf.uv4()); break; } uv[0] = tfuv.x; uv[1] = tfuv.y; } else { int vert_idx = userdata->mesh.tessfaces[face_num].vertices()[vert_num]; float3 orco = get_float3(userdata->mesh.vertices[vert_idx].undeformed_co()); float2 tmp = map_to_sphere(make_float3(orco[0], orco[1], orco[2])); uv[0] = tmp.x; uv[1] = tmp.y; } }