svn merge -r36583:36603 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/gpu/intern/gpu_draw.c
34  *  \ingroup gpu
35  */
36
37
38 #include <string.h>
39
40 #include "GL/glew.h"
41
42 #include "BLI_math.h"
43 #include "BLI_utildefines.h"
44
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_node_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_smoke_types.h"
53 #include "DNA_view3d_types.h"
54
55 #include "MEM_guardedalloc.h"
56
57 #include "IMB_imbuf.h"
58 #include "IMB_imbuf_types.h"
59
60 #include "BKE_bmfont.h"
61 #include "BKE_global.h"
62 #include "BKE_image.h"
63 #include "BKE_main.h"
64 #include "BKE_material.h"
65 #include "BKE_node.h"
66 #include "BKE_object.h"
67
68
69 #include "BLI_threads.h"
70 #include "BLI_blenlib.h"
71
72 #include "GPU_buffers.h"
73 #include "GPU_draw.h"
74 #include "GPU_extensions.h"
75 #include "GPU_material.h"
76
77 #include "smoke_API.h"
78
79 extern Material defmaterial; /* from material.c */
80
81 /* These are some obscure rendering functions shared between the
82  * game engine and the blender, in this module to avoid duplicaten
83  * and abstract them away from the rest a bit */
84
85 /* Text Rendering */
86
87 static void gpu_mcol(unsigned int ucol)
88 {
89         /* mcol order is swapped */
90         char *cp= (char *)&ucol;
91         glColor3ub(cp[3], cp[2], cp[1]);
92 }
93
94 void GPU_render_text(MTFace *tface, int mode,
95         const char *textstr, int textlen, unsigned int *col,
96         float *v1, float *v2, float *v3, float *v4, int glattrib)
97 {
98         if ((mode & TF_BMFONT) && (textlen>0) && tface->tpage) {
99                 Image* ima = (Image*)tface->tpage;
100                 int index, character;
101                 float centerx, centery, sizex, sizey, transx, transy, movex, movey, advance;
102                 float advance_tab;
103                 
104                 /* multiline */
105                 float line_start= 0.0f, line_height;
106                 
107                 if (v4)
108                         line_height= MAX4(v1[1], v2[1], v3[1], v4[2]) - MIN4(v1[1], v2[1], v3[1], v4[2]);
109                 else
110                         line_height= MAX3(v1[1], v2[1], v3[1]) - MIN3(v1[1], v2[1], v3[1]);
111                 line_height *= 1.2f; /* could be an option? */
112                 /* end multiline */
113
114                 
115                 /* color has been set */
116                 if (tface->mode & TF_OBCOL)
117                         col= NULL;
118                 else if (!col)
119                         glColor3f(1.0f, 1.0f, 1.0f);
120
121                 glPushMatrix();
122                 
123                 /* get the tab width */
124                 matrixGlyph((ImBuf *)ima->ibufs.first, ' ', & centerx, &centery,
125                         &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
126                 
127                 advance_tab= advance * 4; /* tab width could also be an option */
128                 
129                 
130                 for (index = 0; index < textlen; index++) {
131                         float uv[4][2];
132
133                         // lets calculate offset stuff
134                         character = textstr[index];
135                         
136                         if (character=='\n') {
137                                 glTranslatef(line_start, -line_height, 0.0);
138                                 line_start = 0.0f;
139                                 continue;
140                         }
141                         else if (character=='\t') {
142                                 glTranslatef(advance_tab, 0.0, 0.0);
143                                 line_start -= advance_tab; /* so we can go back to the start of the line */
144                                 continue;
145                                 
146                         }
147                         
148                         // space starts at offset 1
149                         // character = character - ' ' + 1;
150                         matrixGlyph((ImBuf *)ima->ibufs.first, character, & centerx, &centery,
151                                 &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
152
153                         uv[0][0] = (tface->uv[0][0] - centerx) * sizex + transx;
154                         uv[0][1] = (tface->uv[0][1] - centery) * sizey + transy;
155                         uv[1][0] = (tface->uv[1][0] - centerx) * sizex + transx;
156                         uv[1][1] = (tface->uv[1][1] - centery) * sizey + transy;
157                         uv[2][0] = (tface->uv[2][0] - centerx) * sizex + transx;
158                         uv[2][1] = (tface->uv[2][1] - centery) * sizey + transy;
159                         
160                         glBegin(GL_POLYGON);
161                         if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[0]);
162                         else glTexCoord2fv(uv[0]);
163                         if(col) gpu_mcol(col[0]);
164                         glVertex3f(sizex * v1[0] + movex, sizey * v1[1] + movey, v1[2]);
165                         
166                         if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[1]);
167                         else glTexCoord2fv(uv[1]);
168                         if(col) gpu_mcol(col[1]);
169                         glVertex3f(sizex * v2[0] + movex, sizey * v2[1] + movey, v2[2]);
170
171                         if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[2]);
172                         else glTexCoord2fv(uv[2]);
173                         if(col) gpu_mcol(col[2]);
174                         glVertex3f(sizex * v3[0] + movex, sizey * v3[1] + movey, v3[2]);
175
176                         if(v4) {
177                                 uv[3][0] = (tface->uv[3][0] - centerx) * sizex + transx;
178                                 uv[3][1] = (tface->uv[3][1] - centery) * sizey + transy;
179
180                                 if(glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[3]);
181                                 else glTexCoord2fv(uv[3]);
182                                 if(col) gpu_mcol(col[3]);
183                                 glVertex3f(sizex * v4[0] + movex, sizey * v4[1] + movey, v4[2]);
184                         }
185                         glEnd();
186
187                         glTranslatef(advance, 0.0, 0.0);
188                         line_start -= advance; /* so we can go back to the start of the line */
189                 }
190                 glPopMatrix();
191         }
192 }
193
194 /* Checking powers of two for images since opengl 1.x requires it */
195
196 static int is_pow2(int num)
197 {
198         /* (n&(n-1)) zeros the least significant bit of n */
199         return ((num)&(num-1))==0;
200 }
201
202 static int smaller_pow2(int num)
203 {
204         while (!is_pow2(num))
205                 num= num&(num-1);
206
207         return num;     
208 }
209
210 static int is_pow2_limit(int num)
211 {
212         /* take texture clamping into account */
213
214         /* XXX: texturepaint not global!
215         if (G.f & G_TEXTUREPAINT)
216                 return 1;*/
217
218         if (U.glreslimit != 0 && num > U.glreslimit)
219                 return 0;
220
221         return ((num)&(num-1))==0;
222 }
223
224 static int smaller_pow2_limit(int num)
225 {
226         /* XXX: texturepaint not global!
227         if (G.f & G_TEXTUREPAINT)
228                 return 1;*/
229         
230         /* take texture clamping into account */
231         if (U.glreslimit != 0 && num > U.glreslimit)
232                 return U.glreslimit;
233
234         return smaller_pow2(num);
235 }
236
237 /* Current OpenGL state caching for GPU_set_tpage */
238
239 static struct GPUTextureState {
240         int curtile, tile;
241         int curtilemode, tilemode;
242         int curtileXRep, tileXRep;
243         int curtileYRep, tileYRep;
244         Image *ima, *curima;
245
246         int domipmap, linearmipmap;
247
248         int alphamode;
249         MTFace *lasttface;
250 } GTS = {0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, 1, 0, -1, NULL};
251
252 /* Mipmap settings */
253
254 void GPU_set_mipmap(int mipmap)
255 {
256         if (GTS.domipmap != (mipmap != 0)) {
257                 GPU_free_images();
258                 GTS.domipmap = mipmap != 0;
259         }
260 }
261
262 void GPU_set_linear_mipmap(int linear)
263 {
264         if (GTS.linearmipmap != (linear != 0)) {
265                 GPU_free_images();
266                 GTS.linearmipmap = linear != 0;
267         }
268 }
269
270 static int gpu_get_mipmap(void)
271 {
272         return GTS.domipmap;
273 }
274
275 static GLenum gpu_get_mipmap_filter(int mag)
276 {
277         /* linearmipmap is off by default *when mipmapping is off,
278          * use unfiltered display */
279         if(mag) {
280                 if(GTS.linearmipmap || GTS.domipmap)
281                         return GL_LINEAR;
282                 else
283                         return GL_NEAREST;
284         }
285         else {
286                 if(GTS.linearmipmap)
287                         return GL_LINEAR_MIPMAP_LINEAR;
288                 else if(GTS.domipmap)
289                         return GL_LINEAR_MIPMAP_NEAREST;
290                 else
291                         return GL_NEAREST;
292         }
293 }
294
295 /* Set OpenGL state for an MTFace */
296
297 static void gpu_make_repbind(Image *ima)
298 {
299         ImBuf *ibuf;
300         
301         ibuf = BKE_image_get_ibuf(ima, NULL);
302         if(ibuf==NULL)
303                 return;
304
305         if(ima->repbind) {
306                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
307                 MEM_freeN(ima->repbind);
308                 ima->repbind= NULL;
309                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
310         }
311
312         ima->totbind= ima->xrep*ima->yrep;
313
314         if(ima->totbind>1)
315                 ima->repbind= MEM_callocN(sizeof(int)*ima->totbind, "repbind");
316 }
317
318 static void gpu_clear_tpage(void)
319 {
320         if(GTS.lasttface==NULL)
321                 return;
322         
323         GTS.lasttface= NULL;
324         GTS.curtile= 0;
325         GTS.curima= NULL;
326         if(GTS.curtilemode!=0) {
327                 glMatrixMode(GL_TEXTURE);
328                 glLoadIdentity();
329                 glMatrixMode(GL_MODELVIEW);
330         }
331         GTS.curtilemode= 0;
332         GTS.curtileXRep=0;
333         GTS.curtileYRep=0;
334         GTS.alphamode= -1;
335         
336         glDisable(GL_BLEND);
337         glDisable(GL_TEXTURE_2D);
338         glDisable(GL_TEXTURE_GEN_S);
339         glDisable(GL_TEXTURE_GEN_T);
340         glDisable(GL_ALPHA_TEST);
341 }
342
343 static void gpu_set_blend_mode(GPUBlendMode blendmode)
344 {
345         if(blendmode == GPU_BLEND_SOLID) {
346                 glDisable(GL_BLEND);
347                 glDisable(GL_ALPHA_TEST);
348                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
349         }
350         else if(blendmode==GPU_BLEND_ADD) {
351                 glEnable(GL_BLEND);
352                 glBlendFunc(GL_ONE, GL_ONE);
353                 glDisable(GL_ALPHA_TEST);
354         }
355         else if(blendmode==GPU_BLEND_ALPHA) {
356                 glEnable(GL_BLEND);
357                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
358                 
359                 /* if U.glalphaclip == 1.0, some cards go bonkers...
360                  * turn off alpha test in this case */
361
362                 /* added after 2.45 to clip alpha */
363                 if(U.glalphaclip == 1.0f) {
364                         glDisable(GL_ALPHA_TEST);
365                 }
366                 else {
367                         glEnable(GL_ALPHA_TEST);
368                         glAlphaFunc(GL_GREATER, U.glalphaclip);
369                 }
370         }
371         else if(blendmode==GPU_BLEND_CLIP) {
372                 glDisable(GL_BLEND); 
373                 glEnable(GL_ALPHA_TEST);
374                 glAlphaFunc(GL_GREATER, 0.5f);
375         }
376 }
377
378 static void gpu_verify_alpha_mode(MTFace *tface)
379 {
380         /* verify alpha blending modes */
381         if(GTS.alphamode == tface->transp)
382                 return;
383
384         gpu_set_blend_mode(tface->transp);
385         GTS.alphamode= tface->transp;
386 }
387
388 static void gpu_verify_reflection(Image *ima)
389 {
390         if (ima && (ima->flag & IMA_REFLECT)) {
391                 /* enable reflection mapping */
392                 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
393                 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
394
395                 glEnable(GL_TEXTURE_GEN_S);
396                 glEnable(GL_TEXTURE_GEN_T);
397         }
398         else {
399                 /* disable reflection mapping */
400                 glDisable(GL_TEXTURE_GEN_S);
401                 glDisable(GL_TEXTURE_GEN_T);
402         }
403 }
404
405 int GPU_verify_image(Image *ima, ImageUser *iuser, int tftile, int compare, int mipmap)
406 {
407         ImBuf *ibuf = NULL;
408         unsigned int *bind = NULL;
409         int rectw, recth, tpx=0, tpy=0, y;
410         unsigned int *rectrow, *tilerectrow;
411         unsigned int *tilerect= NULL, *scalerect= NULL, *rect= NULL;
412         short texwindx, texwindy, texwinsx, texwinsy;
413
414         /* initialize tile mode and number of repeats */
415         GTS.ima = ima;
416         GTS.tilemode= (ima && (ima->tpageflag & (IMA_TILES|IMA_TWINANIM)));
417         GTS.tileXRep = 0;
418         GTS.tileYRep = 0;
419
420         /* setting current tile according to frame */
421         if(ima && (ima->tpageflag & IMA_TWINANIM))
422                 GTS.tile= ima->lastframe;
423         else
424                 GTS.tile= tftile;
425
426         GTS.tile = MAX2(0, GTS.tile);
427
428         if(ima) {
429                 GTS.tileXRep = ima->xrep;
430                 GTS.tileYRep = ima->yrep;
431         }
432
433         /* if same image & tile, we're done */
434         if(compare && ima == GTS.curima && GTS.curtile == GTS.tile &&
435            GTS.tilemode == GTS.curtilemode && GTS.curtileXRep == GTS.tileXRep &&
436            GTS.curtileYRep == GTS.tileYRep)
437                 return (ima != NULL);
438
439         /* if tiling mode or repeat changed, change texture matrix to fit */
440         if(GTS.tilemode!=GTS.curtilemode || GTS.curtileXRep!=GTS.tileXRep ||
441            GTS.curtileYRep != GTS.tileYRep) {
442
443                 glMatrixMode(GL_TEXTURE);
444                 glLoadIdentity();
445
446                 if(ima && (ima->tpageflag & IMA_TILES))
447                         glScalef(ima->xrep, ima->yrep, 1.0);
448
449                 glMatrixMode(GL_MODELVIEW);
450         }
451
452         /* check if we have a valid image */
453         if(ima==NULL || ima->ok==0)
454                 return 0;
455
456         /* check if we have a valid image buffer */
457         ibuf= BKE_image_get_ibuf(ima, iuser);
458
459         if(ibuf==NULL)
460                 return 0;
461
462         /* ensure we have a char buffer and not only float */
463         if ((ibuf->rect==NULL) && ibuf->rect_float)
464                 IMB_rect_from_float(ibuf);
465
466         /* currently, tpage refresh is used by ima sequences */
467         if(ima->tpageflag & IMA_TPAGE_REFRESH) {
468                 GPU_free_image(ima);
469                 ima->tpageflag &= ~IMA_TPAGE_REFRESH;
470         }
471         
472         if(GTS.tilemode) {
473                 /* tiled mode */
474                 if(ima->repbind==NULL) gpu_make_repbind(ima);
475                 if(GTS.tile>=ima->totbind) GTS.tile= 0;
476                 
477                 /* this happens when you change repeat buttons */
478                 if(ima->repbind) bind= &ima->repbind[GTS.tile];
479                 else bind= &ima->bindcode;
480                 
481                 if(*bind==0) {
482                         
483                         texwindx= ibuf->x/ima->xrep;
484                         texwindy= ibuf->y/ima->yrep;
485                         
486                         if(GTS.tile>=ima->xrep*ima->yrep)
487                                 GTS.tile= ima->xrep*ima->yrep-1;
488         
489                         texwinsy= GTS.tile / ima->xrep;
490                         texwinsx= GTS.tile - texwinsy*ima->xrep;
491         
492                         texwinsx*= texwindx;
493                         texwinsy*= texwindy;
494         
495                         tpx= texwindx;
496                         tpy= texwindy;
497
498                         rect= ibuf->rect + texwinsy*ibuf->x + texwinsx;
499                 }
500         }
501         else {
502                 /* regular image mode */
503                 bind= &ima->bindcode;
504                 
505                 if(*bind==0) {
506                         tpx= ibuf->x;
507                         tpy= ibuf->y;
508                         rect= ibuf->rect;
509                 }
510         }
511
512         if(*bind != 0) {
513                 /* enable opengl drawing with textures */
514                 glBindTexture(GL_TEXTURE_2D, *bind);
515                 return *bind;
516         }
517
518         rectw = tpx;
519         recth = tpy;
520
521         /* for tiles, copy only part of image into buffer */
522         if (GTS.tilemode) {
523                 tilerect= MEM_mallocN(rectw*recth*sizeof(*tilerect), "tilerect");
524
525                 for (y=0; y<recth; y++) {
526                         rectrow= &rect[y*ibuf->x];
527                         tilerectrow= &tilerect[y*rectw];
528                                 
529                         memcpy(tilerectrow, rectrow, tpx*sizeof(*rectrow));
530                 }
531                         
532                 rect= tilerect;
533         }
534
535         /* scale if not a power of two */
536         if (!is_pow2_limit(rectw) || !is_pow2_limit(recth)) {
537                 rectw= smaller_pow2_limit(rectw);
538                 recth= smaller_pow2_limit(recth);
539                 
540                 scalerect= MEM_mallocN(rectw*recth*sizeof(*scalerect), "scalerect");
541                 gluScaleImage(GL_RGBA, tpx, tpy, GL_UNSIGNED_BYTE, rect, rectw, recth, GL_UNSIGNED_BYTE, scalerect);
542                 rect= scalerect;
543         }
544
545         /* create image */
546         glGenTextures(1, (GLuint *)bind);
547         glBindTexture( GL_TEXTURE_2D, *bind);
548
549         if (!(gpu_get_mipmap() && mipmap)) {
550                 glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA,  rectw, recth, 0, GL_RGBA, GL_UNSIGNED_BYTE, rect);
551                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
552                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
553         }
554         else {
555                 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, rectw, recth, GL_RGBA, GL_UNSIGNED_BYTE, rect);
556                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
557                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
558
559                 ima->tpageflag |= IMA_MIPMAP_COMPLETE;
560         }
561
562         /* set to modulate with vertex color */
563         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
564                 
565         /* clean up */
566         if (tilerect)
567                 MEM_freeN(tilerect);
568         if (scalerect)
569                 MEM_freeN(scalerect);
570
571         return *bind;
572 }
573
574 static void gpu_verify_repeat(Image *ima)
575 {
576         /* set either clamp or repeat in X/Y */
577         if (ima->tpageflag & IMA_CLAMP_U)
578            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
579         else
580                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
581
582         if (ima->tpageflag & IMA_CLAMP_V)
583                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
584         else
585                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
586 }
587
588 int GPU_set_tpage(MTFace *tface, int mipmap)
589 {
590         Image *ima;
591         
592         /* check if we need to clear the state */
593         if(tface==NULL) {
594                 gpu_clear_tpage();
595                 return 0;
596         }
597
598         ima= tface->tpage;
599         GTS.lasttface= tface;
600
601         gpu_verify_alpha_mode(tface);
602         gpu_verify_reflection(ima);
603
604         if(GPU_verify_image(ima, NULL, tface->tile, 1, mipmap)) {
605                 GTS.curtile= GTS.tile;
606                 GTS.curima= GTS.ima;
607                 GTS.curtilemode= GTS.tilemode;
608                 GTS.curtileXRep = GTS.tileXRep;
609                 GTS.curtileYRep = GTS.tileYRep;
610
611                 glEnable(GL_TEXTURE_2D);
612         }
613         else {
614                 glDisable(GL_TEXTURE_2D);
615                 
616                 GTS.curtile= 0;
617                 GTS.curima= NULL;
618                 GTS.curtilemode= 0;
619                 GTS.curtileXRep = 0;
620                 GTS.curtileYRep = 0;
621
622                 return 0;
623         }
624         
625         gpu_verify_repeat(ima);
626         
627         /* Did this get lost in the image recode? */
628         /* tag_image_time(ima);*/
629
630         return 1;
631 }
632
633 /* these two functions are called on entering and exiting texture paint mode,
634    temporary disabling/enabling mipmapping on all images for quick texture
635    updates with glTexSubImage2D. images that didn't change don't have to be
636    re-uploaded to OpenGL */
637 void GPU_paint_set_mipmap(int mipmap)
638 {
639         Image* ima;
640         
641         if(!GTS.domipmap)
642                 return;
643
644         if(mipmap) {
645                 for(ima=G.main->image.first; ima; ima=ima->id.next) {
646                         if(ima->bindcode) {
647                                 if(ima->tpageflag & IMA_MIPMAP_COMPLETE) {
648                                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
649                                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
650                                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
651                                 }
652                                 else
653                                         GPU_free_image(ima);
654                         }
655                 }
656
657         }
658         else {
659                 for(ima=G.main->image.first; ima; ima=ima->id.next) {
660                         if(ima->bindcode) {
661                                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
662                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
663                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
664                         }
665                 }
666         }
667 }
668
669 void GPU_paint_update_image(Image *ima, int x, int y, int w, int h, int mipmap)
670 {
671         ImBuf *ibuf;
672         
673         ibuf = BKE_image_get_ibuf(ima, NULL);
674         
675         if (ima->repbind || (gpu_get_mipmap() && mipmap) || !ima->bindcode || !ibuf ||
676                 (!is_pow2(ibuf->x) || !is_pow2(ibuf->y)) ||
677                 (w == 0) || (h == 0)) {
678                 /* these cases require full reload still */
679                 GPU_free_image(ima);
680         }
681         else {
682                 /* for the special case, we can do a partial update
683                  * which is much quicker for painting */
684                 GLint row_length, skip_pixels, skip_rows;
685
686                 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
687                 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
688                 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
689
690                 if ((ibuf->rect==NULL) && ibuf->rect_float)
691                         IMB_rect_from_float(ibuf);
692
693                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
694
695                 glPixelStorei(GL_UNPACK_ROW_LENGTH, ibuf->x);
696                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
697                 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
698
699                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
700                         GL_UNSIGNED_BYTE, ibuf->rect);
701
702                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
703                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
704                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
705
706                 if(ima->tpageflag & IMA_MIPMAP_COMPLETE)
707                         ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
708         }
709 }
710
711 void GPU_update_images_framechange(void)
712 {
713         Image *ima;
714         
715         for(ima=G.main->image.first; ima; ima=ima->id.next) {
716                 if(ima->tpageflag & IMA_TWINANIM) {
717                         if(ima->twend >= ima->xrep*ima->yrep)
718                                 ima->twend= ima->xrep*ima->yrep-1;
719                 
720                         /* check: is bindcode not in the array? free. (to do) */
721                         
722                         ima->lastframe++;
723                         if(ima->lastframe > ima->twend)
724                                 ima->lastframe= ima->twsta;
725                 }
726         }
727 }
728
729 int GPU_update_image_time(Image *ima, double time)
730 {
731         int     inc = 0;
732         float   diff;
733         int     newframe;
734
735         if (!ima)
736                 return 0;
737
738         if (ima->lastupdate<0)
739                 ima->lastupdate = 0;
740
741         if (ima->lastupdate>time)
742                 ima->lastupdate=(float)time;
743
744         if(ima->tpageflag & IMA_TWINANIM) {
745                 if(ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1;
746                 
747                 /* check: is the bindcode not in the array? Then free. (still to do) */
748                 
749                 diff = (float)(time-ima->lastupdate);
750                 inc = (int)(diff*(float)ima->animspeed);
751
752                 ima->lastupdate+=((float)inc/(float)ima->animspeed);
753
754                 newframe = ima->lastframe+inc;
755
756                 if(newframe > (int)ima->twend) {
757                         if(ima->twend-ima->twsta != 0)
758                                 newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta);
759                         else
760                                 newframe = ima->twsta;
761                 }
762
763                 ima->lastframe = newframe;
764         }
765
766         return inc;
767 }
768
769
770 void GPU_free_smoke(SmokeModifierData *smd)
771 {
772         if(smd->type & MOD_SMOKE_TYPE_DOMAIN && smd->domain)
773         {
774                 if(smd->domain->tex)
775                          GPU_texture_free(smd->domain->tex);
776                 smd->domain->tex = NULL;
777
778                 if(smd->domain->tex_shadow)
779                          GPU_texture_free(smd->domain->tex_shadow);
780                 smd->domain->tex_shadow = NULL;
781         }
782 }
783
784 void GPU_create_smoke(SmokeModifierData *smd, int highres)
785 {
786         if(smd->type & MOD_SMOKE_TYPE_DOMAIN && !smd->domain->tex && !highres)
787                 smd->domain->tex = GPU_texture_create_3D(smd->domain->res[0], smd->domain->res[1], smd->domain->res[2], smoke_get_density(smd->domain->fluid));
788         else if(smd->type & MOD_SMOKE_TYPE_DOMAIN && !smd->domain->tex && highres)
789                 smd->domain->tex = GPU_texture_create_3D(smd->domain->res_wt[0], smd->domain->res_wt[1], smd->domain->res_wt[2], smoke_turbulence_get_density(smd->domain->wt));
790
791         smd->domain->tex_shadow = GPU_texture_create_3D(smd->domain->res[0], smd->domain->res[1], smd->domain->res[2], smd->domain->shadow);
792 }
793
794 static ListBase image_free_queue = {NULL, NULL};
795
796 static void gpu_queue_image_for_free(Image *ima)
797 {
798         Image *cpy = MEM_dupallocN(ima);
799
800         BLI_lock_thread(LOCK_OPENGL);
801         BLI_addtail(&image_free_queue, cpy);
802         BLI_unlock_thread(LOCK_OPENGL);
803 }
804
805 void GPU_free_unused_buffers(void)
806 {
807         Image *ima;
808
809         if(!BLI_thread_is_main())
810                 return;
811
812         BLI_lock_thread(LOCK_OPENGL);
813
814         /* images */
815         for(ima=image_free_queue.first; ima; ima=ima->id.next)
816                 GPU_free_image(ima);
817
818         BLI_freelistN(&image_free_queue);
819
820         /* vbo buffers */
821         /* it's probably not necessary to free all buffers every frame */
822         /* GPU_buffer_pool_free_unused(0); */
823
824         BLI_unlock_thread(LOCK_OPENGL);
825 }
826
827 void GPU_free_image(Image *ima)
828 {
829         if(!BLI_thread_is_main()) {
830                 gpu_queue_image_for_free(ima);
831                 return;
832         }
833
834         /* free regular image binding */
835         if(ima->bindcode) {
836                 glDeleteTextures(1, (GLuint *)&ima->bindcode);
837                 ima->bindcode= 0;
838                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
839         }
840
841         /* free glsl image binding */
842         if(ima->gputexture) {
843                 GPU_texture_free(ima->gputexture);
844                 ima->gputexture= NULL;
845         }
846
847         /* free repeated image binding */
848         if(ima->repbind) {
849                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
850         
851                 MEM_freeN(ima->repbind);
852                 ima->repbind= NULL;
853                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
854         }
855 }
856
857 void GPU_free_images(void)
858 {
859         Image* ima;
860
861         if(G.main)
862                 for(ima=G.main->image.first; ima; ima=ima->id.next)
863                         GPU_free_image(ima);
864 }
865
866 /* same as above but only free animated images */
867 void GPU_free_images_anim(void)
868 {
869         Image* ima;
870
871         if(G.main)
872                 for(ima=G.main->image.first; ima; ima=ima->id.next)
873                         if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
874                                 GPU_free_image(ima);
875 }
876
877 /* OpenGL Materials */
878
879 #define FIXEDMAT        8
880
881 /* OpenGL state caching for materials */
882
883 typedef struct GPUMaterialFixed {
884         float diff[4];
885         float spec[4];
886         int hard;
887 } GPUMaterialFixed; 
888
889 static struct GPUMaterialState {
890         GPUMaterialFixed (*matbuf);
891         GPUMaterialFixed matbuf_fixed[FIXEDMAT];
892         int totmat;
893
894         Material **gmatbuf;
895         Material *gmatbuf_fixed[FIXEDMAT];
896         Material *gboundmat;
897         Object *gob;
898         Scene *gscene;
899         int glay;
900         float (*gviewmat)[4];
901         float (*gviewinv)[4];
902
903         GPUBlendMode *blendmode;
904         GPUBlendMode blendmode_fixed[FIXEDMAT];
905         int alphapass;
906
907         int lastmatnr, lastretval;
908         GPUBlendMode lastblendmode;
909 } GMS = {NULL};
910
911 /* fixed function material, alpha handed by caller */
912 static void gpu_material_to_fixed(GPUMaterialFixed *smat, const Material *bmat, const int gamma, const Object *ob)
913 {
914         if (bmat->mode & MA_SHLESS) {
915                 copy_v3_v3(smat->diff, &bmat->r);
916                 smat->diff[3]= 1.0;
917
918                 if(gamma) {
919                         linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
920                 }       
921         }
922         else {
923                 mul_v3_v3fl(smat->diff, &bmat->r, bmat->ref + bmat->emit);
924                 smat->diff[3]= 1.0; /* caller may set this to bmat->alpha */
925
926                 if(bmat->shade_flag & MA_OBCOLOR)
927                         mul_v3_v3(smat->diff, ob->col);
928                 
929                 mul_v3_v3fl(smat->spec, &bmat->specr, bmat->spec);
930                 smat->spec[3]= 1.0; /* always 1 */
931                 smat->hard= CLAMPIS(bmat->har, 0, 128);
932
933                 if(gamma) {
934                         linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
935                         linearrgb_to_srgb_v3_v3(smat->spec, smat->spec);
936                 }       
937         }
938 }
939
940 static Material *gpu_active_node_material(Material *ma)
941 {
942         if(ma && ma->use_nodes && ma->nodetree) {
943                 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
944
945                 if(node)
946                         return (Material *)node->id;
947                 else
948                         return NULL;
949         }
950
951         return ma;
952 }
953
954 void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, Object *ob, int glsl, int *do_alpha_pass)
955 {
956         Material *ma;
957         GPUMaterial *gpumat;
958         GPUBlendMode blendmode;
959         int a;
960         int gamma = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
961         
962         /* initialize state */
963         memset(&GMS, 0, sizeof(GMS));
964         GMS.lastmatnr = -1;
965         GMS.lastretval = -1;
966         GMS.lastblendmode = GPU_BLEND_SOLID;
967
968         GMS.gob = ob;
969         GMS.gscene = scene;
970         GMS.totmat= ob->totcol+1; /* materials start from 1, default material is 0 */
971         GMS.glay= v3d->lay;
972         GMS.gviewmat= rv3d->viewmat;
973         GMS.gviewinv= rv3d->viewinv;
974
975         GMS.alphapass = (v3d && v3d->transp);
976         if(do_alpha_pass)
977                 *do_alpha_pass = 0;
978         
979         if(GMS.totmat > FIXEDMAT) {
980                 GMS.matbuf= MEM_callocN(sizeof(GPUMaterialFixed)*GMS.totmat, "GMS.matbuf");
981                 GMS.gmatbuf= MEM_callocN(sizeof(*GMS.gmatbuf)*GMS.totmat, "GMS.matbuf");
982                 GMS.blendmode= MEM_callocN(sizeof(*GMS.blendmode)*GMS.totmat, "GMS.matbuf");
983         }
984         else {
985                 GMS.matbuf= GMS.matbuf_fixed;
986                 GMS.gmatbuf= GMS.gmatbuf_fixed;
987                 GMS.blendmode= GMS.blendmode_fixed;
988         }
989
990         /* no materials assigned? */
991         if(ob->totcol==0) {
992                 gpu_material_to_fixed(&GMS.matbuf[0], &defmaterial, 0, ob);
993
994                 /* do material 1 too, for displists! */
995                 memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
996
997                 if(glsl) {
998                         GMS.gmatbuf[0]= &defmaterial;
999                         GPU_material_from_blender(GMS.gscene, &defmaterial);
1000                 }
1001
1002                 GMS.blendmode[0]= GPU_BLEND_SOLID;
1003         }
1004         
1005         /* setup materials */
1006         for(a=1; a<=ob->totcol; a++) {
1007                 /* find a suitable material */
1008                 ma= give_current_material(ob, a);
1009                 if(!glsl) ma= gpu_active_node_material(ma);
1010                 if(ma==NULL) ma= &defmaterial;
1011
1012                 /* create glsl material if requested */
1013                 gpumat = (glsl)? GPU_material_from_blender(GMS.gscene, ma): NULL;
1014
1015                 if(gpumat) {
1016                         /* do glsl only if creating it succeed, else fallback */
1017                         GMS.gmatbuf[a]= ma;
1018                         blendmode = GPU_material_blend_mode(gpumat, ob->col);
1019                 }
1020                 else {
1021                         /* fixed function opengl materials */
1022                         gpu_material_to_fixed(&GMS.matbuf[a], ma, gamma, ob);
1023
1024                         blendmode = (ma->alpha == 1.0f)? GPU_BLEND_SOLID: GPU_BLEND_ALPHA;
1025                         if(do_alpha_pass && GMS.alphapass)
1026                                 GMS.matbuf[a].diff[3]= ma->alpha;
1027                         else
1028                                 GMS.matbuf[a].diff[3]= 1.0f;
1029                 }
1030
1031                 /* setting do_alpha_pass = 1 indicates this object needs to be
1032                  * drawn in a second alpha pass for improved blending */
1033                 if(do_alpha_pass) {
1034                         GMS.blendmode[a]= blendmode;
1035                         if(ELEM(blendmode, GPU_BLEND_ALPHA, GPU_BLEND_ADD) && !GMS.alphapass)
1036                                 *do_alpha_pass= 1;
1037                 }
1038         }
1039
1040         /* let's start with a clean state */
1041         GPU_disable_material();
1042 }
1043
1044 int GPU_enable_material(int nr, void *attribs)
1045 {
1046         GPUVertexAttribs *gattribs = attribs;
1047         GPUMaterial *gpumat;
1048         GPUBlendMode blendmode;
1049
1050         /* no GPU_begin_object_materials, use default material */
1051         if(!GMS.matbuf) {
1052                 float diff[4], spec[4];
1053
1054                 memset(&GMS, 0, sizeof(GMS));
1055
1056                 mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit);
1057                 diff[3]= 1.0;
1058
1059                 mul_v3_v3fl(spec, &defmaterial.specr, defmaterial.spec);
1060                 spec[3]= 1.0;
1061
1062                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
1063                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1064                 glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35); /* blender default */
1065
1066                 return 0;
1067         }
1068
1069         /* prevent index to use un-initialized array items */
1070         if(nr>=GMS.totmat)
1071                 nr= 0;
1072
1073         if(gattribs)
1074                 memset(gattribs, 0, sizeof(*gattribs));
1075
1076         /* keep current material */
1077         if(nr==GMS.lastmatnr)
1078                 return GMS.lastretval;
1079
1080         /* unbind glsl material */
1081         if(GMS.gboundmat) {
1082                 if(GMS.alphapass) glDepthMask(0);
1083                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1084                 GMS.gboundmat= NULL;
1085         }
1086
1087         /* draw materials with alpha in alpha pass */
1088         GMS.lastmatnr = nr;
1089         GMS.lastretval = ELEM(GMS.blendmode[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP);
1090         if(GMS.alphapass)
1091                 GMS.lastretval = !GMS.lastretval;
1092
1093         if(GMS.lastretval) {
1094                 if(gattribs && GMS.gmatbuf[nr]) {
1095                         /* bind glsl material and get attributes */
1096                         Material *mat = GMS.gmatbuf[nr];
1097
1098                         gpumat = GPU_material_from_blender(GMS.gscene, mat);
1099                         GPU_material_vertex_attributes(gpumat, gattribs);
1100                         GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT));
1101                         GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gviewmat, GMS.gviewinv, GMS.gob->col);
1102                         GMS.gboundmat= mat;
1103
1104                         if(GMS.alphapass) glDepthMask(1);
1105                 }
1106                 else {
1107                         /* or do fixed function opengl material */
1108                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GMS.matbuf[nr].diff);
1109                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, GMS.matbuf[nr].spec);
1110                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, GMS.matbuf[nr].hard);
1111                 }
1112
1113                 /* set (alpha) blending mode */
1114                 blendmode = (GMS.alphapass)? GPU_BLEND_ALPHA: GPU_BLEND_SOLID;
1115                 GPU_set_material_blend_mode(blendmode);
1116         }
1117
1118         return GMS.lastretval;
1119 }
1120
1121 void GPU_set_material_blend_mode(int blendmode)
1122 {
1123         if(GMS.lastblendmode == blendmode)
1124                 return;
1125         
1126         gpu_set_blend_mode(blendmode);
1127         GMS.lastblendmode = blendmode;
1128 }
1129
1130 int GPU_get_material_blend_mode(void)
1131 {
1132         return GMS.lastblendmode;
1133 }
1134
1135 void GPU_disable_material(void)
1136 {
1137         GMS.lastmatnr= -1;
1138         GMS.lastretval= 1;
1139
1140         if(GMS.gboundmat) {
1141                 if(GMS.alphapass) glDepthMask(0);
1142                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1143                 GMS.gboundmat= NULL;
1144         }
1145
1146         GPU_set_material_blend_mode(GPU_BLEND_SOLID);
1147 }
1148
1149 void GPU_end_object_materials(void)
1150 {
1151         GPU_disable_material();
1152
1153         if(GMS.matbuf && GMS.matbuf != GMS.matbuf_fixed) {
1154                 MEM_freeN(GMS.matbuf);
1155                 MEM_freeN(GMS.gmatbuf);
1156                 MEM_freeN(GMS.blendmode);
1157         }
1158
1159         GMS.matbuf= NULL;
1160         GMS.gmatbuf= NULL;
1161         GMS.blendmode= NULL;
1162
1163         /* resetting the texture matrix after the glScale needed for tiled textures */
1164         if(GTS.tilemode)
1165         {
1166                 glMatrixMode(GL_TEXTURE);
1167                 glLoadIdentity();
1168                 glMatrixMode(GL_MODELVIEW);
1169         }
1170 }
1171
1172 /* Lights */
1173
1174 int GPU_default_lights(void)
1175 {
1176         int a, count = 0;
1177         
1178         /* initialize */
1179         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
1180                 U.light[0].flag= 1;
1181                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
1182                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
1183                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
1184                 U.light[0].spec[3]= 1.0;
1185                 
1186                 U.light[1].flag= 0;
1187                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
1188                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
1189                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
1190                 U.light[1].spec[3]= 1.0;
1191         
1192                 U.light[2].flag= 0;
1193                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
1194                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
1195                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
1196                 U.light[2].spec[3]= 1.0;
1197         }
1198
1199         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
1200
1201         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
1202         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
1203         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
1204
1205         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
1206         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
1207         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
1208
1209         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
1210         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
1211         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
1212
1213         for(a=0; a<8; a++) {
1214                 if(a<3) {
1215                         if(U.light[a].flag) {
1216                                 glEnable(GL_LIGHT0+a);
1217                                 count++;
1218                         }
1219                         else
1220                                 glDisable(GL_LIGHT0+a);
1221                         
1222                         // clear stuff from other opengl lamp usage
1223                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
1224                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
1225                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
1226                 }
1227                 else
1228                         glDisable(GL_LIGHT0+a);
1229         }
1230         
1231         glDisable(GL_LIGHTING);
1232
1233         glDisable(GL_COLOR_MATERIAL);
1234
1235         return count;
1236 }
1237
1238 int GPU_scene_object_lights(Scene *scene, Object *ob, int lay, float viewmat[][4], int ortho)
1239 {
1240         Base *base;
1241         Lamp *la;
1242         int count;
1243         float position[4], direction[4], energy[4], power;
1244         
1245         /* disable all lights */
1246         for(count=0; count<8; count++)
1247                 glDisable(GL_LIGHT0+count);
1248         
1249         /* view direction for specular is not compute correct by default in
1250          * opengl, so we set the settings ourselfs */
1251         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (ortho)? GL_FALSE: GL_TRUE);
1252
1253         count= 0;
1254         
1255         for(base=scene->base.first; base; base=base->next) {
1256                 if(base->object->type!=OB_LAMP)
1257                         continue;
1258
1259                 if(!(base->lay & lay) || !(base->lay & ob->lay))
1260                         continue;
1261
1262                 la= base->object->data;
1263                 
1264                 /* setup lamp transform */
1265                 glPushMatrix();
1266                 glLoadMatrixf((float *)viewmat);
1267                 
1268                 where_is_object_simul(scene, base->object);
1269                 
1270                 if(la->type==LA_SUN) {
1271                         /* sun lamp */
1272                         copy_v3_v3(direction, base->object->obmat[2]);
1273                         direction[3]= 0.0;
1274
1275                         glLightfv(GL_LIGHT0+count, GL_POSITION, direction); 
1276                 }
1277                 else {
1278                         /* other lamps with attenuation */
1279                         copy_v3_v3(position, base->object->obmat[3]);
1280                         position[3]= 1.0f;
1281
1282                         glLightfv(GL_LIGHT0+count, GL_POSITION, position); 
1283                         glLightf(GL_LIGHT0+count, GL_CONSTANT_ATTENUATION, 1.0);
1284                         glLightf(GL_LIGHT0+count, GL_LINEAR_ATTENUATION, 0.0f/la->dist);
1285                         glLightf(GL_LIGHT0+count, GL_QUADRATIC_ATTENUATION, 1.0f/(la->dist*la->dist));
1286                         
1287                         if(la->type==LA_SPOT) {
1288                                 /* spot lamp */
1289                                 negate_v3_v3(direction, base->object->obmat[2]);
1290                                 glLightfv(GL_LIGHT0+count, GL_SPOT_DIRECTION, direction);
1291                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, la->spotsize/2.0f);
1292                                 glLightf(GL_LIGHT0+count, GL_SPOT_EXPONENT, 128.0f*la->spotblend);
1293                         }
1294                         else
1295                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, 180.0);
1296                 }
1297
1298                 power= (ELEM(la->type, LA_SUN, LA_HEMI))? la->energy*M_PI: la->energy*M_PI; //XXX la->power
1299                 
1300                 /* setup energy */
1301                 mul_v3_v3fl(energy, &la->r, la->energy);
1302                 energy[3]= 1.0;
1303
1304                 glLightfv(GL_LIGHT0+count, GL_DIFFUSE, energy); 
1305                 glLightfv(GL_LIGHT0+count, GL_SPECULAR, energy);
1306                 glEnable(GL_LIGHT0+count);
1307                 
1308                 glPopMatrix();                                  
1309                 
1310                 count++;
1311                 if(count==8)
1312                         break;
1313         }
1314
1315         return count;
1316 }
1317
1318 /* Default OpenGL State */
1319
1320 void GPU_state_init(void)
1321 {
1322         /* also called when doing opengl rendering and in the game engine */
1323         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
1324         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
1325         int a, x, y;
1326         GLubyte pat[32*32];
1327         const GLubyte *patc= pat;
1328         
1329         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
1330         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
1331         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1332         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35);
1333
1334         GPU_default_lights();
1335         
1336         glDepthFunc(GL_LEQUAL);
1337         /* scaling matrices */
1338         glEnable(GL_NORMALIZE);
1339
1340         glShadeModel(GL_FLAT);
1341
1342         glDisable(GL_ALPHA_TEST);
1343         glDisable(GL_BLEND);
1344         glDisable(GL_DEPTH_TEST);
1345         glDisable(GL_FOG);
1346         glDisable(GL_LIGHTING);
1347         glDisable(GL_LOGIC_OP);
1348         glDisable(GL_STENCIL_TEST);
1349         glDisable(GL_TEXTURE_1D);
1350         glDisable(GL_TEXTURE_2D);
1351
1352         /* default disabled, enable should be local per function */
1353         glDisableClientState(GL_VERTEX_ARRAY);
1354         glDisableClientState(GL_NORMAL_ARRAY);
1355         glDisableClientState(GL_COLOR_ARRAY);
1356         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1357         
1358         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
1359         glPixelTransferi(GL_RED_SCALE, 1);
1360         glPixelTransferi(GL_RED_BIAS, 0);
1361         glPixelTransferi(GL_GREEN_SCALE, 1);
1362         glPixelTransferi(GL_GREEN_BIAS, 0);
1363         glPixelTransferi(GL_BLUE_SCALE, 1);
1364         glPixelTransferi(GL_BLUE_BIAS, 0);
1365         glPixelTransferi(GL_ALPHA_SCALE, 1);
1366         glPixelTransferi(GL_ALPHA_BIAS, 0);
1367         
1368         glPixelTransferi(GL_DEPTH_BIAS, 0);
1369         glPixelTransferi(GL_DEPTH_SCALE, 1);
1370         glDepthRange(0.0, 1.0);
1371         
1372         a= 0;
1373         for(x=0; x<32; x++) {
1374                 for(y=0; y<4; y++) {
1375                         if( (x) & 1) pat[a++]= 0x88;
1376                         else pat[a++]= 0x22;
1377                 }
1378         }
1379         
1380         glPolygonStipple(patc);
1381
1382         glMatrixMode(GL_TEXTURE);
1383         glLoadIdentity();
1384         glMatrixMode(GL_MODELVIEW);
1385
1386         glFrontFace(GL_CCW);
1387         glCullFace(GL_BACK);
1388         glDisable(GL_CULL_FACE);
1389
1390         /* calling this makes drawing very slow when AA is not set up in ghost
1391            on Linux/NVIDIA.
1392         glDisable(GL_MULTISAMPLE); */
1393 }
1394
1395 /* debugging aid */
1396 static void gpu_get_print(const char *name, GLenum type)
1397 {
1398         float value[16];
1399         int a;
1400         
1401         memset(value, 0, sizeof(value));
1402         glGetFloatv(type, value);
1403
1404         printf("%s: ", name);
1405         for(a=0; a<16; a++)
1406                 printf("%.2f ", value[a]);
1407         printf("\n");
1408 }
1409
1410 void GPU_state_print(void)
1411 {
1412         gpu_get_print("GL_ACCUM_ALPHA_BITS", GL_ACCUM_ALPHA_BITS);
1413         gpu_get_print("GL_ACCUM_BLUE_BITS", GL_ACCUM_BLUE_BITS);
1414         gpu_get_print("GL_ACCUM_CLEAR_VALUE", GL_ACCUM_CLEAR_VALUE);
1415         gpu_get_print("GL_ACCUM_GREEN_BITS", GL_ACCUM_GREEN_BITS);
1416         gpu_get_print("GL_ACCUM_RED_BITS", GL_ACCUM_RED_BITS);
1417         gpu_get_print("GL_ACTIVE_TEXTURE", GL_ACTIVE_TEXTURE);
1418         gpu_get_print("GL_ALIASED_POINT_SIZE_RANGE", GL_ALIASED_POINT_SIZE_RANGE);
1419         gpu_get_print("GL_ALIASED_LINE_WIDTH_RANGE", GL_ALIASED_LINE_WIDTH_RANGE);
1420         gpu_get_print("GL_ALPHA_BIAS", GL_ALPHA_BIAS);
1421         gpu_get_print("GL_ALPHA_BITS", GL_ALPHA_BITS);
1422         gpu_get_print("GL_ALPHA_SCALE", GL_ALPHA_SCALE);
1423         gpu_get_print("GL_ALPHA_TEST", GL_ALPHA_TEST);
1424         gpu_get_print("GL_ALPHA_TEST_FUNC", GL_ALPHA_TEST_FUNC);
1425         gpu_get_print("GL_ALPHA_TEST_REF", GL_ALPHA_TEST_REF);
1426         gpu_get_print("GL_ARRAY_BUFFER_BINDING", GL_ARRAY_BUFFER_BINDING);
1427         gpu_get_print("GL_ATTRIB_STACK_DEPTH", GL_ATTRIB_STACK_DEPTH);
1428         gpu_get_print("GL_AUTO_NORMAL", GL_AUTO_NORMAL);
1429         gpu_get_print("GL_AUX_BUFFERS", GL_AUX_BUFFERS);
1430         gpu_get_print("GL_BLEND", GL_BLEND);
1431         gpu_get_print("GL_BLEND_COLOR", GL_BLEND_COLOR);
1432         gpu_get_print("GL_BLEND_DST_ALPHA", GL_BLEND_DST_ALPHA);
1433         gpu_get_print("GL_BLEND_DST_RGB", GL_BLEND_DST_RGB);
1434         gpu_get_print("GL_BLEND_EQUATION_RGB", GL_BLEND_EQUATION_RGB);
1435         gpu_get_print("GL_BLEND_EQUATION_ALPHA", GL_BLEND_EQUATION_ALPHA);
1436         gpu_get_print("GL_BLEND_SRC_ALPHA", GL_BLEND_SRC_ALPHA);
1437         gpu_get_print("GL_BLEND_SRC_RGB", GL_BLEND_SRC_RGB);
1438         gpu_get_print("GL_BLUE_BIAS", GL_BLUE_BIAS);
1439         gpu_get_print("GL_BLUE_BITS", GL_BLUE_BITS);
1440         gpu_get_print("GL_BLUE_SCALE", GL_BLUE_SCALE);
1441         gpu_get_print("GL_CLIENT_ACTIVE_TEXTURE", GL_CLIENT_ACTIVE_TEXTURE);
1442         gpu_get_print("GL_CLIENT_ATTRIB_STACK_DEPTH", GL_CLIENT_ATTRIB_STACK_DEPTH);
1443         gpu_get_print("GL_CLIP_PLANE0", GL_CLIP_PLANE0);
1444         gpu_get_print("GL_COLOR_ARRAY", GL_COLOR_ARRAY);
1445         gpu_get_print("GL_COLOR_ARRAY_BUFFER_BINDING", GL_COLOR_ARRAY_BUFFER_BINDING);
1446         gpu_get_print("GL_COLOR_ARRAY_SIZE", GL_COLOR_ARRAY_SIZE);
1447         gpu_get_print("GL_COLOR_ARRAY_STRIDE", GL_COLOR_ARRAY_STRIDE);
1448         gpu_get_print("GL_COLOR_ARRAY_TYPE", GL_COLOR_ARRAY_TYPE);
1449         gpu_get_print("GL_COLOR_CLEAR_VALUE", GL_COLOR_CLEAR_VALUE);
1450         gpu_get_print("GL_COLOR_LOGIC_OP", GL_COLOR_LOGIC_OP);
1451         gpu_get_print("GL_COLOR_MATERIAL", GL_COLOR_MATERIAL);
1452         gpu_get_print("GL_COLOR_MATERIAL_FACE", GL_COLOR_MATERIAL_FACE);
1453         gpu_get_print("GL_COLOR_MATERIAL_PARAMETER", GL_COLOR_MATERIAL_PARAMETER);
1454         gpu_get_print("GL_COLOR_MATRIX", GL_COLOR_MATRIX);
1455         gpu_get_print("GL_COLOR_MATRIX_STACK_DEPTH", GL_COLOR_MATRIX_STACK_DEPTH);
1456         gpu_get_print("GL_COLOR_SUM", GL_COLOR_SUM);
1457         gpu_get_print("GL_COLOR_TABLE", GL_COLOR_TABLE);
1458         gpu_get_print("GL_COLOR_WRITEMASK", GL_COLOR_WRITEMASK);
1459         gpu_get_print("GL_COMPRESSED_TEXTURE_FORMATS", GL_COMPRESSED_TEXTURE_FORMATS);
1460         gpu_get_print("GL_CONVOLUTION_1D", GL_CONVOLUTION_1D);
1461         gpu_get_print("GL_CONVOLUTION_2D", GL_CONVOLUTION_2D);
1462         gpu_get_print("GL_CULL_FACE", GL_CULL_FACE);
1463         gpu_get_print("GL_CULL_FACE_MODE", GL_CULL_FACE_MODE);
1464         gpu_get_print("GL_CURRENT_COLOR", GL_CURRENT_COLOR);
1465         gpu_get_print("GL_CURRENT_FOG_COORD", GL_CURRENT_FOG_COORD);
1466         gpu_get_print("GL_CURRENT_INDEX", GL_CURRENT_INDEX);
1467         gpu_get_print("GL_CURRENT_NORMAL", GL_CURRENT_NORMAL);
1468         gpu_get_print("GL_CURRENT_PROGRAM", GL_CURRENT_PROGRAM);
1469         gpu_get_print("GL_CURRENT_RASTER_COLOR", GL_CURRENT_RASTER_COLOR);
1470         gpu_get_print("GL_CURRENT_RASTER_DISTANCE", GL_CURRENT_RASTER_DISTANCE);
1471         gpu_get_print("GL_CURRENT_RASTER_INDEX", GL_CURRENT_RASTER_INDEX);
1472         gpu_get_print("GL_CURRENT_RASTER_POSITION", GL_CURRENT_RASTER_POSITION);
1473         gpu_get_print("GL_CURRENT_RASTER_POSITION_VALID", GL_CURRENT_RASTER_POSITION_VALID);
1474         gpu_get_print("GL_CURRENT_RASTER_SECONDARY_COLOR", GL_CURRENT_RASTER_SECONDARY_COLOR);
1475         gpu_get_print("GL_CURRENT_RASTER_TEXTURE_COORDS", GL_CURRENT_RASTER_TEXTURE_COORDS);
1476         gpu_get_print("GL_CURRENT_SECONDARY_COLOR", GL_CURRENT_SECONDARY_COLOR);
1477         gpu_get_print("GL_CURRENT_TEXTURE_COORDS", GL_CURRENT_TEXTURE_COORDS);
1478         gpu_get_print("GL_DEPTH_BIAS", GL_DEPTH_BIAS);
1479         gpu_get_print("GL_DEPTH_BITS", GL_DEPTH_BITS);
1480         gpu_get_print("GL_DEPTH_CLEAR_VALUE", GL_DEPTH_CLEAR_VALUE);
1481         gpu_get_print("GL_DEPTH_FUNC", GL_DEPTH_FUNC);
1482         gpu_get_print("GL_DEPTH_RANGE", GL_DEPTH_RANGE);
1483         gpu_get_print("GL_DEPTH_SCALE", GL_DEPTH_SCALE);
1484         gpu_get_print("GL_DEPTH_TEST", GL_DEPTH_TEST);
1485         gpu_get_print("GL_DEPTH_WRITEMASK", GL_DEPTH_WRITEMASK);
1486         gpu_get_print("GL_DITHER", GL_DITHER);
1487         gpu_get_print("GL_DOUBLEBUFFER", GL_DOUBLEBUFFER);
1488         gpu_get_print("GL_DRAW_BUFFER", GL_DRAW_BUFFER);
1489         gpu_get_print("GL_DRAW_BUFFER0", GL_DRAW_BUFFER0);
1490         gpu_get_print("GL_EDGE_FLAG", GL_EDGE_FLAG);
1491         gpu_get_print("GL_EDGE_FLAG_ARRAY", GL_EDGE_FLAG_ARRAY);
1492         gpu_get_print("GL_EDGE_FLAG_ARRAY_BUFFER_BINDING", GL_EDGE_FLAG_ARRAY_BUFFER_BINDING);
1493         gpu_get_print("GL_EDGE_FLAG_ARRAY_STRIDE", GL_EDGE_FLAG_ARRAY_STRIDE);
1494         gpu_get_print("GL_ELEMENT_ARRAY_BUFFER_BINDING", GL_ELEMENT_ARRAY_BUFFER_BINDING);
1495         gpu_get_print("GL_FEEDBACK_BUFFER_SIZE", GL_FEEDBACK_BUFFER_SIZE);
1496         gpu_get_print("GL_FEEDBACK_BUFFER_TYPE", GL_FEEDBACK_BUFFER_TYPE);
1497         gpu_get_print("GL_FOG", GL_FOG);
1498         gpu_get_print("GL_FOG_COORD_ARRAY", GL_FOG_COORD_ARRAY);
1499         gpu_get_print("GL_FOG_COORD_ARRAY_BUFFER_BINDING", GL_FOG_COORD_ARRAY_BUFFER_BINDING);
1500         gpu_get_print("GL_FOG_COORD_ARRAY_STRIDE", GL_FOG_COORD_ARRAY_STRIDE);
1501         gpu_get_print("GL_FOG_COORD_ARRAY_TYPE", GL_FOG_COORD_ARRAY_TYPE);
1502         gpu_get_print("GL_FOG_COORD_SRC", GL_FOG_COORD_SRC);
1503         gpu_get_print("GL_FOG_COLOR", GL_FOG_COLOR);
1504         gpu_get_print("GL_FOG_DENSITY", GL_FOG_DENSITY);
1505         gpu_get_print("GL_FOG_END", GL_FOG_END);
1506         gpu_get_print("GL_FOG_HINT", GL_FOG_HINT);
1507         gpu_get_print("GL_FOG_INDEX", GL_FOG_INDEX);
1508         gpu_get_print("GL_FOG_MODE", GL_FOG_MODE);
1509         gpu_get_print("GL_FOG_START", GL_FOG_START);
1510         gpu_get_print("GL_FRAGMENT_SHADER_DERIVATIVE_HINT", GL_FRAGMENT_SHADER_DERIVATIVE_HINT);
1511         gpu_get_print("GL_FRONT_FACE", GL_FRONT_FACE);
1512         gpu_get_print("GL_GENERATE_MIPMAP_HINT", GL_GENERATE_MIPMAP_HINT);
1513         gpu_get_print("GL_GREEN_BIAS", GL_GREEN_BIAS);
1514         gpu_get_print("GL_GREEN_BITS", GL_GREEN_BITS);
1515         gpu_get_print("GL_GREEN_SCALE", GL_GREEN_SCALE);
1516         gpu_get_print("GL_HISTOGRAM", GL_HISTOGRAM);
1517         gpu_get_print("GL_INDEX_ARRAY", GL_INDEX_ARRAY);
1518         gpu_get_print("GL_INDEX_ARRAY_BUFFER_BINDING", GL_INDEX_ARRAY_BUFFER_BINDING);
1519         gpu_get_print("GL_INDEX_ARRAY_STRIDE", GL_INDEX_ARRAY_STRIDE);
1520         gpu_get_print("GL_INDEX_ARRAY_TYPE", GL_INDEX_ARRAY_TYPE);
1521         gpu_get_print("GL_INDEX_BITS", GL_INDEX_BITS);
1522         gpu_get_print("GL_INDEX_CLEAR_VALUE", GL_INDEX_CLEAR_VALUE);
1523         gpu_get_print("GL_INDEX_LOGIC_OP", GL_INDEX_LOGIC_OP);
1524         gpu_get_print("GL_INDEX_MODE", GL_INDEX_MODE);
1525         gpu_get_print("GL_INDEX_OFFSET", GL_INDEX_OFFSET);
1526         gpu_get_print("GL_INDEX_SHIFT", GL_INDEX_SHIFT);
1527         gpu_get_print("GL_INDEX_WRITEMASK", GL_INDEX_WRITEMASK);
1528         gpu_get_print("GL_LIGHT0", GL_LIGHT0);
1529         gpu_get_print("GL_LIGHTING", GL_LIGHTING);
1530         gpu_get_print("GL_LIGHT_MODEL_AMBIENT", GL_LIGHT_MODEL_AMBIENT);
1531         gpu_get_print("GL_LIGHT_MODEL_COLOR_CONTROL", GL_LIGHT_MODEL_COLOR_CONTROL);
1532         gpu_get_print("GL_LIGHT_MODEL_LOCAL_VIEWER", GL_LIGHT_MODEL_LOCAL_VIEWER);
1533         gpu_get_print("GL_LIGHT_MODEL_TWO_SIDE", GL_LIGHT_MODEL_TWO_SIDE);
1534         gpu_get_print("GL_LINE_SMOOTH", GL_LINE_SMOOTH);
1535         gpu_get_print("GL_LINE_SMOOTH_HINT", GL_LINE_SMOOTH_HINT);
1536         gpu_get_print("GL_LINE_STIPPLE", GL_LINE_STIPPLE);
1537         gpu_get_print("GL_LINE_STIPPLE_PATTERN", GL_LINE_STIPPLE_PATTERN);
1538         gpu_get_print("GL_LINE_STIPPLE_REPEAT", GL_LINE_STIPPLE_REPEAT);
1539         gpu_get_print("GL_LINE_WIDTH", GL_LINE_WIDTH);
1540         gpu_get_print("GL_LINE_WIDTH_GRANULARITY", GL_LINE_WIDTH_GRANULARITY);
1541         gpu_get_print("GL_LINE_WIDTH_RANGE", GL_LINE_WIDTH_RANGE);
1542         gpu_get_print("GL_LIST_BASE", GL_LIST_BASE);
1543         gpu_get_print("GL_LIST_INDEX", GL_LIST_INDEX);
1544         gpu_get_print("GL_LIST_MODE", GL_LIST_MODE);
1545         gpu_get_print("GL_LOGIC_OP_MODE", GL_LOGIC_OP_MODE);
1546         gpu_get_print("GL_MAP1_COLOR_4", GL_MAP1_COLOR_4);
1547         gpu_get_print("GL_MAP1_GRID_DOMAIN", GL_MAP1_GRID_DOMAIN);
1548         gpu_get_print("GL_MAP1_GRID_SEGMENTS", GL_MAP1_GRID_SEGMENTS);
1549         gpu_get_print("GL_MAP1_INDEX", GL_MAP1_INDEX);
1550         gpu_get_print("GL_MAP1_NORMAL", GL_MAP1_NORMAL);
1551         gpu_get_print("GL_MAP1_TEXTURE_COORD_1", GL_MAP1_TEXTURE_COORD_1);
1552         gpu_get_print("GL_MAP1_TEXTURE_COORD_2", GL_MAP1_TEXTURE_COORD_2);
1553         gpu_get_print("GL_MAP1_TEXTURE_COORD_3", GL_MAP1_TEXTURE_COORD_3);
1554         gpu_get_print("GL_MAP1_TEXTURE_COORD_4", GL_MAP1_TEXTURE_COORD_4);
1555         gpu_get_print("GL_MAP1_VERTEX_3", GL_MAP1_VERTEX_3);
1556         gpu_get_print("GL_MAP1_VERTEX_4", GL_MAP1_VERTEX_4);
1557         gpu_get_print("GL_MAP2_COLOR_4", GL_MAP2_COLOR_4);
1558         gpu_get_print("GL_MAP2_GRID_DOMAIN", GL_MAP2_GRID_DOMAIN);
1559         gpu_get_print("GL_MAP2_GRID_SEGMENTS", GL_MAP2_GRID_SEGMENTS);
1560         gpu_get_print("GL_MAP2_INDEX", GL_MAP2_INDEX);
1561         gpu_get_print("GL_MAP2_NORMAL", GL_MAP2_NORMAL);
1562         gpu_get_print("GL_MAP2_TEXTURE_COORD_1", GL_MAP2_TEXTURE_COORD_1);
1563         gpu_get_print("GL_MAP2_TEXTURE_COORD_2", GL_MAP2_TEXTURE_COORD_2);
1564         gpu_get_print("GL_MAP2_TEXTURE_COORD_3", GL_MAP2_TEXTURE_COORD_3);
1565         gpu_get_print("GL_MAP2_TEXTURE_COORD_4", GL_MAP2_TEXTURE_COORD_4);
1566         gpu_get_print("GL_MAP2_VERTEX_3", GL_MAP2_VERTEX_3);
1567         gpu_get_print("GL_MAP2_VERTEX_4", GL_MAP2_VERTEX_4);
1568         gpu_get_print("GL_MAP_COLOR", GL_MAP_COLOR);
1569         gpu_get_print("GL_MAP_STENCIL", GL_MAP_STENCIL);
1570         gpu_get_print("GL_MATRIX_MODE", GL_MATRIX_MODE);
1571         gpu_get_print("GL_MAX_3D_TEXTURE_SIZE", GL_MAX_3D_TEXTURE_SIZE);
1572         gpu_get_print("GL_MAX_CLIENT_ATTRIB_STACK_DEPTH", GL_MAX_CLIENT_ATTRIB_STACK_DEPTH);
1573         gpu_get_print("GL_MAX_ATTRIB_STACK_DEPTH", GL_MAX_ATTRIB_STACK_DEPTH);
1574         gpu_get_print("GL_MAX_CLIP_PLANES", GL_MAX_CLIP_PLANES);
1575         gpu_get_print("GL_MAX_COLOR_MATRIX_STACK_DEPTH", GL_MAX_COLOR_MATRIX_STACK_DEPTH);
1576         gpu_get_print("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS", GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1577         gpu_get_print("GL_MAX_CUBE_MAP_TEXTURE_SIZE", GL_MAX_CUBE_MAP_TEXTURE_SIZE);
1578         gpu_get_print("GL_MAX_DRAW_BUFFERS", GL_MAX_DRAW_BUFFERS);
1579         gpu_get_print("GL_MAX_ELEMENTS_INDICES", GL_MAX_ELEMENTS_INDICES);
1580         gpu_get_print("GL_MAX_ELEMENTS_VERTICES", GL_MAX_ELEMENTS_VERTICES);
1581         gpu_get_print("GL_MAX_EVAL_ORDER", GL_MAX_EVAL_ORDER);
1582         gpu_get_print("GL_MAX_FRAGMENT_UNIFORM_COMPONENTS", GL_MAX_FRAGMENT_UNIFORM_COMPONENTS);
1583         gpu_get_print("GL_MAX_LIGHTS", GL_MAX_LIGHTS);
1584         gpu_get_print("GL_MAX_LIST_NESTING", GL_MAX_LIST_NESTING);
1585         gpu_get_print("GL_MAX_MODELVIEW_STACK_DEPTH", GL_MAX_MODELVIEW_STACK_DEPTH);
1586         gpu_get_print("GL_MAX_NAME_STACK_DEPTH", GL_MAX_NAME_STACK_DEPTH);
1587         gpu_get_print("GL_MAX_PIXEL_MAP_TABLE", GL_MAX_PIXEL_MAP_TABLE);
1588         gpu_get_print("GL_MAX_PROJECTION_STACK_DEPTH", GL_MAX_PROJECTION_STACK_DEPTH);
1589         gpu_get_print("GL_MAX_TEXTURE_COORDS", GL_MAX_TEXTURE_COORDS);
1590         gpu_get_print("GL_MAX_TEXTURE_IMAGE_UNITS", GL_MAX_TEXTURE_IMAGE_UNITS);
1591         gpu_get_print("GL_MAX_TEXTURE_LOD_BIAS", GL_MAX_TEXTURE_LOD_BIAS);
1592         gpu_get_print("GL_MAX_TEXTURE_SIZE", GL_MAX_TEXTURE_SIZE);
1593         gpu_get_print("GL_MAX_TEXTURE_STACK_DEPTH", GL_MAX_TEXTURE_STACK_DEPTH);
1594         gpu_get_print("GL_MAX_TEXTURE_UNITS", GL_MAX_TEXTURE_UNITS);
1595         gpu_get_print("GL_MAX_VARYING_FLOATS", GL_MAX_VARYING_FLOATS);
1596         gpu_get_print("GL_MAX_VERTEX_ATTRIBS", GL_MAX_VERTEX_ATTRIBS);
1597         gpu_get_print("GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS", GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1598         gpu_get_print("GL_MAX_VERTEX_UNIFORM_COMPONENTS", GL_MAX_VERTEX_UNIFORM_COMPONENTS);
1599         gpu_get_print("GL_MAX_VIEWPORT_DIMS", GL_MAX_VIEWPORT_DIMS);
1600         gpu_get_print("GL_MINMAX", GL_MINMAX);
1601         gpu_get_print("GL_MODELVIEW_MATRIX", GL_MODELVIEW_MATRIX);
1602         gpu_get_print("GL_MODELVIEW_STACK_DEPTH", GL_MODELVIEW_STACK_DEPTH);
1603         gpu_get_print("GL_NAME_STACK_DEPTH", GL_NAME_STACK_DEPTH);
1604         gpu_get_print("GL_NORMAL_ARRAY", GL_NORMAL_ARRAY);
1605         gpu_get_print("GL_NORMAL_ARRAY_BUFFER_BINDING", GL_NORMAL_ARRAY_BUFFER_BINDING);
1606         gpu_get_print("GL_NORMAL_ARRAY_STRIDE", GL_NORMAL_ARRAY_STRIDE);
1607         gpu_get_print("GL_NORMAL_ARRAY_TYPE", GL_NORMAL_ARRAY_TYPE);
1608         gpu_get_print("GL_NORMALIZE", GL_NORMALIZE);
1609         gpu_get_print("GL_NUM_COMPRESSED_TEXTURE_FORMATS", GL_NUM_COMPRESSED_TEXTURE_FORMATS);
1610         gpu_get_print("GL_PACK_ALIGNMENT", GL_PACK_ALIGNMENT);
1611         gpu_get_print("GL_PACK_IMAGE_HEIGHT", GL_PACK_IMAGE_HEIGHT);
1612         gpu_get_print("GL_PACK_LSB_FIRST", GL_PACK_LSB_FIRST);
1613         gpu_get_print("GL_PACK_ROW_LENGTH", GL_PACK_ROW_LENGTH);
1614         gpu_get_print("GL_PACK_SKIP_IMAGES", GL_PACK_SKIP_IMAGES);
1615         gpu_get_print("GL_PACK_SKIP_PIXELS", GL_PACK_SKIP_PIXELS);
1616         gpu_get_print("GL_PACK_SKIP_ROWS", GL_PACK_SKIP_ROWS);
1617         gpu_get_print("GL_PACK_SWAP_BYTES", GL_PACK_SWAP_BYTES);
1618         gpu_get_print("GL_PERSPECTIVE_CORRECTION_HINT", GL_PERSPECTIVE_CORRECTION_HINT);
1619         gpu_get_print("GL_PIXEL_MAP_A_TO_A_SIZE", GL_PIXEL_MAP_A_TO_A_SIZE);
1620         gpu_get_print("GL_PIXEL_MAP_B_TO_B_SIZE", GL_PIXEL_MAP_B_TO_B_SIZE);
1621         gpu_get_print("GL_PIXEL_MAP_G_TO_G_SIZE", GL_PIXEL_MAP_G_TO_G_SIZE);
1622         gpu_get_print("GL_PIXEL_MAP_I_TO_A_SIZE", GL_PIXEL_MAP_I_TO_A_SIZE);
1623         gpu_get_print("GL_PIXEL_MAP_I_TO_B_SIZE", GL_PIXEL_MAP_I_TO_B_SIZE);
1624         gpu_get_print("GL_PIXEL_MAP_I_TO_G_SIZE", GL_PIXEL_MAP_I_TO_G_SIZE);
1625         gpu_get_print("GL_PIXEL_MAP_I_TO_I_SIZE", GL_PIXEL_MAP_I_TO_I_SIZE);
1626         gpu_get_print("GL_PIXEL_MAP_I_TO_R_SIZE", GL_PIXEL_MAP_I_TO_R_SIZE);
1627         gpu_get_print("GL_PIXEL_MAP_R_TO_R_SIZE", GL_PIXEL_MAP_R_TO_R_SIZE);
1628         gpu_get_print("GL_PIXEL_MAP_S_TO_S_SIZE", GL_PIXEL_MAP_S_TO_S_SIZE);
1629         gpu_get_print("GL_PIXEL_PACK_BUFFER_BINDING", GL_PIXEL_PACK_BUFFER_BINDING);
1630         gpu_get_print("GL_PIXEL_UNPACK_BUFFER_BINDING", GL_PIXEL_UNPACK_BUFFER_BINDING);
1631         gpu_get_print("GL_POINT_DISTANCE_ATTENUATION", GL_POINT_DISTANCE_ATTENUATION);
1632         gpu_get_print("GL_POINT_FADE_THRESHOLD_SIZE", GL_POINT_FADE_THRESHOLD_SIZE);
1633         gpu_get_print("GL_POINT_SIZE", GL_POINT_SIZE);
1634         gpu_get_print("GL_POINT_SIZE_GRANULARITY", GL_POINT_SIZE_GRANULARITY);
1635         gpu_get_print("GL_POINT_SIZE_MAX", GL_POINT_SIZE_MAX);
1636         gpu_get_print("GL_POINT_SIZE_MIN", GL_POINT_SIZE_MIN);
1637         gpu_get_print("GL_POINT_SIZE_RANGE", GL_POINT_SIZE_RANGE);
1638         gpu_get_print("GL_POINT_SMOOTH", GL_POINT_SMOOTH);
1639         gpu_get_print("GL_POINT_SMOOTH_HINT", GL_POINT_SMOOTH_HINT);
1640         gpu_get_print("GL_POINT_SPRITE", GL_POINT_SPRITE);
1641         gpu_get_print("GL_POLYGON_MODE", GL_POLYGON_MODE);
1642         gpu_get_print("GL_POLYGON_OFFSET_FACTOR", GL_POLYGON_OFFSET_FACTOR);
1643         gpu_get_print("GL_POLYGON_OFFSET_UNITS", GL_POLYGON_OFFSET_UNITS);
1644         gpu_get_print("GL_POLYGON_OFFSET_FILL", GL_POLYGON_OFFSET_FILL);
1645         gpu_get_print("GL_POLYGON_OFFSET_LINE", GL_POLYGON_OFFSET_LINE);
1646         gpu_get_print("GL_POLYGON_OFFSET_POINT", GL_POLYGON_OFFSET_POINT);
1647         gpu_get_print("GL_POLYGON_SMOOTH", GL_POLYGON_SMOOTH);
1648         gpu_get_print("GL_POLYGON_SMOOTH_HINT", GL_POLYGON_SMOOTH_HINT);
1649         gpu_get_print("GL_POLYGON_STIPPLE", GL_POLYGON_STIPPLE);
1650         gpu_get_print("GL_POST_COLOR_MATRIX_COLOR_TABLE", GL_POST_COLOR_MATRIX_COLOR_TABLE);
1651         gpu_get_print("GL_POST_COLOR_MATRIX_RED_BIAS", GL_POST_COLOR_MATRIX_RED_BIAS);
1652         gpu_get_print("GL_POST_COLOR_MATRIX_GREEN_BIAS", GL_POST_COLOR_MATRIX_GREEN_BIAS);
1653         gpu_get_print("GL_POST_COLOR_MATRIX_BLUE_BIAS", GL_POST_COLOR_MATRIX_BLUE_BIAS);
1654         gpu_get_print("GL_POST_COLOR_MATRIX_ALPHA_BIAS", GL_POST_COLOR_MATRIX_ALPHA_BIAS);
1655         gpu_get_print("GL_POST_COLOR_MATRIX_RED_SCALE", GL_POST_COLOR_MATRIX_RED_SCALE);
1656         gpu_get_print("GL_POST_COLOR_MATRIX_GREEN_SCALE", GL_POST_COLOR_MATRIX_GREEN_SCALE);
1657         gpu_get_print("GL_POST_COLOR_MATRIX_BLUE_SCALE", GL_POST_COLOR_MATRIX_BLUE_SCALE);
1658         gpu_get_print("GL_POST_COLOR_MATRIX_ALPHA_SCALE", GL_POST_COLOR_MATRIX_ALPHA_SCALE);
1659         gpu_get_print("GL_POST_CONVOLUTION_COLOR_TABLE", GL_POST_CONVOLUTION_COLOR_TABLE);
1660         gpu_get_print("GL_POST_CONVOLUTION_RED_BIAS", GL_POST_CONVOLUTION_RED_BIAS);
1661         gpu_get_print("GL_POST_CONVOLUTION_GREEN_BIAS", GL_POST_CONVOLUTION_GREEN_BIAS);
1662         gpu_get_print("GL_POST_CONVOLUTION_BLUE_BIAS", GL_POST_CONVOLUTION_BLUE_BIAS);
1663         gpu_get_print("GL_POST_CONVOLUTION_ALPHA_BIAS", GL_POST_CONVOLUTION_ALPHA_BIAS);
1664         gpu_get_print("GL_POST_CONVOLUTION_RED_SCALE", GL_POST_CONVOLUTION_RED_SCALE);
1665         gpu_get_print("GL_POST_CONVOLUTION_GREEN_SCALE", GL_POST_CONVOLUTION_GREEN_SCALE);
1666         gpu_get_print("GL_POST_CONVOLUTION_BLUE_SCALE", GL_POST_CONVOLUTION_BLUE_SCALE);
1667         gpu_get_print("GL_POST_CONVOLUTION_ALPHA_SCALE", GL_POST_CONVOLUTION_ALPHA_SCALE);
1668         gpu_get_print("GL_PROJECTION_MATRIX", GL_PROJECTION_MATRIX);
1669         gpu_get_print("GL_PROJECTION_STACK_DEPTH", GL_PROJECTION_STACK_DEPTH);
1670         gpu_get_print("GL_READ_BUFFER", GL_READ_BUFFER);
1671         gpu_get_print("GL_RED_BIAS", GL_RED_BIAS);
1672         gpu_get_print("GL_RED_BITS", GL_RED_BITS);
1673         gpu_get_print("GL_RED_SCALE", GL_RED_SCALE);
1674         gpu_get_print("GL_RENDER_MODE", GL_RENDER_MODE);
1675         gpu_get_print("GL_RESCALE_NORMAL", GL_RESCALE_NORMAL);
1676         gpu_get_print("GL_RGBA_MODE", GL_RGBA_MODE);
1677         gpu_get_print("GL_SAMPLE_BUFFERS", GL_SAMPLE_BUFFERS);
1678         gpu_get_print("GL_SAMPLE_COVERAGE_VALUE", GL_SAMPLE_COVERAGE_VALUE);
1679         gpu_get_print("GL_SAMPLE_COVERAGE_INVERT", GL_SAMPLE_COVERAGE_INVERT);
1680         gpu_get_print("GL_SAMPLES", GL_SAMPLES);
1681         gpu_get_print("GL_SCISSOR_BOX", GL_SCISSOR_BOX);
1682         gpu_get_print("GL_SCISSOR_TEST", GL_SCISSOR_TEST);
1683         gpu_get_print("GL_SECONDARY_COLOR_ARRAY", GL_SECONDARY_COLOR_ARRAY);
1684         gpu_get_print("GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING", GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING);
1685         gpu_get_print("GL_SECONDARY_COLOR_ARRAY_SIZE", GL_SECONDARY_COLOR_ARRAY_SIZE);
1686         gpu_get_print("GL_SECONDARY_COLOR_ARRAY_STRIDE", GL_SECONDARY_COLOR_ARRAY_STRIDE);
1687         gpu_get_print("GL_SECONDARY_COLOR_ARRAY_TYPE", GL_SECONDARY_COLOR_ARRAY_TYPE);
1688         gpu_get_print("GL_SELECTION_BUFFER_SIZE", GL_SELECTION_BUFFER_SIZE);
1689         gpu_get_print("GL_SEPARABLE_2D", GL_SEPARABLE_2D);
1690         gpu_get_print("GL_SHADE_MODEL", GL_SHADE_MODEL);
1691         gpu_get_print("GL_SMOOTH_LINE_WIDTH_RANGE", GL_SMOOTH_LINE_WIDTH_RANGE);
1692         gpu_get_print("GL_SMOOTH_LINE_WIDTH_GRANULARITY", GL_SMOOTH_LINE_WIDTH_GRANULARITY);
1693         gpu_get_print("GL_SMOOTH_POINT_SIZE_RANGE", GL_SMOOTH_POINT_SIZE_RANGE);
1694         gpu_get_print("GL_SMOOTH_POINT_SIZE_GRANULARITY", GL_SMOOTH_POINT_SIZE_GRANULARITY);
1695         gpu_get_print("GL_STENCIL_BACK_FAIL", GL_STENCIL_BACK_FAIL);
1696         gpu_get_print("GL_STENCIL_BACK_FUNC", GL_STENCIL_BACK_FUNC);
1697         gpu_get_print("GL_STENCIL_BACK_PASS_DEPTH_FAIL", GL_STENCIL_BACK_PASS_DEPTH_FAIL);
1698         gpu_get_print("GL_STENCIL_BACK_PASS_DEPTH_PASS", GL_STENCIL_BACK_PASS_DEPTH_PASS);
1699         gpu_get_print("GL_STENCIL_BACK_REF", GL_STENCIL_BACK_REF);
1700         gpu_get_print("GL_STENCIL_BACK_VALUE_MASK", GL_STENCIL_BACK_VALUE_MASK);
1701         gpu_get_print("GL_STENCIL_BACK_WRITEMASK", GL_STENCIL_BACK_WRITEMASK);
1702         gpu_get_print("GL_STENCIL_BITS", GL_STENCIL_BITS);
1703         gpu_get_print("GL_STENCIL_CLEAR_VALUE", GL_STENCIL_CLEAR_VALUE);
1704         gpu_get_print("GL_STENCIL_FAIL", GL_STENCIL_FAIL);
1705         gpu_get_print("GL_STENCIL_FUNC", GL_STENCIL_FUNC);
1706         gpu_get_print("GL_STENCIL_PASS_DEPTH_FAIL", GL_STENCIL_PASS_DEPTH_FAIL);
1707         gpu_get_print("GL_STENCIL_PASS_DEPTH_PASS", GL_STENCIL_PASS_DEPTH_PASS);
1708         gpu_get_print("GL_STENCIL_REF", GL_STENCIL_REF);
1709         gpu_get_print("GL_STENCIL_TEST", GL_STENCIL_TEST);
1710         gpu_get_print("GL_STENCIL_VALUE_MASK", GL_STENCIL_VALUE_MASK);
1711         gpu_get_print("GL_STENCIL_WRITEMASK", GL_STENCIL_WRITEMASK);
1712         gpu_get_print("GL_STEREO", GL_STEREO);
1713         gpu_get_print("GL_SUBPIXEL_BITS", GL_SUBPIXEL_BITS);
1714         gpu_get_print("GL_TEXTURE_1D", GL_TEXTURE_1D);
1715         gpu_get_print("GL_TEXTURE_BINDING_1D", GL_TEXTURE_BINDING_1D);
1716         gpu_get_print("GL_TEXTURE_2D", GL_TEXTURE_2D);
1717         gpu_get_print("GL_TEXTURE_BINDING_2D", GL_TEXTURE_BINDING_2D);
1718         gpu_get_print("GL_TEXTURE_3D", GL_TEXTURE_3D);
1719         gpu_get_print("GL_TEXTURE_BINDING_3D", GL_TEXTURE_BINDING_3D);
1720         gpu_get_print("GL_TEXTURE_BINDING_CUBE_MAP", GL_TEXTURE_BINDING_CUBE_MAP);
1721         gpu_get_print("GL_TEXTURE_COMPRESSION_HINT", GL_TEXTURE_COMPRESSION_HINT);
1722         gpu_get_print("GL_TEXTURE_COORD_ARRAY", GL_TEXTURE_COORD_ARRAY);
1723         gpu_get_print("GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING", GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING);
1724         gpu_get_print("GL_TEXTURE_COORD_ARRAY_SIZE", GL_TEXTURE_COORD_ARRAY_SIZE);
1725         gpu_get_print("GL_TEXTURE_COORD_ARRAY_STRIDE", GL_TEXTURE_COORD_ARRAY_STRIDE);
1726         gpu_get_print("GL_TEXTURE_COORD_ARRAY_TYPE", GL_TEXTURE_COORD_ARRAY_TYPE);
1727         gpu_get_print("GL_TEXTURE_CUBE_MAP", GL_TEXTURE_CUBE_MAP);
1728         gpu_get_print("GL_TEXTURE_GEN_Q", GL_TEXTURE_GEN_Q);
1729         gpu_get_print("GL_TEXTURE_GEN_R", GL_TEXTURE_GEN_R);
1730         gpu_get_print("GL_TEXTURE_GEN_S", GL_TEXTURE_GEN_S);
1731         gpu_get_print("GL_TEXTURE_GEN_T", GL_TEXTURE_GEN_T);
1732         gpu_get_print("GL_TEXTURE_MATRIX", GL_TEXTURE_MATRIX);
1733         gpu_get_print("GL_TEXTURE_STACK_DEPTH", GL_TEXTURE_STACK_DEPTH);
1734         gpu_get_print("GL_TRANSPOSE_COLOR_MATRIX", GL_TRANSPOSE_COLOR_MATRIX);
1735         gpu_get_print("GL_TRANSPOSE_MODELVIEW_MATRIX", GL_TRANSPOSE_MODELVIEW_MATRIX);
1736         gpu_get_print("GL_TRANSPOSE_PROJECTION_MATRIX", GL_TRANSPOSE_PROJECTION_MATRIX);
1737         gpu_get_print("GL_TRANSPOSE_TEXTURE_MATRIX", GL_TRANSPOSE_TEXTURE_MATRIX);
1738         gpu_get_print("GL_UNPACK_ALIGNMENT", GL_UNPACK_ALIGNMENT);
1739         gpu_get_print("GL_UNPACK_IMAGE_HEIGHT", GL_UNPACK_IMAGE_HEIGHT);
1740         gpu_get_print("GL_UNPACK_LSB_FIRST", GL_UNPACK_LSB_FIRST);
1741         gpu_get_print("GL_UNPACK_ROW_LENGTH", GL_UNPACK_ROW_LENGTH);
1742         gpu_get_print("GL_UNPACK_SKIP_IMAGES", GL_UNPACK_SKIP_IMAGES);
1743         gpu_get_print("GL_UNPACK_SKIP_PIXELS", GL_UNPACK_SKIP_PIXELS);
1744         gpu_get_print("GL_UNPACK_SKIP_ROWS", GL_UNPACK_SKIP_ROWS);
1745         gpu_get_print("GL_UNPACK_SWAP_BYTES", GL_UNPACK_SWAP_BYTES);
1746         gpu_get_print("GL_VERTEX_ARRAY", GL_VERTEX_ARRAY);
1747         gpu_get_print("GL_VERTEX_ARRAY_BUFFER_BINDING", GL_VERTEX_ARRAY_BUFFER_BINDING);
1748         gpu_get_print("GL_VERTEX_ARRAY_SIZE", GL_VERTEX_ARRAY_SIZE);
1749         gpu_get_print("GL_VERTEX_ARRAY_STRIDE", GL_VERTEX_ARRAY_STRIDE);
1750         gpu_get_print("GL_VERTEX_ARRAY_TYPE", GL_VERTEX_ARRAY_TYPE);
1751         gpu_get_print("GL_VERTEX_PROGRAM_POINT_SIZE", GL_VERTEX_PROGRAM_POINT_SIZE);
1752         gpu_get_print("GL_VERTEX_PROGRAM_TWO_SIDE", GL_VERTEX_PROGRAM_TWO_SIDE);
1753         gpu_get_print("GL_VIEWPORT", GL_VIEWPORT);
1754         gpu_get_print("GL_ZOOM_X", GL_ZOOM_X);
1755         gpu_get_print("GL_ZOOM_Y", GL_ZOOM_Y);
1756 }
1757