]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rmisc.c
-safe now does something approximately close to correct. Not a full fix
[divverent/darkplaces.git] / gl_rmisc.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_misc.c
21
22 #include "quakedef.h"
23
24
25 /*
26 ===============
27 R_Envmap_f
28
29 Grab six views for environment mapping tests
30 ===============
31 */
32 float CalcFov (float fov_x, float width, float height);
33 struct
34 {
35         float angles[3];
36         char *name;
37 }
38 envmapinfo[6] =
39 {
40         {{  0,   0, 0}, "ft"},
41         {{  0,  90, 0}, "rt"},
42         {{  0, 180, 0}, "bk"},
43         {{  0, 270, 0}, "lf"},
44         {{-90,  90, 0}, "up"},
45         {{ 90,  90, 0}, "dn"}
46 };
47 void R_Envmap_f (void)
48 {
49         int             i, size;
50         char    filename[256];
51         char    basename[256];
52         byte    *buffer, gamma[256];
53
54         if (Cmd_Argc() != 3)
55         {
56                 Con_Printf ("envmap <basename> <size>: save out 6 cubic environment map images, usable with loadsky, note that size must one of 128, 256, 512, or 1024 and can't be bigger than your current resolution\n");
57                 return;
58         }
59
60         if (!r_render.value)
61                 return;
62
63         strcpy(basename, Cmd_Argv(1));
64         size = atoi(Cmd_Argv(2));
65         if (size != 128 && size != 256 && size != 512 && size != 1024)
66         {
67                 Con_Printf("envmap: size must be one of 128, 256, 512, or 1024\n");
68                 return;
69         }
70         if (size > vid.realwidth || size > vid.realheight)
71         {
72                 Con_Printf("envmap: your resolution is not big enough to render that size\n");
73                 return;
74         }
75
76         buffer = malloc(size*size*3);
77         if (buffer == NULL)
78         {
79                 Con_Printf("envmap: unable to allocate memory for image\n");
80                 return;
81         }
82
83         BuildGammaTable8((lighthalf && hardwaregammasupported) ? 2.0f : 1.0f, 1, 1, 0, gamma);
84
85 //      glDrawBuffer  (GL_FRONT);
86 //      glReadBuffer  (GL_FRONT);
87         glDrawBuffer  (GL_BACK);
88         glReadBuffer  (GL_BACK);
89         envmap = true;
90
91         r_refdef.x = 0;
92         r_refdef.y = 0;
93         r_refdef.width = size;
94         r_refdef.height = size;
95
96         r_refdef.fov_x = 90;
97         r_refdef.fov_y = 90;
98
99         for (i = 0;i < 6;i++)
100         {
101                 VectorCopy(envmapinfo[i].angles, r_refdef.viewangles);
102                 glClearColor(0,0,0,0);
103                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: clear the screen (around the view as well)
104                 R_RenderView ();
105                 glReadPixels (0, 0, size, size, GL_RGB, GL_UNSIGNED_BYTE, buffer);
106                 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[i].name);
107                 Image_GammaRemapRGB(buffer, buffer, size * size, gamma, gamma, gamma);
108                 Image_WriteTGARGB_preflipped(filename, size, size, buffer);
109         }
110
111         envmap = false;
112         glDrawBuffer  (GL_BACK);
113         glReadBuffer  (GL_BACK);
114
115         free(buffer);
116
117         // cause refdef to be fixed
118 //      vid.recalc_refdef = 1;
119 }
120
121 static void gl_misc_start(void)
122 {
123 }
124
125 static void gl_misc_shutdown(void)
126 {
127 }
128
129 static void gl_misc_newmap(void)
130 {
131 }
132
133 /*
134 ===============
135 R_Init
136 ===============
137 */
138 static void R_TimeRefresh_f (void);
139 void GL_Misc_Init (void)
140 {
141         Cmd_AddCommand ("envmap", R_Envmap_f);
142         Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
143
144         R_RegisterModule("GL_Misc", gl_misc_start, gl_misc_shutdown, gl_misc_newmap);
145 }
146
147 extern void GL_BuildLightmaps (void);
148
149 /*
150 ===============
151 R_NewMap
152 ===============
153 */
154 void R_NewMap (void)
155 {
156         int             i;
157
158         for (i=0 ; i<256 ; i++)
159                 d_lightstylevalue[i] = 264;             // normal light value
160
161         r_viewleaf = NULL;
162         R_Modules_NewMap();
163
164         GL_BuildLightmaps ();
165
166         SHOWLMP_clear();
167 }
168
169
170 /*
171 ====================
172 R_TimeRefresh_f
173
174 For program optimization
175 ====================
176 */
177 qboolean intimerefresh = 0;
178 static void R_TimeRefresh_f (void)
179 {
180         int                     i;
181         float           start, stop, time;
182
183         intimerefresh = 1;
184         start = Sys_DoubleTime ();
185         glDrawBuffer (GL_FRONT);
186         for (i = 0;i < 128;i++)
187         {
188                 r_refdef.viewangles[0] = 0;
189                 r_refdef.viewangles[1] = i/128.0*360.0;
190                 r_refdef.viewangles[2] = 0;
191                 R_RenderView();
192         }
193         glDrawBuffer  (GL_BACK);
194
195         stop = Sys_DoubleTime ();
196         intimerefresh = 0;
197         time = stop-start;
198         Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
199 }
200
201