You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
39 KiB
213 lines
39 KiB
3 years ago
|
<html>
|
||
|
<head>
|
||
|
<title>camera.rs</title>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<style type="text/css">
|
||
|
.s0 { color: #ed864a;}
|
||
|
.s1 { color: #ebebeb;}
|
||
|
.s2 { color: #ffffff;}
|
||
|
.s3 { color: #ed864a; font-weight: bold;}
|
||
|
.s4 { color: #33ccff; font-weight: bold;}
|
||
|
.s5 { color: #7ec3e6;}
|
||
|
.ln { color: #999999; font-weight: normal; font-style: normal; }
|
||
|
</style>
|
||
|
</head>
|
||
|
<body bgcolor="#131314">
|
||
|
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
|
||
|
<tr><td><center>
|
||
|
<font face="Arial, Helvetica" color="#000000">
|
||
|
camera.rs</font>
|
||
|
</center></td></tr></table>
|
||
|
<pre><a name="l1"><span class="ln">1 </span></a><span class="s0">use </span><span class="s2">std</span><span class="s1">::</span><span class="s2">f32</span><span class="s1">::</span><span class="s2">consts</span><span class="s1">::{</span><span class="s2">FRAC_PI_2</span><span class="s3">, </span><span class="s2">PI</span><span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l2"><span class="ln">2 </span></a><span class="s0">use </span><span class="s2">std</span><span class="s1">::</span><span class="s2">time</span><span class="s1">::{</span><span class="s2">Duration</span><span class="s3">, </span><span class="s2">Instant</span><span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l3"><span class="ln">3 </span></a>
|
||
|
<a name="l4"><span class="ln">4 </span></a><span class="s0">use </span><span class="s2">cgmath</span><span class="s1">::{</span><span class="s2">Decomposed</span><span class="s3">, </span><span class="s2">InnerSpace</span><span class="s3">, </span><span class="s2">Matrix4</span><span class="s3">, </span><span class="s2">Point3</span><span class="s3">, </span><span class="s2">Rad</span><span class="s3">, </span><span class="s2">Vector3</span><span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l5"><span class="ln">5 </span></a><span class="s0">use </span><span class="s2">winit_24</span><span class="s1">::</span><span class="s2">dpi</span><span class="s1">::{</span><span class="s2">LogicalPosition</span><span class="s3">, </span><span class="s2">PhysicalPosition</span><span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l6"><span class="ln">6 </span></a><span class="s0">use </span><span class="s2">winit_24</span><span class="s1">::</span><span class="s2">event</span><span class="s1">::{</span><span class="s2">ElementState</span><span class="s3">, </span><span class="s2">MouseScrollDelta</span><span class="s3">, </span><span class="s2">VirtualKeyCode</span><span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l7"><span class="ln">7 </span></a><span class="s0">use crate</span><span class="s1">::</span><span class="s2">render</span><span class="s1">::</span><span class="s2">OPENGL_TO_WGPU_MATRIX</span><span class="s3">;</span>
|
||
|
<a name="l8"><span class="ln">8 </span></a><span class="s0">use </span><span class="s2">imgui</span><span class="s1">::</span><span class="s2">Condition</span><span class="s3">;</span>
|
||
|
<a name="l9"><span class="ln">9 </span></a><span class="s0">use </span><span class="s2">imgui</span><span class="s1">::*</span><span class="s3">;</span>
|
||
|
<a name="l10"><span class="ln">10 </span></a>
|
||
|
<a name="l11"><span class="ln">11 </span></a><span class="s1">#[</span><span class="s2">derive</span><span class="s1">(</span><span class="s2">Clone</span><span class="s3">, </span><span class="s2">Copy</span><span class="s3">, </span><span class="s2">Debug</span><span class="s3">, </span><span class="s2">PartialEq</span><span class="s1">)]</span>
|
||
|
<a name="l12"><span class="ln">12 </span></a><span class="s0">pub struct </span><span class="s2">Camera </span><span class="s1">{</span>
|
||
|
<a name="l13"><span class="ln">13 </span></a> <span class="s0">pub </span><span class="s2">position</span><span class="s1">: </span><span class="s2">Point3</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">></span><span class="s3">,</span>
|
||
|
<a name="l14"><span class="ln">14 </span></a> <span class="s0">pub </span><span class="s2">yaw</span><span class="s1">: </span><span class="s2">Rad</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">></span><span class="s3">,</span>
|
||
|
<a name="l15"><span class="ln">15 </span></a> <span class="s0">pub </span><span class="s2">pitch</span><span class="s1">: </span><span class="s2">Rad</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">></span><span class="s3">,</span>
|
||
|
<a name="l16"><span class="ln">16 </span></a><span class="s1">}</span>
|
||
|
<a name="l17"><span class="ln">17 </span></a>
|
||
|
<a name="l18"><span class="ln">18 </span></a><span class="s0">impl </span><span class="s2">Camera </span><span class="s1">{</span>
|
||
|
<a name="l19"><span class="ln">19 </span></a> <span class="s0">pub fn </span><span class="s2">new</span><span class="s1"><</span><span class="s2">V</span><span class="s1">: </span><span class="s2">Into</span><span class="s1"><</span><span class="s2">Point3</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">>></span><span class="s3">, </span><span class="s2">Y</span><span class="s1">: </span><span class="s2">Into</span><span class="s1"><</span><span class="s2">Rad</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">>></span><span class="s3">, </span><span class="s2">P</span><span class="s1">: </span><span class="s2">Into</span><span class="s1"><</span><span class="s2">Rad</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">>>>(</span>
|
||
|
<a name="l20"><span class="ln">20 </span></a> <span class="s2">position</span><span class="s1">: </span><span class="s2">V</span><span class="s3">,</span>
|
||
|
<a name="l21"><span class="ln">21 </span></a> <span class="s2">yaw</span><span class="s1">: </span><span class="s2">Y</span><span class="s3">,</span>
|
||
|
<a name="l22"><span class="ln">22 </span></a> <span class="s2">pitch</span><span class="s1">: </span><span class="s2">P</span><span class="s3">,</span>
|
||
|
<a name="l23"><span class="ln">23 </span></a> <span class="s1">) -> </span><span class="s0">Self </span><span class="s1">{</span>
|
||
|
<a name="l24"><span class="ln">24 </span></a> <span class="s0">Self </span><span class="s1">{</span>
|
||
|
<a name="l25"><span class="ln">25 </span></a> <span class="s2">position</span><span class="s1">: </span><span class="s2">position</span><span class="s1">.</span><span class="s2">into</span><span class="s1">()</span><span class="s3">,</span>
|
||
|
<a name="l26"><span class="ln">26 </span></a> <span class="s2">yaw</span><span class="s1">: </span><span class="s2">yaw</span><span class="s1">.</span><span class="s2">into</span><span class="s1">()</span><span class="s3">,</span>
|
||
|
<a name="l27"><span class="ln">27 </span></a> <span class="s2">pitch</span><span class="s1">: </span><span class="s2">pitch</span><span class="s1">.</span><span class="s2">into</span><span class="s1">()</span><span class="s3">,</span>
|
||
|
<a name="l28"><span class="ln">28 </span></a> <span class="s1">}</span>
|
||
|
<a name="l29"><span class="ln">29 </span></a> <span class="s1">}</span>
|
||
|
<a name="l30"><span class="ln">30 </span></a>
|
||
|
<a name="l31"><span class="ln">31 </span></a> <span class="s0">pub fn </span><span class="s2">calc_matrix</span><span class="s1">(&</span><span class="s0">self</span><span class="s3">, </span><span class="s2">projection</span><span class="s1">: </span><span class="s2">cgmath</span><span class="s1">::</span><span class="s2">Matrix4</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">>) -> </span><span class="s2">Matrix4</span><span class="s1"><</span><span class="s2">f32</span><span class="s1">> {</span>
|
||
|
<a name="l32"><span class="ln">32 </span></a>
|
||
|
<a name="l33"><span class="ln">33 </span></a> <span class="s0">let </span><span class="s2">view_vector </span><span class="s1">= </span><span class="s2">Point3</span><span class="s1">::</span><span class="s2">new</span><span class="s1">(</span>
|
||
|
<a name="l34"><span class="ln">34 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s0">self</span><span class="s1">.</span><span class="s2">pitch</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">() * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">yaw</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l35"><span class="ln">35 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s0">self</span><span class="s1">.</span><span class="s2">pitch</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">cos</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l36"><span class="ln">36 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s0">self</span><span class="s1">.</span><span class="s2">pitch</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">() * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">yaw</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">cos</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l37"><span class="ln">37 </span></a> <span class="s1">)</span><span class="s3">;</span>
|
||
|
<a name="l38"><span class="ln">38 </span></a>
|
||
|
<a name="l39"><span class="ln">39 </span></a> <span class="s0">let </span><span class="s2">mx_view </span><span class="s1">= </span><span class="s2">Matrix4</span><span class="s1">::</span><span class="s2">look_at</span><span class="s1">(</span>
|
||
|
<a name="l40"><span class="ln">40 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">position</span><span class="s3">,</span>
|
||
|
<a name="l41"><span class="ln">41 </span></a> <span class="s2">Point3</span><span class="s1">::</span><span class="s2">new</span><span class="s1">(</span>
|
||
|
<a name="l42"><span class="ln">42 </span></a> <span class="s2">view_vector</span><span class="s1">.</span><span class="s2">x </span><span class="s1">+ </span><span class="s0">self</span><span class="s1">.</span><span class="s2">position</span><span class="s1">.</span><span class="s2">x</span><span class="s3">,</span>
|
||
|
<a name="l43"><span class="ln">43 </span></a> <span class="s2">view_vector</span><span class="s1">.</span><span class="s2">y </span><span class="s1">+ </span><span class="s0">self</span><span class="s1">.</span><span class="s2">position</span><span class="s1">.</span><span class="s2">y</span><span class="s3">,</span>
|
||
|
<a name="l44"><span class="ln">44 </span></a> <span class="s2">view_vector</span><span class="s1">.</span><span class="s2">z </span><span class="s1">+ </span><span class="s0">self</span><span class="s1">.</span><span class="s2">position</span><span class="s1">.</span><span class="s2">z</span><span class="s3">,</span>
|
||
|
<a name="l45"><span class="ln">45 </span></a> <span class="s1">)</span><span class="s3">,</span>
|
||
|
<a name="l46"><span class="ln">46 </span></a> <span class="s2">Vector3</span><span class="s1">::</span><span class="s2">unit_y</span><span class="s1">()</span><span class="s3">,</span>
|
||
|
<a name="l47"><span class="ln">47 </span></a> <span class="s1">)</span><span class="s3">;</span>
|
||
|
<a name="l48"><span class="ln">48 </span></a> <span class="s5">// I don't know how this works, but it limits pitching to like</span>
|
||
|
<a name="l49"><span class="ln">49 </span></a> <span class="s5">// 70 degrees. Lame</span>
|
||
|
<a name="l50"><span class="ln">50 </span></a> <span class="s5">// let mx_view = Matrix4::look_at_dir(</span>
|
||
|
<a name="l51"><span class="ln">51 </span></a> <span class="s5">// self.position,</span>
|
||
|
<a name="l52"><span class="ln">52 </span></a> <span class="s5">// Vector3::new(</span>
|
||
|
<a name="l53"><span class="ln">53 </span></a> <span class="s5">// self.yaw.0.cos(),</span>
|
||
|
<a name="l54"><span class="ln">54 </span></a> <span class="s5">// self.pitch.0.sin(),</span>
|
||
|
<a name="l55"><span class="ln">55 </span></a> <span class="s5">// self.yaw.0.sin(),</span>
|
||
|
<a name="l56"><span class="ln">56 </span></a> <span class="s5">// ).normalize(),</span>
|
||
|
<a name="l57"><span class="ln">57 </span></a> <span class="s5">// Vector3::unit_y(),</span>
|
||
|
<a name="l58"><span class="ln">58 </span></a> <span class="s5">// );</span>
|
||
|
<a name="l59"><span class="ln">59 </span></a> <span class="s0">let </span><span class="s2">mx_correction </span><span class="s1">= </span><span class="s2">OPENGL_TO_WGPU_MATRIX</span><span class="s3">;</span>
|
||
|
<a name="l60"><span class="ln">60 </span></a> <span class="s2">mx_correction </span><span class="s1">* </span><span class="s2">projection </span><span class="s1">* </span><span class="s2">mx_view</span>
|
||
|
<a name="l61"><span class="ln">61 </span></a> <span class="s1">}</span>
|
||
|
<a name="l62"><span class="ln">62 </span></a><span class="s1">}</span>
|
||
|
<a name="l63"><span class="ln">63 </span></a>
|
||
|
<a name="l64"><span class="ln">64 </span></a><span class="s1">#[</span><span class="s2">derive</span><span class="s1">(</span><span class="s2">Debug</span><span class="s1">)]</span>
|
||
|
<a name="l65"><span class="ln">65 </span></a><span class="s0">pub struct </span><span class="s2">CameraController </span><span class="s1">{</span>
|
||
|
<a name="l66"><span class="ln">66 </span></a> <span class="s2">amount_left</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l67"><span class="ln">67 </span></a> <span class="s2">amount_right</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l68"><span class="ln">68 </span></a> <span class="s2">amount_forward</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l69"><span class="ln">69 </span></a> <span class="s2">amount_backward</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l70"><span class="ln">70 </span></a> <span class="s2">amount_up</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l71"><span class="ln">71 </span></a> <span class="s2">amount_down</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l72"><span class="ln">72 </span></a> <span class="s2">rotate_horizontal</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l73"><span class="ln">73 </span></a> <span class="s2">rotate_vertical</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l74"><span class="ln">74 </span></a> <span class="s2">scroll</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l75"><span class="ln">75 </span></a> <span class="s2">speed</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l76"><span class="ln">76 </span></a> <span class="s2">sensitivity</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
|
||
|
<a name="l77"><span class="ln">77 </span></a> <span class="s2">active</span><span class="s1">: </span><span class="s2">bool</span><span class="s3">,</span>
|
||
|
<a name="l78"><span class="ln">78 </span></a><span class="s1">}</span>
|
||
|
<a name="l79"><span class="ln">79 </span></a>
|
||
|
<a name="l80"><span class="ln">80 </span></a><span class="s0">impl </span><span class="s2">CameraController </span><span class="s1">{</span>
|
||
|
<a name="l81"><span class="ln">81 </span></a> <span class="s0">pub fn </span><span class="s2">new</span><span class="s1">(</span><span class="s2">speed</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">, </span><span class="s2">sensitivity</span><span class="s1">: </span><span class="s2">f32</span><span class="s1">) -> </span><span class="s0">Self </span><span class="s1">{</span>
|
||
|
<a name="l82"><span class="ln">82 </span></a> <span class="s0">Self </span><span class="s1">{</span>
|
||
|
<a name="l83"><span class="ln">83 </span></a> <span class="s2">amount_left</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l84"><span class="ln">84 </span></a> <span class="s2">amount_right</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l85"><span class="ln">85 </span></a> <span class="s2">amount_forward</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l86"><span class="ln">86 </span></a> <span class="s2">amount_backward</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l87"><span class="ln">87 </span></a> <span class="s2">amount_up</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l88"><span class="ln">88 </span></a> <span class="s2">amount_down</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l89"><span class="ln">89 </span></a> <span class="s2">rotate_horizontal</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l90"><span class="ln">90 </span></a> <span class="s2">rotate_vertical</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l91"><span class="ln">91 </span></a> <span class="s2">scroll</span><span class="s1">: </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
|
||
|
<a name="l92"><span class="ln">92 </span></a> <span class="s2">speed</span><span class="s3">,</span>
|
||
|
<a name="l93"><span class="ln">93 </span></a> <span class="s2">sensitivity</span><span class="s3">,</span>
|
||
|
<a name="l94"><span class="ln">94 </span></a> <span class="s2">active</span><span class="s1">: </span><span class="s0">true</span>
|
||
|
<a name="l95"><span class="ln">95 </span></a> <span class="s1">}</span>
|
||
|
<a name="l96"><span class="ln">96 </span></a> <span class="s1">}</span>
|
||
|
<a name="l97"><span class="ln">97 </span></a>
|
||
|
<a name="l98"><span class="ln">98 </span></a> <span class="s0">pub fn </span><span class="s2">process_keyboard</span><span class="s1">(&</span><span class="s0">mut self</span><span class="s3">, </span><span class="s2">key</span><span class="s1">: </span><span class="s2">VirtualKeyCode</span><span class="s3">, </span><span class="s2">state</span><span class="s1">: </span><span class="s2">ElementState</span><span class="s1">) -> </span><span class="s2">bool </span><span class="s1">{</span>
|
||
|
<a name="l99"><span class="ln">99 </span></a> <span class="s0">let </span><span class="s2">amount </span><span class="s1">= </span><span class="s0">if </span><span class="s2">state </span><span class="s1">== </span><span class="s2">ElementState</span><span class="s1">::</span><span class="s2">Pressed </span><span class="s1">{</span>
|
||
|
<a name="l100"><span class="ln">100 </span></a> <span class="s4">1</span><span class="s1">.</span><span class="s4">0</span>
|
||
|
<a name="l101"><span class="ln">101 </span></a> <span class="s1">} </span><span class="s0">else </span><span class="s1">{</span>
|
||
|
<a name="l102"><span class="ln">102 </span></a> <span class="s4">0</span><span class="s1">.</span><span class="s4">0</span>
|
||
|
<a name="l103"><span class="ln">103 </span></a> <span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l104"><span class="ln">104 </span></a> <span class="s0">let </span><span class="s2">active </span><span class="s1">= </span><span class="s0">match </span><span class="s2">key </span><span class="s1">{</span>
|
||
|
<a name="l105"><span class="ln">105 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">P </span><span class="s1">=> {</span>
|
||
|
<a name="l106"><span class="ln">106 </span></a> <span class="s0">if </span><span class="s2">state </span><span class="s1">== </span><span class="s2">ElementState</span><span class="s1">::</span><span class="s2">Pressed </span><span class="s1">{</span>
|
||
|
<a name="l107"><span class="ln">107 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">active </span><span class="s1">= !</span><span class="s0">self</span><span class="s1">.</span><span class="s2">active</span><span class="s3">;</span>
|
||
|
<a name="l108"><span class="ln">108 </span></a> <span class="s1">}</span>
|
||
|
<a name="l109"><span class="ln">109 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">active</span>
|
||
|
<a name="l110"><span class="ln">110 </span></a> <span class="s1">}</span><span class="s3">,</span>
|
||
|
<a name="l111"><span class="ln">111 </span></a> <span class="s2">_ </span><span class="s1">=> {</span><span class="s0">self</span><span class="s1">.</span><span class="s2">active</span><span class="s1">}</span>
|
||
|
<a name="l112"><span class="ln">112 </span></a> <span class="s1">}</span><span class="s3">;</span>
|
||
|
<a name="l113"><span class="ln">113 </span></a> <span class="s0">if </span><span class="s2">active </span><span class="s1">{</span>
|
||
|
<a name="l114"><span class="ln">114 </span></a> <span class="s0">match </span><span class="s2">key </span><span class="s1">{</span>
|
||
|
<a name="l115"><span class="ln">115 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">W </span><span class="s1">| </span><span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">Up </span><span class="s1">=> {</span>
|
||
|
<a name="l116"><span class="ln">116 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">amount_forward </span><span class="s1">= </span><span class="s2">amount</span><span class="s3">;</span>
|
||
|
<a name="l117"><span class="ln">117 </span></a> <span class="s0">true</span>
|
||
|
<a name="l118"><span class="ln">118 </span></a> <span class="s1">}</span>
|
||
|
<a name="l119"><span class="ln">119 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">S </span><span class="s1">| </span><span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">Down </span><span class="s1">=> {</span>
|
||
|
<a name="l120"><span class="ln">120 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">amount_backward </span><span class="s1">= </span><span class="s2">amount</span><span class="s3">;</span>
|
||
|
<a name="l121"><span class="ln">121 </span></a> <span class="s0">true</span>
|
||
|
<a name="l122"><span class="ln">122 </span></a> <span class="s1">}</span>
|
||
|
<a name="l123"><span class="ln">123 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">A </span><span class="s1">| </span><span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">Left </span><span class="s1">=> {</span>
|
||
|
<a name="l124"><span class="ln">124 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">amount_left </span><span class="s1">= </span><span class="s2">amount</span><span class="s3">;</span>
|
||
|
<a name="l125"><span class="ln">125 </span></a> <span class="s0">true</span>
|
||
|
<a name="l126"><span class="ln">126 </span></a> <span class="s1">}</span>
|
||
|
<a name="l127"><span class="ln">127 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">D </span><span class="s1">| </span><span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">Right </span><span class="s1">=> {</span>
|
||
|
<a name="l128"><span class="ln">128 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">amount_right </span><span class="s1">= </span><span class="s2">amount</span><span class="s3">;</span>
|
||
|
<a name="l129"><span class="ln">129 </span></a> <span class="s0">true</span>
|
||
|
<a name="l130"><span class="ln">130 </span></a> <span class="s1">}</span>
|
||
|
<a name="l131"><span class="ln">131 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">Space </span><span class="s1">=> {</span>
|
||
|
<a name="l132"><span class="ln">132 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">amount_up </span><span class="s1">= </span><span class="s2">amount</span><span class="s3">;</span>
|
||
|
<a name="l133"><span class="ln">133 </span></a> <span class="s0">true</span>
|
||
|
<a name="l134"><span class="ln">134 </span></a> <span class="s1">}</span>
|
||
|
<a name="l135"><span class="ln">135 </span></a> <span class="s2">VirtualKeyCode</span><span class="s1">::</span><span class="s2">LShift </span><span class="s1">=> {</span>
|
||
|
<a name="l136"><span class="ln">136 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">amount_down </span><span class="s1">= </span><span class="s2">amount</span><span class="s3">;</span>
|
||
|
<a name="l137"><span class="ln">137 </span></a> <span class="s0">true</span>
|
||
|
<a name="l138"><span class="ln">138 </span></a> <span class="s1">}</span>
|
||
|
<a name="l139"><span class="ln">139 </span></a> <span class="s2">_ </span><span class="s1">=> </span><span class="s0">false</span><span class="s3">,</span>
|
||
|
<a name="l140"><span class="ln">140 </span></a> <span class="s1">}</span>
|
||
|
<a name="l141"><span class="ln">141 </span></a> <span class="s1">} </span><span class="s0">else </span><span class="s1">{</span>
|
||
|
<a name="l142"><span class="ln">142 </span></a> <span class="s0">false</span>
|
||
|
<a name="l143"><span class="ln">143 </span></a> <span class="s1">}</span>
|
||
|
<a name="l144"><span class="ln">144 </span></a> <span class="s1">}</span>
|
||
|
<a name="l145"><span class="ln">145 </span></a>
|
||
|
<a name="l146"><span class="ln">146 </span></a> <span class="s0">pub fn </span><span class="s2">process_mouse</span><span class="s1">(&</span><span class="s0">mut self</span><span class="s3">, </span><span class="s2">mouse_dx</span><span class="s1">: </span><span class="s2">f64</span><span class="s3">, </span><span class="s2">mouse_dy</span><span class="s1">: </span><span class="s2">f64</span><span class="s1">) {</span>
|
||
|
<a name="l147"><span class="ln">147 </span></a> <span class="s0">if self</span><span class="s1">.</span><span class="s2">active </span><span class="s1">{</span>
|
||
|
<a name="l148"><span class="ln">148 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">rotate_horizontal </span><span class="s1">= -</span><span class="s2">mouse_dx </span><span class="s0">as </span><span class="s2">f32</span><span class="s3">;</span>
|
||
|
<a name="l149"><span class="ln">149 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">rotate_vertical </span><span class="s1">= </span><span class="s2">mouse_dy </span><span class="s0">as </span><span class="s2">f32</span><span class="s3">;</span>
|
||
|
<a name="l150"><span class="ln">150 </span></a> <span class="s1">}</span>
|
||
|
<a name="l151"><span class="ln">151 </span></a> <span class="s1">}</span>
|
||
|
<a name="l152"><span class="ln">152 </span></a>
|
||
|
<a name="l153"><span class="ln">153 </span></a> <span class="s0">pub fn </span><span class="s2">update_camera</span><span class="s1">(&</span><span class="s0">mut self</span><span class="s3">, </span><span class="s2">camera</span><span class="s1">: &</span><span class="s0">mut </span><span class="s2">Camera</span><span class="s3">, </span><span class="s2">dt</span><span class="s1">: </span><span class="s2">f32</span><span class="s1">) {</span>
|
||
|
<a name="l154"><span class="ln">154 </span></a>
|
||
|
<a name="l155"><span class="ln">155 </span></a> <span class="s5">// Move forward/backward and left/right</span>
|
||
|
<a name="l156"><span class="ln">156 </span></a> <span class="s0">let </span><span class="s2">view_vector </span><span class="s1">= </span><span class="s2">Vector3</span><span class="s1">::</span><span class="s2">new</span><span class="s1">(</span>
|
||
|
<a name="l157"><span class="ln">157 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">() * </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">yaw</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l158"><span class="ln">158 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">cos</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l159"><span class="ln">159 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">() * </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">yaw</span><span class="s1">.</span><span class="s4">0</span><span class="s1">.</span><span class="s2">cos</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l160"><span class="ln">160 </span></a> <span class="s1">)</span><span class="s3">;</span>
|
||
|
<a name="l161"><span class="ln">161 </span></a>
|
||
|
<a name="l162"><span class="ln">162 </span></a> <span class="s5">// Offset the yaw 90 degrees and set the pitch to level for our</span>
|
||
|
<a name="l163"><span class="ln">163 </span></a> <span class="s5">// right / left hand translation vectors</span>
|
||
|
<a name="l164"><span class="ln">164 </span></a> <span class="s0">let </span><span class="s2">offset </span><span class="s1">= </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">yaw</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">+ </span><span class="s2">PI</span><span class="s1">/</span><span class="s4">2</span><span class="s1">.</span><span class="s4">0</span><span class="s3">;</span>
|
||
|
<a name="l165"><span class="ln">165 </span></a> <span class="s0">let </span><span class="s2">pitch </span><span class="s1">= </span><span class="s2">PI</span><span class="s1">/</span><span class="s4">2</span><span class="s1">.</span><span class="s4">0</span><span class="s3">;</span>
|
||
|
<a name="l166"><span class="ln">166 </span></a> <span class="s0">let </span><span class="s2">left_vector </span><span class="s1">= </span><span class="s2">Vector3</span><span class="s1">::</span><span class="s2">new</span><span class="s1">(</span>
|
||
|
<a name="l167"><span class="ln">167 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s2">pitch</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">() * </span><span class="s2">offset</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l168"><span class="ln">168 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s2">pitch</span><span class="s1">.</span><span class="s2">cos</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l169"><span class="ln">169 </span></a> <span class="s1">(</span><span class="s4">1</span><span class="s1">.</span><span class="s4">0 </span><span class="s1">* </span><span class="s2">pitch</span><span class="s1">.</span><span class="s2">sin</span><span class="s1">() * </span><span class="s2">offset</span><span class="s1">.</span><span class="s2">cos</span><span class="s1">())</span><span class="s3">,</span>
|
||
|
<a name="l170"><span class="ln">170 </span></a> <span class="s1">)</span><span class="s3">;</span>
|
||
|
<a name="l171"><span class="ln">171 </span></a>
|
||
|
<a name="l172"><span class="ln">172 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">position </span><span class="s1">+= </span><span class="s2">view_vector </span><span class="s1">* (</span><span class="s0">self</span><span class="s1">.</span><span class="s2">amount_forward </span><span class="s1">- </span><span class="s0">self</span><span class="s1">.</span><span class="s2">amount_backward</span><span class="s1">) * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">speed </span><span class="s1">* </span><span class="s2">dt</span><span class="s3">;</span>
|
||
|
<a name="l173"><span class="ln">173 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">position </span><span class="s1">+= </span><span class="s2">left_vector </span><span class="s1">* (</span><span class="s0">self</span><span class="s1">.</span><span class="s2">amount_left </span><span class="s1">- </span><span class="s0">self</span><span class="s1">.</span><span class="s2">amount_right</span><span class="s1">) * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">speed </span><span class="s1">* </span><span class="s2">dt</span><span class="s3">;</span>
|
||
|
<a name="l174"><span class="ln">174 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">position</span><span class="s1">.</span><span class="s2">y </span><span class="s1">+= (</span><span class="s0">self</span><span class="s1">.</span><span class="s2">amount_up </span><span class="s1">- </span><span class="s0">self</span><span class="s1">.</span><span class="s2">amount_down</span><span class="s1">) * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">speed </span><span class="s1">* </span><span class="s2">dt</span><span class="s3">;</span>
|
||
|
<a name="l175"><span class="ln">175 </span></a>
|
||
|
<a name="l176"><span class="ln">176 </span></a> <span class="s5">// Rotate</span>
|
||
|
<a name="l177"><span class="ln">177 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">yaw </span><span class="s1">+= </span><span class="s2">Rad</span><span class="s1">(</span><span class="s0">self</span><span class="s1">.</span><span class="s2">rotate_horizontal</span><span class="s1">) * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">sensitivity </span><span class="s1">* </span><span class="s2">dt</span><span class="s3">;</span>
|
||
|
<a name="l178"><span class="ln">178 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch </span><span class="s1">+= </span><span class="s2">Rad</span><span class="s1">(</span><span class="s0">self</span><span class="s1">.</span><span class="s2">rotate_vertical</span><span class="s1">) * </span><span class="s0">self</span><span class="s1">.</span><span class="s2">sensitivity </span><span class="s1">* </span><span class="s2">dt</span><span class="s3">;</span>
|
||
|
<a name="l179"><span class="ln">179 </span></a>
|
||
|
<a name="l180"><span class="ln">180 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">rotate_horizontal </span><span class="s1">= </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">;</span>
|
||
|
<a name="l181"><span class="ln">181 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">rotate_vertical </span><span class="s1">= </span><span class="s4">0</span><span class="s1">.</span><span class="s4">0</span><span class="s3">;</span>
|
||
|
<a name="l182"><span class="ln">182 </span></a>
|
||
|
<a name="l183"><span class="ln">183 </span></a> <span class="s5">// Keep the camera's angle from going too high/low.</span>
|
||
|
<a name="l184"><span class="ln">184 </span></a> <span class="s0">if </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch </span><span class="s1">< -</span><span class="s2">Rad</span><span class="s1">(</span><span class="s4">0</span><span class="s1">.</span><span class="s4">001</span><span class="s1">) {</span>
|
||
|
<a name="l185"><span class="ln">185 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch </span><span class="s1">= -</span><span class="s2">Rad</span><span class="s1">(</span><span class="s4">0</span><span class="s1">.</span><span class="s4">001</span><span class="s1">)</span><span class="s3">;</span>
|
||
|
<a name="l186"><span class="ln">186 </span></a> <span class="s1">} </span><span class="s0">else if </span><span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch </span><span class="s1">> </span><span class="s2">Rad</span><span class="s1">(</span><span class="s2">PI </span><span class="s1">- </span><span class="s4">0</span><span class="s1">.</span><span class="s4">001</span><span class="s1">) {</span>
|
||
|
<a name="l187"><span class="ln">187 </span></a> <span class="s2">camera</span><span class="s1">.</span><span class="s2">pitch </span><span class="s1">= </span><span class="s2">Rad</span><span class="s1">(</span><span class="s2">PI </span><span class="s1">- </span><span class="s4">0</span><span class="s1">.</span><span class="s4">001</span><span class="s1">)</span><span class="s3">;</span>
|
||
|
<a name="l188"><span class="ln">188 </span></a> <span class="s1">}</span>
|
||
|
<a name="l189"><span class="ln">189 </span></a> <span class="s1">}</span>
|
||
|
<a name="l190"><span class="ln">190 </span></a><span class="s1">}</span>
|
||
|
<a name="l191"><span class="ln">191 </span></a></pre>
|
||
|
</body>
|
||
|
</html>
|