soc-2008-mxcurioni: merged changes to revision 15441
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderGL.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "KX_BlenderGL.h"
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 /* 
36  * This little block needed for linking to Blender... 
37  */
38 #ifdef WIN32
39 #include "BLI_winstuff.h"
40 #endif
41
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "BMF_Api.h"
46
47 #include "GL/glew.h"
48 #include "BIF_gl.h"
49
50 #include "BL_Material.h" // MAXTEX
51
52 /* Data types encoding the game world: */
53 #include "DNA_object_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_world_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_meshdata_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_view3d_types.h"
62 #include "DNA_material_types.h"
63
64 #include "BKE_global.h"
65 #include "BKE_bmfont.h"
66 #include "BKE_image.h"
67
68 extern "C" {
69 #include "BDR_drawmesh.h"
70 #include "BIF_mywindow.h"
71 #include "BIF_toolbox.h"
72 #include "BIF_graphics.h" /* For CURSOR_NONE CURSOR_WAIT CURSOR_STD */
73
74 }
75
76 /* end of blender block */
77
78 /* was in drawmesh.c */
79 void spack(unsigned int ucol)
80 {
81         char *cp= (char *)&ucol;
82         
83         glColor3ub(cp[3], cp[2], cp[1]);
84 }
85
86 void BL_warp_pointer(int x,int y)
87 {
88         warp_pointer(x,y);
89 }
90
91 void BL_SwapBuffers()
92 {
93         myswapbuffers();
94 }
95
96 void BL_RenderText(int mode,const char* textstr,int textlen,struct MTFace* tface,
97                    unsigned int *col,float v1[3],float v2[3],float v3[3],float v4[3])
98 {
99         Image* ima;
100
101         if(mode & TF_BMFONT) {
102                         //char string[MAX_PROPSTRING];
103                         int characters, index, character;
104                         float centerx, centery, sizex, sizey, transx, transy, movex, movey, advance;
105                         
106 //                      bProperty *prop;
107
108                         // string = "Frank van Beek";
109
110                         characters = textlen;
111
112                         ima = (struct Image*) tface->tpage;
113                         if (ima == NULL) {
114                                 characters = 0;
115                         }
116
117                         /* When OBCOL flag is on the color is set in IndexPrimitives_3DText */                  
118                         if (tface->mode & TF_OBCOL) { /* Color has been set */
119                                 col= NULL;
120                         } else {
121                                 if(!col) glColor3f(1.0f, 1.0f, 1.0f);                   
122                         }
123
124                         glPushMatrix();
125                         for (index = 0; index < characters; index++) {
126                                 // lets calculate offset stuff
127                                 character = textstr[index];
128                                 
129                                 // space starts at offset 1
130                                 // character = character - ' ' + 1;
131                                 
132                                 matrixGlyph((ImBuf *)ima->ibufs.first, character, & centerx, &centery, &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
133
134                                 glBegin(GL_POLYGON);
135                                 // printf(" %c %f %f %f %f\n", character, tface->uv[0][0], tface->uv[0][1], );
136                                 // glTexCoord2f((tface->uv[0][0] - centerx) * sizex + transx, (tface->uv[0][1] - centery) * sizey + transy);
137                                 glTexCoord2f((tface->uv[0][0] - centerx) * sizex + transx, (tface->uv[0][1] - centery) * sizey + transy);
138
139                                 if(col) spack(col[0]);
140                                 // glVertex3fv(v1);
141                                 glVertex3f(sizex * v1[0] + movex, sizey * v1[1] + movey, v1[2]);
142                                 
143                                 glTexCoord2f((tface->uv[1][0] - centerx) * sizex + transx, (tface->uv[1][1] - centery) * sizey + transy);
144                                 if(col) spack(col[1]);
145                                 // glVertex3fv(v2);
146                                 glVertex3f(sizex * v2[0] + movex, sizey * v2[1] + movey, v2[2]);
147         
148                                 glTexCoord2f((tface->uv[2][0] - centerx) * sizex + transx, (tface->uv[2][1] - centery) * sizey + transy);
149                                 if(col) spack(col[2]);
150                                 // glVertex3fv(v3);
151                                 glVertex3f(sizex * v3[0] + movex, sizey * v3[1] + movey, v3[2]);
152         
153                                 if(v4) {
154                                         // glTexCoord2f((tface->uv[3][0] - centerx) * sizex + transx, 1.0 - (1.0 - tface->uv[3][1]) * sizey - transy);
155                                         glTexCoord2f((tface->uv[3][0] - centerx) * sizex + transx, (tface->uv[3][1] - centery) * sizey + transy);
156                                         if(col) spack(col[3]);
157                                         // glVertex3fv(v4);
158                                         glVertex3f(sizex * v4[0] + movex, sizey * v4[1] + movey, v4[2]);
159                                 }
160                                 glEnd();
161
162                                 glTranslatef(advance, 0.0, 0.0);
163                         }
164                         glPopMatrix();
165
166                 }
167 }
168
169
170 void DisableForText()
171 {
172         if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
173
174         if(glIsEnabled(GL_LIGHTING)) {
175                 glDisable(GL_LIGHTING);
176                 glDisable(GL_COLOR_MATERIAL);
177         }
178
179         if(GLEW_ARB_multitexture)
180                 for(int i=0; i<MAXTEX; i++)
181                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
182
183         if(GLEW_ARB_texture_cube_map)
184                 if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
185                         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
186
187         if(glIsEnabled(GL_TEXTURE_2D))
188                 glDisable(GL_TEXTURE_2D);
189 }
190
191 void BL_print_gamedebug_line(char* text, int xco, int yco, int width, int height)
192 {       
193         /* gl prepping */
194         DisableForText();
195         //glDisable(GL_TEXTURE_2D);
196
197         glMatrixMode(GL_PROJECTION);
198         glPushMatrix();
199         glLoadIdentity();
200         
201         glOrtho(0, width,
202                         0, height, 0, 1);
203         
204         glMatrixMode(GL_MODELVIEW);
205         glPushMatrix();
206         glLoadIdentity();
207         glMatrixMode(GL_TEXTURE);
208         glPushMatrix();
209         glLoadIdentity();
210
211         /* the actual drawing */
212         glColor3ub(255, 255, 255);
213         glRasterPos2s(xco, height-yco);
214         BMF_DrawString(G.fonts, text);
215
216         glMatrixMode(GL_TEXTURE);
217         glPopMatrix();
218         glMatrixMode(GL_PROJECTION);
219         glPopMatrix();
220         glMatrixMode(GL_MODELVIEW);
221         glPopMatrix();
222         glEnable(GL_DEPTH_TEST);
223 }
224
225 void BL_print_gamedebug_line_padded(char* text, int xco, int yco, int width, int height)
226 {
227         /* This is a rather important line :( The gl-mode hasn't been left
228          * behind quite as neatly as we'd have wanted to. I don't know
229          * what cause it, though :/ .*/
230         DisableForText();
231         //glDisable(GL_TEXTURE_2D);
232
233         glMatrixMode(GL_PROJECTION);
234         glPushMatrix();
235         glLoadIdentity();
236         
237         glOrtho(0, width,
238                         0, height, 0, 1);
239         
240         glMatrixMode(GL_MODELVIEW);
241         glPushMatrix();
242         glLoadIdentity();
243         glMatrixMode(GL_TEXTURE);
244         glPushMatrix();
245         glLoadIdentity();
246
247         /* draw in black first*/
248         glColor3ub(0, 0, 0);
249         glRasterPos2s(xco+1, height-yco-1);
250         BMF_DrawString(G.fonts, text);
251         glColor3ub(255, 255, 255);
252         glRasterPos2s(xco, height-yco);
253         BMF_DrawString(G.fonts, text);
254
255         glMatrixMode(GL_TEXTURE);
256         glPopMatrix();
257         glMatrixMode(GL_PROJECTION);
258         glPopMatrix();
259         glMatrixMode(GL_MODELVIEW);
260         glPopMatrix();
261         glEnable(GL_DEPTH_TEST);
262 }
263
264 void BL_HideMouse()
265 {
266         set_cursor(CURSOR_NONE);
267 }
268
269
270 void BL_WaitMouse()
271 {
272         set_cursor(CURSOR_WAIT);
273 }
274
275
276 void BL_NormalMouse()
277 {
278         set_cursor(CURSOR_STD);
279 }
280 #define MAX_FILE_LENGTH 512
281
282
283 void BL_MakeScreenShot(struct ScrArea *area, const char* filename)
284 {
285         char copyfilename[MAX_FILE_LENGTH];
286         strcpy(copyfilename,filename);
287
288         // filename read - only
289         
290           /* XXX will need to change at some point */
291         BIF_screendump(0);
292
293         // write+read filename
294         write_screendump((char*) copyfilename);
295 }
296