1 /* 2 Copyright (c) 2017-2018 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.graphics.fpcamera; 29 30 import derelict.opengl.gl; 31 32 import dlib.core.memory; 33 import dlib.math.vector; 34 import dlib.math.matrix; 35 import dlib.math.transformation; 36 import dlib.math.utils; 37 38 import dagon.core.ownership; 39 import dagon.logics.entity; 40 import dagon.logics.controller; 41 42 class FirstPersonCamera: EntityController 43 { 44 Matrix4x4f transformation; 45 Matrix4x4f weaponTransformation; 46 Matrix4x4f characterMatrix; 47 Matrix4x4f invTransformation; 48 Vector3f position; 49 Vector3f eyePosition; 50 Vector3f weaponPosition; 51 52 float turn = 0.0f; 53 float pitch = 0.0f; 54 float roll = 0.0f; 55 56 float weaponPitch = 0.0f; 57 float weaponRoll = 0.0f; 58 59 float weaponPitchCoef = 1.0f; 60 61 this(Entity e) 62 { 63 super(e); 64 this.position = e.position; 65 eyePosition = Vector3f(0.0f, 1.0f, 0.0f); 66 weaponPosition = Vector3f(0.0f, 0.0f, -1.0f); 67 transformation = worldTrans(); 68 invTransformation = transformation.inverse; 69 weaponTransformation = transformation * translationMatrix(weaponPosition); 70 } 71 72 Matrix4x4f worldTrans() 73 { 74 Matrix4x4f m = translationMatrix(position + eyePosition); 75 m *= rotationMatrix(Axis.y, degtorad(turn)); 76 characterMatrix = m; 77 m *= rotationMatrix(Axis.x, degtorad(pitch)); 78 m *= rotationMatrix(Axis.z, degtorad(roll)); 79 return m; 80 } 81 82 override void update(double dt) 83 { 84 transformation = worldTrans(); 85 invTransformation = transformation.inverse; 86 87 weaponTransformation = translationMatrix(position + eyePosition); 88 weaponTransformation *= rotationMatrix(Axis.y, degtorad(turn)); 89 weaponTransformation *= rotationMatrix(Axis.x, degtorad(weaponPitch)); 90 weaponTransformation *= rotationMatrix(Axis.z, degtorad(weaponRoll)); 91 weaponTransformation *= translationMatrix(weaponPosition); 92 93 entity.transformation = transformation; 94 entity.invTransformation = invTransformation; 95 } 96 97 Matrix4x4f viewMatrix() 98 { 99 return invTransformation; 100 } 101 102 Matrix4x4f invViewMatrix() 103 { 104 return transformation; 105 } 106 } 107