Stage two of the giant animation recode project; Ipo/Action/NLA cleanup
[blender.git] / source / blender / src / glutil.c
1
2
3 /**
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <math.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_vec_types.h"
44
45 #include "BKE_utildefines.h"
46
47 #include "BLI_arithb.h"
48 #include "BIF_gl.h"
49 #include "BIF_glutil.h"
50
51         /* Invert line handling */
52         
53 #define glToggle(mode, onoff)   (((onoff)?glEnable:glDisable)(mode))
54
55 void set_inverted_drawing(int enable) 
56 {
57         glLogicOp(enable?GL_INVERT:GL_COPY);
58
59         /* Use GL_BLEND_EQUATION_EXT on sgi (if we have it),
60          * apparently GL_COLOR_LOGIC_OP doesn't work on O2?
61          * Is this an sgi bug or our bug?
62          */
63 #if defined(__sgi) && defined(GL_BLEND_EQUATION_EXT)
64         glBlendEquationEXT(enable?GL_LOGIC_OP:GL_FUNC_ADD_EXT);
65         glToggle(GL_BLEND, enable);
66 #else
67         glToggle(GL_COLOR_LOGIC_OP, enable);
68 #endif
69
70         glToggle(GL_DITHER, !enable);
71 }
72
73 void sdrawXORline(int x0, int y0, int x1, int y1)
74 {
75         if(x0==x1 && y0==y1) return;
76
77         set_inverted_drawing(1);
78         
79         glBegin(GL_LINES);
80         glVertex2i(x0, y0);
81         glVertex2i(x1, y1);
82         glEnd();
83         
84         set_inverted_drawing(0);
85 }
86
87 void glutil_draw_front_xor_line(int x0, int y0, int x1, int y1)
88 {
89         glReadBuffer(GL_FRONT);
90         glDrawBuffer(GL_FRONT);
91         sdrawXORline(x0, y0, x1, y1);
92         glFlush();
93         glReadBuffer(GL_BACK);
94         glDrawBuffer(GL_BACK);
95         
96 }
97
98 void sdrawXORline4(int nr, int x0, int y0, int x1, int y1)
99 {
100         static short old[4][2][2];
101         static char flags[4]= {0, 0, 0, 0};
102         
103                 /* with builtin memory, max 4 lines */
104
105         set_inverted_drawing(1);
106                 
107         glBegin(GL_LINES);
108         if(nr== -1) { /* flush */
109                 for (nr=0; nr<4; nr++) {
110                         if (flags[nr]) {
111                                 glVertex2sv(old[nr][0]);
112                                 glVertex2sv(old[nr][1]);
113                                 flags[nr]= 0;
114                         }
115                 }
116         } else {
117                 if(nr>=0 && nr<4) {
118                         if(flags[nr]) {
119                                 glVertex2sv(old[nr][0]);
120                                 glVertex2sv(old[nr][1]);
121                         }
122
123                         old[nr][0][0]= x0;
124                         old[nr][0][1]= y0;
125                         old[nr][1][0]= x1;
126                         old[nr][1][1]= y1;
127                         
128                         flags[nr]= 1;
129                 }
130                 
131                 glVertex2i(x0, y0);
132                 glVertex2i(x1, y1);
133         }
134         glEnd();
135         
136         set_inverted_drawing(0);
137 }
138
139 void fdrawXORcirc(float xofs, float yofs, float rad)
140 {
141         set_inverted_drawing(1);
142
143         glPushMatrix();
144         glTranslatef(xofs, yofs, 0.0);
145         glutil_draw_lined_arc(0.0, M_PI*2.0, rad, 20);
146         glPopMatrix();
147
148         set_inverted_drawing(0);
149 }
150
151 void glutil_draw_filled_arc(float start, float angle, float radius, int nsegments) {
152         int i;
153         
154         glBegin(GL_TRIANGLE_FAN);
155         glVertex2f(0.0, 0.0);
156         for (i=0; i<nsegments; i++) {
157                 float t= (float) i/(nsegments-1);
158                 float cur= start + t*angle;
159                 
160                 glVertex2f(cos(cur)*radius, sin(cur)*radius);
161         }
162         glEnd();
163 }
164
165 void glutil_draw_lined_arc(float start, float angle, float radius, int nsegments) {
166         int i;
167         
168         glBegin(GL_LINE_STRIP);
169         for (i=0; i<nsegments; i++) {
170                 float t= (float) i/(nsegments-1);
171                 float cur= start + t*angle;
172                 
173                 glVertex2f(cos(cur)*radius, sin(cur)*radius);
174         }
175         glEnd();
176 }
177
178 int glaGetOneInteger(int param)
179 {
180         GLint i;
181         glGetIntegerv(param, &i);
182         return i;
183 }
184
185 float glaGetOneFloat(int param)
186 {
187         GLfloat v;
188         glGetFloatv(param, &v);
189         return v;
190 }
191
192 void glaRasterPosSafe2f(float x, float y, float known_good_x, float known_good_y)
193 {
194         GLubyte dummy= 0;
195
196                 /* As long as known good coordinates are correct
197                  * this is guarenteed to generate an ok raster
198                  * position (ignoring potential (real) overflow
199                  * issues).
200                  */
201         glRasterPos2f(known_good_x, known_good_y);
202
203                 /* Now shift the raster position to where we wanted
204                  * it in the first place using the glBitmap trick.
205                  */
206         glBitmap(0, 0, 0, 0, x - known_good_x, y - known_good_y, &dummy);
207 }
208
209 static int get_cached_work_texture(int *w_r, int *h_r)
210 {
211         static GLint texid= -1;
212         static int tex_w= 256;
213         static int tex_h= 256;
214
215         if (texid==-1) {
216                 GLint ltexid= glaGetOneInteger(GL_TEXTURE_2D);
217                 unsigned char *tbuf;
218
219                 glGenTextures(1, &texid);
220
221                 glBindTexture(GL_TEXTURE_2D, texid);
222
223                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
225                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
226
227                 tbuf= MEM_callocN(tex_w*tex_h*4, "tbuf");
228                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tbuf);
229                 MEM_freeN(tbuf);
230
231                 glBindTexture(GL_TEXTURE_2D, ltexid);
232         }
233
234         *w_r= tex_w;
235         *h_r= tex_h;
236         return texid;
237 }
238
239 void glaDrawPixelsTex(float x, float y, int img_w, int img_h, void *rect)
240 {
241         unsigned char *uc_rect= (unsigned char*) rect;
242         float xzoom= glaGetOneFloat(GL_ZOOM_X), yzoom= glaGetOneFloat(GL_ZOOM_Y);
243         int ltexid= glaGetOneInteger(GL_TEXTURE_2D);
244         int lrowlength= glaGetOneInteger(GL_UNPACK_ROW_LENGTH);
245         int subpart_x, subpart_y, tex_w, tex_h;
246         int texid= get_cached_work_texture(&tex_w, &tex_h);
247         int nsubparts_x= (img_w+(tex_w-1))/tex_w;
248         int nsubparts_y= (img_h+(tex_h-1))/tex_h;
249
250         glPixelStorei(GL_UNPACK_ROW_LENGTH, img_w);
251         glBindTexture(GL_TEXTURE_2D, texid);
252
253         for (subpart_y=0; subpart_y<nsubparts_y; subpart_y++) {
254                 for (subpart_x=0; subpart_x<nsubparts_x; subpart_x++) {
255                         int subpart_w= (subpart_x==nsubparts_x-1)?(img_w-subpart_x*tex_w):tex_w;
256                         int subpart_h= (subpart_y==nsubparts_y-1)?(img_h-subpart_y*tex_h):tex_h;
257                         float rast_x= x+subpart_x*tex_w*xzoom;
258                         float rast_y= y+subpart_y*tex_h*yzoom;
259
260                         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]);
261
262                         glColor3ub(255, 255, 255);
263                         glEnable(GL_TEXTURE_2D);
264                         glBegin(GL_QUADS);
265                         glTexCoord2f(0, 0);
266                         glVertex2f(rast_x, rast_y);
267
268                         glTexCoord2f((float) subpart_w/tex_w, 0);
269                         glVertex2f(rast_x+subpart_w*xzoom, rast_y);
270
271                         glTexCoord2f((float) subpart_w/tex_w, (float) subpart_h/tex_h);
272                         glVertex2f(rast_x+subpart_w*xzoom, rast_y+subpart_h*yzoom);
273
274                         glTexCoord2f(0, (float) subpart_h/tex_h);
275                         glVertex2f(rast_x, rast_y+subpart_h*yzoom);
276                         glEnd();
277                         glDisable(GL_TEXTURE_2D);
278                 }
279         }
280
281         glBindTexture(GL_TEXTURE_2D, ltexid);
282         glPixelStorei(GL_UNPACK_ROW_LENGTH, lrowlength);
283 }
284
285 void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, void *rect)
286 {
287         unsigned char *uc_rect= (unsigned char*) rect;
288
289         float xzoom= glaGetOneFloat(GL_ZOOM_X);
290         float yzoom= glaGetOneFloat(GL_ZOOM_Y);
291                 
292                 /* The pixel space coordinate of the intersection of
293                  * the [zoomed] image with the origin.
294                  */
295         float ix= -x/xzoom;
296         float iy= -y/yzoom;
297         
298                 /* The maximum pixel amounts the image can be cropped
299                  * at the lower left without exceeding the origin.
300                  */
301         int off_x= floor(MAX2(ix, 0));
302         int off_y= floor(MAX2(iy, 0));
303                 
304                 /* The zoomed space coordinate of the raster position 
305                  * (starting at the lower left most unclipped pixel).
306                  */
307         float rast_x= x + off_x*xzoom;
308         float rast_y= y + off_y*yzoom;
309
310         GLfloat scissor[4];
311         int draw_w, draw_h;
312
313                 /* Determine the smallest number of pixels we need to draw
314                  * before the image would go off the upper right corner.
315                  * 
316                  * It may seem this is just an optimization but some graphics 
317                  * cards (ATI) freak out if there is a large zoom factor and
318                  * a large number of pixels off the screen (probably at some
319                  * level the number of image pixels to draw is getting multiplied
320                  * by the zoom and then clamped). Making sure we draw the
321                  * fewest pixels possible keeps everyone mostly happy (still
322                  * fails if we zoom in on one really huge pixel so that it
323                  * covers the entire screen).
324                  */
325         glGetFloatv(GL_SCISSOR_BOX, scissor);
326         draw_w = MIN2(img_w-off_x, ceil((scissor[2]-rast_x)/xzoom));
327         draw_h = MIN2(img_h-off_y, ceil((scissor[3]-rast_y)/yzoom));
328
329         if (draw_w>0 && draw_h>0) {
330                 int old_row_length = glaGetOneInteger(GL_UNPACK_ROW_LENGTH);
331
332                         /* Don't use safe RasterPos (slower) if we can avoid it. */
333                 if (rast_x>=0 && rast_y>=0) {
334                         glRasterPos2f(rast_x, rast_y);
335                 } else {
336                         glaRasterPosSafe2f(rast_x, rast_y, 0, 0);
337                 }
338
339                 glPixelStorei(GL_UNPACK_ROW_LENGTH, img_w);
340                 glDrawPixels(draw_w, draw_h, GL_RGBA, GL_UNSIGNED_BYTE, uc_rect + (off_y*img_w + off_x)*4);
341                 glPixelStorei(GL_UNPACK_ROW_LENGTH,  old_row_length);
342         }
343 }
344
345 /* 2D Drawing Assistance */
346
347 void glaDefine2DArea(rcti *screen_rect)
348 {
349         int sc_w= screen_rect->xmax - screen_rect->xmin;
350         int sc_h= screen_rect->ymax - screen_rect->ymin;
351
352         glViewport(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
353         glScissor(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
354
355                 /* The 0.375 magic number is to shift the matrix so that
356                  * both raster and vertex integer coordinates fall at pixel
357                  * centers properly. For a longer discussion see the OpenGL
358                  * Programming Guide, Appendix H, Correctness Tips.
359                  */
360
361         glMatrixMode(GL_PROJECTION);
362         glLoadIdentity();
363         glOrtho(0.0, sc_w, 0.0, sc_h, -1, 1);
364         glTranslatef(0.375, 0.375, 0.0);
365
366         glMatrixMode(GL_MODELVIEW);
367         glLoadIdentity();
368 }
369
370 struct gla2DDrawInfo {
371         int orig_vp[4], orig_sc[4];
372         float orig_projmat[16], orig_viewmat[16];
373
374         rcti screen_rect;
375         rctf world_rect;
376
377         float wo_to_sc[2];
378 };
379
380 void gla2DGetMap(gla2DDrawInfo *di, rctf *rect) 
381 {
382         *rect= di->world_rect;
383 }
384
385 void gla2DSetMap(gla2DDrawInfo *di, rctf *rect) 
386 {
387         int sc_w, sc_h;
388         float wo_w, wo_h;
389
390         di->world_rect= *rect;
391         
392         sc_w= (di->screen_rect.xmax-di->screen_rect.xmin);
393         sc_h= (di->screen_rect.ymax-di->screen_rect.ymin);
394         wo_w= (di->world_rect.xmax-di->world_rect.xmin);
395         wo_h= (di->world_rect.ymax-di->world_rect.ymin);
396         
397         di->wo_to_sc[0]= sc_w/wo_w;
398         di->wo_to_sc[1]= sc_h/wo_h;
399 }
400
401
402 gla2DDrawInfo *glaBegin2DDraw(rcti *screen_rect, rctf *world_rect) 
403 {
404         gla2DDrawInfo *di= MEM_mallocN(sizeof(*di), "gla2DDrawInfo");
405         int sc_w, sc_h;
406         float wo_w, wo_h;
407
408         glGetIntegerv(GL_VIEWPORT, (GLint *)di->orig_vp);
409         glGetIntegerv(GL_SCISSOR_BOX, (GLint *)di->orig_sc);
410         glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *)di->orig_projmat);
411         glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)di->orig_viewmat);
412
413         di->screen_rect= *screen_rect;
414         if (world_rect) {
415                 di->world_rect= *world_rect;
416         } else {
417                 di->world_rect.xmin= di->screen_rect.xmin;
418                 di->world_rect.ymin= di->screen_rect.ymin;
419                 di->world_rect.xmax= di->screen_rect.xmax;
420                 di->world_rect.ymax= di->screen_rect.ymax;
421         }
422
423         sc_w= (di->screen_rect.xmax-di->screen_rect.xmin);
424         sc_h= (di->screen_rect.ymax-di->screen_rect.ymin);
425         wo_w= (di->world_rect.xmax-di->world_rect.xmin);
426         wo_h= (di->world_rect.ymax-di->world_rect.ymin);
427
428         di->wo_to_sc[0]= sc_w/wo_w;
429         di->wo_to_sc[1]= sc_h/wo_h;
430
431         glaDefine2DArea(&di->screen_rect);
432
433         return di;
434 }
435
436 void gla2DDrawTranslatePt(gla2DDrawInfo *di, float wo_x, float wo_y, int *sc_x_r, int *sc_y_r)
437 {
438         *sc_x_r= (wo_x - di->world_rect.xmin)*di->wo_to_sc[0];
439         *sc_y_r= (wo_y - di->world_rect.ymin)*di->wo_to_sc[1];
440 }
441 void gla2DDrawTranslatePtv(gla2DDrawInfo *di, float world[2], int screen_r[2])
442 {
443         screen_r[0]= (world[0] - di->world_rect.xmin)*di->wo_to_sc[0];
444         screen_r[1]= (world[1] - di->world_rect.ymin)*di->wo_to_sc[1];
445 }
446
447 void glaEnd2DDraw(gla2DDrawInfo *di)
448 {
449         glViewport(di->orig_vp[0], di->orig_vp[1], di->orig_vp[2], di->orig_vp[3]);
450         glScissor(di->orig_vp[0], di->orig_vp[1], di->orig_vp[2], di->orig_vp[3]);
451         glMatrixMode(GL_PROJECTION);
452         glLoadMatrixf(di->orig_projmat);
453         glMatrixMode(GL_MODELVIEW);
454         glLoadMatrixf(di->orig_viewmat);
455
456         MEM_freeN(di);
457 }
458
459 /* **************** glPoint hack ************************ */
460
461 static int curmode=0;
462 static int pointhack=0;
463 static GLubyte Squaredot[16] = { 0xff,0xff,0xff,0xff,
464                                                                  0xff,0xff,0xff,0xff,
465                                                                  0xff,0xff,0xff,0xff, 
466                                                                  0xff,0xff,0xff,0xff};
467
468 void bglBegin(int mode)
469 {
470         curmode= mode;
471         
472         if(mode==GL_POINTS) {
473                 float value[4];
474                 glGetFloatv(GL_POINT_SIZE_RANGE, value);
475                 if(value[1]<2.0) {
476                         glGetFloatv(GL_POINT_SIZE, value);
477                         pointhack= floor(value[0]+0.5);
478                         if(pointhack>4) pointhack= 4;
479                 }
480                 else glBegin(mode);
481         }
482 }
483
484
485 void bglVertex3fv(float *vec)
486 {
487         switch(curmode) {
488         case GL_POINTS:
489                 if(pointhack) {
490                         glRasterPos3fv(vec);
491                         glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
492                 }
493                 else glVertex3fv(vec);
494                 break;
495         }
496 }
497
498 void bglVertex3f(float x, float y, float z)
499 {
500         switch(curmode) {
501         case GL_POINTS:
502                 if(pointhack) {
503                         glRasterPos3f(x, y, z);
504                         glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
505                 }
506                 else glVertex3f(x, y, z);
507                 break;
508         }
509 }
510
511 void bglVertex2fv(float *vec)
512 {
513         switch(curmode) {
514         case GL_POINTS:
515                 if(pointhack) {
516                         glRasterPos2fv(vec);
517                         glBitmap(pointhack, pointhack, (float)pointhack/2, pointhack/2, 0.0, 0.0, Squaredot);
518                 }
519                 else glVertex2fv(vec);
520                 break;
521         }
522 }
523
524
525 void bglEnd(void)
526 {
527         if(pointhack) pointhack= 0;
528         else glEnd();
529         
530 }
531
532 /* *************** glPolygonOffset hack ************* */
533
534 // both temporal, so here for now (ton)
535 #include "BKE_global.h"
536 #include "DNA_view3d_types.h"
537
538 /* dist is only for ortho now... */
539 void bglPolygonOffset(float dist) 
540 {
541         static float winmat[16], offset=0.0;    
542         
543         if(dist!=0.0) {
544                 float offs;
545                 
546                 // glEnable(GL_POLYGON_OFFSET_FILL);
547                 // glPolygonOffset(-1.0, -1.0);
548
549                 /* hack below is to mimic polygon offset */
550                 glMatrixMode(GL_PROJECTION);
551                 glGetFloatv(GL_PROJECTION_MATRIX, (float *)winmat);
552                 
553                 /* dist is from camera to center point */
554                 
555                 if(winmat[15]>0.5) offs= 0.00001*dist*G.vd->dist;  // ortho tweaking
556                 else offs= 0.0005*dist;  // should be clipping value or so...
557                 
558                 winmat[14]-= offs;
559                 offset+= offs;
560                 
561                 glLoadMatrixf(winmat);
562                 glMatrixMode(GL_MODELVIEW);
563         }
564         else {
565
566                 glMatrixMode(GL_PROJECTION);
567                 winmat[14]+= offset;
568                 offset= 0.0;
569                 glLoadMatrixf(winmat);
570                 glMatrixMode(GL_MODELVIEW);
571         }
572 }
573
574
575
576