Blender  V2.59
KX_BlenderGL.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_BlenderGL.cpp 36083 2011-04-10 09:37:04Z campbellbarton $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include "KX_BlenderGL.h"
00035 
00036 /* 
00037  * This little block needed for linking to Blender... 
00038  */
00039 #ifdef WIN32
00040 #include <vector>
00041 #include "BLI_winstuff.h"
00042 #endif
00043 
00044 #include <stdlib.h>
00045 #include <string.h>
00046 
00047 #include "GL/glew.h"
00048 
00049 #include "MEM_guardedalloc.h"
00050 
00051 #include "BL_Material.h" // MAXTEX
00052 
00053 /* Data types encoding the game world: */
00054 #include "DNA_object_types.h"
00055 #include "DNA_scene_types.h"
00056 #include "DNA_screen_types.h"
00057 #include "DNA_camera_types.h"
00058 #include "DNA_world_types.h"
00059 #include "DNA_mesh_types.h"
00060 #include "DNA_meshdata_types.h"
00061 #include "DNA_image_types.h"
00062 #include "DNA_view3d_types.h"
00063 #include "DNA_material_types.h"
00064 #include "DNA_windowmanager_types.h"
00065 
00066 #include "BKE_global.h"
00067 #include "BKE_main.h"
00068 #include "BKE_bmfont.h"
00069 #include "BKE_image.h"
00070 
00071 #include "BLI_path_util.h"
00072 
00073 extern "C" {
00074 #include "IMB_imbuf_types.h"
00075 #include "IMB_imbuf.h"
00076 #include "WM_api.h"
00077 #include "WM_types.h"
00078 #include "wm_event_system.h"
00079 #include "wm_cursors.h"
00080 #include "wm_window.h"
00081 #include "BLF_api.h"
00082 }
00083 
00084 /* end of blender block */
00085 void BL_warp_pointer(wmWindow *win, int x,int y)
00086 {
00087         WM_cursor_warp(win, x, y);
00088 }
00089 
00090 void BL_SwapBuffers(wmWindow *win)
00091 {
00092         wm_window_swap_buffers(win);
00093 }
00094 
00095 void DisableForText()
00096 {
00097         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
00098 
00099         if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
00100         if(glIsEnabled(GL_ALPHA_TEST)) glDisable(GL_ALPHA_TEST);
00101 
00102         if(glIsEnabled(GL_LIGHTING)) {
00103                 glDisable(GL_LIGHTING);
00104                 glDisable(GL_COLOR_MATERIAL);
00105         }
00106 
00107         if(GLEW_ARB_multitexture) {
00108                 for(int i=0; i<MAXTEX; i++) {
00109                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
00110 
00111                         if(GLEW_ARB_texture_cube_map)
00112                                 if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
00113                                         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
00114 
00115                         if(glIsEnabled(GL_TEXTURE_2D))
00116                                 glDisable(GL_TEXTURE_2D);
00117                 }
00118 
00119                 glActiveTextureARB(GL_TEXTURE0_ARB);
00120         }
00121         else {
00122                 if(GLEW_ARB_texture_cube_map)
00123                         if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
00124                                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
00125 
00126                 if(glIsEnabled(GL_TEXTURE_2D))
00127                         glDisable(GL_TEXTURE_2D);
00128         }
00129 }
00130 
00131 /* Print 3D text */
00132 void BL_print_game_line(int fontid, const char* text, int size, int dpi, float* color, double* mat, float aspect)
00133 {
00134         /* gl prepping */
00135         DisableForText();
00136 
00137         /* the actual drawing */
00138         glColor4fv(color);
00139 
00140         /* multiply the text matrix by the object matrix */
00141         BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
00142         BLF_matrix(fontid, mat);
00143 
00144         /* aspect is the inverse scale that allows you to increase */
00145         /* your resolution without sizing the final text size      */
00146         /* the bigger the size, the smaller the aspect             */
00147         BLF_aspect(fontid, aspect, aspect, aspect);
00148 
00149         BLF_size(fontid, size, dpi);
00150         BLF_position(fontid, 0, 0, 0);
00151         BLF_draw(fontid, (char *)text, strlen(text));
00152 
00153         BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
00154 }
00155 
00156 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
00157 {       
00158         /* gl prepping */
00159         DisableForText();
00160         glDisable(GL_DEPTH_TEST);
00161 
00162         glMatrixMode(GL_PROJECTION);
00163         glPushMatrix();
00164         glLoadIdentity();
00165 
00166         glOrtho(0, width, 0, height, -100, 100);
00167 
00168         glMatrixMode(GL_MODELVIEW);
00169         glPushMatrix();
00170         glLoadIdentity();
00171 
00172         /* the actual drawing */
00173         glColor3ub(255, 255, 255);
00174         BLF_draw_default((float)xco, (float)(height-yco), 0.0f, (char *)text, 65535); /* XXX, use real len */
00175 
00176         glMatrixMode(GL_PROJECTION);
00177         glPopMatrix();
00178         glMatrixMode(GL_MODELVIEW);
00179         glPopMatrix();
00180         glEnable(GL_DEPTH_TEST);
00181 }
00182 
00183 void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height)
00184 {
00185         /* This is a rather important line :( The gl-mode hasn't been left
00186          * behind quite as neatly as we'd have wanted to. I don't know
00187          * what cause it, though :/ .*/
00188         DisableForText();
00189         glDisable(GL_DEPTH_TEST);
00190 
00191         glMatrixMode(GL_PROJECTION);
00192         glPushMatrix();
00193         glLoadIdentity();
00194         
00195         glOrtho(0, width, 0, height, -100, 100);
00196         
00197         glMatrixMode(GL_MODELVIEW);
00198         glPushMatrix();
00199         glLoadIdentity();
00200 
00201         /* draw in black first*/
00202         glColor3ub(0, 0, 0);
00203         BLF_draw_default((float)(xco+2), (float)(height-yco-2), 0.0f, text, 65535); /* XXX, use real len */
00204         glColor3ub(255, 255, 255);
00205         BLF_draw_default((float)xco, (float)(height-yco), 0.0f, text, 65535); /* XXX, use real len */
00206 
00207         glMatrixMode(GL_PROJECTION);
00208         glPopMatrix();
00209         glMatrixMode(GL_MODELVIEW);
00210         glPopMatrix();
00211         glEnable(GL_DEPTH_TEST);
00212 }
00213 
00214 void BL_HideMouse(wmWindow *win)
00215 {
00216         WM_cursor_set(win, CURSOR_NONE);
00217 }
00218 
00219 
00220 void BL_WaitMouse(wmWindow *win)
00221 {
00222         WM_cursor_set(win, CURSOR_WAIT);
00223 }
00224 
00225 
00226 void BL_NormalMouse(wmWindow *win)
00227 {
00228         WM_cursor_set(win, CURSOR_STD);
00229 }
00230 #define MAX_FILE_LENGTH 512
00231 
00232 /* get shot from frontbuffer sort of a copy from screendump.c */
00233 static unsigned int *screenshot(ScrArea *curarea, int *dumpsx, int *dumpsy)
00234 {
00235         int x=0, y=0;
00236         unsigned int *dumprect= NULL;
00237         
00238         x= curarea->totrct.xmin;
00239         y= curarea->totrct.ymin;
00240         *dumpsx= curarea->totrct.xmax-x;
00241         *dumpsy= curarea->totrct.ymax-y;
00242 
00243         if (*dumpsx && *dumpsy) {
00244                 
00245                 dumprect= (unsigned int *)MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
00246                 glReadBuffer(GL_FRONT);
00247                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
00248                 glFinish();
00249                 glReadBuffer(GL_BACK);
00250         }
00251 
00252         return dumprect;
00253 }
00254 
00255 /* based on screendump.c::screenshot_exec */
00256 void BL_MakeScreenShot(ScrArea *curarea, const char* filename)
00257 {
00258         char path[MAX_FILE_LENGTH];
00259         strcpy(path,filename);
00260 
00261         unsigned int *dumprect;
00262         int dumpsx, dumpsy;
00263         
00264         dumprect= screenshot(curarea, &dumpsx, &dumpsy);
00265         if(dumprect) {
00266                 ImBuf *ibuf;
00267                 BLI_path_abs(path, G.main->name);
00268                 /* BKE_add_image_extension() checks for if extension was already set */
00269                 BKE_add_image_extension(path, R_PNG); /* scene->r.imtype */
00270                 ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0);
00271                 ibuf->rect= dumprect;
00272                 ibuf->ftype= PNG;
00273 
00274                 IMB_saveiff(ibuf, path, IB_rect);
00275 
00276                 ibuf->rect= NULL;
00277                 IMB_freeImBuf(ibuf);
00278                 MEM_freeN(dumprect);
00279         }
00280 }
00281