blender/source/gameengine/BlenderRoutines/KX_BlenderGL.cpp

282 lines
6.7 KiB
C++
Raw Normal View History

/*
2002-10-12 11:37:38 +00:00
* $Id$
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2002-10-12 11:37:38 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
/** \file gameengine/BlenderRoutines/KX_BlenderGL.cpp
* \ingroup blroutines
*/
2002-10-12 11:37:38 +00:00
#include "KX_BlenderGL.h"
2002-10-12 11:37:38 +00:00
/*
* This little block needed for linking to Blender...
*/
#ifdef WIN32
#include <vector>
2002-10-12 11:37:38 +00:00
#include "BLI_winstuff.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "GL/glew.h"
2002-10-12 11:37:38 +00:00
#include "MEM_guardedalloc.h"
#include "BL_Material.h" // MAXTEX
2002-10-12 11:37:38 +00:00
/* Data types encoding the game world: */
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_camera_types.h"
#include "DNA_world_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
2002-10-12 11:37:38 +00:00
#include "DNA_image_types.h"
#include "DNA_view3d_types.h"
#include "DNA_material_types.h"
#include "DNA_windowmanager_types.h"
2002-10-12 11:37:38 +00:00
#include "BKE_global.h"
#include "BKE_main.h"
2002-10-12 11:37:38 +00:00
#include "BKE_bmfont.h"
#include "BKE_image.h"
2002-10-12 11:37:38 +00:00
#include "BLI_path_util.h"
2002-10-12 11:37:38 +00:00
extern "C" {
#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "WM_api.h"
#include "WM_types.h"
#include "wm_event_system.h"
#include "wm_cursors.h"
#include "wm_window.h"
#include "BLF_api.h"
2002-10-12 11:37:38 +00:00
}
/* end of blender block */
void BL_warp_pointer(wmWindow *win, int x,int y)
2002-10-12 11:37:38 +00:00
{
WM_cursor_warp(win, x, y);
2002-10-12 11:37:38 +00:00
}
void BL_SwapBuffers(wmWindow *win)
2002-10-12 11:37:38 +00:00
{
wm_window_swap_buffers(win);
2002-10-12 11:37:38 +00:00
}
void DisableForText()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
if(glIsEnabled(GL_ALPHA_TEST)) glDisable(GL_ALPHA_TEST);
if(glIsEnabled(GL_LIGHTING)) {
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
}
if(GLEW_ARB_multitexture) {
for(int i=0; i<MAXTEX; i++) {
Merge of apricot branch game engine changes into trunk, excluding GLSL. GLEW ==== Added the GLEW opengl extension library into extern/, always compiled into Blender now. This is much nicer than doing this kind of extension management manually, and will be used in the game engine, for GLSL, and other opengl extensions. * According to the GLEW website it works on Windows, Linux, Mac OS X, FreeBSD, Irix, and Solaris. There might still be platform specific issues due to this commit, so let me know and I'll look into it. * This means also that all extensions will now always be compiled in, regardless of the glext.h on the platform where compilation happens. Game Engine =========== Refactoring of the use of opengl extensions and other drawing code in the game engine, and cleaning up some hacks related to GLSL integration. These changes will be merged into trunk too after this. The game engine graphics demos & apricot level survived my tests, but this could use some good testing of course. For users: please test with the options "Generate Display Lists" and "Vertex Arrays" enabled, these should be the fastest and are supposed to be "unreliable", but if that's the case that's probably due to bugs that can be fixed. * The game engine now also uses GLEW for extensions, replacing the custom opengl extensions code that was there. Removes a lot of #ifdef's, but the runtime checks stay of course. * Removed the WITHOUT_GLEXT environment variable. This was added to work around a specific bug and only disabled multitexturing anyway. It might also have caused a slowdown since it was retrieving the environment variable for every vertex in immediate mode (bug #13680). * Refactored the code to allow drawing skinned meshes with vertex arrays too, removing some specific immediate mode drawing functions for this that only did extra normal calculation. Now it always splits vertices of flat faces instead. * Refactored normal recalculation with some minor optimizations, required for the above change. * Removed some outdated code behind the __NLA_OLDDEFORM #ifdef. * Fixed various bugs in setting of multitexture coordinates and vertex attributes for vertex arrays. These were not being enabled/disabled correct according to the opengl spec, leading to crashes. Also tangent attributes used an immediate mode call for vertex arrays, which can't work. * Fixed use of uninitialized variable in RAS_TexVert. * Exporting skinned meshes was doing O(n^2) lookups for vertices and deform weights, now uses same trick as regular meshes.
2008-06-17 10:27:34 +00:00
glActiveTextureARB(GL_TEXTURE0_ARB+i);
if(GLEW_ARB_texture_cube_map)
if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
if(glIsEnabled(GL_TEXTURE_2D))
glDisable(GL_TEXTURE_2D);
}
glActiveTextureARB(GL_TEXTURE0_ARB);
}
else {
if(GLEW_ARB_texture_cube_map)
if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
if(glIsEnabled(GL_TEXTURE_2D))
glDisable(GL_TEXTURE_2D);
}
}
Patch:[#25163] BGE support for Blender Font objects - unicode support Problem/Bug: ------------ There were no way to have proper unicode characters (e.g. Japanese) in Blender Game Engine. Now we can :) You can see a sample here: http://blog.mikepan.com/multi-language-support-in-blender/ Functionality Explanation: -------------------------- This patch converts the Blender Font Objects to a new BGE type: KX_FontObject This object inherits KX_GameObject.cpp and has the following properties: - text (the text of the object) - size (taken from the Blender object, usually is 1.0) - resolution (1.0 by default, maybe not really needed, but at least for debugging/the time being it's nice to have) The way we deal with linked objects is different than Blender. In Blender the text and size are a property of the Text databock. Therefore linked objects necessarily share the same text (and size, although the size of the object datablock affects that too). In BGE they are stored and accessed per object. Without that it would be problematic to have addObject adding texts that don't share the same data. Known problems/limitations/ToDo: -------------------------------- 1) support for packed font and the <builtin> 2) figure why some fonts are displayed in a different size in 3DView/BGE (BLF) 3) investigate some glitches I see some times 4) support for multiline 5) support for more Blender Font Object options (text aligment, text boxes, ...) [1] Diego (bdiego) evantually will help on that. For the time being we are using the "default" (ui) font to replace the <builtin>. [2] but not all of them. I need to cross check who is calculating the size/dpi in/correctly - Blender or BLF. (e.g. fonts that work well - MS Gothic) [3] I think this may be related to the resolution we are drawing the font [4] It can't/will not be handled inside BFL. So the way I see it is to implement a mini text library/api that works as a middlelayer between the drawing step and BLF. So instead of: BLF_draw(fontid, (char *)text, strlen(text)); We would do: MAGIC_ROUTINE_IM_NOT_BLF_draw(fontir, (char *)text, styleflag, width, height); [5] don't hold your breath ... but if someone wants to have fun in the holidays the (4) and (5) are part of the same problem. Code Explanation: ----------------- The patch should be simple to read. They are three may parts: 1) BL_BlenderDataConversion.cpp:: converts the OB_FONT object into a KX_FontObject.cpp and store it in the KX_Scene->m_fonts 2) KetsjiEngine.cpp::RenderFonts:: loop through the texts and call their internal drawing routine. 3) KX_FontObject.cpp:: a) constructor: load the font of the object, and store other values. b) DrawText: calculate the aspect for the given size (sounds hacky but this is how blf works) and call the render routine in RenderTools 4) KX_BlenderGL.cpp (called from rendertools) ::BL_print_game_line:: Draws the text. Using the BLF API *) In order to handle visibility of the object added with AddObject I'm adding to the m_scene.m_fonts list only the Fonts in a visible layer - unlike Cameras and Lamps where all the objects are added. Acknowledgements: ---------------- Thanks Benoit for the review and adjustment suggestions. Thanks Diego for the BFL expertise, patches and support (Latin community ftw) Thanks my boss for letting me do part of this patch during work time. Good thing we are starting a project in a partnership with a Japanese Foundation and eventual will need unicode in BGE :) for more details on that - www.nereusprogram.org - let's call it the main sponsor of this "bug feature" ;)
2010-12-16 10:25:41 +00:00
/* Print 3D text */
void BL_print_game_line(int fontid, const char* text, int size, int dpi, float* color, double* mat, float aspect)
{
/* gl prepping */
DisableForText();
/* the actual drawing */
glColor4fv(color);
/* multiply the text matrix by the object matrix */
BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
BLF_matrix(fontid, mat);
/* aspect is the inverse scale that allows you to increase */
/* your resolution without sizing the final text size */
/* the bigger the size, the smaller the aspect */
BLF_aspect(fontid, aspect, aspect, aspect);
BLF_size(fontid, size, dpi);
BLF_position(fontid, 0, 0, 0);
BLF_draw(fontid, (char *)text, strlen(text));
BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
}
void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
2002-10-12 11:37:38 +00:00
{
/* gl prepping */
DisableForText();
glDisable(GL_DEPTH_TEST);
2002-10-12 11:37:38 +00:00
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, 0, height, -100, 100);
2002-10-12 11:37:38 +00:00
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
/* the actual drawing */
glColor3ub(255, 255, 255);
2011-04-10 09:37:04 +00:00
BLF_draw_default((float)xco, (float)(height-yco), 0.0f, (char *)text, 65535); /* XXX, use real len */
2002-10-12 11:37:38 +00:00
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_DEPTH_TEST);
}
void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height)
2002-10-12 11:37:38 +00:00
{
/* This is a rather important line :( The gl-mode hasn't been left
* behind quite as neatly as we'd have wanted to. I don't know
* what cause it, though :/ .*/
DisableForText();
glDisable(GL_DEPTH_TEST);
2002-10-12 11:37:38 +00:00
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, 0, height, -100, 100);
2002-10-12 11:37:38 +00:00
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
/* draw in black first*/
glColor3ub(0, 0, 0);
2011-04-10 09:37:04 +00:00
BLF_draw_default((float)(xco+2), (float)(height-yco-2), 0.0f, text, 65535); /* XXX, use real len */
2002-10-12 11:37:38 +00:00
glColor3ub(255, 255, 255);
2011-04-10 09:37:04 +00:00
BLF_draw_default((float)xco, (float)(height-yco), 0.0f, text, 65535); /* XXX, use real len */
2002-10-12 11:37:38 +00:00
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_DEPTH_TEST);
}
void BL_HideMouse(wmWindow *win)
2002-10-12 11:37:38 +00:00
{
WM_cursor_set(win, CURSOR_NONE);
2002-10-12 11:37:38 +00:00
}
void BL_WaitMouse(wmWindow *win)
2002-10-12 11:37:38 +00:00
{
WM_cursor_set(win, CURSOR_WAIT);
2002-10-12 11:37:38 +00:00
}
void BL_NormalMouse(wmWindow *win)
2002-10-12 11:37:38 +00:00
{
WM_cursor_set(win, CURSOR_STD);
2002-10-12 11:37:38 +00:00
}
#define MAX_FILE_LENGTH 512
/* get shot from frontbuffer sort of a copy from screendump.c */
static unsigned int *screenshot(ScrArea *curarea, int *dumpsx, int *dumpsy)
{
int x=0, y=0;
unsigned int *dumprect= NULL;
x= curarea->totrct.xmin;
y= curarea->totrct.ymin;
*dumpsx= curarea->totrct.xmax-x;
*dumpsy= curarea->totrct.ymax-y;
if (*dumpsx && *dumpsy) {
dumprect= (unsigned int *)MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
glReadBuffer(GL_FRONT);
glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
glFinish();
glReadBuffer(GL_BACK);
}
2002-10-12 11:37:38 +00:00
return dumprect;
}
/* based on screendump.c::screenshot_exec */
void BL_MakeScreenShot(ScrArea *curarea, const char* filename)
2002-10-12 11:37:38 +00:00
{
char path[MAX_FILE_LENGTH];
strcpy(path,filename);
2002-10-12 11:37:38 +00:00
unsigned int *dumprect;
int dumpsx, dumpsy;
2002-10-12 11:37:38 +00:00
dumprect= screenshot(curarea, &dumpsx, &dumpsy);
if(dumprect) {
ImBuf *ibuf;
BLI_path_abs(path, G.main->name);
/* BKE_add_image_extension() checks for if extension was already set */
BKE_add_image_extension(path, R_PNG); /* scene->r.imtype */
ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0);
ibuf->rect= dumprect;
ibuf->ftype= PNG;
IMB_saveiff(ibuf, path, IB_rect);
ibuf->rect= NULL;
IMB_freeImBuf(ibuf);
MEM_freeN(dumprect);
}
2002-10-12 11:37:38 +00:00
}