svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / editors / screen / glutil.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdio.h>
29 #include <math.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_vec_types.h"
35 #include "DNA_listBase.h"
36
37 #include "BKE_utildefines.h"
38
39 #include "BLI_arithb.h"
40 #include "BLI_threads.h"
41
42 #include "BIF_gl.h"
43 #include "BIF_glutil.h"
44
45 #ifndef GL_CLAMP_TO_EDGE
46 #define GL_CLAMP_TO_EDGE                        0x812F
47 #endif
48
49
50 /* ******************************************** */
51
52 /* defined in BIF_gl.h */
53 GLubyte stipple_halftone[128] = {
54         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
55         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
56         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
57         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
58         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
59         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
60         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
61         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
62         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
63         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
64         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
65         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
66         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
67         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
68         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
69         0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
70
71
72 /*  repeat this pattern
73         X000X000
74         00000000
75         00X000X0
76         00000000 */
77
78
79 GLubyte stipple_quarttone[128] = { 
80         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
81         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
82         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
83         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
84         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
85         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
86         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
87         136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0};
88
89
90 void fdrawbezier(float vec[4][3])
91 {
92         float dist;
93         float curve_res = 24, spline_step = 0.0f;
94         
95         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
96         
97         /* check direction later, for top sockets */
98         vec[1][0]= vec[0][0]+dist;
99         vec[1][1]= vec[0][1];
100         
101         vec[2][0]= vec[3][0]-dist;
102         vec[2][1]= vec[3][1];
103         /* we can reuse the dist variable here to increment the GL curve eval amount*/
104         dist = 1.0f/curve_res;
105         
106         cpack(0x0);
107         glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
108         glBegin(GL_LINE_STRIP);
109         while (spline_step < 1.000001f) {
110                 /*if(do_shaded)
111                         UI_ThemeColorBlend(th_col1, th_col2, spline_step);*/
112                 glEvalCoord1f(spline_step);
113                 spline_step += dist;
114         }
115         glEnd();
116 }
117
118 void fdrawline(float x1, float y1, float x2, float y2)
119 {
120         float v[2];
121         
122         glBegin(GL_LINE_STRIP);
123         v[0] = x1; v[1] = y1;
124         glVertex2fv(v);
125         v[0] = x2; v[1] = y2;
126         glVertex2fv(v);
127         glEnd();
128 }
129
130 void fdrawbox(float x1, float y1, float x2, float y2)
131 {
132         float v[2];
133         
134         glBegin(GL_LINE_STRIP);
135         
136         v[0] = x1; v[1] = y1;
137         glVertex2fv(v);
138         v[0] = x1; v[1] = y2;
139         glVertex2fv(v);
140         v[0] = x2; v[1] = y2;
141         glVertex2fv(v);
142         v[0] = x2; v[1] = y1;
143         glVertex2fv(v);
144         v[0] = x1; v[1] = y1;
145         glVertex2fv(v);
146         
147         glEnd();
148 }
149
150 void sdrawline(short x1, short y1, short x2, short y2)
151 {
152         short v[2];
153         
154         glBegin(GL_LINE_STRIP);
155         v[0] = x1; v[1] = y1;
156         glVertex2sv(v);
157         v[0] = x2; v[1] = y2;
158         glVertex2sv(v);
159         glEnd();
160 }
161
162 /*
163
164         x1,y2
165         |  \
166         |   \
167         |    \
168         x1,y1-- x2,y1
169
170 */
171
172 static void sdrawtripoints(short x1, short y1, short x2, short y2)
173 {
174         short v[2];
175         v[0]= x1; v[1]= y1;
176         glVertex2sv(v);
177         v[0]= x1; v[1]= y2;
178         glVertex2sv(v);
179         v[0]= x2; v[1]= y1;
180         glVertex2sv(v);
181 }
182
183 void sdrawtri(short x1, short y1, short x2, short y2)
184 {
185         glBegin(GL_LINE_STRIP);
186         sdrawtripoints(x1, y1, x2, y2);
187         glEnd();
188 }
189
190 void sdrawtrifill(short x1, short y1, short x2, short y2)
191 {
192         glBegin(GL_TRIANGLES);
193         sdrawtripoints(x1, y1, x2, y2);
194         glEnd();
195 }
196
197 void sdrawbox(short x1, short y1, short x2, short y2)
198 {
199         short v[2];
200         
201         glBegin(GL_LINE_STRIP);
202         
203         v[0] = x1; v[1] = y1;
204         glVertex2sv(v);
205         v[0] = x1; v[1] = y2;
206         glVertex2sv(v);
207         v[0] = x2; v[1] = y2;
208         glVertex2sv(v);
209         v[0] = x2; v[1] = y1;
210         glVertex2sv(v);
211         v[0] = x1; v[1] = y1;
212         glVertex2sv(v);
213         
214         glEnd();
215 }
216
217
218 /* ******************************************** */
219
220 void setlinestyle(int nr)
221 {
222         if(nr==0) {
223                 glDisable(GL_LINE_STIPPLE);
224         }
225         else {
226                 
227                 glEnable(GL_LINE_STIPPLE);
228                 glLineStipple(nr, 0xAAAA);
229         }
230 }
231
232         /* Invert line handling */
233         
234 #define glToggle(mode, onoff)   (((onoff)?glEnable:glDisable)(mode))
235
236 void set_inverted_drawing(int enable) 
237 {
238         glLogicOp(enable?GL_INVERT:GL_COPY);
239
240         /* Use GL_BLEND_EQUATION_EXT on sgi (if we have it),
241          * apparently GL_COLOR_LOGIC_OP doesn't work on O2?
242          * Is this an sgi bug or our bug?
243          */
244 #if defined(__sgi) && defined(GL_BLEND_EQUATION_EXT)
245         glBlendEquationEXT(enable?GL_LOGIC_OP:GL_FUNC_ADD_EXT);
246         glToggle(GL_BLEND, enable);
247 #else
248         glToggle(GL_COLOR_LOGIC_OP, enable);
249 #endif
250
251         glToggle(GL_DITHER, !enable);
252 }
253
254 void sdrawXORline(int x0, int y0, int x1, int y1)
255 {
256         if(x0==x1 && y0==y1) return;
257
258         set_inverted_drawing(1);
259         
260         glBegin(GL_LINES);
261         glVertex2i(x0, y0);
262         glVertex2i(x1, y1);
263         glEnd();
264         
265         set_inverted_drawing(0);
266 }
267
268 void sdrawXORline4(int nr, int x0, int y0, int x1, int y1)
269 {
270         static short old[4][2][2];
271         static char flags[4]= {0, 0, 0, 0};
272         
273                 /* with builtin memory, max 4 lines */
274
275         set_inverted_drawing(1);
276                 
277         glBegin(GL_LINES);
278         if(nr== -1) { /* flush */
279                 for (nr=0; nr<4; nr++) {
280                         if (flags[nr]) {
281                                 glVertex2sv(old[nr][0]);
282                                 glVertex2sv(old[nr][1]);
283                                 flags[nr]= 0;
284                         }
285                 }
286         } else {
287                 if(nr>=0 && nr<4) {
288                         if(flags[nr]) {
289                                 glVertex2sv(old[nr][0]);
290                                 glVertex2sv(old[nr][1]);
291                         }
292
293                         old[nr][0][0]= x0;
294                         old[nr][0][1]= y0;
295                         old[nr][1][0]= x1;
296                         old[nr][1][1]= y1;
297                         
298                         flags[nr]= 1;
299                 }
300                 
301                 glVertex2i(x0, y0);
302                 glVertex2i(x1, y1);
303         }
304         glEnd();
305         
306         set_inverted_drawing(0);
307 }
308
309 void fdrawXORellipse(float xofs, float yofs, float hw, float hh)
310 {
311         if(hw==0) return;
312
313         set_inverted_drawing(1);
314
315         glPushMatrix();
316         glTranslatef(xofs, yofs, 0.0);
317         glScalef(1,hh/hw,1);
318         glutil_draw_lined_arc(0.0, M_PI*2.0, hw, 20);
319         glPopMatrix();
320
321         set_inverted_drawing(0);
322 }
323 void fdrawXORcirc(float xofs, float yofs, float rad)
324 {
325         set_inverted_drawing(1);
326
327         glPushMatrix();
328         glTranslatef(xofs, yofs, 0.0);
329         glutil_draw_lined_arc(0.0, M_PI*2.0, rad, 20);
330         glPopMatrix();
331
332         set_inverted_drawing(0);
333 }
334
335 void glutil_draw_filled_arc(float start, float angle, float radius, int nsegments) {
336         int i;
337         
338         glBegin(GL_TRIANGLE_FAN);
339         glVertex2f(0.0, 0.0);
340         for (i=0; i<nsegments; i++) {
341                 float t= (float) i/(nsegments-1);
342                 float cur= start + t*angle;
343                 
344                 glVertex2f(cos(cur)*radius, sin(cur)*radius);
345         }
346         glEnd();
347 }
348
349 void glutil_draw_lined_arc(float start, float angle, float radius, int nsegments) {
350         int i;
351         
352         glBegin(GL_LINE_STRIP);
353         for (i=0; i<nsegments; i++) {
354                 float t= (float) i/(nsegments-1);
355                 float cur= start + t*angle;
356                 
357                 glVertex2f(cos(cur)*radius, sin(cur)*radius);
358         }
359         glEnd();
360 }
361
362 int glaGetOneInteger(int param)
363 {
364         GLint i;
365         glGetIntegerv(param, &i);
366         return i;
367 }
368
369 float glaGetOneFloat(int param)
370 {
371         GLfloat v;
372         glGetFloatv(param, &v);
373         return v;
374 }
375
376 void glaRasterPosSafe2f(float x, float y, float known_good_x, float known_good_y)
377 {
378         GLubyte dummy= 0;
379
380                 /* As long as known good coordinates are correct
381                  * this is guarenteed to generate an ok raster
382                  * position (ignoring potential (real) overflow
383                  * issues).
384                  */
385         glRasterPos2f(known_good_x, known_good_y);
386
387                 /* Now shift the raster position to where we wanted
388                  * it in the first place using the glBitmap trick.
389                  */
390         glBitmap(0, 0, 0, 0, x - known_good_x, y - known_good_y, &dummy);
391 }
392
393 static int get_cached_work_texture(int *w_r, int *h_r)
394 {
395         static GLint texid= -1;
396         static int tex_w= 256;
397         static int tex_h= 256;
398
399         if (texid==-1) {
400                 GLint ltexid= glaGetOneInteger(GL_TEXTURE_2D);
401                 unsigned char *tbuf;
402
403                 glGenTextures(1, (GLuint *)&texid);
404
405                 glBindTexture(GL_TEXTURE_2D, texid);
406
407                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
408                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
409
410                 tbuf= MEM_callocN(tex_w*tex_h*4, "tbuf");
411                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tbuf);
412                 MEM_freeN(tbuf);
413
414                 glBindTexture(GL_TEXTURE_2D, ltexid);
415         }
416
417         *w_r= tex_w;
418         *h_r= tex_h;
419         return texid;
420 }
421
422 void glaDrawPixelsTexScaled(float x, float y, int img_w, int img_h, int format, void *rect, float scaleX, float scaleY)
423 {
424         unsigned char *uc_rect= (unsigned char*) rect;
425         float *f_rect= (float *)rect;
426         float xzoom= glaGetOneFloat(GL_ZOOM_X), yzoom= glaGetOneFloat(GL_ZOOM_Y);
427         int ltexid= glaGetOneInteger(GL_TEXTURE_2D);
428         int lrowlength= glaGetOneInteger(GL_UNPACK_ROW_LENGTH);
429         int subpart_x, subpart_y, tex_w, tex_h;
430         int texid= get_cached_work_texture(&tex_w, &tex_h);
431         int nsubparts_x= (img_w+(tex_w-1))/tex_w;
432         int nsubparts_y= (img_h+(tex_h-1))/tex_h;
433
434         /* Specify the color outside this function, and tex will modulate it.
435          * This is useful for changing alpha without using glPixelTransferf()
436          */
437         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
438         glPixelStorei(GL_UNPACK_ROW_LENGTH, img_w);
439         glBindTexture(GL_TEXTURE_2D, texid);
440
441          /* don't want nasty border artifacts */
442          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
443          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
444
445         for (subpart_y=0; subpart_y<nsubparts_y; subpart_y++) {
446                 for (subpart_x=0; subpart_x<nsubparts_x; subpart_x++) {
447                         int subpart_w= (subpart_x==nsubparts_x-1)?(img_w-subpart_x*tex_w):tex_w;
448                         int subpart_h= (subpart_y==nsubparts_y-1)?(img_h-subpart_y*tex_h):tex_h;
449                         float rast_x= x+subpart_x*tex_w*xzoom;
450                         float rast_y= y+subpart_y*tex_h*yzoom;
451                         
452                         if(format==GL_FLOAT)
453                                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, subpart_w, subpart_h, GL_RGBA, GL_FLOAT, &f_rect[(subpart_y*tex_w)*img_w*4 + (subpart_x*tex_w)*4]);
454                         else
455                                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, subpart_w, subpart_h, GL_RGBA, GL_UNSIGNED_BYTE, &uc_rect[(subpart_y*tex_w)*img_w*4 + (subpart_x*tex_w)*4]);
456                                                         
457                         glEnable(GL_TEXTURE_2D);
458                         glBegin(GL_QUADS);
459                         glTexCoord2f(0, 0);
460                         glVertex2f(rast_x, rast_y);
461
462                         glTexCoord2f((float) (subpart_w-1)/tex_w, 0);
463                         glVertex2f(rast_x+subpart_w*xzoom*scaleX, rast_y);
464
465                         glTexCoord2f((float) (subpart_w-1)/tex_w, (float) (subpart_h-1)/tex_h);
466                         glVertex2f(rast_x+subpart_w*xzoom*scaleX, rast_y+subpart_h*yzoom*scaleY);
467
468                         glTexCoord2f(0, (float) (subpart_h-1)/tex_h);
469                         glVertex2f(rast_x, rast_y+subpart_h*yzoom*scaleY);
470                         glEnd();
471                         glDisable(GL_TEXTURE_2D);
472                 }
473         }
474
475         glBindTexture(GL_TEXTURE_2D, ltexid);
476         glPixelStorei(GL_UNPACK_ROW_LENGTH, lrowlength);
477         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
478 }
479
480 void glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *rect)
481 {
482         glaDrawPixelsTexScaled(x, y, img_w, img_h, format, rect, 1.0f, 1.0f);
483 }
484
485 void glaDrawPixelsSafe_to32(float fx, float fy, int img_w, int img_h, int row_w, float *rectf)
486 {
487         float *rf;
488         int x, y;
489         char *rect32, *rc;
490         
491         /* copy imgw-imgh to a temporal 32 bits rect */
492         if(img_w<1 || img_h<1) return;
493         
494         rc= rect32= MEM_mallocN(img_w*img_h*sizeof(int), "temp 32 bits");
495         
496         for(y=0; y<img_h; y++) {
497                 rf= rectf;
498                 for(x=0; x<img_w; x++, rf+=4, rc+=4) {
499                         rc[0]= FTOCHAR(rf[0]);
500                         rc[1]= FTOCHAR(rf[1]);
501                         rc[2]= FTOCHAR(rf[2]);
502                         rc[3]= FTOCHAR(rf[3]);
503                 }
504                 rectf+= 4*row_w;
505         }
506         
507         glaDrawPixelsSafe(fx, fy, img_w, img_h, img_w, GL_RGBA, GL_UNSIGNED_BYTE, rect32);
508
509         MEM_freeN(rect32);
510 }
511
512 void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, int row_w, int format, int type, void *rect)
513 {
514         float xzoom= glaGetOneFloat(GL_ZOOM_X);
515         float yzoom= glaGetOneFloat(GL_ZOOM_Y);
516                 
517                 /* The pixel space coordinate of the intersection of
518                  * the [zoomed] image with the origin.
519                  */
520         float ix= -x/xzoom;
521         float iy= -y/yzoom;
522         
523                 /* The maximum pixel amounts the image can be cropped
524                  * at the lower left without exceeding the origin.
525                  */
526         int off_x= floor(MAX2(ix, 0));
527         int off_y= floor(MAX2(iy, 0));
528                 
529                 /* The zoomed space coordinate of the raster position 
530                  * (starting at the lower left most unclipped pixel).
531                  */
532         float rast_x= x + off_x*xzoom;
533         float rast_y= y + off_y*yzoom;
534
535         GLfloat scissor[4];
536         int draw_w, draw_h;
537
538                 /* Determine the smallest number of pixels we need to draw
539                  * before the image would go off the upper right corner.
540                  * 
541                  * It may seem this is just an optimization but some graphics 
542                  * cards (ATI) freak out if there is a large zoom factor and
543                  * a large number of pixels off the screen (probably at some
544                  * level the number of image pixels to draw is getting multiplied
545                  * by the zoom and then clamped). Making sure we draw the
546                  * fewest pixels possible keeps everyone mostly happy (still
547                  * fails if we zoom in on one really huge pixel so that it
548                  * covers the entire screen).
549                  */
550         glGetFloatv(GL_SCISSOR_BOX, scissor);
551         draw_w = MIN2(img_w-off_x, ceil((scissor[2]-rast_x)/xzoom));
552         draw_h = MIN2(img_h-off_y, ceil((scissor[3]-rast_y)/yzoom));
553
554         if (draw_w>0 && draw_h>0) {
555                 int old_row_length = glaGetOneInteger(GL_UNPACK_ROW_LENGTH);
556
557                         /* Don't use safe RasterPos (slower) if we can avoid it. */
558                 if (rast_x>=0 && rast_y>=0) {
559                         glRasterPos2f(rast_x, rast_y);
560                 } else {
561                         glaRasterPosSafe2f(rast_x, rast_y, 0, 0);
562                 }
563
564                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_w);
565                 if(format==GL_LUMINANCE || format==GL_RED) {
566                         if(type==GL_FLOAT) {
567                                 float *f_rect= (float *)rect;
568                                 glDrawPixels(draw_w, draw_h, format, type, f_rect + (off_y*row_w + off_x));
569                         }
570                         else if(type==GL_INT || type==GL_UNSIGNED_INT) {
571                                 int *i_rect= (int *)rect;
572                                 glDrawPixels(draw_w, draw_h, format, type, i_rect + (off_y*row_w + off_x));
573                         }
574                 }
575                 else { /* RGBA */
576                         if(type==GL_FLOAT) {
577                                 float *f_rect= (float *)rect;
578                                 glDrawPixels(draw_w, draw_h, format, type, f_rect + (off_y*row_w + off_x)*4);
579                         }
580                         else if(type==GL_UNSIGNED_BYTE) {
581                                 unsigned char *uc_rect= (unsigned char *) rect;
582                                 glDrawPixels(draw_w, draw_h, format, type, uc_rect + (off_y*row_w + off_x)*4);
583                         }
584                 }
585                 
586                 glPixelStorei(GL_UNPACK_ROW_LENGTH,  old_row_length);
587         }
588 }
589
590 /* 2D Drawing Assistance */
591
592 void glaDefine2DArea(rcti *screen_rect)
593 {
594         int sc_w= screen_rect->xmax - screen_rect->xmin + 1;
595         int sc_h= screen_rect->ymax - screen_rect->ymin + 1;
596
597         glViewport(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
598         glScissor(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
599
600                 /* The 0.375 magic number is to shift the matrix so that
601                  * both raster and vertex integer coordinates fall at pixel
602                  * centers properly. For a longer discussion see the OpenGL
603                  * Programming Guide, Appendix H, Correctness Tips.
604                  */
605
606         glMatrixMode(GL_PROJECTION);
607         glLoadIdentity();
608         glOrtho(0.0, sc_w, 0.0, sc_h, -1, 1);
609         glTranslatef(0.375, 0.375, 0.0);
610
611         glMatrixMode(GL_MODELVIEW);
612         glLoadIdentity();
613 }
614
615 struct gla2DDrawInfo {
616         int orig_vp[4], orig_sc[4];
617         float orig_projmat[16], orig_viewmat[16];
618
619         rcti screen_rect;
620         rctf world_rect;
621
622         float wo_to_sc[2];
623 };
624
625 void gla2DGetMap(gla2DDrawInfo *di, rctf *rect) 
626 {
627         *rect= di->world_rect;
628 }
629
630 void gla2DSetMap(gla2DDrawInfo *di, rctf *rect) 
631 {
632         int sc_w, sc_h;
633         float wo_w, wo_h;
634
635         di->world_rect= *rect;
636         
637         sc_w= (di->screen_rect.xmax-di->screen_rect.xmin);
638         sc_h= (di->screen_rect.ymax-di->screen_rect.ymin);
639         wo_w= (di->world_rect.xmax-di->world_rect.xmin);
640         wo_h= (di->world_rect.ymax-di->world_rect.ymin);
641         
642         di->wo_to_sc[0]= sc_w/wo_w;
643         di->wo_to_sc[1]= sc_h/wo_h;
644 }
645
646
647 gla2DDrawInfo *glaBegin2DDraw(rcti *screen_rect, rctf *world_rect) 
648 {
649         gla2DDrawInfo *di= MEM_mallocN(sizeof(*di), "gla2DDrawInfo");
650         int sc_w, sc_h;
651         float wo_w, wo_h;
652
653         glGetIntegerv(GL_VIEWPORT, (GLint *)di->orig_vp);
654         glGetIntegerv(GL_SCISSOR_BOX, (GLint *)di->orig_sc);
655         glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *)di->orig_projmat);
656         glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)di->orig_viewmat);
657
658         di->screen_rect= *screen_rect;
659         if (world_rect) {
660                 di->world_rect= *world_rect;
661         } else {
662                 di->world_rect.xmin= di->screen_rect.xmin;
663                 di->world_rect.ymin= di->screen_rect.ymin;
664                 di->world_rect.xmax= di->screen_rect.xmax;
665                 di->world_rect.ymax= di->screen_rect.ymax;
666         }
667
668         sc_w= (di->screen_rect.xmax-di->screen_rect.xmin);
669         sc_h= (di->screen_rect.ymax-di->screen_rect.ymin);
670         wo_w= (di->world_rect.xmax-di->world_rect.xmin);
671         wo_h= (di->world_rect.ymax-di->world_rect.ymin);
672
673         di->wo_to_sc[0]= sc_w/wo_w;
674         di->wo_to_sc[1]= sc_h/wo_h;
675
676         glaDefine2DArea(&di->screen_rect);
677
678         return di;
679 }
680
681 void gla2DDrawTranslatePt(gla2DDrawInfo *di, float wo_x, float wo_y, int *sc_x_r, int *sc_y_r)
682 {
683         *sc_x_r= (wo_x - di->world_rect.xmin)*di->wo_to_sc[0];
684         *sc_y_r= (wo_y - di->world_rect.ymin)*di->wo_to_sc[1];
685 }
686 void gla2DDrawTranslatePtv(gla2DDrawInfo *di, float world[2], int screen_r[2])
687 {
688         screen_r[0]= (world[0] - di->world_rect.xmin)*di->wo_to_sc[0];
689         screen_r[1]= (world[1] - di->world_rect.ymin)*di->wo_to_sc[1];
690 }
691
692 void glaEnd2DDraw(gla2DDrawInfo *di)
693 {
694         glViewport(di->orig_vp[0], di->orig_vp[1], di->orig_vp[2], di->orig_vp[3]);
695         glScissor(di->orig_vp[0], di->orig_vp[1], di->orig_vp[2], di->orig_vp[3]);
696         glMatrixMode(GL_PROJECTION);
697         glLoadMatrixf(di->orig_projmat);
698         glMatrixMode(GL_MODELVIEW);
699         glLoadMatrixf(di->orig_viewmat);
700
701         MEM_freeN(di);
702 }
703
704 /* **************** glPoint hack ************************ */
705
706 static int curmode=0;
707 static int pointhack=0;
708 static GLubyte Squaredot[16] = { 0xff,0xff,0xff,0xff,
709                                                                  0xff,0xff,0xff,0xff,
710                                                                  0xff,0xff,0xff,0xff, 
711                                                                  0xff,0xff,0xff,0xff};
712
713 void bglBegin(int mode)
714 {
715         curmode= mode;
716         
717         if(mode==GL_POINTS) {
718                 float value[4];
719                 glGetFloatv(GL_POINT_SIZE_RANGE, value);
720                 if(value[1]<2.0) {
721                         glGetFloatv(GL_POINT_SIZE, value);
722                         pointhack= floor(value[0]+0.5);
723                         if(pointhack>4) pointhack= 4;
724                 }
725                 else glBegin(mode);
726         }
727 }
728
729
730 void bglVertex3fv(float *vec)
731 {
732         switch(curmode) {
733         case GL_POINTS:
734                 if(pointhack) {
735                         glRasterPos3fv(vec);
736                         glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
737                 }
738                 else glVertex3fv(vec);
739                 break;
740         }
741 }
742
743 void bglVertex3f(float x, float y, float z)
744 {
745         switch(curmode) {
746         case GL_POINTS:
747                 if(pointhack) {
748                         glRasterPos3f(x, y, z);
749                         glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
750                 }
751                 else glVertex3f(x, y, z);
752                 break;
753         }
754 }
755
756 void bglVertex2fv(float *vec)
757 {
758         switch(curmode) {
759         case GL_POINTS:
760                 if(pointhack) {
761                         glRasterPos2fv(vec);
762                         glBitmap(pointhack, pointhack, (float)pointhack/2, pointhack/2, 0.0, 0.0, Squaredot);
763                 }
764                 else glVertex2fv(vec);
765                 break;
766         }
767 }
768
769
770 void bglEnd(void)
771 {
772         if(pointhack) pointhack= 0;
773         else glEnd();
774         
775 }
776
777 /* Uses current OpenGL state to get view matrices for gluProject/gluUnProject */
778 void bgl_get_mats(bglMats *mats)
779 {
780         const double badvalue= 1.0e-6;
781
782         glGetDoublev(GL_MODELVIEW_MATRIX, mats->modelview);
783         glGetDoublev(GL_PROJECTION_MATRIX, mats->projection);
784         glGetIntegerv(GL_VIEWPORT, (GLint *)mats->viewport);
785         
786         /* Very strange code here - it seems that certain bad values in the
787            modelview matrix can cause gluUnProject to give bad results. */
788         if(mats->modelview[0] < badvalue &&
789            mats->modelview[0] > -badvalue)
790                 mats->modelview[0]= 0;
791         if(mats->modelview[5] < badvalue &&
792            mats->modelview[5] > -badvalue)
793                 mats->modelview[5]= 0;
794         
795         /* Set up viewport so that gluUnProject will give correct values */
796         mats->viewport[0] = 0;
797         mats->viewport[1] = 0;
798 }
799
800 /* *************** glPolygonOffset hack ************* */
801
802 /* dist is only for ortho now... */
803 void bglPolygonOffset(float viewdist, float dist) 
804 {
805         static float winmat[16], offset=0.0;    
806         
807         if(dist!=0.0) {
808                 float offs;
809                 
810                 // glEnable(GL_POLYGON_OFFSET_FILL);
811                 // glPolygonOffset(-1.0, -1.0);
812
813                 /* hack below is to mimic polygon offset */
814                 glMatrixMode(GL_PROJECTION);
815                 glGetFloatv(GL_PROJECTION_MATRIX, (float *)winmat);
816                 
817                 /* dist is from camera to center point */
818                 
819                 if(winmat[15]>0.5) offs= 0.00001*dist*viewdist;  // ortho tweaking
820                 else offs= 0.0005*dist;  // should be clipping value or so...
821                 
822                 winmat[14]-= offs;
823                 offset+= offs;
824                 
825                 glLoadMatrixf(winmat);
826                 glMatrixMode(GL_MODELVIEW);
827         }
828         else {
829
830                 glMatrixMode(GL_PROJECTION);
831                 winmat[14]+= offset;
832                 offset= 0.0;
833                 glLoadMatrixf(winmat);
834                 glMatrixMode(GL_MODELVIEW);
835         }
836 }
837
838 int is_a_really_crappy_intel_card(void)
839 {
840         static int well_is_it= -1;
841
842                 /* Do you understand the implication? Do you? */
843         if (well_is_it==-1)
844                 well_is_it= (strcmp((char*) glGetString(GL_VENDOR), "Intel Inc.") == 0);
845
846         return well_is_it;
847 }
848
849 void bglFlush(void) 
850 {
851         glFlush();
852 #ifdef __APPLE__
853 //      if(is_a_really_crappy_intel_card())
854 // XXX          myswapbuffers(); //hack to get mac intel graphics to show frontbuffer
855 #endif
856 }
857
858