Commit 944750c2 authored by stonecompass's avatar stonecompass
Browse files

Everything almost working again.

parent 051e6597
......@@ -12,13 +12,11 @@ struct Camera
CameraMode mode;
r32 zoom;
math::Vec3 position;
math::Vec3 target;
math::Vec3 forward;
math::Vec3 right;
math::Vec3 up;
math::Vec3 pos;
math::Mat4 view_matrix;
math::Mat4 projection_matrix;
......@@ -30,141 +28,144 @@ struct Camera
r32 fov;
};
static Camera create_camera(math::Vec3 position, math::Vec3 target, math::Mat4 projection_matrix)
static Camera create_camera(math::Vec3 forward, math::Mat4 view_matrix, math::Mat4 projection_matrix)
{
Camera camera;
camera.mode = CameraMode::FREE_ROAM;
camera.position = position;
camera.target = target;
camera.forward = math::normalize(target - position);
camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward));
camera.up = math::normalize(math::cross(camera.forward, camera.right));
camera.forward = math::Vec3(0, 0, 1);
camera.right = math::Vec3(1, 0, 0);
camera.up = math::Vec3(0, 1, 0);
camera.yaw = (r32)atan2(camera.forward.x, camera.forward.z) / DEGREE_IN_RADIANS;
camera.pitch = (r32)asin(camera.forward.y) / DEGREE_IN_RADIANS;
camera.view_matrix = math::look_at(camera.forward, camera.position);
camera.view_matrix = view_matrix;
camera.projection_matrix = projection_matrix;
return(camera);
}
static void translate_forward(Camera &camera, r32 amount)
{
r32 dist = math::distance(camera.target, camera.position);
camera.position += camera.forward * amount;
camera.view_matrix = math::look_at(camera.forward, camera.position);
camera.target = camera.position + camera.forward * dist;
}
static void translate_up(Camera &camera, r32 amount)
{
r32 dist = math::distance(camera.target, camera.position);
camera.position += camera.up * amount;
camera.view_matrix = math::look_at(camera.forward, camera.position);
camera.target = camera.position + camera.forward * dist;
}
static void translate_right(Camera &camera, r32 amount)
static Camera create_camera(math::Vec3 position, math::Vec3 target, math::Mat4 projection_matrix)
{
r32 dist = math::distance(camera.target, camera.position);
camera.position += camera.right * amount;
camera.view_matrix = math::look_at(camera.forward, camera.position);
camera.target = camera.position + camera.forward * dist;
Camera camera;
return(camera);
}
static void update_rotation(Camera &camera)
{
r32 pitch = camera.pitch * DEGREE_IN_RADIANS;
r32 yaw = camera.yaw * DEGREE_IN_RADIANS;
r32 dist = math::distance(camera.target, camera.position);
camera.forward.x = math::cos(pitch) * math::sin(yaw);
camera.forward.y = math::sin(pitch);
camera.forward.z = math::cos(pitch) * math::cos(yaw);
camera.forward = math::normalize(camera.forward);
/* static void translate_forward(Camera &camera, r32 amount) */
/* { */
/* r32 dist = math::distance(camera.target, camera.position); */
/* camera.position += camera.forward * amount; */
/* camera.view_matrix = math::look_at(camera.forward, camera.position); */
/* camera.target = camera.position + camera.forward * dist; */
/* } */
/* static void translate_up(Camera &camera, r32 amount) */
/* { */
/* r32 dist = math::distance(camera.target, camera.position); */
/* camera.position += camera.up * amount; */
/* camera.view_matrix = math::look_at(camera.forward, camera.position); */
/* camera.target = camera.position + camera.forward * dist; */
/* } */
/* static void translate_right(Camera &camera, r32 amount) */
/* { */
/* r32 dist = math::distance(camera.target, camera.position); */
/* camera.position += camera.right * amount; */
/* camera.view_matrix = math::look_at(camera.forward, camera.position); */
/* camera.target = camera.position + camera.forward * dist; */
/* } */
/* static void update_rotation(Camera &camera) */
/* { */
/* r32 pitch = camera.pitch * DEGREE_IN_RADIANS; */
/* r32 yaw = camera.yaw * DEGREE_IN_RADIANS; */
/* r32 dist = math::distance(camera.target, camera.position); */
/* camera.forward.x = math::cos(pitch) * math::sin(yaw); */
/* camera.forward.y = math::sin(pitch); */
/* camera.forward.z = math::cos(pitch) * math::cos(yaw); */
/* camera.forward = math::normalize(camera.forward); */
camera.target = camera.position + camera.forward * dist;
/* camera.target = camera.position + camera.forward * dist; */
camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward));
camera.up = math::normalize(math::cross(camera.forward, camera.right));
/* camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward)); */
/* camera.up = math::normalize(math::cross(camera.forward, camera.right)); */
camera.view_matrix = math::look_at(camera.forward, camera.position);
}
/* camera.view_matrix = math::look_at(camera.forward, camera.position); */
/* } */
static void update_arcball_rotation(Camera &camera, r32 pitch_amount, r32 yaw_amount)
{
r32 pitch = pitch_amount * DEGREE_IN_RADIANS;
r32 yaw = yaw_amount * DEGREE_IN_RADIANS;
/* static void update_arcball_rotation(Camera &camera, r32 pitch_amount, r32 yaw_amount) */
/* { */
/* assert(false); */
/* /\* r32 pitch = pitch_amount * DEGREE_IN_RADIANS; *\/ */
/* /\* r32 yaw = yaw_amount * DEGREE_IN_RADIANS; *\/ */
math::Vec3 camera_focus_vector = camera.position - camera.target;
/* /\* math::Vec3 camera_focus_vector = camera.position - camera.target; *\/ */
math::Quat orientation = math::Quat();
orientation = math::rotate(orientation, pitch, camera.right);
orientation = math::rotate(orientation, yaw, math::Vec3(0.0f, 1.0f, 0.0f));
camera.position = to_matrix(orientation) * camera_focus_vector + camera.target;
/* /\* math::Quat orientation = math::Quat(); *\/ */
/* /\* orientation = math::rotate(orientation, pitch, camera.right); *\/ */
/* /\* orientation = math::rotate(orientation, yaw, math::Vec3(0.0f, 1.0f, 0.0f)); *\/ */
/* /\* camera.position = to_matrix(orientation) * camera_focus_vector + camera.target; *\/ */
camera.forward = math::normalize(camera.target - camera.position);
camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward));
camera.up = math::normalize(math::cross(camera.forward, camera.right));
/* /\* camera.forward = math::normalize(camera.target - camera.position); *\/ */
/* /\* camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward)); *\/ */
/* /\* camera.up = math::normalize(math::cross(camera.forward, camera.right)); *\/ */
camera.view_matrix = math::look_at(camera.forward, camera.position);
}
/* /\* camera.view_matrix = math::look_at(camera.forward, camera.position); *\/ */
/* } */
static void rotate_around_x(Camera &camera, r32 amount)
{
camera.pitch += amount;
/* static void rotate_around_x(Camera &camera, r32 amount) */
/* { */
/* camera.pitch += amount; */
if(camera.pitch > 89.0f)
camera.pitch = 89.0f;
if(camera.pitch < -89.0f)
camera.pitch = -89.0f;
if(camera.mode == CameraMode::FREE_ROAM)
update_rotation(camera);
else
update_arcball_rotation(camera, amount * 10.0f, 0.0f);
}
static void invert_pitch(Camera &camera)
{
camera.pitch = -camera.pitch;
update_rotation(camera);
}
static void rotate_around_y(Camera &camera, r32 amount)
{
camera.yaw += amount;
if(camera.mode == CameraMode::FREE_ROAM)
update_rotation(camera);
else
update_arcball_rotation(camera, 0.0f, -amount * 10.0f);
}
static void center(Camera &camera)
{
camera.mode = CameraMode::CENTERED;
}
static void set_target(Camera &camera, math::Vec3 target)
{
camera.target = target;
/* if(camera.pitch > 89.0f) */
/* camera.pitch = 89.0f; */
/* if(camera.pitch < -89.0f) */
/* camera.pitch = -89.0f; */
/* if(camera.mode == CameraMode::FREE_ROAM) */
/* update_rotation(camera); */
/* else */
/* update_arcball_rotation(camera, amount * 10.0f, 0.0f); */
/* } */
/* static void invert_pitch(Camera &camera) */
/* { */
/* camera.pitch = -camera.pitch; */
/* update_rotation(camera); */
/* } */
/* static void rotate_around_y(Camera &camera, r32 amount) */
/* { */
/* camera.yaw += amount; */
/* if(camera.mode == CameraMode::FREE_ROAM) */
/* update_rotation(camera); */
/* else */
/* update_arcball_rotation(camera, 0.0f, -amount * 10.0f); */
/* } */
/* static void center(Camera &camera) */
/* { */
/* camera.mode = CameraMode::CENTERED; */
/* } */
/* static void set_target(Camera &camera, math::Vec3 target) */
/* { */
/* camera.target = target; */
camera.forward = math::normalize(camera.target - camera.position);
camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward));
camera.up = math::normalize(math::cross(camera.forward, camera.right));
/* camera.forward = math::normalize(camera.target - camera.position); */
/* camera.right = math::normalize(math::cross(math::Vec3(0, 1, 0), camera.forward)); */
/* camera.up = math::normalize(math::cross(camera.forward, camera.right)); */
camera.yaw = (r32)atan2(camera.forward.x, camera.forward.z) / DEGREE_IN_RADIANS;
camera.pitch = (r32)asin(camera.forward.y) / DEGREE_IN_RADIANS;
/* camera.yaw = (r32)atan2(camera.forward.x, camera.forward.z) / DEGREE_IN_RADIANS; */
/* camera.pitch = (r32)asin(camera.forward.y) / DEGREE_IN_RADIANS; */
camera.view_matrix = math::look_at(camera.forward, camera.position);
}
/* camera.view_matrix = math::look_at(camera.forward, camera.position); */
/* } */
static void free_roam(Camera &camera)
{
camera.mode = CameraMode::FREE_ROAM;
}
/* static void free_roam(Camera &camera) */
/* { */
/* camera.mode = CameraMode::FREE_ROAM; */
/* } */
#endif
......@@ -199,7 +199,8 @@ namespace editor
if(scene::has_camera_component(scene_manager->selected_entity, scene_manager->loaded_scene))
{
if(ImGui::CollapsingHeader("Camera"))
bool is_main_camera = HANDLES_EQUAL(scene_manager->selected_entity, scene.main_camera_handle);
if(ImGui::CollapsingHeader((is_main_camera ? "Camera (MAIN)" : "Camera")))
{
scene::Scene &scene = scene::get_scene(scene_manager->loaded_scene);
......@@ -216,21 +217,14 @@ namespace editor
{
scene::stop_camera_preview(scene_manager);
}
bool is_main_camera = HANDLES_EQUAL(scene_manager->selected_entity, scene.main_camera_handle);
ImGui::Checkbox("Main camera", &is_main_camera);
if(is_main_camera)
if(ImGui::Button("Make main camera"))
{
scene::set_main_camera(scene_manager->selected_entity, scene_manager->loaded_scene);
}
scene::CameraComponent &camera_component = scene::get_camera_comp(scene_manager->selected_entity, scene_manager->loaded_scene);
ImGui::DragFloat3("Camera target", camera_component.camera.target.e);
r32 degrees = camera_component.camera.fov / DEGREE_IN_RADIANS;
ImGui::InputFloat("FOV (Degrees)", &degrees);
......@@ -925,7 +919,7 @@ namespace editor
sprintf(full_path, "%s/%s", current_structure.path, current_structure.files[i].name);
Camera &camera = scene::get_editor_camera(scene_manager->loaded_scene);
place_entity_from_template(camera.position + camera.forward * 15.0f, full_path, scene_manager->loaded_scene, true, true);
place_entity_from_template(scene_manager->editor_camera_transform.transform.position + camera.forward * 15.0f, full_path, scene_manager->loaded_scene, true, true);
}
else if(has_extension(current_structure.files[i].name, ".gsc"))
{
......
......@@ -1086,6 +1086,8 @@ int main(int argc, char **args)
if(scene_manager->scene_loaded)
{
scene::Scene &current_scene = scene::get_scene(scene_manager->loaded_scene);
#ifdef EDITOR
if(scene_manager->mode == scene::SceneMode::RUNNING)
{
......@@ -1095,7 +1097,6 @@ int main(int argc, char **args)
{
game.update_editor(&game_memory);
scene::Scene &current_scene = scene::get_scene(scene_manager->loaded_scene);
editor::_render_main_menu(&editor_state, scene_manager, &input_controller, delta_time);
if(editor_state.mode == editor::EditorMode::BUILT_IN)
......@@ -1109,11 +1110,12 @@ int main(int argc, char **args)
game.update(&game_memory);
#endif
scene::update_scene_camera(scene_manager);
if(scene_manager->scene_loaded) // Check again, since there could be a call to unload_current_scene() in game.update()
{
#ifdef EDITOR
current_scene = scene::get_scene(scene_manager->loaded_scene);
scene::update_cameras(current_scene, scene_manager);
#ifdef EDITOR
update_scene_editor(scene_manager->loaded_scene, &input_controller, render_state, delta_time);
#endif
......
......@@ -2364,7 +2364,7 @@ static void set_uniform(rendering::Transform transform, const rendering::RenderP
break;
case rendering::UniformMappingType::CAMERA_POSITION:
{
set_vec3_uniform(gl_shader.program, location, camera.position);
set_vec3_uniform(gl_shader.program, location, camera.pos);
}
break;
case rendering::UniformMappingType::DISSOLVE:
......
......@@ -678,8 +678,6 @@ namespace rendering
Camera camera = {};
camera.zoom = 1.0f;
camera.position = math::Vec3(0.0f);
camera.target = math::Vec3();
camera.view_matrix = math::Mat4();
camera.projection_matrix = math::ortho(0.0f, (r32)renderer->framebuffer_width, 0.0f,
......@@ -5307,6 +5305,9 @@ namespace rendering
transform.model = math::scale(transform.model, transform.scale);
transform.model = math::to_matrix(transform.orientation) * transform.model;
transform.model = math::translate(transform.model, transform.position);
transform.forward = math::forward(transform.model);
transform.right = math::right(transform.model);
transform.up = math::up(transform.model);
transform.dirty = false;
}
......
......@@ -530,6 +530,10 @@ namespace rendering
math::Quat orientation;
math::Vec3 euler_angles;
math::Vec3 forward;
math::Vec3 right;
math::Vec3 up;
math::Mat4 model;
b32 dirty;
......
......@@ -391,7 +391,6 @@ namespace scene
if(has_camera_component(entity.handle, scene_handle))
{
CameraComponent &camera_comp = get_camera_comp(entity.handle, scene_handle);
fprintf(file, "camera::target: %f %f %f\n", camera_comp.camera.target.x, camera_comp.camera.target.y, camera_comp.camera.target.z);
fprintf(file, "camera::fov: %f\n", camera_comp.camera.fov);
fprintf(file, "camera::near: %f\n", camera_comp.camera.near_plane);
fprintf(file, "camera::far: %f\n", camera_comp.camera.far_plane);
......@@ -595,6 +594,8 @@ namespace scene
math::Vec3 new_position;
sscanf(buffer, "position: %f %f %f", &new_position.x, &new_position.y, &new_position.z);
rendering::set_position(transform.transform, new_position);
rendering::recompute_transform(transform.transform);
}
else if(starts_with(buffer, "scale"))
{
......@@ -604,6 +605,8 @@ namespace scene
math::Vec3 new_scale;
sscanf(buffer, "scale: %f %f %f", &new_scale.x, &new_scale.y, &new_scale.z);
rendering::set_scale(transform.transform, new_scale);
rendering::recompute_transform(transform.transform);
}
else if(starts_with(buffer, "rotation"))
{
......@@ -613,6 +616,8 @@ namespace scene
math::Vec3 new_rotation;
sscanf(buffer, "rotation: %f %f %f", &new_rotation.x, &new_rotation.y, &new_rotation.z);
rendering::set_rotation(transform.transform, new_rotation);
rendering::recompute_transform(transform.transform);
}
else if(starts_with(buffer, "id"))
{
......@@ -625,15 +630,8 @@ namespace scene
}
CameraComponent &camera_comp = get_camera_comp(handle, scene);
TransformComponent &transform = get_transform_comp(handle, scene);
camera_comp.camera.position = transform.transform.position;
if(starts_with(buffer, "camera::target"))
{
sscanf(buffer, "camera::target: %f %f %f", &camera_comp.camera.target.x, &camera_comp.camera.target.y, &camera_comp.camera.target.z);
}
else if(starts_with(buffer, "camera::fov"))
if(starts_with(buffer, "camera::fov"))
{
sscanf(buffer, "camera::fov: %f", &camera_comp.camera.fov);
}
......@@ -884,10 +882,6 @@ static Camera get_standard_camera(SceneManager& manager)
sscanf(buffer, "camera::position: %f %f %f", &position.x, &position.y, &position.z);
set_position(transform, position);
}
else if(starts_with(buffer, "camera::target"))
{
sscanf(buffer, "camera::target: %f %f %f", &camera_comp.camera.target.x, &camera_comp.camera.target.y, &camera_comp.camera.target.z);
}
else if(starts_with(buffer, "camera::fov"))
{
sscanf(buffer, "camera::fov: %f", &camera_comp.camera.fov);
......@@ -950,6 +944,9 @@ static Camera get_standard_camera(SceneManager& manager)
{
SceneHandle handle = create_scene(scene_manager, persistent, initial_entity_array_size);
_create_scene_from_file(scene_file_path, scene_manager, handle);
update_cameras(get_scene(handle), scene_manager);
return handle;
}
......@@ -1087,7 +1084,7 @@ static Camera get_standard_camera(SceneManager& manager)
{
SceneManager *scene_manager = handle.manager;
if(!scene_manager)
if(!scene_manager || handle.handle <= 0)
return;
if(scene_manager->callbacks.on_scene_will_be_freed)
......@@ -1181,6 +1178,7 @@ static Camera get_standard_camera(SceneManager& manager)
i32 width = scene.renderer->window_width;
i32 height = scene.renderer->window_height;
Camera &camera = scene.scene_manager->editor_camera;
TransformComponent &transform = scene.scene_manager->editor_camera_transform;
r32 x = (2.0f * mouse_x) / width - 1.0f;
r32 y = 1.0f - (2.0f * mouse_y) / height;
......@@ -1195,7 +1193,7 @@ static Camera get_standard_camera(SceneManager& manager)
ray_wor = math::normalize(ray_wor);
math::Ray ray;
ray.origin = camera.position;
ray.origin = transform.transform.position;
ray.direction = ray_wor;
return ray;
......@@ -1394,7 +1392,7 @@ static Camera get_standard_camera(SceneManager& manager)
if(triangle_ray_intersection(local_ray, v1.position, v2.position, v3.position, &intersection_point))
{
intersection_point = transform.model * intersection_point;
r32 new_dist = math::distance(scene.scene_manager->editor_camera.position, intersection_point);
r32 new_dist = math::distance(scene.scene_manager->editor_camera_transform.transform.position, intersection_point);
if(new_dist < dist)
{
......@@ -1830,8 +1828,17 @@ static Camera get_standard_camera(SceneManager& manager)
}
}
static void update_editor_camera(Camera &camera, Scene &scene, InputController *input_controller, r64 delta_time)
static void update_editor_camera(Camera &camera, TransformComponent &component, Scene &scene, InputController *input_controller, r64 delta_time)
{
if(ImGui::IsMouseDragging(0) || ImGui::IsMouseDragging(1))
{
set_mouse_lock(true, *scene.renderer);
}
else
{
set_mouse_lock(false, *scene.renderer);
}
if(KEY(Key_LeftCtrl))
return;
......@@ -1840,40 +1847,41 @@ static Camera get_standard_camera(SceneManager& manager)
{
if(KEY(Key_W))
{
translate_forward(camera, (r32)delta_time * 10.0f);
scene::set_position(component, component.transform.position + component.transform.forward * (r32)delta_time * 10.0f);
}
if(KEY(Key_S))
{
translate_forward(camera, (r32)-delta_time * 10.0f);
scene::set_position(component, component.transform.position - component.transform.forward * (r32)delta_time * 10.0f);
}
if(KEY(Key_A))
{
translate_right(camera, (r32)-delta_time * 10.0f);
scene::set_position(component, component.transform.position - component.transform.right * (r32)delta_time * 10.0f);
}
if(KEY(Key_D))
{
translate_right(camera, (r32)delta_time * 10.0f);
scene::set_position(component, component.transform.position + component.transform.right * (r32)delta_time * 10.0f);
}
}
if(MOUSE(Mouse_Middle))
{
translate_up(camera, (r32)input_controller->mouse_y_delta * 0.01f);
translate_right(camera, (r32)-input_controller->mouse_x_delta * 0.01f);
scene::set_position(component, component.transform.position + component.transform.up * input_controller->mouse_y_delta * 0.01f);
scene::set_position(component, component.transform.position + component.transform.right * input_controller->mouse_x_delta * 0.01f);
}
if(KEY_DOWN(Key_LeftAlt))
center(camera);
else if(KEY_UP(Key_LeftAlt))
free_roam(camera);
// @Incomplete
// if(KEY_DOWN(Key_LeftAlt))
// center(camera);
// else if(KEY_UP(Key_LeftAlt))
// free_roam(camera);
if(MOUSE(Mouse_Right))
{
rotate_around_x(camera, (r32)-input_controller->mouse_y_delta * 0.1f);
rotate_around_y(camera, (r32)input_controller->mouse_x_delta * 0.1f);
scene::set_rotation(component, component.transform.euler_angles - math::Vec3((r32)input_controller->mouse_y_delta * 0.1f, 0.0f, 0.0f));
scene::set_rotation(component, component.transform.euler_angles - math::Vec3(0.0f, (r32)input_controller->mouse_x_delta * 0.1f, 0.0f));
}
if(MOUSE_DOWN(Mouse_Right))
......@@ -1903,6 +1911,8 @@ static Camera get_standard_camera(SceneManager& manager)
{
if(IS_ENTITY_HANDLE_VALID(entity) && !HANDLES_EQUAL(entity, manager->selected_entity))
{
stop_camera_preview(manager);
// Deselect the previously selected entity
if(IS_ENTITY_HANDLE_VALID(manager->selected_entity))
scene::set_wireframe_enabled(false, manager->selected_entity, manager->loaded_scene);
......@@ -1969,29 +1979,30 @@ static Camera get_standard_camera(SceneManager& manager)
scene_manager->camera_preview.handle = EMPTY_ENTITY_HANDLE;
}
static void update_scene_camera(SceneManager *scene_manager)
static void update_camera(Camera &camera, TransformComponent &transform, SceneManager *scene_manager)
{
Scene& scene = get_scene(scene_manager->loaded_scene);
Camera camera;
camera.view_matrix = math::look_at_with_target(transform.transform.position, transform.transform.position + transform.transform.forward);
camera.projection_matrix = math::perspective((r32)scene_manager->renderer->window_width / (r32)scene_manager->renderer->window_height, camera.fov, camera.near_plane, camera.far_plane);
camera.pos = transform.transform.position;
camera.forward = transform.transform.forward;
camera.right = transform.transform.right;
}
if(IS_ENTITY_HANDLE_VALID(scene.main_camera_handle))
{
CameraComponent &main = get_camera_comp(scene.main_camera_handle, scene_manager->loaded_scene);
TransformComponent &transform = get_transform_comp(scene.main_camera_handle, scene_manager->loaded_scene);
camera = main.camera;