]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/grwxgl.cpp
use a better multi_sw_ok_to_commit() check
[taylor/freespace2.git] / src / graphics / grwxgl.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "SDL_opengl.h"
10
11 #include "pstypes.h"
12 #include "2d.h"
13 #include "grwxgl.h"
14 #include "gropengl.h"
15 #include "gropenglinternal.h"
16 #include "grgl1.h"
17 #include "grinternal.h"
18 #include "mouse.h"
19
20
21 extern bool OGL_inited;
22 extern int GL_one_inited;
23
24 extern void opengl_set_variables();
25 extern void opengl_init_viewport();
26
27
28 static void wxgl_init_func_pointers()
29 {
30         gr_screen.gf_flip = gr_wxgl_flip;
31         gr_screen.gf_set_clip = gr_opengl1_set_clip;
32         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
33
34         gr_screen.gf_clear = gr_opengl_clear;
35
36         gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
37         gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
38
39         gr_screen.gf_rect = gr_opengl1_rect;
40         gr_screen.gf_shade = gr_opengl1_shade;
41         gr_screen.gf_string = gr_opengl1_string;
42         gr_screen.gf_circle = gr_opengl1_circle;
43
44         gr_screen.gf_line = gr_opengl1_line;
45         gr_screen.gf_aaline = gr_opengl1_aaline;
46         gr_screen.gf_pixel = gr_opengl1_pixel;
47         gr_screen.gf_scaler = gr_opengl1_scaler;
48         gr_screen.gf_tmapper = gr_opengl1_tmapper;
49
50         gr_screen.gf_gradient = gr_opengl1_gradient;
51
52         gr_screen.gf_print_screen = gr_opengl1_print_screen;
53
54         gr_screen.gf_fade_in = gr_opengl1_fade_in;
55         gr_screen.gf_fade_out = gr_opengl1_fade_out;
56         gr_screen.gf_flash = gr_opengl1_flash;
57
58         gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
59
60         gr_screen.gf_save_screen = gr_opengl1_save_screen;
61         gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
62         gr_screen.gf_free_screen = gr_opengl1_free_screen;
63
64         gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
65         gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
66         gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
67
68         gr_screen.gf_stream_start = gr_opengl1_stream_start;
69         gr_screen.gf_stream_frame = gr_opengl1_stream_frame;
70         gr_screen.gf_stream_stop = gr_opengl1_stream_stop;
71
72         gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
73
74         gr_screen.gf_lock = gr_opengl_lock;
75         gr_screen.gf_unlock = gr_opengl_unlock;
76
77         gr_screen.gf_fog_set = gr_opengl1_fog_set;
78
79         gr_screen.gf_get_region = gr_opengl1_get_region;
80
81         gr_screen.gf_set_cull = gr_opengl_set_cull;
82
83         gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
84
85         gr_screen.gf_preload_init = gr_opengl1_preload_init;
86         gr_screen.gf_preload = gr_opengl1_preload;
87
88         gr_screen.gf_zbias = gr_opengl_zbias;
89
90         gr_screen.gf_set_viewport = gr_wxgl_set_viewport;
91
92         gr_screen.gf_activate = gr_opengl_activate;
93
94         gr_screen.gf_release_texture = gr_opengl1_release_texture;
95 }
96
97 static void wxgl_init()
98 {
99         if (GL_one_inited) {
100                 return;
101         }
102
103         glShadeModel(GL_SMOOTH);
104         glEnable(GL_DITHER);
105         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
106         glHint(GL_FOG_HINT, GL_NICEST);
107
108         glEnable(GL_DEPTH_TEST);
109         glEnable(GL_BLEND);
110
111         glEnable(GL_TEXTURE_2D);
112
113         glDepthRange(0.0, 1.0);
114
115         glPixelStorei(GL_PACK_ALIGNMENT, 1);
116         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117
118         glFlush();
119
120         wxgl_init_func_pointers();
121         opengl1_tcache_init();
122
123         gr_opengl_clear();
124         gr_opengl_set_cull(1);
125
126         GL_one_inited = 1;
127
128 }
129
130 void gr_wxgl_flip()
131 {
132 #ifndef NDEBUG
133         GLenum error = glGetError();
134
135         if (error != GL_NO_ERROR) {
136                 mprintf(("!!DEBUG!! OpenGL Error: %d\n", error));
137         }
138 #endif
139 }
140
141 void gr_wxgl_set_viewport(int width, int height)
142 {
143         GL_viewport_x = 0;
144         GL_viewport_y = 0;
145         GL_viewport_w = width;
146         GL_viewport_h = height;
147         GL_viewport_scale_w = 1.0f;
148         GL_viewport_scale_h = 1.0f;
149
150         glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
151
152         glMatrixMode(GL_PROJECTION);
153         glLoadIdentity();
154         glOrtho(0, GL_viewport_w, GL_viewport_h, 0, 0.0, 1.0);
155         glMatrixMode(GL_MODELVIEW);
156         glLoadIdentity();
157         glScalef(GL_viewport_scale_w, GL_viewport_scale_h, 1.0f);
158
159         // update gr_screen, which will deal with view scaling too
160         gr_screen.max_w = width;
161         gr_screen.max_h = height;
162 }
163
164 void gr_wxgl_cleanup()
165 {
166         opengl1_tcache_cleanup();
167
168         opengl_free_render_buffer();
169
170         OGL_inited = false;
171         GL_one_inited = 0;
172 }
173
174 void gr_wxgl_init()
175 {
176         if ( OGL_inited )       {
177                 gr_opengl_cleanup();
178                 OGL_inited = false;
179         }
180
181         mprintf(( "Setting up OpenGL for wxWidgets...\n" ));
182
183         OGL_inited = true;
184
185         const char *gl_version = (const char*)glGetString(GL_VERSION);
186         int v_major = 0, v_minor = 0;
187
188         sscanf(gl_version, "%d.%d", &v_major, &v_minor);
189
190         int GL_version = (v_major * 10) + v_minor;
191
192         // version check, require 1.2+ for sake of simplicity
193         if (GL_version < 12) {
194                 Error(LOCATION, "Minimum OpenGL version is 1.2!");
195         }
196
197         mprintf(("  Vendor   : %s\n", glGetString(GL_VENDOR)));
198         mprintf(("  Renderer : %s\n", glGetString(GL_RENDERER)));
199         mprintf(("  Version  : %s\n", gl_version));
200
201         // initial viewport setup
202         opengl_init_viewport();
203
204         // set up generic variables before further init() calls
205         opengl_set_variables();
206
207         // main GL init
208         wxgl_init();
209
210         mprintf(("\n"));
211
212
213         gr_screen.bits_per_pixel = 16;
214         gr_screen.bytes_per_pixel = 2;
215
216         // screen values
217         Gr_red.bits = 5;
218         Gr_red.shift = 11;
219         Gr_red.scale = 8;
220         Gr_red.mask = 0x7C01;
221
222         Gr_green.bits = 5;
223         Gr_green.shift = 6;
224         Gr_green.scale = 8;
225         Gr_green.mask = 0x3E1;
226
227         Gr_blue.bits = 5;
228         Gr_blue.shift = 1;
229         Gr_blue.scale = 8;
230         Gr_blue.mask = 0x20;
231
232         Gr_alpha.bits = 1;
233         Gr_alpha.shift = 0;
234         Gr_alpha.scale = 255;
235         Gr_alpha.mask = 0x1;
236
237         // DDOI - set these so no one else does!
238         // texture values, always 1555 - 16-bit
239         Gr_t_red.mask = 0x7C01;
240         Gr_t_red.shift = 11;
241         Gr_t_red.scale = 8;
242
243         Gr_t_green.mask = 0x3E1;
244         Gr_t_green.shift = 6;
245         Gr_t_green.scale = 8;
246
247         Gr_t_blue.mask = 0x20;
248         Gr_t_blue.shift = 1;
249         Gr_t_blue.scale = 8;
250
251         Gr_t_alpha.mask = 0x1;
252         Gr_t_alpha.shift = 0;
253         Gr_t_alpha.scale = 255;
254
255         // alpha-texture values
256         Gr_ta_red.mask = 0x0f00;
257         Gr_ta_red.shift = 8;
258         Gr_ta_red.scale = 16;
259
260         Gr_ta_green.mask = 0x00f0;
261         Gr_ta_green.shift = 4;
262         Gr_ta_green.scale = 16;
263
264         Gr_ta_blue.mask = 0x000f;
265         Gr_ta_blue.shift = 0;
266         Gr_ta_blue.scale = 16;
267
268         Gr_ta_alpha.mask = 0xf000;
269         Gr_ta_alpha.shift = 12;
270         Gr_ta_alpha.scale = 16;
271
272         // default to screen
273         Gr_current_red = &Gr_red;
274         Gr_current_blue = &Gr_blue;
275         Gr_current_green = &Gr_green;
276         Gr_current_alpha = &Gr_alpha;
277
278
279         Mouse_hidden++;
280         gr_reset_clip();
281         gr_clear();
282         gr_flip();
283         gr_clear();
284 }