Blender V2.61 - r43446

KX_BlenderGL.cpp

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