]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bkgrnd2d/bkgrnd2d.cpp
added install.py; updated COMPILING; fixed q3 shader transparency rendering; jedi...
[xonotic/netradiant.git] / contrib / bkgrnd2d / bkgrnd2d.cpp
1 /*\r
2 Copyright (C) 2003 Reed Mideke.\r
3 \r
4 This file is part of GtkRadiant.\r
5 \r
6 GtkRadiant is free software; you can redistribute it and/or modify\r
7 it under the terms of the GNU General Public License as published by\r
8 the Free Software Foundation; either version 2 of the License, or\r
9 (at your option) any later version.\r
10 \r
11 GtkRadiant is distributed in the hope that it will be useful,\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 GNU General Public License for more details.\r
15 \r
16 You should have received a copy of the GNU General Public License\r
17 along with GtkRadiant; if not, write to the Free Software\r
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
19 */\r
20 \r
21 //\r
22 // bkgrnd2d Plugin\r
23 //\r
24 // Code by reyalP aka Reed Mideke\r
25 //\r
26 // Based on various other plugins\r
27 //\r
28 \r
29 #include "bkgrnd2d.h"\r
30 \r
31 CBackgroundRender render;\r
32 \r
33 CBackgroundImage backgroundXY(XY),backgroundXZ(XZ),backgroundYZ(YZ);\r
34 \r
35 CBackgroundRender::CBackgroundRender()\r
36 {\r
37         refCount = 1;\r
38 }\r
39 \r
40 CBackgroundRender::~CBackgroundRender()\r
41 {\r
42 }\r
43 \r
44 void CBackgroundRender::Register()\r
45 {\r
46         g_QglTable.m_pfnHookGL2DWindow( this );\r
47 }\r
48 \r
49 void CBackgroundRender::Draw2D( VIEWTYPE vt )\r
50 {\r
51         switch(vt)\r
52         {\r
53         case XY:\r
54                 backgroundXY.Render();\r
55                 break;\r
56         case XZ:\r
57                 backgroundXZ.Render();\r
58                 break;\r
59         case YZ:\r
60                 backgroundYZ.Render();\r
61                 break;\r
62         }\r
63 }\r
64 \r
65 \r
66 CBackgroundImage::CBackgroundImage(VIEWTYPE vt)\r
67 {\r
68         m_tex = NULL;\r
69         m_alpha = 0.5;\r
70 \r
71         // TODO, sensible defaults ? Or not show until we have extents ?\r
72         m_xmin = m_ymin = 0.0f;\r
73         m_xmax = m_ymax = 0.0f;\r
74 \r
75         m_bActive = false;\r
76 \r
77         m_vt = vt;\r
78 \r
79         switch(m_vt)\r
80         {\r
81                 case XY:\r
82                         m_ix = 0;\r
83                         m_iy = 1;\r
84                         break;\r
85                 case XZ:\r
86                         m_ix = 0;\r
87                         m_iy = 2;\r
88                         break;\r
89                 case YZ:\r
90                         m_ix = 1;\r
91                         m_iy = 2;\r
92                         break;\r
93         }\r
94 }\r
95 \r
96 /*\r
97  * should cleanup, but I don't think we can be sure it happens before our \r
98  * interfaces are gone\r
99 CBackgroundImage::~CBackgroundImage()\r
100 {\r
101 }\r
102 */\r
103 \r
104 void CBackgroundImage::Cleanup()\r
105 {\r
106         if(m_tex) {\r
107                 g_QglTable.m_pfn_qglDeleteTextures(1,&m_tex->texture_number);\r
108                 g_free(m_tex);\r
109                 m_tex = NULL;\r
110         }\r
111 }\r
112 \r
113 void CBackgroundImage::Render()\r
114 {\r
115         if (!m_bActive || !Valid())\r
116                 return;\r
117         g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);\r
118 \r
119         g_QglTable.m_pfn_qglEnable(GL_TEXTURE_2D);\r
120         g_QglTable.m_pfn_qglEnable(GL_BLEND);\r
121         g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
122         g_QglTable.m_pfn_qglTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);\r
123         g_QglTable.m_pfn_qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);\r
124         g_QglTable.m_pfn_qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);\r
125 \r
126         g_QglTable.m_pfn_qglPolygonMode(GL_FRONT,GL_FILL);\r
127         // TODO, just so we can tell if we end up going the wrong way\r
128         // g_QglTable.m_pfn_qglPolygonMode(GL_BACK,GL_LINE);\r
129         // TODO any other state we should not assume ?\r
130 \r
131         g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, m_tex->texture_number);\r
132         g_QglTable.m_pfn_qglBegin(GL_QUADS);\r
133 \r
134         g_QglTable.m_pfn_qglColor4f(1.0,1.0,1.0,m_alpha);\r
135         g_QglTable.m_pfn_qglTexCoord2f(0.0,1.0);\r
136         g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymin);\r
137 \r
138         g_QglTable.m_pfn_qglTexCoord2f(1.0,1.0);\r
139         g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymin);\r
140 \r
141         g_QglTable.m_pfn_qglTexCoord2f(1.0,0.0);\r
142         g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymax);\r
143 \r
144         g_QglTable.m_pfn_qglTexCoord2f(0.0,0.0);\r
145         g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymax);\r
146 \r
147         g_QglTable.m_pfn_qglEnd();\r
148         g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, 0);\r
149 \r
150         g_QglTable.m_pfn_qglPopAttrib();\r
151 }\r
152 \r
153 bool CBackgroundImage::Load(const char *filename)\r
154 {\r
155         qtexture_t *newtex;\r
156         \r
157         unsigned char *image = NULL; // gets allocated with what ? g_malloc\r
158         int width = 0, height = 0;\r
159 \r
160         g_FuncTable.m_pfnLoadImage(filename,&image,&width,&height);\r
161 \r
162         if(!image) {\r
163                 Syn_Printf(MSG_WARN "load %s failed\n",filename);\r
164                 return false;\r
165         }\r
166 \r
167 // just in case we want to build for an old version\r
168 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=900\r
169 #ifdef BKGRND2D_JPG_WORKAROUND\r
170         if ( strlen(filename) > 4 && !strcmp(".jpg",filename + strlen(filename) - 4)) {\r
171                 Syn_Printf(MSG_PREFIX ".jpg workaround, clearing alpha channel\n");\r
172                 int size = width*height*4;\r
173                 int i;\r
174                 for (i = 3; i < size; i+=4) {\r
175                         image[i] = 255;\r
176                 }\r
177         }\r
178 #endif\r
179         \r
180         //TODO bug for stored texture size\r
181         //TODO whose gl context are we in, anyway ?\r
182         newtex = g_FuncTable.m_pfnLoadTextureRGBA(image,width,height);\r
183 \r
184         g_free(image);\r
185 \r
186         if(!newtex) {\r
187                 Syn_Printf(MSG_WARN "image to texture failed\n");\r
188                 return false;\r
189         }\r
190 \r
191         Cleanup();\r
192         m_tex = newtex;\r
193 \r
194         g_FuncTable.m_pfnSysUpdateWindows(W_XY);\r
195 \r
196         return true;\r
197 }\r
198 \r
199 bool CBackgroundImage::SetExtentsMM()\r
200 {\r
201         entity_s *worldentity;\r
202         const char *val;\r
203         int xmin = 0, ymin = 0, xmax = 0, ymax = 0;\r
204 \r
205         worldentity = (entity_s *)g_FuncTable.m_pfnGetEntityHandle(0);\r
206         if(!worldentity) {\r
207                 Syn_Printf(MSG_WARN "SetExtentsMM worldspawn not found\n");\r
208                 return false;\r
209         }\r
210         //TODO val is not NULL even if key does not exist\r
211         val = g_EntityTable.m_pfnValueForKey(worldentity,"mapcoordsmins");\r
212         if(!val || !val[0]) {\r
213                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins not found\n");\r
214                 return false;\r
215         }\r
216 // we could be more robust\r
217 // note contortions due to splashs strange idea of min and max\r
218         if(sscanf(val, "%d %d",&xmin,&ymax) != 2)\r
219         {\r
220                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins malformed\n");\r
221                 return false;\r
222         }\r
223 \r
224         val = g_EntityTable.m_pfnValueForKey(worldentity,"mapcoordsmaxs");\r
225         if(!val || !val[0]) {\r
226                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs not found\n");\r
227                 return false;\r
228         }\r
229         if(sscanf(val, "%d %d",&xmax,&ymin) != 2)\r
230         {\r
231                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs malformed\n");\r
232                 return false;\r
233         }\r
234         //might do sanity check before we commit\r
235         m_xmin = (float)xmin;\r
236         m_ymin = (float)ymin;\r
237         m_xmax = (float)xmax;\r
238         m_ymax = (float)ymax;\r
239 \r
240         g_FuncTable.m_pfnSysUpdateWindows(W_XY);\r
241         return true;\r
242 }\r
243 \r
244 // TODO, this should just be exported from core\r
245 // ripped directly from radiant/select.cpp:Select_GetBounds\r
246 //\r
247 static bool get_selection_bounds (vec3_t mins, vec3_t maxs)\r
248 {\r
249         brush_t *b;\r
250         int             i;\r
251         brush_t *selected_brushes = g_DataTable.m_pfnSelectedBrushes();\r
252         //TODO should never happen\r
253         if(!selected_brushes) {\r
254           Sys_Printf (MSG_PREFIX "selected_brushes = NULL\n"); \r
255           return false;\r
256         }\r
257         // this should mean no selection\r
258         if(selected_brushes == selected_brushes->next) {\r
259           Sys_Printf (MSG_PREFIX "nothing selected\n"); \r
260 \r
261           return false;\r
262         }\r
263 \r
264         for (i=0 ; i<3 ; i++)\r
265         {\r
266                 mins[i] = 99999;\r
267                 maxs[i] = -99999;\r
268         }\r
269 \r
270         for (b=selected_brushes->next ; b != selected_brushes ; b=b->next)\r
271         {\r
272                 if (b->owner->eclass->fixedsize)\r
273                 {\r
274                         for (i=0 ; i<3 ; i++)\r
275                         {\r
276                                 if (b->owner->origin[i] < mins[i])\r
277                                         mins[i] = b->owner->origin[i];\r
278                                 if (b->owner->origin[i] > maxs[i])\r
279                                         maxs[i] = b->owner->origin[i];\r
280                         }\r
281                 }\r
282                 else\r
283                 {\r
284                         for (i=0 ; i<3 ; i++)\r
285                         {\r
286                                 if (b->mins[i] < mins[i])\r
287                                         mins[i] = b->mins[i];\r
288                                 if (b->maxs[i] > maxs[i])\r
289                                         maxs[i] = b->maxs[i];\r
290                         }\r
291                 }\r
292         }\r
293   return true;\r
294 }\r
295 \r
296 bool CBackgroundImage::SetExtentsSel()\r
297 {\r
298         vec3_t mins,maxs;\r
299 \r
300         if(!get_selection_bounds(mins,maxs)) \r
301                 return false;\r
302 \r
303         if(((int)mins[m_ix] == (int)maxs[m_ix]) ||\r
304      ((int)mins[m_iy] == (int)maxs[m_iy])) {\r
305                 Syn_Printf(MSG_PREFIX "tiny selection\n");\r
306                 return false;\r
307         }\r
308 \r
309         m_xmin = mins[m_ix];\r
310         m_ymin = mins[m_iy];\r
311         m_xmax = maxs[m_ix];\r
312         m_ymax = maxs[m_iy];\r
313 \r
314         g_FuncTable.m_pfnSysUpdateWindows(W_XY);\r
315 \r
316   return true;\r
317 }\r
318 \r