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