~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/src/glutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
 
3
3
/**
4
 
 * $Id: glutil.c,v 1.5 2003/05/02 10:44:14 phase Exp $
 
4
 * $Id: glutil.c,v 1.18 2005/03/19 21:08:12 zuster Exp $
5
5
 *
6
6
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7
7
 *
38
38
#include <config.h>
39
39
#endif
40
40
 
41
 
#ifdef WIN32
42
 
#include "BLI_winstuff.h"
43
 
#endif
44
 
 
45
41
#include "MEM_guardedalloc.h"
46
42
 
47
43
#include "DNA_vec_types.h"
48
44
 
 
45
#include "BKE_utildefines.h"
 
46
 
 
47
#include "BLI_arithb.h"
49
48
#include "BIF_gl.h"
50
49
#include "BIF_glutil.h"
51
50
 
87
86
 
88
87
void glutil_draw_front_xor_line(int x0, int y0, int x1, int y1)
89
88
{
 
89
        glReadBuffer(GL_FRONT);
90
90
        glDrawBuffer(GL_FRONT);
91
91
        sdrawXORline(x0, y0, x1, y1);
92
 
        glFinish();
 
92
        glFlush();
 
93
        glReadBuffer(GL_BACK);
93
94
        glDrawBuffer(GL_BACK);
 
95
        
94
96
}
95
97
 
96
98
void sdrawXORline4(int nr, int x0, int y0, int x1, int y1)
134
136
        set_inverted_drawing(0);
135
137
}
136
138
 
137
 
void sdrawXORcirc(short xofs, short yofs, float rad)
 
139
void fdrawXORcirc(float xofs, float yofs, float rad)
138
140
{
139
141
        set_inverted_drawing(1);
140
142
 
141
143
        glPushMatrix();
142
144
        glTranslatef(xofs, yofs, 0.0);
143
 
        glutil_draw_lined_arc(0.0, M_PI*2, rad, 20);
 
145
        glutil_draw_lined_arc(0.0, M_PI*2.0, rad, 20);
144
146
        glPopMatrix();
145
147
 
146
148
        set_inverted_drawing(0);
175
177
 
176
178
int glaGetOneInteger(int param)
177
179
{
178
 
        int i;
 
180
        GLint i;
179
181
        glGetIntegerv(param, &i);
180
182
        return i;
181
183
}
182
184
 
183
185
float glaGetOneFloat(int param)
184
186
{
185
 
        float v;
 
187
        GLfloat v;
186
188
        glGetFloatv(param, &v);
187
189
        return v;
188
190
}
206
208
 
207
209
static int get_cached_work_texture(int *w_r, int *h_r)
208
210
{
209
 
        static int texid= -1;
 
211
        static GLint texid= -1;
210
212
        static int tex_w= 256;
211
213
        static int tex_h= 256;
212
214
 
283
285
void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, void *rect)
284
286
{
285
287
        unsigned char *uc_rect= (unsigned char*) rect;
286
 
        float origin_x= 0.375;
287
 
        float origin_y= 0.375;
288
 
 
289
 
                /* Trivial case */
290
 
        if (x>=origin_x && y>=origin_y) {
291
 
                glRasterPos2f(x, y);
292
 
                glDrawPixels(img_w, img_h, GL_RGBA, GL_UNSIGNED_BYTE, uc_rect);
293
 
        } else {
294
 
                int old_row_length= glaGetOneInteger(GL_UNPACK_ROW_LENGTH);
295
 
                float xzoom= glaGetOneFloat(GL_ZOOM_X);
296
 
                float yzoom= glaGetOneFloat(GL_ZOOM_Y);
297
 
 
298
 
                        /* The pixel space coordinate of the intersection of
299
 
                         * the [zoomed] image with the origin.
300
 
                         */
301
 
                float ix= (origin_x-x)/xzoom;
302
 
                float iy= (origin_y-y)/yzoom;
 
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;
303
297
        
304
 
                        /* The maximum pixel amounts the image can cropped
305
 
                         * without exceeding the origin.
306
 
                         */
307
 
                int off_x= floor((ix>origin_x)?ix:origin_x);
308
 
                int off_y= floor((iy>origin_y)?iy:origin_y);
309
 
                
310
 
                        /* The zoomed space coordinate of the raster
311
 
                         * position.
312
 
                         */
313
 
                float rast_x= x + off_x*xzoom;
314
 
                float rast_y= y + off_y*yzoom;
315
 
                
316
 
                if (off_x<img_w && off_y<img_h) {
317
 
                        glaRasterPosSafe2f(rast_x, rast_y, origin_x, origin_y);
318
 
                        glPixelStorei(GL_UNPACK_ROW_LENGTH, img_w);
319
 
                        glDrawPixels(img_w-off_x, img_h-off_y, GL_RGBA, GL_UNSIGNED_BYTE, uc_rect+off_y*img_w*4+off_x*4);
320
 
                        glPixelStorei(GL_UNPACK_ROW_LENGTH,  old_row_length);
 
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);
321
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);
322
342
        }
323
343
}
324
344
 
363
383
        int sc_w, sc_h;
364
384
        float wo_w, wo_h;
365
385
 
366
 
        glGetIntegerv(GL_VIEWPORT, di->orig_vp);
367
 
        glGetIntegerv(GL_SCISSOR_BOX, di->orig_sc);
368
 
        glGetFloatv(GL_PROJECTION_MATRIX, di->orig_projmat);
369
 
        glGetFloatv(GL_MODELVIEW_MATRIX, di->orig_viewmat);
 
386
        glGetIntegerv(GL_VIEWPORT, (GLint *)di->orig_vp);
 
387
        glGetIntegerv(GL_SCISSOR_BOX, (GLint *)di->orig_sc);
 
388
        glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *)di->orig_projmat);
 
389
        glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)di->orig_viewmat);
370
390
 
371
391
        di->screen_rect= *screen_rect;
372
392
        if (world_rect) {
413
433
 
414
434
        MEM_freeN(di);
415
435
}
 
436
 
 
437
/* **************** glPoint hack ************************ */
 
438
 
 
439
static int curmode=0;
 
440
static int pointhack=0;
 
441
static GLubyte Squaredot[16] = { 0xff,0xff,0xff,0xff,
 
442
                                                                 0xff,0xff,0xff,0xff,
 
443
                                                                 0xff,0xff,0xff,0xff, 
 
444
                                                                 0xff,0xff,0xff,0xff};
 
445
 
 
446
void bglBegin(int mode)
 
447
{
 
448
        curmode= mode;
 
449
        
 
450
        if(mode==GL_POINTS) {
 
451
                float value[4];
 
452
                glGetFloatv(GL_POINT_SIZE_RANGE, value);
 
453
                if(value[1]<2.0) {
 
454
                        glGetFloatv(GL_POINT_SIZE, value);
 
455
                        pointhack= floor(value[0]+0.5);
 
456
                        if(pointhack>4) pointhack= 4;
 
457
                }
 
458
                else glBegin(mode);
 
459
        }
 
460
}
 
461
 
 
462
 
 
463
void bglVertex3fv(float *vec)
 
464
{
 
465
        switch(curmode) {
 
466
        case GL_POINTS:
 
467
                if(pointhack) {
 
468
                        glRasterPos3fv(vec);
 
469
                        glBitmap(pointhack, pointhack, (float)pointhack/2.0, (float)pointhack/2.0, 0.0, 0.0, Squaredot);
 
470
                }
 
471
                else glVertex3fv(vec);
 
472
                break;
 
473
        }
 
474
}
 
475
 
 
476
void bglVertex2fv(float *vec)
 
477
{
 
478
        switch(curmode) {
 
479
        case GL_POINTS:
 
480
                if(pointhack) {
 
481
                        glRasterPos2fv(vec);
 
482
                        glBitmap(pointhack, pointhack, (float)pointhack/2, pointhack/2, 0.0, 0.0, Squaredot);
 
483
                }
 
484
                else glVertex2fv(vec);
 
485
                break;
 
486
        }
 
487
}
 
488
 
 
489
 
 
490
void bglEnd(void)
 
491
{
 
492
        if(pointhack) pointhack= 0;
 
493
        else glEnd();
 
494
        
 
495
}
 
496
 
 
497
/* *************** glPolygonOffset hack ************* */
 
498
 
 
499
// both temporal, so here for now (ton)
 
500
#include "BKE_global.h"
 
501
#include "DNA_view3d_types.h"
 
502
 
 
503
/* dist is only for ortho now... */
 
504
void bglPolygonOffset(float dist) 
 
505
{
 
506
        static float winmat[16], offset=0.0;    
 
507
        
 
508
        if(dist!=0.0) {
 
509
                float offs;
 
510
                
 
511
                // glEnable(GL_POLYGON_OFFSET_FILL);
 
512
                // glPolygonOffset(-1.0, -1.0);
 
513
 
 
514
                /* hack below is to mimic polygon offset */
 
515
                glMatrixMode(GL_PROJECTION);
 
516
                glGetFloatv(GL_PROJECTION_MATRIX, (float *)winmat);
 
517
                
 
518
                /* dist is from camera to center point */
 
519
                
 
520
                if(winmat[15]>0.5) offs= 0.00001*dist*G.vd->dist;  // ortho tweaking
 
521
                else offs= 0.0005*dist;  // should be clipping value or so...
 
522
                
 
523
                winmat[14]-= offs;
 
524
                offset+= offs;
 
525
                
 
526
                glLoadMatrixf(winmat);
 
527
                glMatrixMode(GL_MODELVIEW);
 
528
        }
 
529
        else {
 
530
 
 
531
                glMatrixMode(GL_PROJECTION);
 
532
                winmat[14]+= offset;
 
533
                offset= 0.0;
 
534
                glLoadMatrixf(winmat);
 
535
                glMatrixMode(GL_MODELVIEW);
 
536
        }
 
537
}
 
538
 
 
539
 
 
540
 
 
541