]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/spritemodel/spritemodel.cpp
set eol-style
[xonotic/netradiant.git] / plugins / spritemodel / spritemodel.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 //\r
23 // Sprite Model Plugin\r
24 //\r
25 // Code by Hydra aka Dominic Clifton\r
26 //\r
27 // Based on MD3Model source code by SPoG\r
28 //\r
29 \r
30 #include "spritemodel.h"\r
31 \r
32 void LoadSpriteModel(entity_interfaces_t *interfaces, const char *name)\r
33 {\r
34   IShader *pShader;\r
35 \r
36   pShader = QERApp_Shader_ForName(name);\r
37 \r
38   if (!pShader)\r
39   {\r
40     Sys_Printf("ERROR: can't find shader (or image) for: %s\n", name );\r
41     return;\r
42   }\r
43 \r
44   CSpriteModel *model = new CSpriteModel();\r
45   model->Construct(pShader);\r
46   interfaces->pRender = (IRender*)model;\r
47   interfaces->pRender->IncRef();\r
48   //interfaces->pSelect = (ISelect*)model;\r
49   //interfaces->pSelect->IncRef();\r
50   interfaces->pSelect = NULL;\r
51   interfaces->pEdit = NULL;\r
52   model->DecRef();\r
53 \r
54 }\r
55 \r
56 void CSpriteModel::Construct(IShader *pShader)\r
57 {\r
58   m_pShader = pShader;\r
59   aabb_clear(&m_BBox);\r
60   /*\r
61         md3Surface_t *pSurface = (md3Surface_t *)(((unsigned char *)pHeader) + pHeader->ofsSurfaces);\r
62   m_nSurfaces = pHeader->numSurfaces;\r
63   CMD3Surface* surfaces = new CMD3Surface[m_nSurfaces];\r
64   for (int i = 0; i < m_nSurfaces; i++ )\r
65         {\r
66     surfaces[i].Construct(pSurface);\r
67                 pSurface = (md3Surface_t *) ((( char * ) pSurface) + pSurface->ofsEnd);\r
68   }\r
69   m_children = surfaces;\r
70   AccumulateBBox();\r
71   */\r
72 }\r
73 \r
74 CSpriteModel::CSpriteModel()\r
75 {\r
76   refCount = 1;\r
77   //m_nSurfaces = 0;\r
78   //m_children = NULL;\r
79   m_pShader = NULL;\r
80 }\r
81 \r
82 CSpriteModel::~CSpriteModel()\r
83 {\r
84   // if(m_children) delete[] m_children;\r
85   if (m_pShader)\r
86     m_pShader->DecRef();\r
87 }\r
88 \r
89 void CSpriteModel::Draw(int state, int rflags) const\r
90 {\r
91 \r
92 /*\r
93   // Draw a point in the middle of the bbox\r
94   vec3_t middle = {0,0,0};\r
95   g_QglTable.m_pfn_qglPointSize (4);\r
96   g_QglTable.m_pfn_qglColor3f (0,1,0);\r
97   g_QglTable.m_pfn_qglBegin (GL_POINTS);\r
98   g_QglTable.m_pfn_qglVertex3fv (middle);\r
99   g_QglTable.m_pfn_qglEnd ();\r
100 */\r
101 \r
102   qtexture_t    *q = m_pShader->getTexture();\r
103 \r
104   // convert pixels to units and divide in half again so we draw in the middle\r
105   // of the bbox.\r
106   int h = q->height / 8;\r
107   int w = q->width / 8;\r
108 \r
109   // setup opengl stuff\r
110 \r
111   g_QglTable.m_pfn_qglPushAttrib (GL_ALL_ATTRIB_BITS);  // GL_ENABLE_BIT\r
112   //g_QglTable.m_pfn_qglColor3f (1,1,1);   //testing\r
113   //g_QglTable.m_pfn_qglColor4f (1,1,1,1); //testing\r
114   g_QglTable.m_pfn_qglBindTexture (GL_TEXTURE_2D, q->texture_number);\r
115 \r
116   //g_QglTable.m_pfn_qglEnable (GL_TEXTURE_2D); // FIXME: ? this forces textures, even in wireframe mode, bad... ?\r
117 \r
118   g_QglTable.m_pfn_qglAlphaFunc (GL_LESS, 1);\r
119   g_QglTable.m_pfn_qglEnable (GL_ALPHA_TEST);\r
120 \r
121   // get rid of this when sprite always faces camera\r
122   g_QglTable.m_pfn_qglDisable(GL_CULL_FACE);\r
123   g_QglTable.m_pfn_qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);\r
124 \r
125   // draw the sprite\r
126 \r
127 #if 0\r
128   // using x/y axis, it appears FLAT without the proper transform and rotation.\r
129 \r
130   g_QglTable.m_pfn_qglBegin(GL_QUADS);\r
131   g_QglTable.m_pfn_qglTexCoord2f (0,0);\r
132   g_QglTable.m_pfn_qglVertex3f (0-w,0-h, 0);\r
133   g_QglTable.m_pfn_qglTexCoord2f (1,0);\r
134   g_QglTable.m_pfn_qglVertex3f ( w,0-h, 0);\r
135   g_QglTable.m_pfn_qglTexCoord2f (1,1);\r
136   g_QglTable.m_pfn_qglVertex3f ( w, h, 0);\r
137   g_QglTable.m_pfn_qglTexCoord2f (0,1);\r
138   g_QglTable.m_pfn_qglVertex3f (0-w, h, 0);\r
139   g_QglTable.m_pfn_qglEnd ();\r
140 #else\r
141 \r
142   // so draw it using y/z instead.\r
143   g_QglTable.m_pfn_qglBegin(GL_QUADS);\r
144   g_QglTable.m_pfn_qglTexCoord2f (0,0);\r
145   g_QglTable.m_pfn_qglVertex3f (0,w,h);\r
146   g_QglTable.m_pfn_qglTexCoord2f (1,0);\r
147   g_QglTable.m_pfn_qglVertex3f (0,0-w,h);\r
148   g_QglTable.m_pfn_qglTexCoord2f (1,1);\r
149   g_QglTable.m_pfn_qglVertex3f (0,0-w,0-h);\r
150   g_QglTable.m_pfn_qglTexCoord2f (0,1);\r
151   g_QglTable.m_pfn_qglVertex3f (0,w,0-h);\r
152   g_QglTable.m_pfn_qglEnd ();\r
153 #endif\r
154 \r
155   g_QglTable.m_pfn_qglBindTexture (GL_TEXTURE_2D, 0);\r
156   g_QglTable.m_pfn_qglPopAttrib();\r
157 }\r
158 \r
159 /*\r
160 bool CSpriteModel::TestRay(const ray_t *ray, vec_t *dist) const\r
161 {\r
162   vec_t depth_start = *dist;\r
163   vec_t depth_local = *dist;\r
164 \r
165   if (aabb_test_ray(&m_BBox, ray) == 0)\r
166     return false;\r
167 \r
168   for(int i=0; i<m_nSurfaces; i++)\r
169   {\r
170     if(m_children[i].TestRay(ray, &depth_local))\r
171     {\r
172       if (depth_local < *dist) *dist = depth_local;\r
173     }\r
174   }\r
175 \r
176   return *dist < depth_start;\r
177 }\r
178 */\r