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