|
Blender
V2.59
|
00001 /* 00002 * $Id: KX_BlenderRenderTools.cpp 35166 2011-02-25 13:29:48Z jesterking $ 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 "GL/glew.h" 00035 00036 #include "RAS_IRenderTools.h" 00037 #include "RAS_IRasterizer.h" 00038 #include "RAS_LightObject.h" 00039 #include "RAS_ICanvas.h" 00040 #include "RAS_GLExtensionManager.h" 00041 00042 #include "KX_GameObject.h" 00043 #include "KX_PolygonMaterial.h" 00044 #include "KX_BlenderMaterial.h" 00045 #include "KX_RayCast.h" 00046 #include "KX_IPhysicsController.h" 00047 #include "KX_Light.h" 00048 00049 #include "PHY_IPhysicsEnvironment.h" 00050 00051 #include "STR_String.h" 00052 00053 #include "GPU_draw.h" 00054 00055 #include "KX_BlenderGL.h" // for text printing 00056 #include "KX_BlenderRenderTools.h" 00057 00058 unsigned int KX_BlenderRenderTools::m_numgllights; 00059 00060 KX_BlenderRenderTools::KX_BlenderRenderTools() 00061 { 00062 glGetIntegerv(GL_MAX_LIGHTS, (GLint*) &m_numgllights); 00063 if (m_numgllights < 8) 00064 m_numgllights = 8; 00065 } 00066 00067 KX_BlenderRenderTools::~KX_BlenderRenderTools() 00068 { 00069 } 00070 00071 void KX_BlenderRenderTools::BeginFrame(RAS_IRasterizer* rasty) 00072 { 00073 m_clientobject = NULL; 00074 m_lastlightlayer = -1; 00075 m_lastauxinfo = NULL; 00076 m_lastlighting = true; /* force disable in DisableOpenGLLights() */ 00077 DisableOpenGLLights(); 00078 } 00079 00080 void KX_BlenderRenderTools::EndFrame(RAS_IRasterizer* rasty) 00081 { 00082 } 00083 00084 /* ProcessLighting performs lighting on objects. the layer is a bitfield that 00085 * contains layer information. There are 20 'official' layers in blender. A 00086 * light is applied on an object only when they are in the same layer. OpenGL 00087 * has a maximum of 8 lights (simultaneous), so 20 * 8 lights are possible in 00088 * a scene. */ 00089 00090 void KX_BlenderRenderTools::ProcessLighting(RAS_IRasterizer *rasty, bool uselights, const MT_Transform& viewmat) 00091 { 00092 bool enable = false; 00093 int layer= -1; 00094 00095 /* find the layer */ 00096 if(uselights) { 00097 if(m_clientobject) 00098 layer = static_cast<KX_GameObject*>(m_clientobject)->GetLayer(); 00099 } 00100 00101 /* avoid state switching */ 00102 if(m_lastlightlayer == layer && m_lastauxinfo == m_auxilaryClientInfo) 00103 return; 00104 00105 m_lastlightlayer = layer; 00106 m_lastauxinfo = m_auxilaryClientInfo; 00107 00108 /* enable/disable lights as needed */ 00109 if(layer >= 0) 00110 enable = applyLights(layer, viewmat); 00111 00112 if(enable) 00113 EnableOpenGLLights(rasty); 00114 else 00115 DisableOpenGLLights(); 00116 } 00117 00118 void KX_BlenderRenderTools::EnableOpenGLLights(RAS_IRasterizer *rasty) 00119 { 00120 if(m_lastlighting == true) 00121 return; 00122 00123 glEnable(GL_LIGHTING); 00124 glEnable(GL_COLOR_MATERIAL); 00125 00126 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); 00127 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); 00128 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (rasty->GetCameraOrtho())? GL_FALSE: GL_TRUE); 00129 if (GLEW_EXT_separate_specular_color || GLEW_VERSION_1_2) 00130 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); 00131 00132 m_lastlighting = true; 00133 } 00134 00135 void KX_BlenderRenderTools::DisableOpenGLLights() 00136 { 00137 if(m_lastlighting == false) 00138 return; 00139 00140 glDisable(GL_LIGHTING); 00141 glDisable(GL_COLOR_MATERIAL); 00142 00143 m_lastlighting = false; 00144 } 00145 00146 00147 void KX_BlenderRenderTools::SetClientObject(RAS_IRasterizer *rasty, void* obj) 00148 { 00149 if (m_clientobject != obj) 00150 { 00151 bool ccw = (obj == NULL || !((KX_GameObject*)obj)->IsNegativeScaling()); 00152 rasty->SetFrontFace(ccw); 00153 00154 m_clientobject = obj; 00155 } 00156 } 00157 00158 bool KX_BlenderRenderTools::RayHit(KX_ClientObjectInfo* client, KX_RayCast* result, void * const data) 00159 { 00160 double* const oglmatrix = (double* const) data; 00161 MT_Point3 resultpoint(result->m_hitPoint); 00162 MT_Vector3 resultnormal(result->m_hitNormal); 00163 MT_Vector3 left(oglmatrix[0],oglmatrix[1],oglmatrix[2]); 00164 MT_Vector3 dir = -(left.cross(resultnormal)).safe_normalized(); 00165 left = (dir.cross(resultnormal)).safe_normalized(); 00166 // for the up vector, we take the 'resultnormal' returned by the physics 00167 00168 double maat[16]={ 00169 left[0], left[1], left[2], 0, 00170 dir[0], dir[1], dir[2], 0, 00171 resultnormal[0],resultnormal[1],resultnormal[2], 0, 00172 0, 0, 0, 1}; 00173 glTranslated(resultpoint[0],resultpoint[1],resultpoint[2]); 00174 //glMultMatrixd(oglmatrix); 00175 glMultMatrixd(maat); 00176 return true; 00177 } 00178 00179 void KX_BlenderRenderTools::applyTransform(RAS_IRasterizer* rasty,double* oglmatrix,int objectdrawmode ) 00180 { 00181 /* FIXME: 00182 blender: intern/moto/include/MT_Vector3.inl:42: MT_Vector3 operator/(const 00183 MT_Vector3&, double): Assertion `!MT_fuzzyZero(s)' failed. 00184 00185 Program received signal SIGABRT, Aborted. 00186 [Switching to Thread 16384 (LWP 1519)] 00187 0x40477571 in kill () from /lib/libc.so.6 00188 (gdb) bt 00189 #7 0x08334368 in MT_Vector3::normalized() const () 00190 #8 0x0833e6ec in KX_BlenderRenderTools::applyTransform(RAS_IRasterizer*, double*, int) () 00191 */ 00192 00193 if (objectdrawmode & RAS_IPolyMaterial::BILLBOARD_SCREENALIGNED || 00194 objectdrawmode & RAS_IPolyMaterial::BILLBOARD_AXISALIGNED) 00195 { 00196 // rotate the billboard/halo 00197 //page 360/361 3D Game Engine Design, David Eberly for a discussion 00198 // on screen aligned and axis aligned billboards 00199 // assumed is that the preprocessor transformed all billboard polygons 00200 // so that their normal points into the positive x direction (1.0 , 0.0 , 0.0) 00201 // when new parenting for objects is done, this rotation 00202 // will be moved into the object 00203 00204 MT_Point3 objpos (oglmatrix[12],oglmatrix[13],oglmatrix[14]); 00205 MT_Point3 campos = rasty->GetCameraPosition(); 00206 MT_Vector3 dir = (campos - objpos).safe_normalized(); 00207 MT_Vector3 up(0,0,1.0); 00208 00209 KX_GameObject* gameobj = (KX_GameObject*)m_clientobject; 00210 // get scaling of halo object 00211 MT_Vector3 size = gameobj->GetSGNode()->GetLocalScale(); 00212 00213 bool screenaligned = (objectdrawmode & RAS_IPolyMaterial::BILLBOARD_SCREENALIGNED)!=0;//false; //either screen or axisaligned 00214 if (screenaligned) 00215 { 00216 up = (up - up.dot(dir) * dir).safe_normalized(); 00217 } else 00218 { 00219 dir = (dir - up.dot(dir)*up).safe_normalized(); 00220 } 00221 00222 MT_Vector3 left = dir.normalized(); 00223 dir = (left.cross(up)).normalized(); 00224 00225 // we have calculated the row vectors, now we keep 00226 // local scaling into account: 00227 00228 left *= size[0]; 00229 dir *= size[1]; 00230 up *= size[2]; 00231 double maat[16]={ 00232 left[0], left[1],left[2], 0, 00233 dir[0], dir[1],dir[2],0, 00234 up[0],up[1],up[2],0, 00235 0,0,0,1}; 00236 glTranslated(objpos[0],objpos[1],objpos[2]); 00237 glMultMatrixd(maat); 00238 00239 } else 00240 { 00241 if (objectdrawmode & RAS_IPolyMaterial::SHADOW) 00242 { 00243 // shadow must be cast to the ground, physics system needed here! 00244 MT_Point3 frompoint(oglmatrix[12],oglmatrix[13],oglmatrix[14]); 00245 KX_GameObject *gameobj = (KX_GameObject*)m_clientobject; 00246 MT_Vector3 direction = MT_Vector3(0,0,-1); 00247 00248 direction.normalize(); 00249 direction *= 100000; 00250 00251 MT_Point3 topoint = frompoint + direction; 00252 00253 KX_Scene* kxscene = (KX_Scene*) m_auxilaryClientInfo; 00254 PHY_IPhysicsEnvironment* physics_environment = kxscene->GetPhysicsEnvironment(); 00255 KX_IPhysicsController* physics_controller = gameobj->GetPhysicsController(); 00256 00257 KX_GameObject *parent = gameobj->GetParent(); 00258 if (!physics_controller && parent) 00259 physics_controller = parent->GetPhysicsController(); 00260 if (parent) 00261 parent->Release(); 00262 00263 KX_RayCast::Callback<KX_BlenderRenderTools> callback(this, physics_controller, oglmatrix); 00264 if (!KX_RayCast::RayTest(physics_environment, frompoint, topoint, callback)) 00265 { 00266 // couldn't find something to cast the shadow on... 00267 glMultMatrixd(oglmatrix); 00268 } 00269 else 00270 { // we found the "ground", but the cast matrix doesn't take 00271 // scaling in consideration, so we must apply the object scale 00272 MT_Vector3 size = gameobj->GetSGNode()->GetLocalScale(); 00273 glScalef(size[0], size[1], size[2]); 00274 } 00275 } else 00276 { 00277 00278 // 'normal' object 00279 glMultMatrixd(oglmatrix); 00280 } 00281 } 00282 } 00283 void KX_BlenderRenderTools::RenderText3D(int fontid, 00284 const char* text, 00285 int size, 00286 int dpi, 00287 float* color, 00288 double* mat, 00289 float aspect) 00290 { 00291 BL_print_game_line(fontid, text, size, dpi, color, mat, aspect); 00292 } 00293 00294 void KX_BlenderRenderTools::RenderText2D(RAS_TEXT_RENDER_MODE mode, 00295 const char* text, 00296 int xco, 00297 int yco, 00298 int width, 00299 int height) 00300 { 00301 if(mode == RAS_IRenderTools::RAS_TEXT_PADDED) 00302 BL_print_gamedebug_line_padded(text, xco, yco, width, height); 00303 else 00304 BL_print_gamedebug_line(text, xco, yco, width, height); 00305 } 00306 00307 /* Render Text renders text into a (series of) polygon, using a texture font, 00308 * Each character consists of one polygon (one quad or two triangles) */ 00309 00310 void KX_BlenderRenderTools::RenderText( 00311 int mode, 00312 RAS_IPolyMaterial* polymat, 00313 float v1[3], float v2[3], float v3[3], float v4[3], int glattrib) 00314 { 00315 const STR_String& mytext = ((CValue*)m_clientobject)->GetPropertyText("Text"); 00316 00317 const unsigned int flag = polymat->GetFlag(); 00318 struct MTFace* tface = 0; 00319 unsigned int *col = 0; 00320 00321 if(flag & RAS_BLENDERMAT) { 00322 KX_BlenderMaterial *bl_mat = static_cast<KX_BlenderMaterial*>(polymat); 00323 tface = bl_mat->GetMTFace(); 00324 col = bl_mat->GetMCol(); 00325 } else { 00326 KX_PolygonMaterial* blenderpoly = static_cast<KX_PolygonMaterial*>(polymat); 00327 tface = blenderpoly->GetMTFace(); 00328 col = blenderpoly->GetMCol(); 00329 } 00330 00331 GPU_render_text(tface, mode, mytext, mytext.Length(), col, v1, v2, v3, v4, glattrib); 00332 } 00333 00334 00335 void KX_BlenderRenderTools::PushMatrix() 00336 { 00337 glPushMatrix(); 00338 } 00339 00340 void KX_BlenderRenderTools::PopMatrix() 00341 { 00342 glPopMatrix(); 00343 } 00344 00345 00346 int KX_BlenderRenderTools::applyLights(int objectlayer, const MT_Transform& viewmat) 00347 { 00348 // taken from blender source, incompatibility between Blender Object / GameObject 00349 KX_Scene* kxscene = (KX_Scene*)m_auxilaryClientInfo; 00350 float glviewmat[16]; 00351 unsigned int count; 00352 std::vector<struct RAS_LightObject*>::iterator lit = m_lights.begin(); 00353 00354 for(count=0; count<m_numgllights; count++) 00355 glDisable((GLenum)(GL_LIGHT0+count)); 00356 00357 viewmat.getValue(glviewmat); 00358 00359 glPushMatrix(); 00360 glLoadMatrixf(glviewmat); 00361 for (lit = m_lights.begin(), count = 0; !(lit==m_lights.end()) && count < m_numgllights; ++lit) 00362 { 00363 RAS_LightObject* lightdata = (*lit); 00364 KX_LightObject *kxlight = (KX_LightObject*)lightdata->m_light; 00365 00366 if(kxlight->ApplyLight(kxscene, objectlayer, count)) 00367 count++; 00368 } 00369 glPopMatrix(); 00370 00371 return count; 00372 } 00373 00374 void KX_BlenderRenderTools::MotionBlur(RAS_IRasterizer* rasterizer) 00375 { 00376 int state = rasterizer->GetMotionBlurState(); 00377 float motionblurvalue; 00378 if(state) 00379 { 00380 motionblurvalue = rasterizer->GetMotionBlurValue(); 00381 if(state==1) 00382 { 00383 //bugfix:load color buffer into accum buffer for the first time(state=1) 00384 glAccum(GL_LOAD, 1.0); 00385 rasterizer->SetMotionBlurState(2); 00386 } 00387 else if(motionblurvalue>=0.0 && motionblurvalue<=1.0) 00388 { 00389 glAccum(GL_MULT, motionblurvalue); 00390 glAccum(GL_ACCUM, 1-motionblurvalue); 00391 glAccum(GL_RETURN, 1.0); 00392 glFlush(); 00393 } 00394 } 00395 }