]> icculus.org git repositories - divverent/netradiant.git/blob - contrib/prtview/prtview.cpp
fix OpenGL erorr bug on XP software renderer; use svn:ignore
[divverent/netradiant.git] / contrib / prtview / prtview.cpp
1 /*
2 PrtView plugin for GtkRadiant
3 Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21 #include "prtview.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "profile/profile.h"
26
27 #include "qerplugin.h"
28 #include "iscenegraph.h"
29 #include "iglrender.h"
30 #include "iplugin.h"
31 #include "stream/stringstream.h"
32
33 #include "portals.h"
34 #include "AboutDialog.h"
35 #include "ConfigDialog.h"
36 #include "LoadPortalFileDialog.h"
37
38 #define Q3R_CMD_SPLITTER "-"
39 #define Q3R_CMD_ABOUT "About Portal Viewer"
40 #define Q3R_CMD_LOAD "Load .prt file"
41 #define Q3R_CMD_RELEASE "Unload .prt file"
42 #define Q3R_CMD_SHOW_3D "Toggle portals (3D)"
43 #define Q3R_CMD_SHOW_2D "Toggle portals (2D)"
44 #define Q3R_CMD_OPTIONS "Configure Portal Viewer"
45
46 CopiedString INIfn;
47
48 /////////////////////////////////////////////////////////////////////////////
49 // CPrtViewApp construction
50
51 #define RENDER_2D "Render2D"
52 #define WIDTH_2D "Width2D"
53 #define AA_2D "AntiAlias2D"
54 #define COLOR_2D "Color2D"
55
56 #define RENDER_3D "Render3D"
57 #define WIDTH_3D "Width3D"
58 #define AA_3D "AntiAlias3D"
59 #define COLOR_3D "Color3D"
60 #define COLOR_FOG "ColorFog"
61 #define FOG "Fog"
62 #define ZBUFFER "ZBuffer"
63 #define POLYGON "Polygons"
64 #define LINE "Lines"
65 #define TRANS_3D "Transparency"
66 #define CLIP_RANGE "ClipRange"
67 #define CLIP "Clip"
68
69
70 void PrtView_construct()
71 {
72   StringOutputStream tmp(64);
73   tmp << GlobalRadiant().getSettingsPath() << "prtview.ini";
74   INIfn = tmp.c_str();
75
76   portals.show_2d = INIGetInt(RENDER_2D, FALSE) ? true : false;
77   portals.aa_2d = INIGetInt(AA_2D, FALSE) ? true : false;
78   portals.width_2d = (float)INIGetInt(WIDTH_2D, 10);
79   portals.color_2d = (PackedColour)INIGetInt(COLOR_2D, RGB(0, 0, 255)) & 0xFFFFFF;
80
81   if (portals.width_2d > 40.0f)
82     portals.width_2d = 40.0f;
83   else if (portals.width_2d < 2.0f)
84     portals.width_2d = 2.0f;
85
86   portals.show_3d = INIGetInt(RENDER_3D, TRUE) ? true : false;
87
88   portals.zbuffer = INIGetInt(ZBUFFER, 1);
89   portals.fog = INIGetInt(FOG, FALSE) ? true : false;
90   portals.polygons = INIGetInt(POLYGON, TRUE);
91   portals.lines = INIGetInt(LINE, TRUE);
92   portals.aa_3d = INIGetInt(AA_3D, FALSE) ? true : false;
93   portals.width_3d = (float)INIGetInt(WIDTH_3D, 4);
94   portals.color_3d = (PackedColour)INIGetInt(COLOR_3D, RGB(255, 255, 0)) & 0xFFFFFF;
95   portals.color_fog = (PackedColour)INIGetInt(COLOR_FOG, RGB(127, 127, 127)) & 0xFFFFFF;
96   portals.trans_3d = (float)INIGetInt(TRANS_3D, 50);
97   portals.clip = INIGetInt(CLIP, FALSE) ? true : false;
98   portals.clip_range = (float)INIGetInt(CLIP_RANGE, 16);
99
100   if (portals.clip_range < 1)
101     portals.clip_range = 1;
102   else if (portals.clip_range > 128)
103     portals.clip_range = 128;
104
105   if (portals.zbuffer < 0)
106     portals.zbuffer = 0;
107   else if (portals.zbuffer > 2)
108     portals.zbuffer = 0;
109
110   if (portals.width_3d > 40.0f)
111     portals.width_3d = 40.0f;
112   else if (portals.width_3d < 2.0f)
113     portals.width_3d = 2.0f;
114
115   if (portals.trans_3d > 100.0f)
116     portals.trans_3d = 100.0f;
117   else if (portals.trans_3d < 0.0f)
118     portals.trans_3d = 0.0f;
119
120   SaveConfig();
121
122   portals.FixColors();
123   
124   Portals_constructShaders();
125   GlobalShaderCache().attachRenderable(render);
126 }
127
128 void PrtView_destroy()
129 {
130   GlobalShaderCache().detachRenderable(render);
131   Portals_destroyShaders();
132 }
133
134 void SaveConfig () 
135 {
136   INISetInt(RENDER_2D, portals.show_2d, "Draw in 2D windows");
137   INISetInt(WIDTH_2D, (int)portals.width_2d, "Width of lines in 2D windows (in units of 1/2)");
138   INISetInt(COLOR_2D, (int)portals.color_2d, "Color of lines in 2D windows");
139   INISetInt(AA_2D, portals.aa_2d, "Draw lines in 2D window anti-aliased");
140
141   INISetInt(ZBUFFER, portals.zbuffer, "ZBuffer level in 3D window");
142   INISetInt(FOG, portals.fog, "Use depth cueing in 3D window");
143   INISetInt(POLYGON, portals.polygons, "Render using polygons polygons in 3D window");
144   INISetInt(LINE, portals.polygons, "Render using lines in 3D window");
145   INISetInt(RENDER_3D, portals.show_3d, "Draw in 3D windows");
146   INISetInt(WIDTH_3D, (int)portals.width_3d, "Width of lines in 3D window (in units of 1/2)");
147   INISetInt(COLOR_3D, (int)portals.color_3d, "Color of lines/polygons in 3D window");
148   INISetInt(COLOR_FOG, (int)portals.color_fog, "Color of distant lines/polygons in 3D window");
149   INISetInt(AA_3D, portals.aa_3d, "Draw lines in 3D window anti-aliased");
150   INISetInt(TRANS_3D, (int)portals.trans_3d, "Transparency in 3d view (0 = solid, 100 = invisible)");
151   INISetInt(CLIP, portals.clip, "Cubic clipper active for portal viewer");
152   INISetInt(CLIP_RANGE, (int)portals.clip_range, "Portal viewer cubic clip distance (in units of 64)");
153 }
154
155
156 #define CONFIG_SECTION "Configuration"
157
158 int INIGetInt(char *key, int def)
159 {
160   char value[1024];
161
162   if (read_var (INIfn.c_str(), CONFIG_SECTION, key, value))
163     return atoi (value);
164   else
165     return def;
166 }
167
168 void INISetInt(char *key, int val, char *comment /* = NULL */)
169 {
170   char s[1000];
171
172   if(comment)
173     sprintf(s, "%d        ; %s", val, comment);
174   else
175     sprintf(s, "%d", val);
176   save_var (INIfn.c_str(), CONFIG_SECTION, key, s);
177 }
178
179
180 // plugin name
181 static const char *PLUGIN_NAME = "Portal Viewer";
182 // commands in the menu
183 static const char *PLUGIN_COMMANDS =
184         Q3R_CMD_ABOUT ";"
185         Q3R_CMD_SPLITTER ";"
186         Q3R_CMD_OPTIONS ";"
187         Q3R_CMD_SPLITTER ";"
188         Q3R_CMD_SHOW_2D ";"
189         Q3R_CMD_SHOW_3D ";"
190         Q3R_CMD_SPLITTER ";"
191         Q3R_CMD_RELEASE ";"
192         Q3R_CMD_LOAD;
193
194
195
196 const char* QERPlug_Init (void *hApp, void* pMainWidget)
197 {  
198   return "Portal Viewer for Q3Radiant";
199 }
200
201 const char* QERPlug_GetName()
202 {
203   return PLUGIN_NAME;
204 }
205
206 const char* QERPlug_GetCommandList()
207 {
208   return PLUGIN_COMMANDS;
209 }
210
211
212 const char* QERPlug_GetCommandTitleList()
213 {
214   return "";
215 }
216
217
218 void QERPlug_Dispatch(const char* p, float* vMin, float* vMax, bool bSingleBrush)
219 {
220   globalOutputStream() << MSG_PREFIX "Command \"" << p << "\"\n";
221
222   if (!strcmp(p,Q3R_CMD_ABOUT))
223   {
224     DoAboutDlg ();
225   }
226   else if (!strcmp(p,Q3R_CMD_LOAD))
227   {
228     if (DoLoadPortalFileDialog () == IDOK)
229     {
230       portals.Load();
231       SceneChangeNotify();
232     }
233     else
234     {
235       globalOutputStream() << MSG_PREFIX "Portal file load aborted.\n";
236     }
237   }
238   else if (!strcmp(p,Q3R_CMD_RELEASE))
239   {
240     portals.Purge();
241
242     SceneChangeNotify();
243
244     globalOutputStream() << MSG_PREFIX "Portals unloaded.\n";
245   }
246   else if (!strcmp(p,Q3R_CMD_SHOW_2D))
247   {
248     portals.show_2d = !portals.show_2d;
249
250     SceneChangeNotify();
251     SaveConfig();
252
253     if(portals.show_2d)
254       globalOutputStream() << MSG_PREFIX "Portals will be rendered in 2D view.\n";
255     else
256       globalOutputStream() << MSG_PREFIX "Portals will NOT be rendered in 2D view.\n";
257   }
258   else if (!strcmp(p,Q3R_CMD_SHOW_3D))
259   {
260     portals.show_3d = !portals.show_3d;
261     SaveConfig();
262
263     SceneChangeNotify();
264
265     if (portals.show_3d)
266       globalOutputStream() << MSG_PREFIX "Portals will be rendered in 3D view.\n";
267     else
268       globalOutputStream() << MSG_PREFIX "Portals will NOT be rendered in 3D view.\n";
269   }
270   else if (!strcmp(p,Q3R_CMD_OPTIONS))
271   {
272     DoConfigDialog ();
273     SaveConfig();
274
275     SceneChangeNotify();
276   }
277 }
278
279
280 #include "modulesystem/singletonmodule.h"
281
282 class PrtViewPluginDependencies :
283   public GlobalSceneGraphModuleRef,
284   public GlobalRadiantModuleRef,
285   public GlobalShaderCacheModuleRef,
286   public GlobalOpenGLModuleRef, 
287   public GlobalOpenGLStateLibraryModuleRef
288 {
289 };
290
291 class PrtViewPluginModule
292 {
293   _QERPluginTable m_plugin;
294 public:
295   typedef _QERPluginTable Type;
296   STRING_CONSTANT(Name, "prtview");
297
298   PrtViewPluginModule()
299   {
300     m_plugin.m_pfnQERPlug_Init = QERPlug_Init;
301     m_plugin.m_pfnQERPlug_GetName = QERPlug_GetName;
302     m_plugin.m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
303     m_plugin.m_pfnQERPlug_GetCommandTitleList = QERPlug_GetCommandTitleList;
304     m_plugin.m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
305
306     PrtView_construct();
307   }
308   ~PrtViewPluginModule()
309   {
310     PrtView_destroy();
311   }
312   _QERPluginTable* getTable()
313   {
314     return &m_plugin;
315   }
316 };
317
318 typedef SingletonModule<PrtViewPluginModule, PrtViewPluginDependencies> SingletonPrtViewPluginModule;
319
320 SingletonPrtViewPluginModule g_PrtViewPluginModule;
321
322
323 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
324 {
325   initialiseModule(server);
326
327   g_PrtViewPluginModule.selfRegister();
328 }