2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r18677...
[blender-staging.git] / source / blender / editors / screen / glutil.c
1 /**
2  * $Id: glutil.c 11920 2007-09-02 17:25:03Z elubie $
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 glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *rect)
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, rast_y);
464
465                         glTexCoord2f((float) (subpart_w-1)/tex_w, (float) (subpart_h-1)/tex_h);
466                         glVertex2f(rast_x+subpart_w*xzoom, rast_y+subpart_h*yzoom);
467
468                         glTexCoord2f(0, (float) (subpart_h-1)/tex_h);
469                         glVertex2f(rast_x, rast_y+subpart_h*yzoom);
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 glaDrawPixelsSafe_to32(float fx, float fy, int img_w, int img_h, int row_w, float *rectf)
481 {
482         float *rf;
483         int x, y;
484         char *rect32, *rc;
485         
486         /* copy imgw-imgh to a temporal 32 bits rect */
487         if(img_w<1 || img_h<1) return;
488         
489         rc= rect32= MEM_mallocN(img_w*img_h*sizeof(int), "temp 32 bits");
490         
491         for(y=0; y<img_h; y++) {
492                 rf= rectf;
493                 for(x=0; x<img_w; x++, rf+=4, rc+=4) {
494                         rc[0]= FTOCHAR(rf[0]);
495                         rc[1]= FTOCHAR(rf[1]);
496                         rc[2]= FTOCHAR(rf[2]);
497                         rc[3]= FTOCHAR(rf[3]);
498                 }
499                 rectf+= 4*row_w;
500         }
501         
502         glaDrawPixelsSafe(fx, fy, img_w, img_h, img_w, GL_RGBA, GL_UNSIGNED_BYTE, rect32);
503
504         MEM_freeN(rect32);
505 }
506
507 void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, int row_w, int format, int type, void *rect)
508 {
509         float xzoom= glaGetOneFloat(GL_ZOOM_X);
510         float yzoom= glaGetOneFloat(GL_ZOOM_Y);
511                 
512                 /* The pixel space coordinate of the intersection of
513                  * the [zoomed] image with the origin.
514                  */
515         float ix= -x/xzoom;
516         float iy= -y/yzoom;
517         
518                 /* The maximum pixel amounts the image can be cropped
519                  * at the lower left without exceeding the origin.
520                  */
521         int off_x= floor(MAX2(ix, 0));
522         int off_y= floor(MAX2(iy, 0));
523                 
524                 /* The zoomed space coordinate of the raster position 
525                  * (starting at the lower left most unclipped pixel).
526                  */
527         float rast_x= x + off_x*xzoom;
528         float rast_y= y + off_y*yzoom;
529
530         GLfloat scissor[4];
531         int draw_w, draw_h;
532
533                 /* Determine the smallest number of pixels we need to draw
534                  * before the image would go off the upper right corner.
535                  * 
536                  * It may seem this is just an optimization but some graphics 
537                  * cards (ATI) freak out if there is a large zoom factor and
538                  * a large number of pixels off the screen (probably at some
539                  * level the number of image pixels to draw is getting multiplied
540                  * by the zoom and then clamped). Making sure we draw the
541                  * fewest pixels possible keeps everyone mostly happy (still
542                  * fails if we zoom in on one really huge pixel so that it
543                  * covers the entire screen).
544                  */
545         glGetFloatv(GL_SCISSOR_BOX, scissor);
546         draw_w = MIN2(img_w-off_x, ceil((scissor[2]-rast_x)/xzoom));
547         draw_h = MIN2(img_h-off_y, ceil((scissor[3]-rast_y)/yzoom));
548
549         if (draw_w>0 && draw_h>0) {
550                 int old_row_length = glaGetOneInteger(GL_UNPACK_ROW_LENGTH);
551
552                         /* Don't use safe RasterPos (slower) if we can avoid it. */
553                 if (rast_x>=0 && rast_y>=0) {
554                         glRasterPos2f(rast_x, rast_y);
555                 } else {
556                         glaRasterPosSafe2f(rast_x, rast_y, 0, 0);
557                 }
558
559                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_w);
560                 if(format==GL_LUMINANCE || format==GL_RED) {
561                         if(type==GL_FLOAT) {
562                                 float *f_rect= (float *)rect;
563                                 glDrawPixels(draw_w, draw_h, format, type, f_rect + (off_y*row_w + off_x));
564                         }
565                         else if(type==GL_INT || type==GL_UNSIGNED_INT) {
566                                 int *i_rect= (int *)rect;
567                                 glDrawPixels(draw_w, draw_h, format, type, i_rect + (off_y*row_w + off_x));
568                         }
569                 }
570                 else { /* RGBA */
571                         if(type==GL_FLOAT) {
572                                 float *f_rect= (float *)rect;
573                                 glDrawPixels(draw_w, draw_h, format, type, f_rect + (off_y*row_w + off_x)*4);
574                         }
575                         else if(type==GL_UNSIGNED_BYTE) {
576                                 unsigned char *uc_rect= (unsigned char *) rect;
577                                 glDrawPixels(draw_w, draw_h, format, type, uc_rect + (off_y*row_w + off_x)*4);
578                         }
579                 }
580                 
581                 glPixelStorei(GL_UNPACK_ROW_LENGTH,  old_row_length);
582         }
583 }
584
585 /* 2D Drawing Assistance */
586
587 void glaDefine2DArea(rcti *screen_rect)
588 {
589         int sc_w= screen_rect->xmax - screen_rect->xmin;
590         int sc_h= screen_rect->ymax - screen_rect->ymin;
591
592         glViewport(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
593         glScissor(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
594
595                 /* The 0.375 magic number is to shift the matrix so that
596                  * both raster and vertex integer coordinates fall at pixel
597                  * centers properly. For a longer discussion see the OpenGL
598                  * Programming Guide, Appendix H, Correctness Tips.
599                  */
600
601         glMatrixMode(GL_PROJECTION);
602         glLoadIdentity();
603         glOrtho(0.0, sc_w, 0.0, sc_h, -1, 1);
604         glTranslatef(0.375, 0.375, 0.0);
605
606         glMatrixMode(GL_MODELVIEW);
607         glLoadIdentity();
608 }
609
610 struct gla2DDrawInfo {
611         int orig_vp[4], orig_sc[4];
612         float orig_projmat[16], orig_viewmat[16];
613
614         rcti screen_rect;
615         rctf world_rect;
616
617         float wo_to_sc[2];
618 };
619
620 void gla2DGetMap(gla2DDrawInfo *di, rctf *rect) 
621 {
622         *rect= di->world_rect;
623 }
624
625 void gla2DSetMap(gla2DDrawInfo *di, rctf *rect) 
626 {
627         int sc_w, sc_h;
628         float wo_w, wo_h;
629
630         di->world_rect= *rect;
631         
632         sc_w= (di->screen_rect.xmax-di->screen_rect.xmin);
633         sc_h= (di->screen_rect.ymax-di->screen_rect.ymin);
634         wo_w= (di->world_rect.xmax-di->world_rect.xmin);
635         wo_h= (di->world_rect.ymax-di->world_rect.ymin);
636         
637         di->wo_to_sc[0]= sc_w/wo_w;
638         di->wo_to_sc[1]= sc_h/wo_h;
639 }
640
641
642 gla2DDrawInfo *glaBegin2DDraw(rcti *screen_rect, rctf *world_rect) 
643 {
644         gla2DDrawInfo *di= MEM_mallocN(sizeof(*di), "gla2DDrawInfo");
645         int sc_w, sc_h;
646         float wo_w, wo_h;
647
648         glGetIntegerv(GL_VIEWPORT, (GLint *)di->orig_vp);
649         glGetIntegerv(GL_SCISSOR_BOX, (GLint *)di->orig_sc);
650         glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *)di->orig_projmat);
651         glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)di->orig_viewmat);
652
653         di->screen_rect= *screen_rect;
654         if (world_rect) {
655                 di->world_rect= *world_rect;
656         } else {
657                 di->world_rect.xmin= di->screen_rect.xmin;
658                 di->world_rect.ymin= di->screen_rect.ymin;
659                 di->world_rect.xmax= di->screen_rect.xmax;
660                 di->world_rect.ymax= di->screen_rect.ymax;
661         }
662
663         sc_w= (di->screen_rect.xmax-di->screen_rect.xmin);
664         sc_h= (di->screen_rect.ymax-di->screen_rect.ymin);
665         wo_w= (di->world_rect.xmax-di->world_rect.xmin);
666         wo_h= (di->world_rect.ymax-di->world_rect.ymin);
667
668         di->wo_to_sc[0]= sc_w/wo_w;
669         di->wo_to_sc[1]= sc_h/wo_h;
670
671         glaDefine2DArea(&di->screen_rect);
672
673         return di;
674 }
675
676 void gla2DDrawTranslatePt(gla2DDrawInfo *di, float wo_x, float wo_y, int *sc_x_r, int *sc_y_r)
677 {
678         *sc_x_r= (wo_x - di->world_rect.xmin)*di->wo_to_sc[0];
679         *sc_y_r= (wo_y - di->world_rect.ymin)*di->wo_to_sc[1];
680 }
681 void gla2DDrawTranslatePtv(gla2DDrawInfo *di, float world[2], int screen_r[2])
682 {
683         screen_r[0]= (world[0] - di->world_rect.xmin)*di->wo_to_sc[0];
684         screen_r[1]= (world[1] - di->world_rect.ymin)*di->wo_to_sc[1];
685 }
686
687 void glaEnd2DDraw(gla2DDrawInfo *di)
688 {
689         glViewport(di->orig_vp[0], di->orig_vp[1], di->orig_vp[2], di->orig_vp[3]);
690         glScissor(di->orig_vp[0], di->orig_vp[1], di->orig_vp[2], di->orig_vp[3]);
691         glMatrixMode(GL_PROJECTION);
692         glLoadMatrixf(di->orig_projmat);
693         glMatrixMode(GL_MODELVIEW);
694         glLoadMatrixf(di->orig_viewmat);
695
696         MEM_freeN(di);
697 }
698
699 /* **************** glPoint hack ************************ */
700
701 static int curmode=0;
702 static int pointhack=0;
703 static GLubyte Squaredot[16] = { 0xff,0xff,0xff,0xff,
704                                                                  0xff,0xff,0xff,0xff,
705                                                                  0xff,0xff,0xff,0xff, 
706                                                                  0xff,0xff,0xff,0xff};
707
708 void bglBegin(int mode)
709 {
710         curmode= mode;
711         
712         if(mode==GL_POINTS) {
713                 float value[4];
714                 glGetFloatv(GL_POINT_SIZE_RANGE, value);
715                 if(value[1]<2.0) {
716                         glGetFloatv(GL_POINT_SIZE, value);
717                         pointhack= floor(value[0]+0.5);
718                         if(pointhack>4) pointhack= 4;
719                 }
720                 else glBegin(mode);
721         }
722 }
723
724
725 void bglVertex3fv(float *vec)
726 {
727         switch(curmode) {
728         case GL_POINTS:
729                 if(pointhack) {
730                         glRasterPos3fv(vec);
731                         glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
732                 }
733                 else glVertex3fv(vec);
734                 break;
735         }
736 }
737
738 void bglVertex3f(float x, float y, float z)
739 {
740         switch(curmode) {
741         case GL_POINTS:
742                 if(pointhack) {
743                         glRasterPos3f(x, y, z);
744                         glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
745                 }
746                 else glVertex3f(x, y, z);
747                 break;
748         }
749 }
750
751 void bglVertex2fv(float *vec)
752 {
753         switch(curmode) {
754         case GL_POINTS:
755                 if(pointhack) {
756                         glRasterPos2fv(vec);
757                         glBitmap(pointhack, pointhack, (float)pointhack/2, pointhack/2, 0.0, 0.0, Squaredot);
758                 }
759                 else glVertex2fv(vec);
760                 break;
761         }
762 }
763
764
765 void bglEnd(void)
766 {
767         if(pointhack) pointhack= 0;
768         else glEnd();
769         
770 }
771
772 /* Uses current OpenGL state to get view matrices for gluProject/gluUnProject */
773 void bgl_get_mats(bglMats *mats)
774 {
775         const double badvalue= 1.0e-6;
776
777         glGetDoublev(GL_MODELVIEW_MATRIX, mats->modelview);
778         glGetDoublev(GL_PROJECTION_MATRIX, mats->projection);
779         glGetIntegerv(GL_VIEWPORT, (GLint *)mats->viewport);
780         
781         /* Very strange code here - it seems that certain bad values in the
782            modelview matrix can cause gluUnProject to give bad results. */
783         if(mats->modelview[0] < badvalue &&
784            mats->modelview[0] > -badvalue)
785                 mats->modelview[0]= 0;
786         if(mats->modelview[5] < badvalue &&
787            mats->modelview[5] > -badvalue)
788                 mats->modelview[5]= 0;
789         
790         /* Set up viewport so that gluUnProject will give correct values */
791         mats->viewport[0] = 0;
792         mats->viewport[1] = 0;
793 }
794
795 /* *************** glPolygonOffset hack ************* */
796
797 /* dist is only for ortho now... */
798 void bglPolygonOffset(float viewdist, float dist) 
799 {
800         static float winmat[16], offset=0.0;    
801         
802         if(dist!=0.0) {
803                 float offs;
804                 
805                 // glEnable(GL_POLYGON_OFFSET_FILL);
806                 // glPolygonOffset(-1.0, -1.0);
807
808                 /* hack below is to mimic polygon offset */
809                 glMatrixMode(GL_PROJECTION);
810                 glGetFloatv(GL_PROJECTION_MATRIX, (float *)winmat);
811                 
812                 /* dist is from camera to center point */
813                 
814                 if(winmat[15]>0.5) offs= 0.00001*dist*viewdist;  // ortho tweaking
815                 else offs= 0.0005*dist;  // should be clipping value or so...
816                 
817                 winmat[14]-= offs;
818                 offset+= offs;
819                 
820                 glLoadMatrixf(winmat);
821                 glMatrixMode(GL_MODELVIEW);
822         }
823         else {
824
825                 glMatrixMode(GL_PROJECTION);
826                 winmat[14]+= offset;
827                 offset= 0.0;
828                 glLoadMatrixf(winmat);
829                 glMatrixMode(GL_MODELVIEW);
830         }
831 }
832
833 int is_a_really_crappy_intel_card(void)
834 {
835         static int well_is_it= -1;
836
837                 /* Do you understand the implication? Do you? */
838         if (well_is_it==-1)
839                 well_is_it= (strcmp((char*) glGetString(GL_VENDOR), "Intel Inc.") == 0);
840
841         return well_is_it;
842 }
843
844 void bglFlush(void) 
845 {
846         glFlush();
847 #ifdef __APPLE__
848 //      if(is_a_really_crappy_intel_card())
849 // XXX          myswapbuffers(); //hack to get mac intel graphics to show frontbuffer
850 #endif
851 }
852
853