1 /* 2 Copyright (c) 2014-2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.graphics.entity; 30 31 import std.string; 32 33 import derelict.opengl.gl; 34 import dlib.core.memory; 35 import dlib.math.vector; 36 import dlib.math.matrix; 37 import dlib.math.affine; 38 import dlib.math.quaternion; 39 import dlib.geometry.aabb; 40 import dgl.core.interfaces; 41 import dgl.graphics.object3d; 42 import dgl.graphics.mesh; 43 import dgl.dml.dml; 44 45 class Entity: Object3D 46 { 47 int id; 48 string name; 49 uint type = 0; 50 int materialId = -1; 51 int meshId = -1; 52 bool debugDraw = false; 53 54 Drawable drawable; 55 Modifier modifier; 56 57 Vector3f position; 58 Quaternionf rotation; 59 Vector3f scaling; 60 61 Matrix4x4f transformation; 62 63 DMLData props; 64 65 bool visible = true; 66 67 this(Drawable drw, Vector3f pos) 68 { 69 position = pos; 70 rotation = Quaternionf.identity; 71 scaling = Vector3f(1, 1, 1); 72 transformation = translationMatrix(position); 73 drawable = drw; 74 } 75 76 this(Vector3f pos) 77 { 78 position = pos; 79 rotation = Quaternionf.identity; 80 scaling = Vector3f(1, 1, 1); 81 transformation = translationMatrix(position); 82 drawable = null; 83 } 84 85 this() 86 { 87 position = Vector3f(0, 0, 0); 88 rotation = Quaternionf.identity; 89 scaling = Vector3f(1, 1, 1); 90 setTransformation(position, rotation, scaling); 91 drawable = null; 92 } 93 94 void setTransformation(Vector3f pos, Quaternionf rot, Vector3f scal) 95 { 96 position = pos; 97 rotation = rot; 98 scaling = scal; 99 transformation = 100 translationMatrix(pos) * 101 rot.toMatrix4x4 * 102 scaleMatrix(scaling); 103 } 104 105 override Vector3f getPosition() 106 { 107 return transformation.translation; 108 } 109 110 Quaternionf getRotation() 111 { 112 //Quaternionf rot; 113 //rot.fromMatrix(transformation); 114 //return rot; 115 return rotation; 116 } 117 118 Vector3f getScaling() 119 { 120 //return transformation.scaling; 121 return scaling; 122 } 123 124 override AABB getAABB() 125 { 126 // TODO: calculate actual size 127 return AABB(transformation.translation, Vector3f(1, 1, 1)); 128 } 129 130 override void draw(double dt) 131 { 132 if (visible) 133 { 134 glPushMatrix(); 135 glMultMatrixf(transformation.arrayof.ptr); 136 drawModel(dt); 137 glPopMatrix(); 138 } 139 } 140 141 void drawModel(double dt) 142 { 143 if (modifier !is null) 144 modifier.bind(dt); 145 if (drawable !is null) 146 { 147 Drawable3D drw3d = cast(Drawable3D)drawable; 148 if (drw3d) 149 drw3d.draw(this, dt); 150 else 151 drawable.draw(dt); 152 } 153 else if (debugDraw) 154 { 155 drawPoint(); 156 } 157 if (modifier !is null) 158 modifier.unbind(); 159 } 160 161 void drawPoint() 162 { 163 glColor4f(1,1,1,1); 164 glPointSize(5.0f); 165 glBegin(GL_POINTS); 166 glVertex3f(0, 0, 0); 167 glEnd(); 168 glPointSize(1.0f); 169 } 170 171 override string toString() 172 { 173 return format( 174 "type = %s\n" 175 "materialId = %s\n" 176 "meshId = %s\n" 177 "position = %s\n" 178 "rotation = %s\n" 179 "scaling = %s", 180 type, 181 materialId, 182 meshId, 183 getPosition(), 184 getRotation(), 185 getScaling() 186 ); 187 } 188 189 override void free() 190 { 191 Delete(this); 192 } 193 194 ~this() 195 { 196 // TODO: test if name belongs to GC 197 if (name.length) 198 Delete(name); 199 props.free(); 200 } 201 }