reverting commit [43876] Fix for aliased fonts in the game engine
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderGL.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/BlenderRoutines/KX_BlenderGL.cpp
29  *  \ingroup blroutines
30  */
31
32
33 #include "KX_BlenderGL.h"
34
35 /* 
36  * This little block needed for linking to Blender... 
37  */
38 #ifdef WIN32
39 #include <vector>
40 #include "BLI_winstuff.h"
41 #endif
42
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "GL/glew.h"
47
48 #include "MEM_guardedalloc.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 #include "DNA_windowmanager_types.h"
64
65 #include "BKE_global.h"
66 #include "BKE_main.h"
67 #include "BKE_bmfont.h"
68 #include "BKE_image.h"
69
70 #include "BLI_path_util.h"
71
72 extern "C" {
73 #include "IMB_imbuf_types.h"
74 #include "IMB_imbuf.h"
75 #include "WM_api.h"
76 #include "WM_types.h"
77 #include "wm_event_system.h"
78 #include "wm_cursors.h"
79 #include "wm_window.h"
80 #include "BLF_api.h"
81 }
82
83 /* end of blender block */
84 void BL_warp_pointer(wmWindow *win, int x,int y)
85 {
86         WM_cursor_warp(win, x, y);
87 }
88
89 void BL_SwapBuffers(wmWindow *win)
90 {
91         wm_window_swap_buffers(win);
92 }
93
94 void DisableForText()
95 {
96         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
97
98         if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
99         if(glIsEnabled(GL_ALPHA_TEST)) glDisable(GL_ALPHA_TEST);
100
101         if(glIsEnabled(GL_LIGHTING)) {
102                 glDisable(GL_LIGHTING);
103                 glDisable(GL_COLOR_MATERIAL);
104         }
105
106         if(GLEW_ARB_multitexture) {
107                 for(int i=0; i<MAXTEX; i++) {
108                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
109
110                         if(GLEW_ARB_texture_cube_map)
111                                 if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
112                                         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
113
114                         if(glIsEnabled(GL_TEXTURE_2D))
115                                 glDisable(GL_TEXTURE_2D);
116                 }
117
118                 glActiveTextureARB(GL_TEXTURE0_ARB);
119         }
120         else {
121                 if(GLEW_ARB_texture_cube_map)
122                         if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
123                                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
124
125                 if(glIsEnabled(GL_TEXTURE_2D))
126                         glDisable(GL_TEXTURE_2D);
127         }
128 }
129
130 /* Print 3D text */
131 void BL_print_game_line(int fontid, const char* text, int size, int dpi, float* color, double* mat, float aspect)
132 {
133         /* gl prepping */
134         DisableForText();
135
136         /* the actual drawing */
137         glColor4fv(color);
138
139         /* multiply the text matrix by the object matrix */
140         BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
141         BLF_matrix(fontid, mat);
142
143         /* aspect is the inverse scale that allows you to increase */
144         /* your resolution without sizing the final text size      */
145         /* the bigger the size, the smaller the aspect             */
146         BLF_aspect(fontid, aspect, aspect, aspect);
147
148         BLF_size(fontid, size, dpi);
149         BLF_position(fontid, 0, 0, 0);
150         BLF_draw(fontid, (char *)text, 65535);
151
152         BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
153 }
154
155 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
156 {       
157         /* gl prepping */
158         DisableForText();
159         glDisable(GL_DEPTH_TEST);
160
161         glMatrixMode(GL_PROJECTION);
162         glPushMatrix();
163         glLoadIdentity();
164
165         glOrtho(0, width, 0, height, -100, 100);
166
167         glMatrixMode(GL_MODELVIEW);
168         glPushMatrix();
169         glLoadIdentity();
170
171         /* the actual drawing */
172         glColor3ub(255, 255, 255);
173         BLF_draw_default((float)xco, (float)(height-yco), 0.0f, (char *)text, 65535); /* XXX, use real len */
174
175         glMatrixMode(GL_PROJECTION);
176         glPopMatrix();
177         glMatrixMode(GL_MODELVIEW);
178         glPopMatrix();
179         glEnable(GL_DEPTH_TEST);
180 }
181
182 void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height)
183 {
184         /* This is a rather important line :( The gl-mode hasn't been left
185          * behind quite as neatly as we'd have wanted to. I don't know
186          * what cause it, though :/ .*/
187         DisableForText();
188         glDisable(GL_DEPTH_TEST);
189
190         glMatrixMode(GL_PROJECTION);
191         glPushMatrix();
192         glLoadIdentity();
193         
194         glOrtho(0, width, 0, height, -100, 100);
195         
196         glMatrixMode(GL_MODELVIEW);
197         glPushMatrix();
198         glLoadIdentity();
199
200         /* draw in black first*/
201         glColor3ub(0, 0, 0);
202         BLF_draw_default((float)(xco+2), (float)(height-yco-2), 0.0f, text, 65535); /* XXX, use real len */
203         glColor3ub(255, 255, 255);
204         BLF_draw_default((float)xco, (float)(height-yco), 0.0f, text, 65535); /* XXX, use real len */
205
206         glMatrixMode(GL_PROJECTION);
207         glPopMatrix();
208         glMatrixMode(GL_MODELVIEW);
209         glPopMatrix();
210         glEnable(GL_DEPTH_TEST);
211 }
212
213 void BL_HideMouse(wmWindow *win)
214 {
215         WM_cursor_set(win, CURSOR_NONE);
216 }
217
218
219 void BL_WaitMouse(wmWindow *win)
220 {
221         WM_cursor_set(win, CURSOR_WAIT);
222 }
223
224
225 void BL_NormalMouse(wmWindow *win)
226 {
227         WM_cursor_set(win, CURSOR_STD);
228 }
229 #define MAX_FILE_LENGTH 512
230
231 /* get shot from frontbuffer sort of a copy from screendump.c */
232 static unsigned int *screenshot(ScrArea *curarea, int *dumpsx, int *dumpsy)
233 {
234         int x=0, y=0;
235         unsigned int *dumprect= NULL;
236         
237         x= curarea->totrct.xmin;
238         y= curarea->totrct.ymin;
239         *dumpsx= curarea->totrct.xmax-x;
240         *dumpsy= curarea->totrct.ymax-y;
241
242         if (*dumpsx && *dumpsy) {
243                 
244                 dumprect= (unsigned int *)MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
245                 glReadBuffer(GL_FRONT);
246                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
247                 glFinish();
248                 glReadBuffer(GL_BACK);
249         }
250
251         return dumprect;
252 }
253
254 /* based on screendump.c::screenshot_exec */
255 void BL_MakeScreenShot(ScrArea *curarea, const char* filename)
256 {
257         char path[MAX_FILE_LENGTH];
258         strcpy(path,filename);
259
260         unsigned int *dumprect;
261         int dumpsx, dumpsy;
262         
263         dumprect= screenshot(curarea, &dumpsx, &dumpsy);
264         if(dumprect) {
265                 ImBuf *ibuf;
266                 BLI_path_abs(path, G.main->name);
267                 /* BKE_add_image_extension() checks for if extension was already set */
268                 BKE_add_image_extension(path, R_IMF_IMTYPE_PNG); /* scene->r.im_format.imtype */
269                 ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0);
270                 ibuf->rect= dumprect;
271                 ibuf->ftype= PNG;
272
273                 IMB_saveiff(ibuf, path, IB_rect);
274
275                 ibuf->rect= NULL;
276                 IMB_freeImBuf(ibuf);
277                 MEM_freeN(dumprect);
278         }
279 }
280