Clip shadow above ball with a texture
[neverball] / share / video.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "video.h"
16 #include "vec3.h"
17 #include "glext.h"
18 #include "config.h"
19 #include "syswm.h"
20 #include "sync.h"
21
22 /*---------------------------------------------------------------------------*/
23
24 int video_init(const char *title, const char *icon)
25 {
26     SDL_QuitSubSystem(SDL_INIT_VIDEO);
27
28     if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
29     {
30         fprintf(stderr, "%s\n", SDL_GetError());
31         return 0;
32     }
33
34     /* This has to happen before mode setting... */
35
36     set_SDL_icon(icon);
37
38     /* Initialize the video. */
39
40     if (!video_mode(config_get_d(CONFIG_FULLSCREEN),
41                     config_get_d(CONFIG_WIDTH),
42                     config_get_d(CONFIG_HEIGHT)))
43     {
44         fprintf(stderr, "%s\n", SDL_GetError());
45         return 0;
46     }
47
48     /* ...and this has to happen after it. */
49
50     set_EWMH_icon(icon);
51
52     SDL_WM_SetCaption(title, title);
53
54     return 1;
55 }
56
57 /*---------------------------------------------------------------------------*/
58
59 PFNGLACTIVETEXTUREARBPROC glActiveTextureARB_;
60
61 int check_extension(const char *needle)
62 {
63     const GLubyte *haystack, *c;
64
65     /* Search for the given string in the OpenGL extension strings. */
66
67     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
68     {
69         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
70             if (*c != *haystack)
71                 break;
72
73         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
74             return 1;
75     }
76
77     return 0;
78 }
79
80 int video_mode(int f, int w, int h)
81 {
82     int stereo  = config_get_d(CONFIG_STEREO)      ? 1 : 0;
83     int stencil = config_get_d(CONFIG_REFLECTION)  ? 1 : 0;
84     int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
85     int samples = config_get_d(CONFIG_MULTISAMPLE);
86     int vsync   = config_get_d(CONFIG_VSYNC)       ? 1 : 0;
87
88     SDL_GL_SetAttribute(SDL_GL_STEREO,             stereo);
89     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       stencil);
90     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
91     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
92     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,       vsync);
93
94     /* Require 16-bit double buffer with 16-bit depth buffer. */
95
96     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
97     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
98     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
99     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
100     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
101
102     /* Try to set the currently specified mode. */
103
104     if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
105     {
106         config_set_d(CONFIG_FULLSCREEN, f);
107         config_set_d(CONFIG_WIDTH,      w);
108         config_set_d(CONFIG_HEIGHT,     h);
109
110         glViewport(0, 0, w, h);
111         glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
112
113         glEnable(GL_NORMALIZE);
114         glEnable(GL_CULL_FACE);
115         glEnable(GL_DEPTH_TEST);
116         glEnable(GL_TEXTURE_2D);
117         glEnable(GL_LIGHTING);
118         glEnable(GL_BLEND);
119
120         glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,
121                       GL_SEPARATE_SPECULAR_COLOR);
122         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
123
124         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125         glDepthFunc(GL_LEQUAL);
126
127         /* If GL supports multisample, and SDL got a multisample buffer... */
128
129 #ifdef GL_ARB_multisample
130         if (check_extension("ARB_multisample"))
131         {
132             SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
133             if (buffers)
134                 glEnable(GL_MULTISAMPLE_ARB);
135         }
136 #endif
137
138         if (check_extension("ARB_multitexture"))
139         {
140             union
141             {
142                 void *ob;
143                 PFNGLACTIVETEXTUREARBPROC fn;
144             } cast;
145
146             cast.ob = SDL_GL_GetProcAddress("glActiveTextureARB");
147             glActiveTextureARB_ = cast.fn;
148         }
149
150         glReadBuffer(GL_FRONT);
151
152         /* Attempt manual swap control if SDL's is broken. */
153
154         if (vsync && SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &vsync) == -1)
155             sync_init();
156
157         return 1;
158     }
159
160     /* If the mode failed, try it without stereo. */
161
162     else if (stereo)
163     {
164         config_set_d(CONFIG_STEREO, 0);
165         return video_mode(f, w, h);
166     }
167
168     /* If the mode failed, try decreasing the level of multisampling. */
169
170     else if (buffers)
171     {
172         config_set_d(CONFIG_MULTISAMPLE, samples / 2);
173         return video_mode(f, w, h);
174     }
175
176     /* If that mode failed, try it without reflections. */
177
178     else if (stencil)
179     {
180         config_set_d(CONFIG_REFLECTION, 0);
181         return video_mode(f, w, h);
182     }
183
184     /* If THAT mode failed, punt. */
185
186     return 0;
187 }
188
189 /*---------------------------------------------------------------------------*/
190
191 static float ms     = 0;
192 static int   fps    = 0;
193 static int   last   = 0;
194 static int   ticks  = 0;
195 static int   frames = 0;
196
197 int  video_perf(void)
198 {
199     return fps;
200 }
201
202 void video_swap(void)
203 {
204     int dt;
205
206     SDL_GL_SwapBuffers();
207
208     /* Accumulate time passed and frames rendered. */
209
210     dt = (int) SDL_GetTicks() - last;
211
212     frames +=  1;
213     ticks  += dt;
214     last   += dt;
215
216     /* Average over 250ms. */
217
218     if (ticks > 1000)
219     {
220         /* Round the frames-per-second value to the nearest integer. */
221
222         double k = 1000.0 * frames / ticks;
223         double f = floor(k);
224         double c = ceil (k);
225
226         /* Compute frame time and frames-per-second stats. */
227
228         fps = (int) ((c - k < k - f) ? c : f);
229         ms  = (float) ticks / (float) frames;
230
231         /* Reset the counters for the next update. */
232
233         frames = 0;
234         ticks  = 0;
235
236         /* Output statistics if configured. */
237
238         if (config_get_d(CONFIG_STATS))
239             fprintf(stdout, "%4d %8.4f\n", fps, ms);
240     }
241 }
242
243 /*---------------------------------------------------------------------------*/
244
245 static int grabbed = 0;
246
247 void video_set_grab(int w)
248 {
249     if (w)
250     {
251         SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
252
253         SDL_WarpMouse(config_get_d(CONFIG_WIDTH)  / 2,
254                       config_get_d(CONFIG_HEIGHT) / 2);
255
256         SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
257     }
258
259     SDL_WM_GrabInput(SDL_GRAB_ON);
260     SDL_ShowCursor(SDL_DISABLE);
261
262     grabbed = 1;
263 }
264
265 void video_clr_grab(void)
266 {
267     SDL_WM_GrabInput(SDL_GRAB_OFF);
268     SDL_ShowCursor(SDL_ENABLE);
269     grabbed = 0;
270 }
271
272 int  video_get_grab(void)
273 {
274     return grabbed;
275 }
276
277 /*---------------------------------------------------------------------------*/
278
279 void video_push_persp(float fov, float n, float f)
280 {
281     GLdouble m[4][4];
282
283     GLdouble r = fov / 2 * V_PI / 180;
284     GLdouble s = sin(r);
285     GLdouble c = cos(r) / s;
286
287     GLdouble a = ((GLdouble) config_get_d(CONFIG_WIDTH) /
288                   (GLdouble) config_get_d(CONFIG_HEIGHT));
289
290     glMatrixMode(GL_PROJECTION);
291     {
292         glPushMatrix();
293         glLoadIdentity();
294
295         m[0][0] =  c/a;
296         m[0][1] =  0.0;
297         m[0][2] =  0.0;
298         m[0][3] =  0.0;
299         m[1][0] =  0.0;
300         m[1][1] =    c;
301         m[1][2] =  0.0;
302         m[1][3] =  0.0;
303         m[2][0] =  0.0;
304         m[2][1] =  0.0;
305         m[2][2] = -(f + n) / (f - n);
306         m[2][3] = -1.0;
307         m[3][0] =  0.0;
308         m[3][1] =  0.0;
309         m[3][2] = -2.0 * n * f / (f - n);
310         m[3][3] =  0.0;
311
312         glMultMatrixd(&m[0][0]);
313     }
314     glMatrixMode(GL_MODELVIEW);
315 }
316
317 void video_push_ortho(void)
318 {
319     GLdouble w = (GLdouble) config_get_d(CONFIG_WIDTH);
320     GLdouble h = (GLdouble) config_get_d(CONFIG_HEIGHT);
321
322     glMatrixMode(GL_PROJECTION);
323     {
324         glPushMatrix();
325         glLoadIdentity();
326         glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
327     }
328     glMatrixMode(GL_MODELVIEW);
329 }
330
331 void video_pop_matrix(void)
332 {
333     glMatrixMode(GL_PROJECTION);
334     {
335         glPopMatrix();
336     }
337     glMatrixMode(GL_MODELVIEW);
338 }
339
340 void video_clear(void)
341 {
342     if (config_get_d(CONFIG_REFLECTION))
343         glClear(GL_COLOR_BUFFER_BIT |
344                 GL_DEPTH_BUFFER_BIT |
345                 GL_STENCIL_BUFFER_BIT);
346     else
347         glClear(GL_COLOR_BUFFER_BIT |
348                 GL_DEPTH_BUFFER_BIT);
349 }
350
351 /*---------------------------------------------------------------------------*/