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