WM Draw Methods now has a new option Automatic (default). This will
[blender-staging.git] / source / blender / editors / screen / glutil.c
index bead9a6399f9127f6c2fb533c5fa5ed196c7d76f..1ea0c2ccdbdc592df0726eab74fa58d1442c4c8f 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id: glutil.c 11920 2007-09-02 17:25:03Z elubie $
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -35,8 +35,9 @@
 #include "DNA_listBase.h"
 
 #include "BKE_utildefines.h"
+#include "BKE_colortools.h"
 
-#include "BLI_arithb.h"
+#include "BLI_math.h"
 #include "BLI_threads.h"
 
 #include "BIF_gl.h"
 
 /* ******************************************** */
 
+/* defined in BIF_gl.h */
+GLubyte stipple_halftone[128] = {
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
+       0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
+
+
+/*  repeat this pattern
+       X000X000
+       00000000
+       00X000X0
+       00000000 */
+
+
+GLubyte stipple_quarttone[128] = { 
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0,
+       136,136,136,136,0,0,0,0,34,34,34,34,0,0,0,0};
+
+
+void fdrawbezier(float vec[4][3])
+{
+       float dist;
+       float curve_res = 24, spline_step = 0.0f;
+       
+       dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
+       
+       /* check direction later, for top sockets */
+       vec[1][0]= vec[0][0]+dist;
+       vec[1][1]= vec[0][1];
+       
+       vec[2][0]= vec[3][0]-dist;
+       vec[2][1]= vec[3][1];
+       /* we can reuse the dist variable here to increment the GL curve eval amount*/
+       dist = 1.0f/curve_res;
+       
+       cpack(0x0);
+       glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
+       glBegin(GL_LINE_STRIP);
+       while (spline_step < 1.000001f) {
+               /*if(do_shaded)
+                       UI_ThemeColorBlend(th_col1, th_col2, spline_step);*/
+               glEvalCoord1f(spline_step);
+               spline_step += dist;
+       }
+       glEnd();
+}
+
 void fdrawline(float x1, float y1, float x2, float y2)
 {
        float v[2];
@@ -353,7 +420,7 @@ static int get_cached_work_texture(int *w_r, int *h_r)
        return texid;
 }
 
-void glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *rect)
+void glaDrawPixelsTexScaled(float x, float y, int img_w, int img_h, int format, void *rect, float scaleX, float scaleY)
 {
        unsigned char *uc_rect= (unsigned char*) rect;
        float *f_rect= (float *)rect;
@@ -372,10 +439,15 @@ void glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *
        glPixelStorei(GL_UNPACK_ROW_LENGTH, img_w);
        glBindTexture(GL_TEXTURE_2D, texid);
 
-        /* don't want nasty border artifacts */
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+       /* don't want nasty border artifacts */
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
+#ifdef __APPLE__
+       /* workaround for os x 10.5/10.6 driver bug: http://lists.apple.com/archives/Mac-opengl/2008/Jul/msg00117.html */
+       glPixelZoom(1.f, 1.f);
+#endif
+       
        for (subpart_y=0; subpart_y<nsubparts_y; subpart_y++) {
                for (subpart_x=0; subpart_x<nsubparts_x; subpart_x++) {
                        int subpart_w= (subpart_x==nsubparts_x-1)?(img_w-subpart_x*tex_w):tex_w;
@@ -394,13 +466,13 @@ void glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *
                        glVertex2f(rast_x, rast_y);
 
                        glTexCoord2f((float) (subpart_w-1)/tex_w, 0);
-                       glVertex2f(rast_x+subpart_w*xzoom, rast_y);
+                       glVertex2f(rast_x+subpart_w*xzoom*scaleX, rast_y);
 
-                       glTexCoord2f((float) (subpart_w-1)/tex_w, (float) subpart_h/tex_h);
-                       glVertex2f(rast_x+subpart_w*xzoom, rast_y+subpart_h*yzoom);
+                       glTexCoord2f((float) (subpart_w-1)/tex_w, (float) (subpart_h-1)/tex_h);
+                       glVertex2f(rast_x+subpart_w*xzoom*scaleX, rast_y+subpart_h*yzoom*scaleY);
 
-                       glTexCoord2f(0, (float) subpart_h/tex_h);
-                       glVertex2f(rast_x, rast_y+subpart_h*yzoom);
+                       glTexCoord2f(0, (float) (subpart_h-1)/tex_h);
+                       glVertex2f(rast_x, rast_y+subpart_h*yzoom*scaleY);
                        glEnd();
                        glDisable(GL_TEXTURE_2D);
                }
@@ -409,29 +481,32 @@ void glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *
        glBindTexture(GL_TEXTURE_2D, ltexid);
        glPixelStorei(GL_UNPACK_ROW_LENGTH, lrowlength);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+       
+#ifdef __APPLE__
+       /* workaround for os x 10.5/10.6 driver bug (above) */
+       glPixelZoom(xzoom, yzoom);
+#endif
 }
 
-void glaDrawPixelsSafe_to32(float fx, float fy, int img_w, int img_h, int row_w, float *rectf)
+void glaDrawPixelsTex(float x, float y, int img_w, int img_h, int format, void *rect)
+{
+       glaDrawPixelsTexScaled(x, y, img_w, img_h, format, rect, 1.0f, 1.0f);
+}
+
+void glaDrawPixelsSafe_to32(float fx, float fy, int img_w, int img_h, int row_w, float *rectf, int gamma_correct)
 {
-       float *rf;
-       int x, y;
-       char *rect32, *rc;
+       unsigned char *rect32;
        
        /* copy imgw-imgh to a temporal 32 bits rect */
        if(img_w<1 || img_h<1) return;
        
-       rc= rect32= MEM_mallocN(img_w*img_h*sizeof(int), "temp 32 bits");
+       rect32= MEM_mallocN(img_w*img_h*sizeof(int), "temp 32 bits");
        
-       for(y=0; y<img_h; y++) {
-               rf= rectf;
-               for(x=0; x<img_w; x++, rf+=4, rc+=4) {
-                       rc[0]= FTOCHAR(rf[0]);
-                       rc[1]= FTOCHAR(rf[1]);
-                       rc[2]= FTOCHAR(rf[2]);
-                       rc[3]= FTOCHAR(rf[3]);
-               }
-               rectf+= 4*row_w;
-       }
+       if (gamma_correct) {
+               floatbuf_to_srgb_byte(rectf, rect32, 0, img_w, 0, img_h, img_w);
+       } else {
+               floatbuf_to_byte(rectf, rect32, 0, img_w, 0, img_h, img_w);
+       }
        
        glaDrawPixelsSafe(fx, fy, img_w, img_h, img_w, GL_RGBA, GL_UNSIGNED_BYTE, rect32);
 
@@ -520,8 +595,8 @@ void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, int row_w, int fo
 
 void glaDefine2DArea(rcti *screen_rect)
 {
-       int sc_w= screen_rect->xmax - screen_rect->xmin;
-       int sc_h= screen_rect->ymax - screen_rect->ymin;
+       int sc_w= screen_rect->xmax - screen_rect->xmin + 1;
+       int sc_h= screen_rect->ymax - screen_rect->ymin + 1;
 
        glViewport(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
        glScissor(screen_rect->xmin, screen_rect->ymin, sc_w, sc_h);
@@ -655,6 +730,18 @@ void bglBegin(int mode)
        }
 }
 
+int bglPointHack() {
+       float value[4];
+       int pointhack;
+       glGetFloatv(GL_POINT_SIZE_RANGE, value);
+       if(value[1]<2.0) {
+               glGetFloatv(GL_POINT_SIZE, value);
+               pointhack= floor(value[0]+0.5);
+               if(pointhack>4) pointhack= 4;
+               return pointhack;
+       }
+       return 0;
+}
 
 void bglVertex3fv(float *vec)
 {
@@ -764,24 +851,12 @@ void bglPolygonOffset(float viewdist, float dist)
        }
 }
 
-int is_a_really_crappy_intel_card(void)
-{
-       static int well_is_it= -1;
-
-               /* Do you understand the implication? Do you? */
-       if (well_is_it==-1)
-               well_is_it= (strcmp((char*) glGetString(GL_VENDOR), "Intel Inc.") == 0);
-
-       return well_is_it;
-}
-
 void bglFlush(void) 
 {
        glFlush();
 #ifdef __APPLE__
-//     if(is_a_really_crappy_intel_card())
+//     if(GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_MAC, GPU_DRIVER_OFFICIAL))
 // XXX         myswapbuffers(); //hack to get mac intel graphics to show frontbuffer
 #endif
 }
 
-