//-------------------------------------------------------------------------------- // calculate yaw for follow mode //-------------------------------------------------------------------------------- float zz_camera_follow::calc_yaw () { // we should move camera and target vec3 displacement = final_.target_pos - last_.target_pos; final_.camera_pos = get_eye() + displacement; // camera to target final_.camera_dir = final_.target_pos - final_.camera_pos; // ignore pitch final_.camera_dir.z = 0; final_.camera_dir.normalize(); // update target_dir get_target_dir(final_.target_dir); float dot_val = dot(final_.target_dir, final_.camera_dir); dot_val = ZZ_MIN(dot_val, 1.0f); dot_val = ZZ_MAX(dot_val, -1.0f); float new_yaw = static_cast<float>(acos(dot_val)); vec3 cross_vector; cross(cross_vector, final_.target_dir, final_.camera_dir); if (cross_vector.z < 0) // invert new_yaw = -new_yaw; return new_yaw; }
int init_value(t_all *all, char **tab) { char *str; if ((all->objects = malloc(sizeof(t_object) * get_size(tab))) == NULL) return (put_error_int("Error : malloc failed\n")); init_struct(all, get_size(tab)); get_window(all, tab[0]); get_name(all, tab[1]); get_eye(all, tab[3]); init_get_object(all, tab + 5); init_get_light(all, tab + 5); /* printf("name %s eyex %G eyey%G eyez%G X%d Y%d\n", all->my.name, all->eye.x, all->eye.y, all->eye.z, all->my.x_size, all->my.y_size); */ }
int init_value(t_all *all, char **tab) { all->nb_obj = get_size(tab); if ((all->objects = malloc(sizeof(t_object) * get_size(tab))) == NULL) return (put_error_int("Error : malloc failed\n")); init_struct(all, get_size(tab)); write(1, "\n", 1); get_window(all, tab[0]); write(1, "Z", 1); get_name(all, tab[1]); write(1, "Q", 1); get_eye(all, tab[3]); write(1, "S", 1); init_get_object(all, tab + 5); write(1, "D", 1); init_get_light(all, tab + 5); write(1, "W", 1); write(1, "\n", 1); return (0); }
const ray& perspective_camera::generate_ray(ray& out, const unsigned int x, const unsigned int y, const unsigned int sx, const unsigned int sy) { // maximum x is (get_display_width - 1) // maximum y is (get_display_height - 1) assert(x < get_display_width()); assert(y < get_display_height()); out.m_origin = get_eye(); // calculate pixel coordinates by offsetting origin pixel point3 subpixel(m_origin_pixel.x + (m_pixel_pitch * x), m_origin_pixel.y - (m_pixel_pitch * y), -1.0); // z = -1 because we are using the RH coordinate system subpixel.x = subpixel.x + ((m_subpixel_pitch * sx) + (m_subpixel_pitch * 0.5)); subpixel.y = subpixel.y + ((m_subpixel_pitch * sy) + (m_subpixel_pitch * 0.5)); out.m_direction = subpixel - out.m_origin; normalize(out.m_direction, out.m_direction); return out; }
int main(int ac, char **av) { t_lum *tab_lum; t_pos test; t_win mlxwin; ac != 4 ? exit (1) : 0; (mlxwin.mlx = mlx_init()) == NULL ? exit (1) : 0; (mlxwin.win = mlx_new_window(mlxwin.mlx, 1000, 1000, "RTV1")) == NULL ?\ exit (1) : 0; (mlxwin.img = mlx_new_image(mlxwin.mlx, 1000, 1000)) == NULL ? exit (1) : 0; get_eye(&mlxwin, av[1]); mlxwin.tab_obj = get_obj(av[2]); mlxwin.tab_lum = get_lum(av[3]); remplis_image(&mlxwin); mlx_key_hook(mlxwin.win, gere_key, &mlxwin); mlx_expose_hook(mlxwin.win, gere_expose, &mlxwin); mlx_loop(mlxwin.mlx); return (0); }
//-------------------------------------------------------------------------------- // attach target //-------------------------------------------------------------------------------- bool zz_camera_follow::attach_target (zz_model * target_vis) { if (!target_vis) { ZZ_LOG("camera_follow: attach_target() failed. target not found\n"); return false; } if (target_ == target_vis) return true; target_ = target_vis; get_target_pos(final_.target_pos); look_at(final_.target_pos + INITIAL_CAMERA_POS_AT_ATTACH, final_.target_pos, vec3(0, 0, 1)); newly_attached_ = true; final_.camera_pos = get_eye(); last_.camera_pos = final_.camera_pos; last_.target_pos = final_.target_pos; is_dirty_ = true; return true; }
//-------------------------------------------------------------------------------- // follow the target as near as possible //-------------------------------------------------------------------------------- bool zz_camera_follow::update_lookmode (float follow_yaw_last) { bool move_camera; // distance between last_target and current target position float target_diff = last_.target_pos.distance(final_.target_pos); // distance between camera and current target float target_dist = (target_) ? distance(target_) : get_position().distance(vec3_null); // update by camera-target difference // 1. we start moving camera position if the target_diff reaches a certain amount. // 2. we stop moving camera position if the target_diff is too small. if (now_following_) { // if we started moving camera position. if (target_diff > MIN_DISTANCE_THRESHOLD) { // now moving move_camera = true; // should move } else { // we are already very close to each other now_following_ = false; // quit following mode move_camera = true; // but keep in camera moving state for now } } else if (target_diff > MAX_DISTANCE_THRESHOLD) { // we should start moving the camera position now. now_following_ = true; move_camera = true; } else { // we do not need to move the camera position move_camera = false; } // force moving the camera for test if (!znzin->get_use_time_weight()) move_camera = true; if (move_camera) { // if we should move camera and target vec3 displacement; float damp = (target_diff / MAX_DISTANCE_THRESHOLD); // get target displacement by last and current displacement = .5f*time_weight_*(final_.target_pos - last_.target_pos); // .5f to make slower than 1.0f // update last_target_pos last_.target_pos += displacement; move(displacement); } //camera_dir = target_->get_position() - this->get_eye(); // update camera_dir for later use in update() final_.camera_dir = last_.target_pos - get_eye(); // recalc yaw, because eye position was changed in update_lookmode() // and, we need the difference between yaw and yaw_last float yaw_diff = final_.yaw - last_.yaw; current_.yaw = calc_yaw(); final_.yaw = current_.yaw + yaw_diff; // and apply new_value current_.yaw += time_weight_*(final_.yaw - current_.yaw); apply_yaw(final_.camera_dir, current_.yaw, final_.target_dir); // save last_camera_dir(non-pitched) for back-mode last_.camera_dir = final_.camera_dir; // apply pitch apply_pitch(final_.camera_dir, current_.pitch); final_.camera_pos = last_.target_pos - current_.distance * final_.camera_dir; look_at(final_.camera_pos, last_.target_pos, vec3(0, 0, 1)); last_.camera_pos = final_.camera_pos; // for back-mode return move_camera; }