| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | # Copyright (C) 2022 RozK | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | # it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | # the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | # (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU Affero General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | # along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 08:15:38 +01:00
										 |  |  | from engine import vec3_origin, mat4, mat4_projection, mat4_orbit, resolve_input, set_input_mat4 | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  | class _Inputs: | 
					
						
							|  |  |  |     __slots__ = 'projection', 'view' | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  |     def __init__(self, shader, projection, view): | 
					
						
							|  |  |  |         self.projection = resolve_input(shader, projection) | 
					
						
							|  |  |  |         self.view = resolve_input(shader, view) | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  | class Camera: | 
					
						
							| 
									
										
										
										
											2022-12-29 08:15:38 +01:00
										 |  |  |     __slots__ = 'yaw', 'pitch', 'distance', 'projection', 'view' | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  |     def __init__(self): | 
					
						
							| 
									
										
										
										
											2022-12-29 08:15:38 +01:00
										 |  |  |         self.yaw = 0.0 | 
					
						
							|  |  |  |         self.pitch = 0.0 | 
					
						
							|  |  |  |         self.distance = 0.0 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  |         self.projection = mat4() | 
					
						
							|  |  |  |         self.view = mat4() | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def set_projection(self, half_fov, ratio, near_z, far_z): | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  |         mat4_projection(self.projection, half_fov, ratio, near_z, far_z) | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 08:15:38 +01:00
										 |  |  |     def set_view(self, yaw, pitch, distance): | 
					
						
							|  |  |  |         self.yaw = yaw | 
					
						
							|  |  |  |         self.pitch = pitch | 
					
						
							|  |  |  |         self.distance = distance | 
					
						
							|  |  |  |         mat4_orbit(self.view, vec3_origin, yaw, pitch, distance) | 
					
						
							| 
									
										
										
										
											2022-12-20 16:10:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def to_km(self): | 
					
						
							| 
									
										
										
										
											2022-12-29 08:15:38 +01:00
										 |  |  |         mat4_orbit(self.view, vec3_origin, self.yaw, self.pitch, self.distance * 0.001) | 
					
						
							| 
									
										
										
										
											2022-11-29 03:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  |     def resolve_inputs(self, shader, projection = b'u_projection', view = b'u_view'): | 
					
						
							|  |  |  |         return _Inputs(shader, projection, view) | 
					
						
							| 
									
										
										
										
											2022-12-18 03:44:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-18 05:40:52 +01:00
										 |  |  |     def update_inputs(self, inputs): | 
					
						
							|  |  |  |         set_input_mat4(inputs.projection, self.projection) | 
					
						
							|  |  |  |         set_input_mat4(inputs.view, self.view) |