most unused arg warnings corrected.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 __cplusplus
32 extern "C" {
33 #endif
34 #include "BLF_api.h"
35 #ifdef __cplusplus
36 }
37 #endif
38
39 /* 
40  * This little block needed for linking to Blender... 
41  */
42 #ifdef WIN32
43 #include <vector>
44 #include "BLI_winstuff.h"
45 #endif
46
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "GL/glew.h"
51
52 #include "MEM_guardedalloc.h"
53
54 #include "BL_Material.h" // MAXTEX
55
56 /* Data types encoding the game world: */
57 #include "DNA_object_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_screen_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_world_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_meshdata_types.h"
64 #include "DNA_image_types.h"
65 #include "DNA_view3d_types.h"
66 #include "DNA_material_types.h"
67 #include "DNA_windowmanager_types.h"
68
69 #include "BKE_global.h"
70 #include "BKE_bmfont.h"
71 #include "BKE_image.h"
72
73 #include "BLI_path_util.h"
74
75 extern "C" {
76 #include "IMB_imbuf_types.h"
77 #include "IMB_imbuf.h"
78 #include "WM_api.h"
79 #include "WM_types.h"
80 #include "wm_event_system.h"
81 #include "wm_cursors.h"
82 #include "wm_window.h"
83 }
84
85 /* end of blender block */
86
87 /* was in drawmesh.c */
88 void spack(unsigned int ucol)
89 {
90         char *cp= (char *)&ucol;
91         
92         glColor3ub(cp[3], cp[2], cp[1]);
93 }
94
95 void BL_warp_pointer(wmWindow *win, int x,int y)
96 {
97         WM_cursor_warp(win, x, y);
98 }
99
100 void BL_SwapBuffers(wmWindow *win)
101 {
102         wm_window_swap_buffers(win);
103 }
104
105 void DisableForText()
106 {
107         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
108
109         if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
110         if(glIsEnabled(GL_ALPHA_TEST)) glDisable(GL_ALPHA_TEST);
111
112         if(glIsEnabled(GL_LIGHTING)) {
113                 glDisable(GL_LIGHTING);
114                 glDisable(GL_COLOR_MATERIAL);
115         }
116
117         if(GLEW_ARB_multitexture) {
118                 for(int i=0; i<MAXTEX; i++) {
119                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
120
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                 glActiveTextureARB(GL_TEXTURE0_ARB);
130         }
131         else {
132                 if(GLEW_ARB_texture_cube_map)
133                         if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
134                                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
135
136                 if(glIsEnabled(GL_TEXTURE_2D))
137                         glDisable(GL_TEXTURE_2D);
138         }
139 }
140
141 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
142 {       
143         /* gl prepping */
144         DisableForText();
145
146         glMatrixMode(GL_PROJECTION);
147         glPushMatrix();
148         glLoadIdentity();
149
150         glOrtho(0, width, 0, height, -100, 100);
151
152         glMatrixMode(GL_MODELVIEW);
153         glPushMatrix();
154         glLoadIdentity();
155
156         /* the actual drawing */
157         glColor3ub(255, 255, 255);
158         BLF_draw_default(xco, height-yco, 0.0f, (char *)text);
159
160         glMatrixMode(GL_PROJECTION);
161         glPopMatrix();
162         glMatrixMode(GL_MODELVIEW);
163         glPopMatrix();
164         glEnable(GL_DEPTH_TEST);
165 }
166
167 void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height)
168 {
169         /* This is a rather important line :( The gl-mode hasn't been left
170          * behind quite as neatly as we'd have wanted to. I don't know
171          * what cause it, though :/ .*/
172         DisableForText();
173
174         glMatrixMode(GL_PROJECTION);
175         glPushMatrix();
176         glLoadIdentity();
177         
178         glOrtho(0, width, 0, height, -100, 100);
179         
180         glMatrixMode(GL_MODELVIEW);
181         glPushMatrix();
182         glLoadIdentity();
183
184         /* draw in black first*/
185         glColor3ub(0, 0, 0);
186         BLF_draw_default(xco+2, height-yco-2, 0.0f, (char *)text);
187         glColor3ub(255, 255, 255);
188         BLF_draw_default(xco, height-yco, 0.0f, (char *)text);
189
190         glMatrixMode(GL_PROJECTION);
191         glPopMatrix();
192         glMatrixMode(GL_MODELVIEW);
193         glPopMatrix();
194         glEnable(GL_DEPTH_TEST);
195 }
196
197 void BL_HideMouse(wmWindow *win)
198 {
199         WM_cursor_set(win, CURSOR_NONE);
200 }
201
202
203 void BL_WaitMouse(wmWindow *win)
204 {
205         WM_cursor_set(win, CURSOR_WAIT);
206 }
207
208
209 void BL_NormalMouse(wmWindow *win)
210 {
211         WM_cursor_set(win, CURSOR_STD);
212 }
213 #define MAX_FILE_LENGTH 512
214
215 /* get shot from frontbuffer sort of a copy from screendump.c */
216 static unsigned int *screenshot(ScrArea *curarea, int *dumpsx, int *dumpsy)
217 {
218         int x=0, y=0;
219         unsigned int *dumprect= NULL;
220         
221         x= curarea->totrct.xmin;
222         y= curarea->totrct.ymin;
223         *dumpsx= curarea->totrct.xmax-x;
224         *dumpsy= curarea->totrct.ymax-y;
225
226         if (*dumpsx && *dumpsy) {
227                 
228                 dumprect= (unsigned int *)MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
229                 glReadBuffer(GL_FRONT);
230                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
231                 glFinish();
232                 glReadBuffer(GL_BACK);
233         }
234
235         return dumprect;
236 }
237
238 /* based on screendump.c::screenshot_exec */
239 void BL_MakeScreenShot(ScrArea *curarea, const char* filename)
240 {
241         char path[MAX_FILE_LENGTH];
242         strcpy(path,filename);
243
244         unsigned int *dumprect;
245         int dumpsx, dumpsy;
246         
247         dumprect= screenshot(curarea, &dumpsx, &dumpsy);
248         if(dumprect) {
249                 ImBuf *ibuf;
250                 BLI_path_abs(path, G.sce);
251                 /* BKE_add_image_extension() checks for if extension was already set */
252                 BKE_add_image_extension(path, R_PNG); /* scene->r.imtype */
253                 ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0);
254                 ibuf->rect= dumprect;
255                 ibuf->ftype= PNG;
256
257                 IMB_saveiff(ibuf, path, IB_rect);
258
259                 ibuf->rect= NULL;
260                 IMB_freeImBuf(ibuf);
261                 MEM_freeN(dumprect);
262         }
263 }
264