Added Dump 3dView and Dump Screen to the File menu
[blender.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
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
67 #include "BIF_gl.h"
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 #ifdef __APPLE__
79 #include <OpenGL/gl.h>
80 #else
81 #include <GL/gl.h>
82 #endif
83
84
85 void BL_warp_pointer(int x,int y)
86 {
87         warp_pointer(x,y);
88 }
89
90 void BL_SwapBuffers()
91 {
92         myswapbuffers();
93 }
94
95 void BL_RenderText(int mode,const char* textstr,int textlen,struct TFace* tface,
96                                    float v1[3],float v2[3],float v3[3],float v4[3])
97 {
98         Image* ima;
99
100         if(mode & TF_BMFONT) {
101                         //char string[MAX_PROPSTRING];
102                         int characters, index, character;
103                         float centerx, centery, sizex, sizey, transx, transy, movex, movey, advance;
104                         
105 //                      bProperty *prop;
106
107                         // string = "Frank van Beek";
108
109                         characters = textlen;
110
111                         ima = (struct Image*) tface->tpage;
112                         if (ima == NULL) {
113                                 characters = 0;
114                         }
115
116                         glPushMatrix();
117                         for (index = 0; index < characters; index++) {
118                                 // lets calculate offset stuff
119                                 character = textstr[index];
120                                 
121                                 // space starts at offset 1
122                                 // character = character - ' ' + 1;
123                                 
124                                 matrixGlyph(ima->ibuf, character, & centerx, &centery, &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
125                                 
126                                 glBegin(GL_POLYGON);
127                                 // printf(" %c %f %f %f %f\n", character, tface->uv[0][0], tface->uv[0][1], );
128                                 // glTexCoord2f((tface->uv[0][0] - centerx) * sizex + transx, (tface->uv[0][1] - centery) * sizey + transy);
129                                 glTexCoord2f((tface->uv[0][0] - centerx) * sizex + transx, (tface->uv[0][1] - centery) * sizey + transy);
130
131                                 spack(tface->col[0]);
132                                 // glVertex3fv(v1);
133                                 glVertex3f(sizex * v1[0] + movex, sizey * v1[1] + movey, v1[2]);
134                                 
135                                 glTexCoord2f((tface->uv[1][0] - centerx) * sizex + transx, (tface->uv[1][1] - centery) * sizey + transy);
136                                 spack(tface->col[1]);
137                                 // glVertex3fv(v2);
138                                 glVertex3f(sizex * v2[0] + movex, sizey * v2[1] + movey, v2[2]);
139         
140                                 glTexCoord2f((tface->uv[2][0] - centerx) * sizex + transx, (tface->uv[2][1] - centery) * sizey + transy);
141                                 spack(tface->col[2]);
142                                 // glVertex3fv(v3);
143                                 glVertex3f(sizex * v3[0] + movex, sizey * v3[1] + movey, v3[2]);
144         
145                                 if(v4) {
146                                         // glTexCoord2f((tface->uv[3][0] - centerx) * sizex + transx, 1.0 - (1.0 - tface->uv[3][1]) * sizey - transy);
147                                         glTexCoord2f((tface->uv[3][0] - centerx) * sizex + transx, (tface->uv[3][1] - centery) * sizey + transy);
148                                         spack(tface->col[3]);
149                                         // glVertex3fv(v4);
150                                         glVertex3f(sizex * v4[0] + movex, sizey * v4[1] + movey, v4[2]);
151                                 }
152                                 glEnd();
153
154                                 glTranslatef(advance, 0.0, 0.0);
155                         }
156                         glPopMatrix();
157
158                 }
159 }
160
161
162 void BL_print_gamedebug_line(char* text, int xco, int yco, int width, int height)
163 {       
164         /* gl prepping */
165         glDisable(GL_TEXTURE_2D);
166
167         glMatrixMode(GL_PROJECTION);
168         glPushMatrix();
169         glLoadIdentity();
170         
171         glOrtho(0, width,
172                         0, height, 0, 1);
173         
174         glMatrixMode(GL_MODELVIEW);
175         glPushMatrix();
176         glLoadIdentity();
177         glMatrixMode(GL_TEXTURE);
178         glPushMatrix();
179         glLoadIdentity();
180
181         /* the actual drawing */
182         glColor3ub(255, 255, 255);
183         glRasterPos2s(xco, height-yco);
184         BMF_DrawString(G.fonts, text);
185
186         glMatrixMode(GL_TEXTURE);
187         glPopMatrix();
188         glMatrixMode(GL_PROJECTION);
189         glPopMatrix();
190         glMatrixMode(GL_MODELVIEW);
191         glPopMatrix();
192         glEnable(GL_DEPTH_TEST);
193 }
194
195 void BL_print_gamedebug_line_padded(char* text, int xco, int yco, int width, int height)
196 {
197         /* This is a rather important line :( The gl-mode hasn't been left
198          * behind quite as neatly as we'd have wanted to. I don't know
199          * what cause it, though :/ .*/
200         glDisable(GL_TEXTURE_2D);
201
202         glMatrixMode(GL_PROJECTION);
203         glPushMatrix();
204         glLoadIdentity();
205         
206         glOrtho(0, width,
207                         0, height, 0, 1);
208         
209         glMatrixMode(GL_MODELVIEW);
210         glPushMatrix();
211         glLoadIdentity();
212         glMatrixMode(GL_TEXTURE);
213         glPushMatrix();
214         glLoadIdentity();
215
216         /* draw in black first*/
217         glColor3ub(0, 0, 0);
218         glRasterPos2s(xco+1, height-yco-1);
219         BMF_DrawString(G.fonts, text);
220         glColor3ub(255, 255, 255);
221         glRasterPos2s(xco, height-yco);
222         BMF_DrawString(G.fonts, text);
223
224         glMatrixMode(GL_TEXTURE);
225         glPopMatrix();
226         glMatrixMode(GL_PROJECTION);
227         glPopMatrix();
228         glMatrixMode(GL_MODELVIEW);
229         glPopMatrix();
230         glEnable(GL_DEPTH_TEST);
231 }
232
233 void BL_HideMouse()
234 {
235         set_cursor(CURSOR_NONE);
236 }
237
238
239 void BL_WaitMouse()
240 {
241         set_cursor(CURSOR_WAIT);
242 }
243
244
245 void BL_NormalMouse()
246 {
247         set_cursor(CURSOR_STD);
248 }
249 #define MAX_FILE_LENGTH 512
250
251
252 void BL_MakeScreenShot(struct ScrArea *area, const char* filename)
253 {
254         char copyfilename[MAX_FILE_LENGTH];
255         strcpy(copyfilename,filename);
256
257         // filename read - only
258         
259           /* XXX will need to change at some point */
260         BIF_screendump(0);
261
262         // write+read filename
263         write_screendump((char*) copyfilename);
264 }
265