]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/eclassmodel.cpp
some updates to the Linux build system - obtained a core binary and all required...
[xonotic/netradiant.git] / plugins / entity / eclassmodel.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 #include <stdlib.h>\r
23 \r
24 #include "entity_entitymodel.h"\r
25 \r
26 //\r
27 // CEntityEclassModel implementation\r
28 //\r
29 \r
30 CEntityEclassModel::CEntityEclassModel ()\r
31 {\r
32   refCount = 1;\r
33   m_eclass = NULL;\r
34   m_model = NULL;\r
35   VectorSet(m_translate, 0,0,0);\r
36   VectorSet(m_euler, 0,0,0);\r
37   VectorSet(m_scale, 1,1,1);\r
38   VectorSet(m_pivot, 0,0,0);\r
39   m4x4_identity(m_transform);\r
40   m4x4_identity(m_inverse_transform);\r
41 }\r
42 \r
43 CEntityEclassModel::~CEntityEclassModel ()\r
44 {\r
45   if(m_name.c_str()[0] != '\0'\r
46     && m_version.c_str()[0] != '\0')\r
47     GetModelCache()->DeleteByID(m_name.c_str(), m_version.c_str());\r
48 }\r
49 \r
50 \r
51 // IRender\r
52 \r
53 void CEntityEclassModel::Draw(int state, int rflags) const\r
54 {\r
55   // push the current modelview matrix\r
56   // FIXME: put in a check for stack recursion depth..\r
57   // or avoid recursion of opengl matrix stack\r
58   g_QglTable.m_pfn_qglPushMatrix();\r
59   // apply the parent-to-local transform\r
60         g_QglTable.m_pfn_qglMultMatrixf(m_transform);\r
61 \r
62   // draw children\r
63   if(m_model && m_model->pRender)\r
64   {\r
65     m_model->pRender->Draw(state, rflags);\r
66   }\r
67  \r
68   g_QglTable.m_pfn_qglPopMatrix();\r
69 }\r
70 \r
71 // ISelect\r
72 \r
73 bool CEntityEclassModel::TestRay(const ray_t *ray, vec_t *dist) const\r
74 {\r
75   vec_t dist_start = *dist;\r
76   vec_t dist_local = *dist;\r
77         ray_t ray_local = *ray;\r
78 \r
79   if (aabb_intersect_ray(&m_BBox, &ray_local, &dist_local))\r
80     *dist = dist_local;\r
81   return *dist < dist_start;\r
82 }\r
83 \r
84 \r
85 //IEdit\r
86 \r
87 void CEntityEclassModel::Translate(const vec3_t translation)\r
88 {\r
89   VectorIncrement(translation, m_translate);\r
90   UpdateCachedData();\r
91 }\r
92 \r
93 void CEntityEclassModel::Rotate(const vec3_t pivot, const vec3_t rotation)\r
94 {\r
95   m4x4_t rotation_matrix;\r
96 \r
97   m4x4_identity(rotation_matrix);\r
98   m4x4_pivoted_rotate_by_vec3(rotation_matrix, rotation, eXYZ, pivot);\r
99   m4x4_transform_point(rotation_matrix, m_translate);\r
100 \r
101   VectorIncrement(rotation, m_euler);\r
102 \r
103   UpdateCachedData();\r
104 }\r
105 \r
106 void CEntityEclassModel::OnKeyValueChanged(entity_t *e, const char *key, const char* value)\r
107 {\r
108   if(strcmp(key,"origin") == 0)\r
109   {\r
110     sscanf(value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2]); \r
111     UpdateCachedData();\r
112   } \r
113   else if(strcmp(key,"angle") == 0)\r
114   {\r
115     VectorSet(m_euler, 0, 0, (float) atof(value));\r
116     UpdateCachedData();\r
117   }\r
118 }\r
119 \r
120 void CEntityEclassModel::SetEclass(const eclass_t* eclass)\r
121 {\r
122   m_eclass = eclass;\r
123 }\r
124 \r
125 void CEntityEclassModel::SetName(const char *name)\r
126 {\r
127   if(strcmp(m_name.c_str(), name) == 0)\r
128     return;\r
129 \r
130   if(m_name.c_str()[0] != '\0'\r
131     && m_version.c_str()[0] != '\0')\r
132     GetModelCache()->DeleteByID(m_name.c_str(), m_version.c_str());\r
133 \r
134   m_model = NULL;\r
135   m_name = name;\r
136 \r
137   if(m_name.c_str()[0] != '\0')\r
138   {\r
139     const char* dot = strrchr(m_name.c_str(), '.');\r
140     if(dot != NULL)\r
141     {\r
142       m_version = ++dot;\r
143       m_model = GetModelCache()->GetByID(m_name.c_str(), m_version.c_str());\r
144     }\r
145   }\r
146 \r
147   UpdateCachedData();\r
148 }\r
149 \r
150 //\r
151 // CEntityEclassModel\r
152 //\r
153 \r
154 // private:\r
155 \r
156 void CEntityEclassModel::UpdateCachedData()\r
157 {\r
158   aabb_t aabb_temp;\r
159 \r
160   aabb_clear(&aabb_temp);\r
161 \r
162   m4x4_identity(m_transform);\r
163   m4x4_pivoted_transform_by_vec3(m_transform, m_translate, m_euler, eXYZ, m_scale, m_pivot);\r
164   memcpy(m_inverse_transform, m_transform, sizeof(m4x4_t));\r
165   if(m4x4_invert(m_inverse_transform) == 1) {\r
166     Sys_Printf("ERROR: Singular Matrix, cannot invert");\r
167   }\r
168 \r
169   if(m_eclass)\r
170     aabb_construct_for_vec3(&aabb_temp, m_eclass->mins, m_eclass->maxs);\r
171   else\r
172     VectorSet(aabb_temp.extents, 8, 8, 8);\r
173 \r
174   aabb_for_transformed_aabb(&m_BBox, &aabb_temp, m_transform);\r
175 }\r