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