blender/source/gameengine/Rasterizer/RAS_MeshObject.cpp

622 lines
14 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
*/
2011-02-25 13:38:24 +00:00
/** \file gameengine/Rasterizer/RAS_MeshObject.cpp
* \ingroup bgerast
*/
#include "MEM_guardedalloc.h"
#include "DNA_object_types.h"
#include "DNA_key_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
2002-10-12 11:37:38 +00:00
#include "RAS_MeshObject.h"
#include "RAS_IRasterizer.h"
#include "MT_MinMax.h"
#include "MT_Point3.h"
2002-10-12 11:37:38 +00:00
#include <algorithm>
/* polygon sorting */
struct RAS_MeshObject::polygonSlot
{
float m_z;
int m_index[4];
polygonSlot() {}
2002-10-12 11:37:38 +00:00
/* pnorm is the normal from the plane equation that the distance from is
* used to sort again. */
void get(const RAS_TexVert *vertexarray, const unsigned short *indexarray,
int offset, int nvert, const MT_Vector3& pnorm)
{
MT_Vector3 center(0, 0, 0);
int i;
2002-10-12 11:37:38 +00:00
for(i=0; i<nvert; i++) {
m_index[i] = indexarray[offset+i];
center += vertexarray[m_index[i]].getXYZ();
}
2002-10-12 11:37:38 +00:00
/* note we don't divide center by the number of vertices, since all
* polygons have the same number of vertices, and that we leave out
* the 4-th component of the plane equation since it is constant. */
m_z = MT_dot(pnorm, center);
2002-10-12 11:37:38 +00:00
}
void set(unsigned short *indexarray, int offset, int nvert)
2002-10-12 11:37:38 +00:00
{
int i;
for(i=0; i<nvert; i++)
indexarray[offset+i] = m_index[i];
2002-10-12 11:37:38 +00:00
}
};
2002-10-12 11:37:38 +00:00
struct RAS_MeshObject::backtofront
{
bool operator()(const polygonSlot &a, const polygonSlot &b) const
{
return a.m_z < b.m_z;
}
};
2002-10-12 11:37:38 +00:00
struct RAS_MeshObject::fronttoback
{
bool operator()(const polygonSlot &a, const polygonSlot &b) const
{
return a.m_z > b.m_z;
}
};
2002-10-12 11:37:38 +00:00
/* mesh object */
2002-10-12 11:37:38 +00:00
STR_String RAS_MeshObject::s_emptyname = "";
2002-10-12 11:37:38 +00:00
2009-07-30 21:42:29 +00:00
RAS_MeshObject::RAS_MeshObject(Mesh* mesh)
: m_bModified(true),
m_bMeshModified(true),
m_mesh(mesh)
2002-10-12 11:37:38 +00:00
{
if (m_mesh && m_mesh->key)
{
KeyBlock *kb;
int count=0;
// initialize weight cache for shape objects
// count how many keys in this mesh
for(kb= (KeyBlock*)m_mesh->key->block.first; kb; kb= (KeyBlock*)kb->next)
count++;
m_cacheWeightIndex.resize(count,-1);
}
2002-10-12 11:37:38 +00:00
}
RAS_MeshObject::~RAS_MeshObject()
{
vector<RAS_Polygon*>::iterator it;
2002-10-12 11:37:38 +00:00
if (m_mesh && m_mesh->key)
{
KeyBlock *kb;
// remove the weight cache to avoid memory leak
for(kb= (KeyBlock*)m_mesh->key->block.first; kb; kb= (KeyBlock*)kb->next) {
if(kb->weights)
MEM_freeN(kb->weights);
kb->weights= NULL;
}
}
for(it=m_Polygons.begin(); it!=m_Polygons.end(); it++)
delete (*it);
m_sharedvertex_map.clear();
m_Polygons.clear();
m_materials.clear();
2002-10-12 11:37:38 +00:00
}
bool RAS_MeshObject::MeshModified()
{
return m_bMeshModified;
}
2002-10-12 11:37:38 +00:00
//unsigned int RAS_MeshObject::GetLightLayer()
//{
// return m_lightlayer;
//}
2002-10-12 11:37:38 +00:00
int RAS_MeshObject::NumMaterials()
{
return m_materials.size();
}
const STR_String& RAS_MeshObject::GetMaterialName(unsigned int matid)
2002-10-12 11:37:38 +00:00
{
RAS_MeshMaterial* mmat = GetMeshMaterial(matid);
if(mmat)
return mmat->m_bucket->GetPolyMaterial()->GetMaterialName();
return s_emptyname;
2002-10-12 11:37:38 +00:00
}
RAS_MeshMaterial* RAS_MeshObject::GetMeshMaterial(unsigned int matid)
2002-10-12 11:37:38 +00:00
{
if (m_materials.size() > 0 && (matid < m_materials.size()))
{
list<RAS_MeshMaterial>::iterator it = m_materials.begin();
while (matid--) ++it;
return &*it;
2002-10-12 11:37:38 +00:00
}
return NULL;
2002-10-12 11:37:38 +00:00
}
int RAS_MeshObject::NumPolygons()
{
return m_Polygons.size();
}
BGE patch: KX_GameObject::rayCast() improvements to have X-Ray option, return true face normal and hit polygon information. rayCast(to,from,dist,prop,face,xray,poly): The face paremeter determines the orientation of the normal: 0 or omitted => hit normal is always oriented towards the ray origin (as if you casted the ray from outside) 1 => hit normal is the real face normal (only for mesh object, otherwise face has no effect) The ray has X-Ray capability if xray parameter is 1, otherwise the first object hit (other than self object) stops the ray. The prop and xray parameters interact as follow: prop off, xray off: return closest hit or no hit if there is no object on the full extend of the ray. prop off, xray on : idem. prop on, xray off: return closest hit if it matches prop, no hit otherwise. prop on, xray on : return closest hit matching prop or no hit if there is no object matching prop on the full extend of the ray. if poly is 0 or omitted, returns a 3-tuple with object reference, hit point and hit normal or (None,None,None) if no hit. if poly is 1, returns a 4-tuple with in addition a KX_PolyProxy as 4th element. The KX_PolyProxy object holds information on the polygon hit by the ray: the index of the vertex forming the poylgon, material, etc. Attributes (read-only): matname: The name of polygon material, empty if no material. material: The material of the polygon texture: The texture name of the polygon. matid: The material index of the polygon, use this to retrieve vertex proxy from mesh proxy v1: vertex index of the first vertex of the polygon, use this to retrieve vertex proxy from mesh proxy v2: vertex index of the second vertex of the polygon, use this to retrieve vertex proxy from mesh proxy v3: vertex index of the third vertex of the polygon, use this to retrieve vertex proxy from mesh proxy v4: vertex index of the fourth vertex of the polygon, 0 if polygon has only 3 vertex use this to retrieve vertex proxy from mesh proxy visible: visible state of the polygon: 1=visible, 0=invisible collide: collide state of the polygon: 1=receives collision, 0=collision free. Methods: getMaterialName(): Returns the polygon material name with MA prefix getMaterial(): Returns the polygon material getTextureName(): Returns the polygon texture name getMaterialIndex(): Returns the material bucket index of the polygon. getNumVertex(): Returns the number of vertex of the polygon. isVisible(): Returns whether the polygon is visible or not isCollider(): Returns whether the polygon is receives collision or not getVertexIndex(vertex): Returns the mesh vertex index of a polygon vertex getMesh(): Returns a mesh proxy New methods of KX_MeshProxy have been implemented to retrieve KX_PolyProxy objects: getNumPolygons(): Returns the number of polygon in the mesh. getPolygon(index): Gets the specified polygon from the mesh. More details in PyDoc.
2008-08-27 19:34:19 +00:00
RAS_Polygon* RAS_MeshObject::GetPolygon(int num) const
2002-10-12 11:37:38 +00:00
{
return m_Polygons[num];
}
list<RAS_MeshMaterial>::iterator GetFirstMaterial();
list<RAS_MeshMaterial>::iterator GetLastMaterial();
list<RAS_MeshMaterial>::iterator RAS_MeshObject::GetFirstMaterial()
2002-10-12 11:37:38 +00:00
{
return m_materials.begin();
}
list<RAS_MeshMaterial>::iterator RAS_MeshObject::GetLastMaterial()
2002-10-12 11:37:38 +00:00
{
return m_materials.end();
}
BGE performance, 4th round: logic This commit extends the technique of dynamic linked list to the logic system to eliminate as much as possible temporaries, map lookup or full scan. The logic engine is now free of memory allocation, which is an important stability factor. The overhead of the logic system is reduced by a factor between 3 and 6 depending on the logic setup. This is the speed-up you can expect on a logic setup using simple bricks. Heavy bricks like python controllers and ray sensors will still take about the same time to execute so the speed up will be less important. The core of the logic engine has been much reworked but the functionality is still the same except for one thing: the priority system on the execution of controllers. The exact same remark applies to actuators but I'll explain for controllers only: Previously, it was possible, with the "executePriority" attribute to set a controller to run before any other controllers in the game. Other than that, the sequential execution of controllers, as defined in Blender was guaranteed by default. With the new system, the sequential execution of controllers is still guaranteed but only within the controllers of one object. the user can no longer set a controller to run before any other controllers in the game. The "executePriority" attribute controls the execution of controllers within one object. The priority is a small number starting from 0 for the first controller and incrementing for each controller. If this missing feature is a must, a special method can be implemented to set a controller to run before all other controllers. Other improvements: - Systematic use of reference in parameter passing to avoid unnecessary data copy - Use pre increment in iterator instead of post increment to avoid temporary allocation - Use const char* instead of STR_String whenever possible to avoid temporary allocation - Fix reference counting bugs (memory leak) - Fix a crash in certain cases of state switching and object deletion - Minor speed up in property sensor - Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00
void RAS_MeshObject::SetName(const char *name)
2002-10-12 11:37:38 +00:00
{
m_name = name;
}
BGE performance, 4th round: logic This commit extends the technique of dynamic linked list to the logic system to eliminate as much as possible temporaries, map lookup or full scan. The logic engine is now free of memory allocation, which is an important stability factor. The overhead of the logic system is reduced by a factor between 3 and 6 depending on the logic setup. This is the speed-up you can expect on a logic setup using simple bricks. Heavy bricks like python controllers and ray sensors will still take about the same time to execute so the speed up will be less important. The core of the logic engine has been much reworked but the functionality is still the same except for one thing: the priority system on the execution of controllers. The exact same remark applies to actuators but I'll explain for controllers only: Previously, it was possible, with the "executePriority" attribute to set a controller to run before any other controllers in the game. Other than that, the sequential execution of controllers, as defined in Blender was guaranteed by default. With the new system, the sequential execution of controllers is still guaranteed but only within the controllers of one object. the user can no longer set a controller to run before any other controllers in the game. The "executePriority" attribute controls the execution of controllers within one object. The priority is a small number starting from 0 for the first controller and incrementing for each controller. If this missing feature is a must, a special method can be implemented to set a controller to run before all other controllers. Other improvements: - Systematic use of reference in parameter passing to avoid unnecessary data copy - Use pre increment in iterator instead of post increment to avoid temporary allocation - Use const char* instead of STR_String whenever possible to avoid temporary allocation - Fix reference counting bugs (memory leak) - Fix a crash in certain cases of state switching and object deletion - Minor speed up in property sensor - Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00
STR_String& RAS_MeshObject::GetName()
2002-10-12 11:37:38 +00:00
{
return m_name;
}
const STR_String& RAS_MeshObject::GetTextureName(unsigned int matid)
2002-10-12 11:37:38 +00:00
{
RAS_MeshMaterial* mmat = GetMeshMaterial(matid);
if(mmat)
return mmat->m_bucket->GetPolyMaterial()->GetTextureName();
return s_emptyname;
2002-10-12 11:37:38 +00:00
}
RAS_MeshMaterial *RAS_MeshObject::GetMeshMaterial(RAS_IPolyMaterial *mat)
{
list<RAS_MeshMaterial>::iterator mit;
2002-10-12 11:37:38 +00:00
/* find a mesh material */
for(mit = m_materials.begin(); mit != m_materials.end(); mit++)
if(mit->m_bucket->GetPolyMaterial() == mat)
return &*mit;
2002-10-12 11:37:38 +00:00
return NULL;
2002-10-12 11:37:38 +00:00
}
BGE: Support mesh modifiers in the game engine. Realtime modifiers applied on mesh objects will be supported in the game engine with the following limitations: - Only real time modifiers are supported (basically all of them!) - Virtual modifiers resulting from parenting are not supported: armature, curve, lattice. You can still use these modifiers (armature is really not recommended) but in non parent mode. The BGE has it's own parenting capability for armature. - Modifiers are computed on the host (using blender modifier stack). - Modifiers are statically evaluated: any possible time dependency in the modifiers is not supported (don't know enough about modifiers to be more specific). - Modifiers are reevaluated if the underlying mesh is deformed due to shape action or armature action. Beware that this is very CPU intensive; modifiers should really be used for static objects only. - Physics is still based on the original mesh: if you have a mirror modifier, the physic shape will be limited to one half of the resulting object. Therefore, the modifiers should preferably be used on graphic objects. - Scripts have no access to the modified mesh. - Modifiers that are based on objects interaction (boolean,..) will not be dependent on the objects position in the GE. What you see in the 3D view is what you get in the GE regardless on the object position, velocity, etc. Besides that, the feature is compatible with all the BGE features that affect meshes: armature action, shape action, relace mesh, VideoTexture, add object, dupligroup. Known problems: - This feature is a bit hacky: the BGE uses the derived mesh draw functions to display the object. This drawing method is a bit slow and is not 100% compatible with the BGE. There may be some problems in multi-texture mode: the multi-texture coordinates are not sent to the GPU. Texface and GLSL on the other hand should be fully supported. - Culling is still based on the extend of the original mesh. If you have a modifer that extends the size of the mesh, the object may disappear while still in the view frustrum. - Derived mesh is not shared between replicas. The derived mesh is allocated and computed for each object with modifiers, regardless if they are static replicas. - Display list are not created on objects with modifiers. I should be able to fix the above problems before release. However, the feature is already useful for game development. Once you are ready to release the game, you can apply the modifiers to get back display list support and mesh sharing capability. MSVC, scons, Cmake, makefile updated. Enjoy /benoit
2009-04-21 11:01:09 +00:00
int RAS_MeshObject::GetMaterialId(RAS_IPolyMaterial *mat)
{
list<RAS_MeshMaterial>::iterator mit;
int imat;
/* find a mesh material */
for(imat=0, mit = m_materials.begin(); mit != m_materials.end(); mit++, imat++)
if(mit->m_bucket->GetPolyMaterial() == mat)
return imat;
return -1;
}
RAS_Polygon* RAS_MeshObject::AddPolygon(RAS_MaterialBucket *bucket, int numverts)
{
RAS_MeshMaterial *mmat;
RAS_Polygon *poly;
RAS_MeshSlot *slot;
2002-10-12 11:37:38 +00:00
/* find a mesh material */
mmat = GetMeshMaterial(bucket->GetPolyMaterial());
/* none found, create a new one */
if(!mmat) {
RAS_MeshMaterial meshmat;
meshmat.m_bucket = bucket;
meshmat.m_baseslot = meshmat.m_bucket->AddMesh(numverts);
BGE performance, 3rd round: culling and rasterizer. This commit extend the technique of dynamic linked list to the mesh slots so as to eliminate dumb scan or map lookup. It provides massive performance improvement in the culling and in the rasterizer when the majority of objects are static. Other improvements: - Compute the opengl matrix only for objects that are visible. - Simplify hash function for GEN_HasedPtr - Scan light list instead of general object list to render shadows - Remove redundant opengl calls to set specularity, shinyness and diffuse between each mesh slots. - Cache GPU material to avoid frequent call to GPU_material_from_blender - Only set once the fixed elements of mesh slot - Use more inline function The following table shows the performance increase between 2.48, 1st round and this round of improvement. The test was done with a scene containing 40000 objects, of which 1000 are in the view frustrum approximately. The object are simple textured cube to make sure the GPU is not the bottleneck. As some of the rasterizer processing time has moved under culling, I present the sum of scenegraph(includes culling)+rasterizer time Scenegraph+rasterizer(ms) 2.48 1st round 3rd round All objects static, 323.0 86.0 7.2 all visible, 1000 in the view frustrum All objects static, 219.0 49.7 N/A(*) all invisible. All objects moving, 323.0 105.6 34.7 all visible, 1000 in the view frustrum Scene destruction 40min 40min 4s (*) : this time is not representative because the frame rate was at 60fps. In that case, the GPU holds down the GE by frame sync. By design, the overhead of the rasterizer is 0 when the the objects are invisible. This table shows a global speed up between 9x and 45x compared to 2.48a for scenegraph, culling and rasterizer overhead. The speed up goes much higher when objects are invisible. An additional 2-4x speed up is possible in the scenegraph by upgrading the Moto library to use Eigen2 BLAS library instead of C++ classes but the scenegraph is already so fast that it is not a priority right now. Next speed up in logic: many things to do there...
2009-05-07 09:13:01 +00:00
meshmat.m_baseslot->m_mesh = this;
m_materials.push_back(meshmat);
mmat = &m_materials.back();
}
/* add it to the bucket, this also adds new display arrays */
slot = mmat->m_baseslot;
slot->AddPolygon(numverts);
/* create a new polygon */
RAS_DisplayArray *darray = slot->CurrentDisplayArray();
poly = new RAS_Polygon(bucket, darray, numverts);
m_Polygons.push_back(poly);
return poly;
}
2002-10-12 11:37:38 +00:00
void RAS_MeshObject::DebugColor(unsigned int abgr)
{
/*int numpolys = NumPolygons();
for (int i=0;i<numpolys;i++) {
2002-10-12 11:37:38 +00:00
RAS_Polygon* poly = m_polygons[i];
for (int v=0;v<poly->VertexCount();v++)
RAS_TexVert* vtx = poly->GetVertex(v)->setDebugRGBA(abgr);
2002-10-12 11:37:38 +00:00
}
*/
/* m_debugcolor = abgr; */
2002-10-12 11:37:38 +00:00
}
void RAS_MeshObject::SetVertexColor(RAS_IPolyMaterial* mat,MT_Vector4 rgba)
{
RAS_MeshMaterial *mmat = GetMeshMaterial(mat);
RAS_MeshSlot *slot = mmat->m_baseslot;
RAS_MeshSlot::iterator it;
size_t i;
for(slot->begin(it); !slot->end(it); slot->next(it))
for(i=it.startvertex; i<it.endvertex; i++)
it.vertex[i].SetRGBA(rgba);
}
void RAS_MeshObject::AddVertex(RAS_Polygon *poly, int i,
const MT_Point3& xyz,
const MT_Point2& uv,
const MT_Point2& uv2,
const MT_Vector4& tangent,
const unsigned int rgba,
const MT_Vector3& normal,
bool flat,
int origindex)
{
RAS_TexVert texvert(xyz, uv, uv2, tangent, rgba, normal, flat, origindex);
RAS_MeshMaterial *mmat;
RAS_DisplayArray *darray;
RAS_MeshSlot *slot;
int offset;
mmat = GetMeshMaterial(poly->GetMaterial()->GetPolyMaterial());
slot = mmat->m_baseslot;
darray = slot->CurrentDisplayArray();
2002-10-12 11:37:38 +00:00
{ /* Shared Vertex! */
/* find vertices shared between faces, with the restriction
* that they exist in the same display array, and have the
* same uv coordinate etc */
vector<SharedVertex>& sharedmap = m_sharedvertex_map[origindex];
vector<SharedVertex>::iterator it;
2002-10-12 11:37:38 +00:00
for(it = sharedmap.begin(); it != sharedmap.end(); it++)
2002-10-12 11:37:38 +00:00
{
if(it->m_darray != darray)
continue;
if(!it->m_darray->m_vertex[it->m_offset].closeTo(&texvert))
continue;
/* found one, add it and we're done */
if(poly->IsVisible())
slot->AddPolygonVertex(it->m_offset);
poly->SetVertexOffset(i, it->m_offset);
return;
2002-10-12 11:37:38 +00:00
}
}
/* no shared vertex found, add a new one */
offset = slot->AddVertex(texvert);
if(poly->IsVisible())
slot->AddPolygonVertex(offset);
poly->SetVertexOffset(i, offset);
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
{ /* Shared Vertex! */
SharedVertex shared;
shared.m_darray = darray;
shared.m_offset = offset;
m_sharedvertex_map[origindex].push_back(shared);
2002-10-12 11:37:38 +00:00
}
}
int RAS_MeshObject::NumVertices(RAS_IPolyMaterial* mat)
2002-10-12 11:37:38 +00:00
{
RAS_MeshMaterial *mmat;
RAS_MeshSlot *slot;
RAS_MeshSlot::iterator it;
size_t len = 0;
2002-10-12 11:37:38 +00:00
mmat = GetMeshMaterial(mat);
slot = mmat->m_baseslot;
for(slot->begin(it); !slot->end(it); slot->next(it))
len += it.endvertex - it.startvertex;
2002-10-12 11:37:38 +00:00
return len;
}
RAS_TexVert* RAS_MeshObject::GetVertex(unsigned int matid,
unsigned int index)
2002-10-12 11:37:38 +00:00
{
RAS_MeshMaterial *mmat;
RAS_MeshSlot *slot;
RAS_MeshSlot::iterator it;
size_t len;
2002-10-12 11:37:38 +00:00
mmat = GetMeshMaterial(matid);
2002-10-12 11:37:38 +00:00
if(!mmat)
return NULL;
2002-10-12 11:37:38 +00:00
slot = mmat->m_baseslot;
len = 0;
for(slot->begin(it); !slot->end(it); slot->next(it)) {
if(index >= len + it.endvertex - it.startvertex)
len += it.endvertex - it.startvertex;
else
return &it.vertex[index - len];
}
2002-10-12 11:37:38 +00:00
return NULL;
2002-10-12 11:37:38 +00:00
}
const float* RAS_MeshObject::GetVertexLocation(unsigned int orig_index)
{
vector<SharedVertex>& sharedmap = m_sharedvertex_map[orig_index];
vector<SharedVertex>::iterator it= sharedmap.begin();
return it->m_darray->m_vertex[it->m_offset].getXYZ();
}
void RAS_MeshObject::AddMeshUser(void *clientobj, SG_QList *head, RAS_Deformer* deformer)
2002-10-12 11:37:38 +00:00
{
list<RAS_MeshMaterial>::iterator it;
list<RAS_MeshMaterial>::iterator mit;
2002-10-12 11:37:38 +00:00
for(it = m_materials.begin();it!=m_materials.end();++it) {
/* always copy from the base slot, which is never removed
* since new objects can be created with the same mesh data */
if (deformer && !deformer->UseVertexArray())
{
// HACK!
// this deformer doesn't use vertex array => derive mesh
// we must keep only the mesh slots that have unique material id
// this is to match the derived mesh drawing function
// Need a better solution in the future: scan the derive mesh and create vertex array
RAS_IPolyMaterial* curmat = it->m_bucket->GetPolyMaterial();
if (curmat->GetFlag() & RAS_BLENDERGLSL)
{
for(mit = m_materials.begin(); mit != it; ++mit)
{
RAS_IPolyMaterial* mat = mit->m_bucket->GetPolyMaterial();
if ((mat->GetFlag() & RAS_BLENDERGLSL) &&
mat->GetMaterialIndex() == curmat->GetMaterialIndex())
// no need to convert current mesh slot
break;
}
if (mit != it)
continue;
}
}
RAS_MeshSlot *ms = it->m_bucket->CopyMesh(it->m_baseslot);
ms->m_clientObj = clientobj;
ms->SetDeformer(deformer);
it->m_slots.insert(clientobj, ms);
BGE performance, 3rd round: culling and rasterizer. This commit extend the technique of dynamic linked list to the mesh slots so as to eliminate dumb scan or map lookup. It provides massive performance improvement in the culling and in the rasterizer when the majority of objects are static. Other improvements: - Compute the opengl matrix only for objects that are visible. - Simplify hash function for GEN_HasedPtr - Scan light list instead of general object list to render shadows - Remove redundant opengl calls to set specularity, shinyness and diffuse between each mesh slots. - Cache GPU material to avoid frequent call to GPU_material_from_blender - Only set once the fixed elements of mesh slot - Use more inline function The following table shows the performance increase between 2.48, 1st round and this round of improvement. The test was done with a scene containing 40000 objects, of which 1000 are in the view frustrum approximately. The object are simple textured cube to make sure the GPU is not the bottleneck. As some of the rasterizer processing time has moved under culling, I present the sum of scenegraph(includes culling)+rasterizer time Scenegraph+rasterizer(ms) 2.48 1st round 3rd round All objects static, 323.0 86.0 7.2 all visible, 1000 in the view frustrum All objects static, 219.0 49.7 N/A(*) all invisible. All objects moving, 323.0 105.6 34.7 all visible, 1000 in the view frustrum Scene destruction 40min 40min 4s (*) : this time is not representative because the frame rate was at 60fps. In that case, the GPU holds down the GE by frame sync. By design, the overhead of the rasterizer is 0 when the the objects are invisible. This table shows a global speed up between 9x and 45x compared to 2.48a for scenegraph, culling and rasterizer overhead. The speed up goes much higher when objects are invisible. An additional 2-4x speed up is possible in the scenegraph by upgrading the Moto library to use Eigen2 BLAS library instead of C++ classes but the scenegraph is already so fast that it is not a priority right now. Next speed up in logic: many things to do there...
2009-05-07 09:13:01 +00:00
head->QAddBack(ms);
2002-10-12 11:37:38 +00:00
}
}
void RAS_MeshObject::RemoveFromBuckets(void *clientobj)
2002-10-12 11:37:38 +00:00
{
list<RAS_MeshMaterial>::iterator it;
2002-10-12 11:37:38 +00:00
for(it = m_materials.begin();it!=m_materials.end();++it) {
RAS_MeshSlot **msp = it->m_slots[clientobj];
2002-10-12 11:37:38 +00:00
if(!msp)
continue;
2002-10-12 11:37:38 +00:00
RAS_MeshSlot *ms = *msp;
2002-10-12 11:37:38 +00:00
it->m_bucket->RemoveMesh(ms);
it->m_slots.remove(clientobj);
}
2002-10-12 11:37:38 +00:00
}
//void RAS_MeshObject::Transform(const MT_Transform& trans)
//{
//m_trans.translate(MT_Vector3(0,0,1));//.operator *=(trans);
// for (int i=0;i<m_Polygons.size();i++)
// {
// m_Polygons[i]->Transform(trans);
// }
//}
/*
void RAS_MeshObject::RelativeTransform(const MT_Vector3& vec)
{
for (int i=0;i<m_Polygons.size();i++)
{
m_Polygons[i]->RelativeTransform(vec);
}
}
*/
void RAS_MeshObject::SortPolygons(RAS_MeshSlot& ms, const MT_Transform &transform)
{
// Limitations: sorting is quite simple, and handles many
// cases wrong, partially due to polygons being sorted per
// bucket.
//
// a) mixed triangles/quads are sorted wrong
// b) mixed materials are sorted wrong
// c) more than 65k faces are sorted wrong
// d) intersecting objects are sorted wrong
// e) intersecting polygons are sorted wrong
//
// a) can be solved by making all faces either triangles or quads
// if they need to be z-sorted. c) could be solved by allowing
// larger buckets, b) and d) cannot be solved easily if we want
// to avoid excessive state changes while drawing. e) would
// require splitting polygons.
RAS_MeshSlot::iterator it;
size_t j;
for(ms.begin(it); !ms.end(it); ms.next(it)) {
unsigned int nvert = (int)it.array->m_type;
unsigned int totpoly = it.totindex/nvert;
if(totpoly <= 1)
continue;
if(it.array->m_type == RAS_DisplayArray::LINE)
continue;
// Extract camera Z plane...
const MT_Vector3 pnorm(transform.getBasis()[2]);
// unneeded: const MT_Scalar pval = transform.getOrigin()[2];
vector<polygonSlot> slots(totpoly);
/* get indices and z into temporary array */
for(j=0; j<totpoly; j++)
slots[j].get(it.vertex, it.index, j*nvert, nvert, pnorm);
/* sort (stable_sort might be better, if flickering happens?) */
std::sort(slots.begin(), slots.end(), backtofront());
/* get indices from temporary array again */
for(j=0; j<totpoly; j++)
slots[j].set(it.index, j*nvert, nvert);
}
}
2002-10-12 11:37:38 +00:00
void RAS_MeshObject::SchedulePolygons(int drawingmode)
2002-10-12 11:37:38 +00:00
{
if (m_bModified)
{
m_bModified = false;
m_bMeshModified = true;
2002-10-12 11:37:38 +00:00
}
}
static int get_def_index(Object* ob, const char* vgroup)
{
bDeformGroup *curdef;
int index = 0;
for (curdef = (bDeformGroup*)ob->defbase.first; curdef; curdef=(bDeformGroup*)curdef->next, index++)
if (!strcmp(curdef->name, vgroup))
return index;
return -1;
}
void RAS_MeshObject::CheckWeightCache(Object* obj)
{
KeyBlock *kb;
int kbindex, defindex;
MDeformVert *dvert= NULL;
int totvert, i, j;
float *weights;
if (!m_mesh->key)
return;
for(kbindex=0, kb= (KeyBlock*)m_mesh->key->block.first; kb; kb= (KeyBlock*)kb->next, kbindex++)
{
// first check the cases where the weight must be cleared
if (kb->vgroup[0] == 0 ||
m_mesh->dvert == NULL ||
(defindex = get_def_index(obj, kb->vgroup)) == -1) {
if (kb->weights) {
MEM_freeN(kb->weights);
kb->weights = NULL;
}
m_cacheWeightIndex[kbindex] = -1;
} else if (m_cacheWeightIndex[kbindex] != defindex) {
// a weight array is required but the cache is not matching
if (kb->weights) {
MEM_freeN(kb->weights);
kb->weights = NULL;
}
dvert= m_mesh->dvert;
totvert= m_mesh->totvert;
weights= (float*)MEM_callocN(totvert*sizeof(float), "weights");
for (i=0; i < totvert; i++, dvert++) {
for(j=0; j<dvert->totweight; j++) {
if (dvert->dw[j].def_nr == defindex) {
weights[i]= dvert->dw[j].weight;
break;
}
}
}
kb->weights = weights;
m_cacheWeightIndex[kbindex] = defindex;
}
}
}