2.5/Texture paint
[blender.git] / source / blender / gpu / intern / gpu_draw.c
1 /**
2  * $Id$
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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2005 Blender Foundation.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): Brecht Van Lommel.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34
35 #include "GL/glew.h"
36
37 #include "DNA_image_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_node_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_userdef_types.h"
45 #include "DNA_view3d_types.h"
46
47 #include "MEM_guardedalloc.h"
48
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "BKE_bmfont.h"
53 #include "BKE_global.h"
54 #include "BKE_image.h"
55 #include "BKE_main.h"
56 #include "BKE_material.h"
57 #include "BKE_node.h"
58 #include "BKE_object.h"
59 #include "BKE_utildefines.h"
60
61 #include "GPU_extensions.h"
62 #include "GPU_material.h"
63 #include "GPU_draw.h"
64
65 /* These are some obscure rendering functions shared between the
66  * game engine and the blender, in this module to avoid duplicaten
67  * and abstract them away from the rest a bit */
68
69 /* Text Rendering */
70
71 static void gpu_mcol(unsigned int ucol)
72 {
73         /* mcol order is swapped */
74         char *cp= (char *)&ucol;
75         glColor3ub(cp[3], cp[2], cp[1]);
76 }
77
78 void GPU_render_text(MTFace *tface, int mode,
79         const char *textstr, int textlen, unsigned int *col,
80         float *v1, float *v2, float *v3, float *v4, int glattrib)
81 {
82         if ((mode & TF_BMFONT) && (textlen>0) && tface->tpage) {
83                 Image* ima = (Image*)tface->tpage;
84                 int index, character;
85                 float centerx, centery, sizex, sizey, transx, transy, movex, movey, advance;
86                 float advance_tab;
87                 
88                 /* multiline */
89                 float line_start= 0.0f, line_height;
90                 
91                 if (v4)
92                         line_height= MAX4(v1[1], v2[1], v3[1], v4[2]) - MIN4(v1[1], v2[1], v3[1], v4[2]);
93                 else
94                         line_height= MAX3(v1[1], v2[1], v3[1]) - MIN3(v1[1], v2[1], v3[1]);
95                 line_height *= 1.2; /* could be an option? */
96                 /* end multiline */
97
98                 
99                 /* color has been set */
100                 if (tface->mode & TF_OBCOL)
101                         col= NULL;
102                 else if (!col)
103                         glColor3f(1.0f, 1.0f, 1.0f);
104
105                 glPushMatrix();
106                 
107                 /* get the tab width */
108                 matrixGlyph((ImBuf *)ima->ibufs.first, ' ', & centerx, &centery,
109                         &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
110                 
111                 advance_tab= advance * 4; /* tab width could also be an option */
112                 
113                 
114                 for (index = 0; index < textlen; index++) {
115                         float uv[4][2];
116
117                         // lets calculate offset stuff
118                         character = textstr[index];
119                         
120                         if (character=='\n') {
121                                 glTranslatef(line_start, -line_height, 0.0);
122                                 line_start = 0.0f;
123                                 continue;
124                         }
125                         else if (character=='\t') {
126                                 glTranslatef(advance_tab, 0.0, 0.0);
127                                 line_start -= advance_tab; /* so we can go back to the start of the line */
128                                 continue;
129                                 
130                         }
131                         
132                         // space starts at offset 1
133                         // character = character - ' ' + 1;
134                         matrixGlyph((ImBuf *)ima->ibufs.first, character, & centerx, &centery,
135                                 &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
136
137                         uv[0][0] = (tface->uv[0][0] - centerx) * sizex + transx;
138                         uv[0][1] = (tface->uv[0][1] - centery) * sizey + transy;
139                         uv[1][0] = (tface->uv[1][0] - centerx) * sizex + transx;
140                         uv[1][1] = (tface->uv[1][1] - centery) * sizey + transy;
141                         uv[2][0] = (tface->uv[2][0] - centerx) * sizex + transx;
142                         uv[2][1] = (tface->uv[2][1] - centery) * sizey + transy;
143                         
144                         glBegin(GL_POLYGON);
145                         if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[0]);
146                         else glTexCoord2fv(uv[0]);
147                         if(col) gpu_mcol(col[0]);
148                         glVertex3f(sizex * v1[0] + movex, sizey * v1[1] + movey, v1[2]);
149                         
150                         if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[1]);
151                         else glTexCoord2fv(uv[1]);
152                         if(col) gpu_mcol(col[1]);
153                         glVertex3f(sizex * v2[0] + movex, sizey * v2[1] + movey, v2[2]);
154
155                         if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[2]);
156                         else glTexCoord2fv(uv[2]);
157                         if(col) gpu_mcol(col[2]);
158                         glVertex3f(sizex * v3[0] + movex, sizey * v3[1] + movey, v3[2]);
159
160                         if(v4) {
161                                 uv[3][0] = (tface->uv[3][0] - centerx) * sizex + transx;
162                                 uv[3][1] = (tface->uv[3][1] - centery) * sizey + transy;
163
164                                 if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[3]);
165                                 else glTexCoord2fv(uv[3]);
166                                 if(col) gpu_mcol(col[3]);
167                                 glVertex3f(sizex * v4[0] + movex, sizey * v4[1] + movey, v4[2]);
168                         }
169                         glEnd();
170
171                         glTranslatef(advance, 0.0, 0.0);
172                         line_start -= advance; /* so we can go back to the start of the line */
173                 }
174                 glPopMatrix();
175         }
176 }
177
178 /* Checking powers of two for images since opengl 1.x requires it */
179
180 static int is_pow2(int num)
181 {
182         /* (n&(n-1)) zeros the least significant bit of n */
183         return ((num)&(num-1))==0;
184 }
185
186 static int smaller_pow2(int num)
187 {
188         while (!is_pow2(num))
189                 num= num&(num-1);
190
191         return num;     
192 }
193
194 static int is_pow2_limit(int num)
195 {
196         /* take texture clamping into account */
197
198         /* XXX: texturepaint not global!
199            if (G.f & G_TEXTUREPAINT)
200            return 1;*/
201
202         if (U.glreslimit != 0 && num > U.glreslimit)
203                 return 0;
204
205         return ((num)&(num-1))==0;
206 }
207
208 static int smaller_pow2_limit(int num)
209 {
210         /* XXX: texturepaint not global!
211            if (G.f & G_TEXTUREPAINT)
212            return 1;*/
213         
214         /* take texture clamping into account */
215         if (U.glreslimit != 0 && num > U.glreslimit)
216                 return U.glreslimit;
217
218         return smaller_pow2(num);
219 }
220
221 /* Current OpenGL state caching for GPU_set_tpage */
222
223 static struct GPUTextureState {
224         int curtile, tile;
225         int curtilemode, tilemode;
226         int curtileXRep, tileXRep;
227         int curtileYRep, tileYRep;
228         Image *ima, *curima;
229
230         int domipmap, linearmipmap;
231
232         int alphamode;
233         MTFace *lasttface;
234 } GTS = {0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, 1, 0, -1, NULL};
235
236 /* Mipmap settings */
237
238 void GPU_set_mipmap(int mipmap)
239 {
240         if (GTS.domipmap != (mipmap != 0)) {
241                 GPU_free_images();
242                 GTS.domipmap = mipmap != 0;
243         }
244 }
245
246 void GPU_set_linear_mipmap(int linear)
247 {
248         if (GTS.linearmipmap != (linear != 0)) {
249                 GPU_free_images();
250                 GTS.linearmipmap = linear != 0;
251         }
252 }
253
254 static int gpu_get_mipmap(void)
255 {
256         return GTS.domipmap 
257                 /* XXX: texturepaint not global!
258                    && (!(G.f & G_TEXTUREPAINT))*/;
259 }
260
261 static GLenum gpu_get_mipmap_filter(int mag)
262 {
263         /* linearmipmap is off by default *when mipmapping is off,
264          * use unfiltered display */
265         if(mag) {
266                 if(GTS.linearmipmap || GTS.domipmap)
267                         return GL_LINEAR;
268                 else
269                         return GL_NEAREST;
270         }
271         else {
272                 if(GTS.linearmipmap)
273                         return GL_LINEAR_MIPMAP_LINEAR;
274                 else if(GTS.domipmap)
275                         return GL_LINEAR_MIPMAP_NEAREST;
276                 else
277                         return GL_NEAREST;
278         }
279 }
280
281 /* Set OpenGL state for an MTFace */
282
283 static void gpu_make_repbind(Image *ima)
284 {
285         ImBuf *ibuf;
286         
287         ibuf = BKE_image_get_ibuf(ima, NULL);
288         if(ibuf==NULL)
289                 return;
290
291         if(ima->repbind) {
292                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
293                 MEM_freeN(ima->repbind);
294                 ima->repbind= 0;
295                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
296         }
297
298         ima->totbind= ima->xrep*ima->yrep;
299
300         if(ima->totbind>1)
301                 ima->repbind= MEM_callocN(sizeof(int)*ima->totbind, "repbind");
302 }
303
304 static void gpu_clear_tpage()
305 {
306         if(GTS.lasttface==0)
307                 return;
308         
309         GTS.lasttface= 0;
310         GTS.curtile= 0;
311         GTS.curima= 0;
312         if(GTS.curtilemode!=0) {
313                 glMatrixMode(GL_TEXTURE);
314                 glLoadIdentity();
315                 glMatrixMode(GL_MODELVIEW);
316         }
317         GTS.curtilemode= 0;
318         GTS.curtileXRep=0;
319         GTS.curtileYRep=0;
320         GTS.alphamode= -1;
321         
322         glDisable(GL_BLEND);
323         glDisable(GL_TEXTURE_2D);
324         glDisable(GL_TEXTURE_GEN_S);
325         glDisable(GL_TEXTURE_GEN_T);
326         glDisable(GL_ALPHA_TEST);
327 }
328
329 static void gpu_set_blend_mode(GPUBlendMode blendmode)
330 {
331         if(blendmode == GPU_BLEND_SOLID) {
332                 glDisable(GL_BLEND);
333                 glDisable(GL_ALPHA_TEST);
334                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
335         }
336         else if(blendmode==GPU_BLEND_ADD) {
337                 glEnable(GL_BLEND);
338                 glBlendFunc(GL_ONE, GL_ONE);
339                 glDisable(GL_ALPHA_TEST);
340         }
341         else if(blendmode==GPU_BLEND_ALPHA) {
342                 glEnable(GL_BLEND);
343                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
344                 
345                 /* if U.glalphaclip == 1.0, some cards go bonkers...
346                  * turn off alpha test in this case */
347
348                 /* added after 2.45 to clip alpha */
349                 if(U.glalphaclip == 1.0) {
350                         glDisable(GL_ALPHA_TEST);
351                 }
352                 else {
353                         glEnable(GL_ALPHA_TEST);
354                         glAlphaFunc(GL_GREATER, U.glalphaclip);
355                 }
356         }
357         else if(blendmode==GPU_BLEND_CLIP) {
358                 glDisable(GL_BLEND); 
359                 glEnable(GL_ALPHA_TEST);
360                 glAlphaFunc(GL_GREATER, 0.5f);
361         }
362 }
363
364 static void gpu_verify_alpha_mode(MTFace *tface)
365 {
366         /* verify alpha blending modes */
367         if(GTS.alphamode == tface->transp)
368                 return;
369
370         gpu_set_blend_mode(tface->transp);
371         GTS.alphamode= tface->transp;
372 }
373
374 static void gpu_verify_reflection(Image *ima)
375 {
376         if (ima && (ima->flag & IMA_REFLECT)) {
377                 /* enable reflection mapping */
378                 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
379                 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
380
381                 glEnable(GL_TEXTURE_GEN_S);
382                 glEnable(GL_TEXTURE_GEN_T);
383         }
384         else {
385                 /* disable reflection mapping */
386                 glDisable(GL_TEXTURE_GEN_S);
387                 glDisable(GL_TEXTURE_GEN_T);
388         }
389 }
390
391 int GPU_verify_image(Image *ima, int tftile, int tfmode, int compare)
392 {
393         ImBuf *ibuf = NULL;
394         unsigned int *bind = NULL;
395         int rectw, recth, tpx=0, tpy=0, y;
396         unsigned int *rectrow, *tilerectrow;
397         unsigned int *tilerect= NULL, *scalerect= NULL, *rect= NULL;
398         short texwindx, texwindy, texwinsx, texwinsy;
399
400         /* initialize tile mode and number of repeats */
401         GTS.ima = ima;
402         GTS.tilemode= (ima && (ima->tpageflag & (IMA_TILES|IMA_TWINANIM)));
403         GTS.tileXRep = 0;
404         GTS.tileYRep = 0;
405
406         /* setting current tile according to frame */
407         if(ima && (ima->tpageflag & IMA_TWINANIM))
408                 GTS.tile= ima->lastframe;
409         else
410                 GTS.tile= tftile;
411
412         GTS.tile = MAX2(0, GTS.tile);
413
414         if(ima) {
415                 GTS.tileXRep = ima->xrep;
416                 GTS.tileYRep = ima->yrep;
417         }
418
419         /* if same image & tile, we're done */
420         if(compare && ima == GTS.curima && GTS.curtile == GTS.tile &&
421            GTS.tilemode == GTS.curtilemode && GTS.curtileXRep == GTS.tileXRep &&
422            GTS.curtileYRep == GTS.tileYRep)
423                 return (ima!=0);
424
425         /* if tiling mode or repeat changed, change texture matrix to fit */
426         if(GTS.tilemode!=GTS.curtilemode || GTS.curtileXRep!=GTS.tileXRep ||
427            GTS.curtileYRep != GTS.tileYRep) {
428
429                 glMatrixMode(GL_TEXTURE);
430                 glLoadIdentity();
431
432                 if(ima && (ima->tpageflag & IMA_TILES))
433                         glScalef(ima->xrep, ima->yrep, 1.0);
434
435                 glMatrixMode(GL_MODELVIEW);
436         }
437
438         /* check if we have a valid image */
439         if(ima==NULL || ima->ok==0)
440                 return 0;
441
442         /* check if we have a valid image buffer */
443         ibuf= BKE_image_get_ibuf(ima, NULL);
444
445         if(ibuf==NULL)
446                 return 0;
447
448         /* ensure we have a char buffer and not only float */
449         if ((ibuf->rect==NULL) && ibuf->rect_float)
450                 IMB_rect_from_float(ibuf);
451
452         if(GTS.tilemode) {
453                 /* tiled mode */
454                 if(ima->repbind==0) gpu_make_repbind(ima);
455                 if(GTS.tile>=ima->totbind) GTS.tile= 0;
456                 
457                 /* this happens when you change repeat buttons */
458                 if(ima->repbind) bind= &ima->repbind[GTS.tile];
459                 else bind= &ima->bindcode;
460                 
461                 if(*bind==0) {
462                         
463                         texwindx= ibuf->x/ima->xrep;
464                         texwindy= ibuf->y/ima->yrep;
465                         
466                         if(GTS.tile>=ima->xrep*ima->yrep)
467                                 GTS.tile= ima->xrep*ima->yrep-1;
468         
469                         texwinsy= GTS.tile / ima->xrep;
470                         texwinsx= GTS.tile - texwinsy*ima->xrep;
471         
472                         texwinsx*= texwindx;
473                         texwinsy*= texwindy;
474         
475                         tpx= texwindx;
476                         tpy= texwindy;
477
478                         rect= ibuf->rect + texwinsy*ibuf->x + texwinsx;
479                 }
480         }
481         else {
482                 /* regular image mode */
483                 bind= &ima->bindcode;
484                 
485                 if(*bind==0) {
486                         tpx= ibuf->x;
487                         tpy= ibuf->y;
488                         rect= ibuf->rect;
489                 }
490         }
491
492         if(*bind != 0) {
493                 /* enable opengl drawing with textures */
494                 glBindTexture(GL_TEXTURE_2D, *bind);
495                 return *bind;
496         }
497
498         rectw = tpx;
499         recth = tpy;
500
501         /* for tiles, copy only part of image into buffer */
502         if (GTS.tilemode) {
503                 tilerect= MEM_mallocN(rectw*recth*sizeof(*tilerect), "tilerect");
504
505                 for (y=0; y<recth; y++) {
506                         rectrow= &rect[y*ibuf->x];
507                         tilerectrow= &tilerect[y*rectw];
508                                 
509                         memcpy(tilerectrow, rectrow, tpx*sizeof(*rectrow));
510                 }
511                         
512                 rect= tilerect;
513         }
514
515         /* scale if not a power of two */
516         if (!is_pow2_limit(rectw) || !is_pow2_limit(recth)) {
517                 rectw= smaller_pow2_limit(rectw);
518                 recth= smaller_pow2_limit(recth);
519                 
520                 scalerect= MEM_mallocN(rectw*recth*sizeof(*scalerect), "scalerect");
521                 gluScaleImage(GL_RGBA, tpx, tpy, GL_UNSIGNED_BYTE, rect, rectw, recth, GL_UNSIGNED_BYTE, scalerect);
522                 rect= scalerect;
523         }
524
525         /* create image */
526         glGenTextures(1, (GLuint *)bind);
527         glBindTexture( GL_TEXTURE_2D, *bind);
528
529         if (!gpu_get_mipmap()) {
530                 glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA,  rectw, recth, 0, GL_RGBA, GL_UNSIGNED_BYTE, rect);
531                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
532                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
533         }
534         else {
535                 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, rectw, recth, GL_RGBA, GL_UNSIGNED_BYTE, rect);
536                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
537                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
538
539                 ima->tpageflag |= IMA_MIPMAP_COMPLETE;
540         }
541
542         /* set to modulate with vertex color */
543         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
544                 
545         /* clean up */
546         if (tilerect)
547                 MEM_freeN(tilerect);
548         if (scalerect)
549                 MEM_freeN(scalerect);
550
551         return *bind;
552 }
553
554 static void gpu_verify_repeat(Image *ima)
555 {
556         /* set either clamp or repeat in X/Y */
557         if (ima->tpageflag & IMA_CLAMP_U)
558            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
559         else
560                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
561
562         if (ima->tpageflag & IMA_CLAMP_V)
563                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
564         else
565                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
566 }
567
568 int GPU_set_tpage(MTFace *tface)
569 {
570         Image *ima;
571         
572         /* check if we need to clear the state */
573         if(tface==0) {
574                 gpu_clear_tpage();
575                 return 0;
576         }
577
578         ima= tface->tpage;
579         GTS.lasttface= tface;
580
581         gpu_verify_alpha_mode(tface);
582         gpu_verify_reflection(ima);
583
584         if(GPU_verify_image(ima, tface->tile, tface->mode, 1)) {
585                 GTS.curtile= GTS.tile;
586                 GTS.curima= GTS.ima;
587                 GTS.curtilemode= GTS.tilemode;
588                 GTS.curtileXRep = GTS.tileXRep;
589                 GTS.curtileYRep = GTS.tileYRep;
590
591                 glEnable(GL_TEXTURE_2D);
592         }
593         else {
594                 glDisable(GL_TEXTURE_2D);
595                 
596                 GTS.curtile= 0;
597                 GTS.curima= 0;
598                 GTS.curtilemode= 0;
599                 GTS.curtileXRep = 0;
600                 GTS.curtileYRep = 0;
601
602                 return 0;
603         }
604         
605         gpu_verify_repeat(ima);
606         
607         /* Did this get lost in the image recode? */
608         /* tag_image_time(ima);*/
609
610         return 1;
611 }
612
613 /* these two functions are called on entering and exiting texture paint mode,
614    temporary disabling/enabling mipmapping on all images for quick texture
615    updates with glTexSubImage2D. images that didn't change don't have to be
616    re-uploaded to OpenGL */
617 void GPU_paint_set_mipmap(int mipmap)
618 {
619         Image* ima;
620         
621         if(!GTS.domipmap)
622                 return;
623
624         if(mipmap) {
625                 for(ima=G.main->image.first; ima; ima=ima->id.next) {
626                         if(ima->bindcode) {
627                                 if(ima->tpageflag & IMA_MIPMAP_COMPLETE) {
628                                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
629                                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
630                                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
631                                 }
632                                 else
633                                         GPU_free_image(ima);
634                         }
635                 }
636
637         }
638         else {
639                 for(ima=G.main->image.first; ima; ima=ima->id.next) {
640                         if(ima->bindcode) {
641                                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
642                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
643                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
644                         }
645                 }
646         }
647 }
648
649 void GPU_paint_update_image(Image *ima, int x, int y, int w, int h)
650 {
651         ImBuf *ibuf;
652         
653         ibuf = BKE_image_get_ibuf(ima, NULL);
654         
655         if (ima->repbind || gpu_get_mipmap() || !ima->bindcode || !ibuf ||
656                 (!is_pow2(ibuf->x) || !is_pow2(ibuf->y)) ||
657                 (w == 0) || (h == 0)) {
658                 /* these cases require full reload still */
659                 GPU_free_image(ima);
660         }
661         else {
662                 /* for the special case, we can do a partial update
663                  * which is much quicker for painting */
664                 GLint row_length, skip_pixels, skip_rows;
665
666                 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
667                 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
668                 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
669
670                 if ((ibuf->rect==NULL) && ibuf->rect_float)
671                         IMB_rect_from_float(ibuf);
672
673                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
674
675                 glPixelStorei(GL_UNPACK_ROW_LENGTH, ibuf->x);
676                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
677                 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
678
679                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
680                         GL_UNSIGNED_BYTE, ibuf->rect);
681
682                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
683                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
684                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
685
686                 if(ima->tpageflag & IMA_MIPMAP_COMPLETE)
687                         ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
688         }
689 }
690
691 void GPU_update_images_framechange(void)
692 {
693         Image *ima;
694         
695         for(ima=G.main->image.first; ima; ima=ima->id.next) {
696                 if(ima->tpageflag & IMA_TWINANIM) {
697                         if(ima->twend >= ima->xrep*ima->yrep)
698                                 ima->twend= ima->xrep*ima->yrep-1;
699                 
700                         /* check: is bindcode not in the array? free. (to do) */
701                         
702                         ima->lastframe++;
703                         if(ima->lastframe > ima->twend)
704                                 ima->lastframe= ima->twsta;
705                 }
706         }
707 }
708
709 int GPU_update_image_time(Image *ima, double time)
710 {
711         int     inc = 0;
712         float   diff;
713         int     newframe;
714
715         if (!ima)
716                 return 0;
717
718         if (ima->lastupdate<0)
719                 ima->lastupdate = 0;
720
721         if (ima->lastupdate>time)
722                 ima->lastupdate=(float)time;
723
724         if(ima->tpageflag & IMA_TWINANIM) {
725                 if(ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1;
726                 
727                 /* check: is the bindcode not in the array? Then free. (still to do) */
728                 
729                 diff = (float)(time-ima->lastupdate);
730                 inc = (int)(diff*(float)ima->animspeed);
731
732                 ima->lastupdate+=((float)inc/(float)ima->animspeed);
733
734                 newframe = ima->lastframe+inc;
735
736                 if(newframe > (int)ima->twend) {
737                         if(ima->twend-ima->twsta != 0)
738                                 newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta);
739                         else
740                                 newframe = ima->twsta;
741                 }
742
743                 ima->lastframe = newframe;
744         }
745
746         return inc;
747 }
748
749 void GPU_free_image(Image *ima)
750 {
751         /* free regular image binding */
752         if(ima->bindcode) {
753                 glDeleteTextures(1, (GLuint *)&ima->bindcode);
754                 ima->bindcode= 0;
755                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
756         }
757
758         /* free glsl image binding */
759         if(ima->gputexture) {
760                 GPU_texture_free(ima->gputexture);
761                 ima->gputexture= NULL;
762         }
763
764         /* free repeated image binding */
765         if(ima->repbind) {
766                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
767         
768                 MEM_freeN(ima->repbind);
769                 ima->repbind= NULL;
770                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
771         }
772 }
773
774 void GPU_free_images(void)
775 {
776         Image* ima;
777
778         if(G.main)
779                 for(ima=G.main->image.first; ima; ima=ima->id.next)
780                         GPU_free_image(ima);
781 }
782
783 /* OpenGL Materials */
784
785 #define FIXEDMAT        8
786
787 /* OpenGL state caching for materials */
788
789 static struct GPUMaterialState {
790         float (*matbuf)[2][4];
791         float matbuf_fixed[FIXEDMAT][2][4];
792         int totmat;
793
794         Material **gmatbuf;
795         Material *gmatbuf_fixed[FIXEDMAT];
796         Material *gboundmat;
797         Object *gob;
798         Scene *gscene;
799         int glay;
800         float (*gviewmat)[4];
801         float (*gviewinv)[4];
802
803         GPUBlendMode *blendmode;
804         GPUBlendMode blendmode_fixed[FIXEDMAT];
805         int alphapass;
806
807         int lastmatnr, lastretval;
808         GPUBlendMode lastblendmode;
809 } GMS = {NULL};
810
811 Material *gpu_active_node_material(Material *ma)
812 {
813         if(ma && ma->use_nodes && ma->nodetree) {
814                 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
815
816                 if(node)
817                         return (Material *)node->id;
818                 else
819                         return NULL;
820         }
821
822         return ma;
823 }
824
825 void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, Object *ob, int glsl, int *do_alpha_pass)
826 {
827         extern Material defmaterial; /* from material.c */
828         Material *ma;
829         GPUMaterial *gpumat;
830         GPUBlendMode blendmode;
831         int a;
832         
833         /* initialize state */
834         memset(&GMS, 0, sizeof(GMS));
835         GMS.lastmatnr = -1;
836         GMS.lastretval = -1;
837         GMS.lastblendmode = GPU_BLEND_SOLID;
838
839         GMS.gob = ob;
840         GMS.gscene = scene;
841         GMS.totmat= ob->totcol+1; /* materials start from 1, default material is 0 */
842         GMS.glay= v3d->lay;
843         GMS.gviewmat= rv3d->viewmat;
844         GMS.gviewinv= rv3d->viewinv;
845
846         GMS.alphapass = (v3d && v3d->transp);
847         if(do_alpha_pass)
848                 *do_alpha_pass = 0;
849         
850         if(GMS.totmat > FIXEDMAT) {
851                 GMS.matbuf= MEM_callocN(sizeof(*GMS.matbuf)*GMS.totmat, "GMS.matbuf");
852                 GMS.gmatbuf= MEM_callocN(sizeof(*GMS.gmatbuf)*GMS.totmat, "GMS.matbuf");
853                 GMS.blendmode= MEM_callocN(sizeof(*GMS.blendmode)*GMS.totmat, "GMS.matbuf");
854         }
855         else {
856                 GMS.matbuf= GMS.matbuf_fixed;
857                 GMS.gmatbuf= GMS.gmatbuf_fixed;
858                 GMS.blendmode= GMS.blendmode_fixed;
859         }
860
861         /* no materials assigned? */
862         if(ob->totcol==0) {
863                 GMS.matbuf[0][0][0]= (defmaterial.ref+defmaterial.emit)*defmaterial.r;
864                 GMS.matbuf[0][0][1]= (defmaterial.ref+defmaterial.emit)*defmaterial.g;
865                 GMS.matbuf[0][0][2]= (defmaterial.ref+defmaterial.emit)*defmaterial.b;
866                 GMS.matbuf[0][0][3]= 1.0;
867
868                 GMS.matbuf[0][1][0]= defmaterial.spec*defmaterial.specr;
869                 GMS.matbuf[0][1][1]= defmaterial.spec*defmaterial.specg;
870                 GMS.matbuf[0][1][2]= defmaterial.spec*defmaterial.specb;
871                 GMS.matbuf[0][1][3]= 1.0;
872                 
873                 /* do material 1 too, for displists! */
874                 QUATCOPY(GMS.matbuf[1][0], GMS.matbuf[0][0]);
875                 QUATCOPY(GMS.matbuf[1][1], GMS.matbuf[0][1]);
876
877                 if(glsl) {
878                         GMS.gmatbuf[0]= &defmaterial;
879                         GPU_material_from_blender(GMS.gscene, &defmaterial);
880                 }
881
882                 GMS.blendmode[0]= GPU_BLEND_SOLID;
883         }
884         
885         /* setup materials */
886         for(a=1; a<=ob->totcol; a++) {
887                 /* find a suitable material */
888                 ma= give_current_material(ob, a);
889                 if(!glsl) ma= gpu_active_node_material(ma);
890                 if(ma==NULL) ma= &defmaterial;
891
892                 /* create glsl material if requested */
893                 gpumat = (glsl)? GPU_material_from_blender(GMS.gscene, ma): NULL;
894
895                 if(gpumat) {
896                         /* do glsl only if creating it succeed, else fallback */
897                         GMS.gmatbuf[a]= ma;
898                         blendmode = GPU_material_blend_mode(gpumat, ob->col);
899                 }
900                 else {
901                         /* fixed function opengl materials */
902                         if (ma->mode & MA_SHLESS) {
903                                 GMS.matbuf[a][0][0]= ma->r;
904                                 GMS.matbuf[a][0][1]= ma->g;
905                                 GMS.matbuf[a][0][2]= ma->b;
906                         } else {
907                                 GMS.matbuf[a][0][0]= (ma->ref+ma->emit)*ma->r;
908                                 GMS.matbuf[a][0][1]= (ma->ref+ma->emit)*ma->g;
909                                 GMS.matbuf[a][0][2]= (ma->ref+ma->emit)*ma->b;
910
911                                 GMS.matbuf[a][1][0]= ma->spec*ma->specr;
912                                 GMS.matbuf[a][1][1]= ma->spec*ma->specg;
913                                 GMS.matbuf[a][1][2]= ma->spec*ma->specb;
914                                 GMS.matbuf[a][1][3]= 1.0;
915                         }
916
917                         blendmode = (ma->alpha == 1.0f)? GPU_BLEND_SOLID: GPU_BLEND_ALPHA;
918                         if(do_alpha_pass && GMS.alphapass)
919                                 GMS.matbuf[a][0][3]= ma->alpha;
920                         else
921                                 GMS.matbuf[a][0][3]= 1.0f;
922                 }
923
924                 /* setting do_alpha_pass = 1 indicates this object needs to be
925                  * drawn in a second alpha pass for improved blending */
926                 if(do_alpha_pass) {
927                         GMS.blendmode[a]= blendmode;
928                         if(ELEM(blendmode, GPU_BLEND_ALPHA, GPU_BLEND_ADD) && !GMS.alphapass)
929                                 *do_alpha_pass= 1;
930                 }
931         }
932
933         /* let's start with a clean state */
934         GPU_disable_material();
935 }
936
937 int GPU_enable_material(int nr, void *attribs)
938 {
939         extern Material defmaterial; /* from material.c */
940         GPUVertexAttribs *gattribs = attribs;
941         GPUMaterial *gpumat;
942         GPUBlendMode blendmode;
943
944         /* no GPU_begin_object_materials, use default material */
945         if(!GMS.matbuf) {
946                 float diff[4], spec[4];
947
948                 memset(&GMS, 0, sizeof(GMS));
949
950                 diff[0]= (defmaterial.ref+defmaterial.emit)*defmaterial.r;
951                 diff[1]= (defmaterial.ref+defmaterial.emit)*defmaterial.g;
952                 diff[2]= (defmaterial.ref+defmaterial.emit)*defmaterial.b;
953                 diff[3]= 1.0;
954
955                 spec[0]= defmaterial.spec*defmaterial.specr;
956                 spec[1]= defmaterial.spec*defmaterial.specg;
957                 spec[2]= defmaterial.spec*defmaterial.specb;
958                 spec[3]= 1.0;
959
960                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
961                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
962
963                 return 0;
964         }
965
966         /* prevent index to use un-initialized array items */
967         if(nr>=GMS.totmat)
968                 nr= 0;
969
970         if(gattribs)
971                 memset(gattribs, 0, sizeof(*gattribs));
972
973         /* keep current material */
974         if(nr==GMS.lastmatnr)
975                 return GMS.lastretval;
976
977         /* unbind glsl material */
978         if(GMS.gboundmat) {
979                 if(GMS.alphapass) glDepthMask(0);
980                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
981                 GMS.gboundmat= NULL;
982         }
983
984         /* draw materials with alpha in alpha pass */
985         GMS.lastmatnr = nr;
986         GMS.lastretval = ELEM(GMS.blendmode[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP);
987         if(GMS.alphapass)
988                 GMS.lastretval = !GMS.lastretval;
989
990         if(GMS.lastretval) {
991                 if(gattribs && GMS.gmatbuf[nr]) {
992                         /* bind glsl material and get attributes */
993                         Material *mat = GMS.gmatbuf[nr];
994
995                         gpumat = GPU_material_from_blender(GMS.gscene, mat);
996                         GPU_material_vertex_attributes(gpumat, gattribs);
997                         GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0);
998                         GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gviewmat, GMS.gviewinv, GMS.gob->col);
999                         GMS.gboundmat= mat;
1000
1001                         if(GMS.alphapass) glDepthMask(1);
1002                 }
1003                 else {
1004                         /* or do fixed function opengl material */
1005                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GMS.matbuf[nr][0]);
1006                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, GMS.matbuf[nr][1]);
1007                 }
1008
1009                 /* set (alpha) blending mode */
1010                 blendmode = (GMS.alphapass)? GPU_BLEND_ALPHA: GPU_BLEND_SOLID;
1011                 GPU_set_material_blend_mode(blendmode);
1012         }
1013
1014         return GMS.lastretval;
1015 }
1016
1017 void GPU_set_material_blend_mode(int blendmode)
1018 {
1019         if(GMS.lastblendmode == blendmode)
1020                 return;
1021         
1022         gpu_set_blend_mode(blendmode);
1023         GMS.lastblendmode = blendmode;
1024 }
1025
1026 int GPU_get_material_blend_mode(void)
1027 {
1028         return GMS.lastblendmode;
1029 }
1030
1031 void GPU_disable_material(void)
1032 {
1033         GMS.lastmatnr= -1;
1034         GMS.lastretval= 1;
1035
1036         if(GMS.gboundmat) {
1037                 if(GMS.alphapass) glDepthMask(0);
1038                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1039                 GMS.gboundmat= NULL;
1040         }
1041
1042         GPU_set_material_blend_mode(GPU_BLEND_SOLID);
1043 }
1044
1045 void GPU_end_object_materials(void)
1046 {
1047         GPU_disable_material();
1048
1049         if(GMS.matbuf && GMS.matbuf != GMS.matbuf_fixed) {
1050                 MEM_freeN(GMS.matbuf);
1051                 MEM_freeN(GMS.gmatbuf);
1052                 MEM_freeN(GMS.blendmode);
1053         }
1054
1055         GMS.matbuf= NULL;
1056         GMS.gmatbuf= NULL;
1057         GMS.blendmode= NULL;
1058 }
1059
1060 /* Lights */
1061
1062 int GPU_default_lights(void)
1063 {
1064         int a, count = 0;
1065         
1066         /* initialize */
1067         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
1068                 U.light[0].flag= 1;
1069                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
1070                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
1071                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
1072                 U.light[0].spec[3]= 1.0;
1073                 
1074                 U.light[1].flag= 0;
1075                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
1076                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
1077                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
1078                 U.light[1].spec[3]= 1.0;
1079         
1080                 U.light[2].flag= 0;
1081                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
1082                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
1083                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
1084                 U.light[2].spec[3]= 1.0;
1085         }
1086
1087         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
1088
1089         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
1090         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
1091         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
1092
1093         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
1094         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
1095         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
1096
1097         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
1098         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
1099         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
1100
1101         for(a=0; a<8; a++) {
1102                 if(a<3) {
1103                         if(U.light[a].flag) {
1104                                 glEnable(GL_LIGHT0+a);
1105                                 count++;
1106                         }
1107                         else
1108                                 glDisable(GL_LIGHT0+a);
1109                         
1110                         // clear stuff from other opengl lamp usage
1111                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
1112                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
1113                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
1114                 }
1115                 else
1116                         glDisable(GL_LIGHT0+a);
1117         }
1118         
1119         glDisable(GL_LIGHTING);
1120
1121         glDisable(GL_COLOR_MATERIAL);
1122
1123         return count;
1124 }
1125
1126 int GPU_scene_object_lights(Scene *scene, Object *ob, int lay, float viewmat[][4], int ortho)
1127 {
1128         Base *base;
1129         Lamp *la;
1130         int count;
1131         float position[4], direction[4], energy[4];
1132         
1133         /* disable all lights */
1134         for(count=0; count<8; count++)
1135                 glDisable(GL_LIGHT0+count);
1136         
1137         /* view direction for specular is not compute correct by default in
1138          * opengl, so we set the settings ourselfs */
1139         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (ortho)? GL_FALSE: GL_TRUE);
1140
1141         count= 0;
1142         
1143         for(base=scene->base.first; base; base=base->next) {
1144                 if(base->object->type!=OB_LAMP)
1145                         continue;
1146
1147                 if(!(base->lay & lay) || !(base->lay & ob->lay))
1148                         continue;
1149
1150                 la= base->object->data;
1151                 
1152                 /* setup lamp transform */
1153                 glPushMatrix();
1154                 glLoadMatrixf((float *)viewmat);
1155                 
1156                 where_is_object_simul(scene, base->object);
1157                 
1158                 if(la->type==LA_SUN) {
1159                         /* sun lamp */
1160                         VECCOPY(direction, base->object->obmat[2]);
1161                         direction[3]= 0.0;
1162
1163                         glLightfv(GL_LIGHT0+count, GL_POSITION, direction); 
1164                 }
1165                 else {
1166                         /* other lamps with attenuation */
1167                         VECCOPY(position, base->object->obmat[3]);
1168                         position[3]= 1.0f;
1169
1170                         glLightfv(GL_LIGHT0+count, GL_POSITION, position); 
1171                         glLightf(GL_LIGHT0+count, GL_CONSTANT_ATTENUATION, 1.0);
1172                         glLightf(GL_LIGHT0+count, GL_LINEAR_ATTENUATION, la->att1/la->dist);
1173                         glLightf(GL_LIGHT0+count, GL_QUADRATIC_ATTENUATION, la->att2/(la->dist*la->dist));
1174                         
1175                         if(la->type==LA_SPOT) {
1176                                 /* spot lamp */
1177                                 direction[0]= -base->object->obmat[2][0];
1178                                 direction[1]= -base->object->obmat[2][1];
1179                                 direction[2]= -base->object->obmat[2][2];
1180                                 glLightfv(GL_LIGHT0+count, GL_SPOT_DIRECTION, direction);
1181                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, la->spotsize/2.0);
1182                                 glLightf(GL_LIGHT0+count, GL_SPOT_EXPONENT, 128.0*la->spotblend);
1183                         }
1184                         else
1185                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, 180.0);
1186                 }
1187                 
1188                 /* setup energy */
1189                 energy[0]= la->energy*la->r;
1190                 energy[1]= la->energy*la->g;
1191                 energy[2]= la->energy*la->b;
1192                 energy[3]= 1.0;
1193
1194                 glLightfv(GL_LIGHT0+count, GL_DIFFUSE, energy); 
1195                 glLightfv(GL_LIGHT0+count, GL_SPECULAR, energy);
1196                 glEnable(GL_LIGHT0+count);
1197                 
1198                 glPopMatrix();                                  
1199                 
1200                 count++;
1201                 if(count==8)
1202                         break;
1203         }
1204
1205         return count;
1206 }
1207
1208 /* Default OpenGL State */
1209
1210 void GPU_state_init(void)
1211 {
1212         /* also called when doing opengl rendering and in the game engine */
1213         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
1214         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
1215         float mat_shininess[] = { 35.0 };
1216         int a, x, y;
1217         GLubyte pat[32*32];
1218         const GLubyte *patc= pat;
1219         
1220         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
1221         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
1222         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1223         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
1224
1225         GPU_default_lights();
1226         
1227         glDepthFunc(GL_LEQUAL);
1228         /* scaling matrices */
1229         glEnable(GL_NORMALIZE);
1230
1231         glShadeModel(GL_FLAT);
1232
1233         glDisable(GL_ALPHA_TEST);
1234         glDisable(GL_BLEND);
1235         glDisable(GL_DEPTH_TEST);
1236         glDisable(GL_FOG);
1237         glDisable(GL_LIGHTING);
1238         glDisable(GL_LOGIC_OP);
1239         glDisable(GL_STENCIL_TEST);
1240         glDisable(GL_TEXTURE_1D);
1241         glDisable(GL_TEXTURE_2D);
1242
1243         /* default on, disable/enable should be local per function */
1244         glEnableClientState(GL_VERTEX_ARRAY);
1245         glEnableClientState(GL_NORMAL_ARRAY);
1246         
1247         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
1248         glPixelTransferi(GL_RED_SCALE, 1);
1249         glPixelTransferi(GL_RED_BIAS, 0);
1250         glPixelTransferi(GL_GREEN_SCALE, 1);
1251         glPixelTransferi(GL_GREEN_BIAS, 0);
1252         glPixelTransferi(GL_BLUE_SCALE, 1);
1253         glPixelTransferi(GL_BLUE_BIAS, 0);
1254         glPixelTransferi(GL_ALPHA_SCALE, 1);
1255         glPixelTransferi(GL_ALPHA_BIAS, 0);
1256         
1257         glPixelTransferi(GL_DEPTH_BIAS, 0);
1258         glPixelTransferi(GL_DEPTH_SCALE, 1);
1259         glDepthRange(0.0, 1.0);
1260         
1261         a= 0;
1262         for(x=0; x<32; x++) {
1263                 for(y=0; y<4; y++) {
1264                         if( (x) & 1) pat[a++]= 0x88;
1265                         else pat[a++]= 0x22;
1266                 }
1267         }
1268         
1269         glPolygonStipple(patc);
1270
1271         glMatrixMode(GL_TEXTURE);
1272         glLoadIdentity();
1273         glMatrixMode(GL_MODELVIEW);
1274
1275         glFrontFace(GL_CCW);
1276         glCullFace(GL_BACK);
1277         glDisable(GL_CULL_FACE);
1278 }
1279