Added mouse-based rotation and rotation-dependant movement

This commit is contained in:
Eric-Paul Ickhorn 2023-10-13 14:37:02 +02:00
parent 4ae08a432b
commit 80e6f97275
1 changed files with 45 additions and 16 deletions

View File

@ -65,7 +65,7 @@ float triangle_vertices[108] =
}; };
tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f }; tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f };
tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f }; // tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f };
tc_block_s block; tc_block_s block;
@ -117,10 +117,12 @@ void render()
glViewport(0, 0, fb_width, fb_height); glViewport(0, 0, fb_width, fb_height);
mat4x4_perspective(projection_matrix, 120, ((float) fb_width) / ((float) fb_height), 0.0001f, 1000.0f); mat4x4_perspective(projection_matrix, 120, ((float) fb_width) / ((float) fb_height), 0.0001f, 1000.0f);
tc_vec3_s camera_rotation = tc_game_state_g.renderer.active_camera.rotation;
mat4x4_identity(view_matrix); mat4x4_identity(view_matrix);
mat4x4_rotate_X(view_matrix, view_matrix, tc_camera_rotation.x); mat4x4_rotate_X(view_matrix, view_matrix, camera_rotation.x);
mat4x4_rotate_Y(view_matrix, view_matrix, tc_camera_rotation.y); mat4x4_rotate_Y(view_matrix, view_matrix, camera_rotation.y);
mat4x4_rotate_Z(view_matrix, view_matrix, tc_camera_rotation.z); mat4x4_rotate_Z(view_matrix, view_matrix, camera_rotation.z);
mat4x4_translate_in_place(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z); mat4x4_translate_in_place(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z);
@ -255,6 +257,20 @@ bool update()
} }
break; break;
case SDL_MOUSEMOTION:
{
if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
tc_game_state_g.renderer.active_camera.rotation.y += event.motion.xrel / 50.0f;
tc_game_state_g.renderer.active_camera.rotation.x += event.motion.yrel / 50.0f;
if(tc_game_state_g.renderer.active_camera.rotation.x > (3.1415f/2.0f))
tc_game_state_g.renderer.active_camera.rotation.x = (3.1415f/2.0f);
if(tc_game_state_g.renderer.active_camera.rotation.x < -(3.1415f/2.0f))
tc_game_state_g.renderer.active_camera.rotation.x = -(3.1415f/2.0f);
} break;
} }
} }
@ -268,35 +284,48 @@ bool update()
tc_camera_position.y += 0.25; tc_camera_position.y += 0.25;
} }
tc_vec3_s difference_in_perspective;
difference_in_perspective.x = 0.0f;
difference_in_perspective.y = 0.0f;
difference_in_perspective.z = 0.0f;
if(go_left) if(go_left)
{ {
tc_camera_position.x -= 0.25; difference_in_perspective.x -= 0.45f;
} }
if(go_right) if(go_right)
{ {
tc_camera_position.x += 0.25; difference_in_perspective.x += 0.45f;
} }
if(go_forward) if(go_forward)
{ {
tc_camera_position.z += 0.25; difference_in_perspective.z += 0.4f;
} }
if(go_backwards) if(go_backwards)
{ {
tc_camera_position.z -= 0.25; difference_in_perspective.z -= 0.4f;
} }
if(rotate_left) mat4x4 position_matrix;
{ mat4x4_identity(position_matrix);
tc_camera_rotation.y -= 0.1f / (3.1415 * 2);
}
if(rotate_right) mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z);
{
tc_camera_rotation.y += 0.1f / (3.1415 * 2); mat4x4_rotate_X(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.x);
} mat4x4_rotate_Y(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.y);
mat4x4_rotate_Z(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.z);
vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z };
vec4 raw_difference = { 0.0f, 0.0f, 0.0f };
mat4x4_mul_vec4(raw_difference, position_matrix, viewed_difference);
tc_camera_position.x += raw_difference[0];
tc_camera_position.z += raw_difference[2];
tc_game_state_g.main_world->loading_center.x = ((int32_t) tc_camera_position.x) / 32; tc_game_state_g.main_world->loading_center.x = ((int32_t) tc_camera_position.x) / 32;
tc_game_state_g.main_world->loading_center.y = ((int32_t) tc_camera_position.y) / 32; tc_game_state_g.main_world->loading_center.y = ((int32_t) tc_camera_position.y) / 32;