blender/source/gameengine/Rasterizer/RAS_MeshObject.cpp

727 lines
16 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,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
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>
2002-10-12 11:37:38 +00:00
STR_String RAS_MeshObject::s_emptyname = "";
KX_ArrayOptimizer::~KX_ArrayOptimizer()
{
for (vector<KX_VertexArray*>::iterator itv = m_VertexArrayCache1.begin();
!(itv == m_VertexArrayCache1.end());++itv)
2002-10-12 11:37:38 +00:00
{
delete (*itv);
}
for (vector<KX_IndexArray*>::iterator iti = m_IndexArrayCache1.begin();
!(iti == m_IndexArrayCache1.end());++iti)
2002-10-12 11:37:38 +00:00
{
delete (*iti);
}
m_TriangleArrayCount.clear();
m_VertexArrayCache1.clear();
m_IndexArrayCache1.clear();
}
RAS_MeshObject::RAS_MeshObject(Mesh* mesh, int lightlayer)
2002-10-12 11:37:38 +00:00
: m_bModified(true),
m_lightlayer(lightlayer),
m_zsort(false),
m_MeshMod(true),
m_mesh(mesh),
m_class(0)
2002-10-12 11:37:38 +00:00
{
}
bool RAS_MeshObject::MeshModified()
{
return m_MeshMod;
}
2002-10-12 11:37:38 +00:00
RAS_MeshObject::~RAS_MeshObject()
{
for (vector<RAS_Polygon*>::iterator it=m_Polygons.begin();!(it==m_Polygons.end());it++)
{
delete (*it);
}
ClearArrayData();
}
unsigned int RAS_MeshObject::GetLightLayer()
2002-10-12 11:37:38 +00:00
{
return m_lightlayer;
}
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_MaterialBucket* bucket = GetMaterialBucket(matid);
return bucket?bucket->GetPolyMaterial()->GetMaterialName():s_emptyname;
2002-10-12 11:37:38 +00:00
}
RAS_MaterialBucket* RAS_MeshObject::GetMaterialBucket(unsigned int matid)
2002-10-12 11:37:38 +00:00
{
if (m_materials.size() > 0 && (matid < m_materials.size()))
{
RAS_MaterialBucket::Set::const_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];
}
RAS_MaterialBucket::Set::iterator RAS_MeshObject::GetFirstMaterial()
2002-10-12 11:37:38 +00:00
{
return m_materials.begin();
}
RAS_MaterialBucket::Set::iterator RAS_MeshObject::GetLastMaterial()
2002-10-12 11:37:38 +00:00
{
return m_materials.end();
}
void RAS_MeshObject::SetName(STR_String name)
{
m_name = name;
}
const STR_String& RAS_MeshObject::GetName()
{
return m_name;
}
const STR_String& RAS_MeshObject::GetTextureName(unsigned int matid)
2002-10-12 11:37:38 +00:00
{
RAS_MaterialBucket* bucket = GetMaterialBucket(matid);
return bucket?bucket->GetPolyMaterial()->GetTextureName():s_emptyname;
2002-10-12 11:37:38 +00:00
}
void RAS_MeshObject::AddPolygon(RAS_Polygon* poly)
{
m_Polygons.push_back(poly);
}
void RAS_MeshObject::DebugColor(unsigned int abgr)
{
/*
int numpolys = NumPolygons();
for (int i=0;i<numpolys;i++)
{
RAS_Polygon* poly = m_polygons[i];
for (int v=0;v<poly->VertexCount();v++)
{
RAS_TexVert* vtx = poly->GetVertex(v);
vtx->setDebugRGBA(abgr);
}
}
*/
m_debugcolor = abgr;
}
void RAS_MeshObject::SetVertexColor(RAS_IPolyMaterial* mat,MT_Vector4 rgba)
{
const vecVertexArray & vertexvec = GetVertexCache(mat);
for (vector<KX_VertexArray*>::const_iterator it = vertexvec.begin(); it != vertexvec.end(); ++it)
{
KX_VertexArray::iterator vit;
for (vit=(*it)->begin(); vit != (*it)->end(); vit++)
{
vit->SetRGBA(rgba);
}
}
}
2002-10-12 11:37:38 +00:00
void RAS_MeshObject::SchedulePoly(const KX_VertexIndex& idx,
int numverts,
RAS_IPolyMaterial* mat)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
2002-10-12 11:37:38 +00:00
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[0]);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[1]);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[2]);
if (!mat->UsesTriangles())
2002-10-12 11:37:38 +00:00
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[3]);
}
void RAS_MeshObject::ScheduleWireframePoly(const KX_VertexIndex& idx,
int numverts,
int edgecode,
RAS_IPolyMaterial* mat)
{
//int indexpos = m_IndexArrayCount[idx.m_vtxarray];
int edgetrace = 1<<(numverts-1);
bool drawedge = (edgecode & edgetrace)!=0;
edgetrace = 1;
int prevvert = idx.m_indexarray[numverts-1];
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
for (int v = 0; v < numverts; v++)
{
unsigned int curvert = idx.m_indexarray[v];
if (drawedge)
{
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(prevvert);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(curvert);
}
prevvert = curvert;
drawedge = (edgecode & edgetrace)!=0;
edgetrace*=2;
}
//m_IndexArrayCount[idx.m_vtxarray] = indexpos;
}
int RAS_MeshObject::FindOrAddVertex(int vtxarray,
const MT_Point3& xyz,
const MT_Point2& uv,
const MT_Point2& uv2,
const MT_Vector4& tangent,
2002-10-12 11:37:38 +00:00
const unsigned int rgbacolor,
const MT_Vector3& normal,
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
bool flat,
2002-10-12 11:37:38 +00:00
RAS_IPolyMaterial* mat,
int origindex)
2002-10-12 11:37:38 +00:00
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
2002-10-12 11:37:38 +00:00
int numverts = ao->m_VertexArrayCache1[vtxarray]->size();//m_VertexArrayCount[vtxarray];
RAS_TexVert newvert(xyz,uv,uv2,tangent,rgbacolor,normal, flat? TV_CALCFACENORMAL: 0,origindex);
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
2002-10-12 11:37:38 +00:00
#define KX_FIND_SHARED_VERTICES
#ifdef KX_FIND_SHARED_VERTICES
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
if(!flat) {
for (std::vector<RAS_MatArrayIndex>::iterator it = m_xyz_index_to_vertex_index_mapping[origindex].begin();
it != m_xyz_index_to_vertex_index_mapping[origindex].end();
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
it++)
2002-10-12 11:37:38 +00:00
{
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
if ((*it).m_arrayindex1 == ao->m_index1 &&
(*it).m_array == vtxarray &&
*(*it).m_matid == *mat &&
(*ao->m_VertexArrayCache1[vtxarray])[(*it).m_index].closeTo(&newvert)
)
{
return (*it).m_index;
}
2002-10-12 11:37:38 +00:00
}
}
#endif // KX_FIND_SHARED_VERTICES
// no vertex found, add one
ao->m_VertexArrayCache1[vtxarray]->push_back(newvert);
2002-10-12 11:37:38 +00:00
// printf("(%f,%f,%f) ",xyz[0],xyz[1],xyz[2]);
RAS_MatArrayIndex idx;
idx.m_arrayindex1 = ao->m_index1;
idx.m_array = vtxarray;
idx.m_index = numverts;
idx.m_matid = mat;
m_xyz_index_to_vertex_index_mapping[origindex].push_back(idx);
2002-10-12 11:37:38 +00:00
return numverts;
}
vecVertexArray& RAS_MeshObject::GetVertexCache (RAS_IPolyMaterial* mat)
2002-10-12 11:37:38 +00:00
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
2002-10-12 11:37:38 +00:00
return ao->m_VertexArrayCache1;
}
int RAS_MeshObject::GetVertexArrayLength(RAS_IPolyMaterial* mat)
{
int len = 0;
const vecVertexArray & vertexvec = GetVertexCache(mat);
vector<KX_VertexArray*>::const_iterator it = vertexvec.begin();
for (; it != vertexvec.end(); ++it)
2002-10-12 11:37:38 +00:00
{
len += (*it)->size();
}
return len;
}
RAS_TexVert* RAS_MeshObject::GetVertex(unsigned int matid,
unsigned int index)
2002-10-12 11:37:38 +00:00
{
RAS_TexVert* vertex = NULL;
RAS_MaterialBucket* bucket = GetMaterialBucket(matid);
if (bucket)
{
RAS_IPolyMaterial* mat = bucket->GetPolyMaterial();
if (mat)
{
const vecVertexArray & vertexvec = GetVertexCache(mat);
vector<KX_VertexArray*>::const_iterator it = vertexvec.begin();
for (unsigned int len = 0; it != vertexvec.end(); ++it)
2002-10-12 11:37:38 +00:00
{
if (index < len + (*it)->size())
{
vertex = &(*(*it))[index-len];
break;
}
else
{
len += (*it)->size();
}
}
}
}
return vertex;
}
const vecIndexArrays& RAS_MeshObject::GetIndexCache (RAS_IPolyMaterial* mat)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
2002-10-12 11:37:38 +00:00
return ao->m_IndexArrayCache1;
}
KX_ArrayOptimizer* RAS_MeshObject::GetArrayOptimizer(RAS_IPolyMaterial* polymat)
{
KX_ArrayOptimizer** aop = m_matVertexArrayS[polymat];
2002-10-12 11:37:38 +00:00
if(aop)
2002-10-12 11:37:38 +00:00
return *aop;
// didn't find array, but an array might already exist
// for a material equal to this one
for(int i=0;i<m_matVertexArrayS.size();i++) {
RAS_IPolyMaterial *mat = (RAS_IPolyMaterial*)(m_matVertexArrayS.getKey(i)->getValue());
if(*mat == *polymat) {
m_matVertexArrayS.insert(polymat, *m_matVertexArrayS.at(i));
return *m_matVertexArrayS.at(i);
}
}
// create new array
2002-10-12 11:37:38 +00:00
int numelements = m_matVertexArrayS.size();
m_sortedMaterials.push_back(polymat);
2002-10-12 11:37:38 +00:00
KX_ArrayOptimizer* ao = new KX_ArrayOptimizer(numelements);
m_matVertexArrayS.insert(polymat, ao);
2002-10-12 11:37:38 +00:00
return ao;
}
void RAS_MeshObject::Bucketize(double* oglmatrix,
void* clientobj,
bool useObjectColor,
const MT_Vector4& rgbavec)
{
KX_MeshSlot ms;
ms.m_clientObj = clientobj;
ms.m_mesh = this;
ms.m_OpenGLMatrix = oglmatrix;
ms.m_bObjectColor = useObjectColor;
ms.m_RGBAcolor = rgbavec;
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
2002-10-12 11:37:38 +00:00
{
RAS_MaterialBucket* bucket = *it;
// KX_ArrayOptimizer* oa = GetArrayOptimizer(bucket->GetPolyMaterial());
2002-10-12 11:37:38 +00:00
bucket->SetMeshSlot(ms);
}
}
void RAS_MeshObject::MarkVisible(double* oglmatrix,
void* clientobj,
bool visible,
bool useObjectColor,
const MT_Vector4& rgbavec)
{
KX_MeshSlot ms;
ms.m_clientObj = clientobj;
ms.m_mesh = this;
ms.m_OpenGLMatrix = oglmatrix;
ms.m_RGBAcolor = rgbavec;
ms.m_bObjectColor= useObjectColor;
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
2002-10-12 11:37:38 +00:00
{
RAS_MaterialBucket* bucket = *it;
// KX_ArrayOptimizer* oa = GetArrayOptimizer(bucket->GetPolyMaterial());
2002-10-12 11:37:38 +00:00
bucket->MarkVisibleMeshSlot(ms,visible,useObjectColor,rgbavec);
}
}
void RAS_MeshObject::RemoveFromBuckets(double* oglmatrix,
void* clientobj)
{
KX_MeshSlot ms;
ms.m_clientObj = clientobj;
ms.m_mesh = this;
ms.m_OpenGLMatrix = oglmatrix;
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
2002-10-12 11:37:38 +00:00
{
RAS_MaterialBucket* bucket = *it;
// RAS_IPolyMaterial* polymat = bucket->GetPolyMaterial();
2002-10-12 11:37:38 +00:00
//KX_ArrayOptimizer* oa = GetArrayOptimizer(polymat);
bucket->RemoveMeshSlot(ms);
}
}
/*
* RAS_MeshObject::GetVertex returns the vertex located somewhere in the vertexpool
* it is the clients responsibility to make sure the array and index are valid
*/
RAS_TexVert* RAS_MeshObject::GetVertex(short array,
unsigned int index,
2002-10-12 11:37:38 +00:00
RAS_IPolyMaterial* polymat)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);
2002-10-12 11:37:38 +00:00
return &((*(ao->m_VertexArrayCache1)[array])[index]);
}
void RAS_MeshObject::ClearArrayData()
{
for (int i=0;i<m_matVertexArrayS.size();i++) {
KX_ArrayOptimizer** ao = m_matVertexArrayS.at(i);
// we have duplicate entries, only free once
for(int j=i+1;j<m_matVertexArrayS.size();j++) {
if(ao == m_matVertexArrayS.at(j)) {
ao = NULL;
break;
}
}
2002-10-12 11:37:38 +00:00
if (ao)
delete *ao;
}
}
/**
* RAS_MeshObject::CreateNewVertices creates vertices within sorted pools of vertices that share same material
*/
int RAS_MeshObject::FindVertexArray(int numverts,
RAS_IPolyMaterial* polymat)
{
// bool found=false;
2002-10-12 11:37:38 +00:00
int array=-1;
KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);
for (unsigned int i=0;i<ao->m_VertexArrayCache1.size();i++)
2002-10-12 11:37:38 +00:00
{
if ( (ao->m_TriangleArrayCount[i] + (numverts-2)) < BUCKET_MAX_TRIANGLES)
{
if((ao->m_VertexArrayCache1[i]->size()+numverts < BUCKET_MAX_INDICES))
{
array = i;
ao->m_TriangleArrayCount[array]+=numverts-2;
break;
}
2002-10-12 11:37:38 +00:00
}
}
if (array == -1)
{
array = ao->m_VertexArrayCache1.size();
vector<RAS_TexVert>* va = new vector<RAS_TexVert>;
ao->m_VertexArrayCache1.push_back(va);
KX_IndexArray *ia = new KX_IndexArray();
ao->m_IndexArrayCache1.push_back(ia);
ao->m_TriangleArrayCount.push_back(numverts-2);
}
return array;
}
//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::UpdateMaterialList()
{
m_materials.clear();
unsigned int numpolys = m_Polygons.size();
2002-10-12 11:37:38 +00:00
// for all polygons, find out which material they use, and add it to the set of materials
for (unsigned int i=0;i<numpolys;i++)
2002-10-12 11:37:38 +00:00
{
m_materials.insert(m_Polygons[i]->GetMaterial());
}
}
struct RAS_MeshObject::polygonSlot
{
float m_z;
int m_index[4];
polygonSlot() {}
/* pnorm is the normal from the plane equation that the distance from is
* used to sort again. */
void get(const KX_VertexArray& vertexarray, const KX_IndexArray& indexarray,
int offset, int nvert, const MT_Vector3& pnorm)
{
MT_Vector3 center(0, 0, 0);
int i;
for(i=0; i<nvert; i++) {
m_index[i] = indexarray[offset+i];
center += vertexarray[m_index[i]].getLocalXYZ();
}
/* 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);
}
void set(KX_IndexArray& indexarray, int offset, int nvert)
{
int i;
for(i=0; i<nvert; i++)
indexarray[offset+i] = m_index[i];
}
};
struct RAS_MeshObject::backtofront
{
bool operator()(const polygonSlot &a, const polygonSlot &b) const
{
return a.m_z < b.m_z;
}
};
struct RAS_MeshObject::fronttoback
{
bool operator()(const polygonSlot &a, const polygonSlot &b) const
{
return a.m_z > b.m_z;
}
};
void RAS_MeshObject::SortPolygons(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.
if (!m_zsort)
return;
// Extract camera Z plane...
const MT_Vector3 pnorm(transform.getBasis()[2]);
// unneeded: const MT_Scalar pval = transform.getOrigin()[2];
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
{
if(!(*it)->IsZSort())
continue;
RAS_IPolyMaterial *mat = (*it)->GetPolyMaterial();
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
vecIndexArrays& indexarrays = ao->m_IndexArrayCache1;
vecVertexArray& vertexarrays = ao->m_VertexArrayCache1;
unsigned int i, j, nvert = (mat->UsesTriangles())? 3: 4;
for(i=0; i<indexarrays.size(); i++) {
KX_IndexArray& indexarray = *indexarrays[i];
KX_VertexArray& vertexarray = *vertexarrays[i];
unsigned int totpoly = indexarray.size()/nvert;
vector<polygonSlot> slots(totpoly);
/* get indices and z into temporary array */
for(j=0; j<totpoly; j++)
slots[j].get(vertexarray, indexarray, 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(indexarray, 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)
{
int i, numpolys = m_Polygons.size();
2002-10-12 11:37:38 +00:00
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
if ((*it)->IsZSort())
m_zsort = true;
2002-10-12 11:37:38 +00:00
if (drawingmode == RAS_IRasterizer::KX_WIREFRAME)
2002-10-12 11:37:38 +00:00
{
for (i=0;i<numpolys;i++)
{
RAS_Polygon* poly = m_Polygons[i];
if (poly->IsVisible())
ScheduleWireframePoly(poly->GetVertexIndexBase(),poly->VertexCount(),poly->GetEdgeCode(),
poly->GetMaterial()->GetPolyMaterial());
2002-10-12 11:37:38 +00:00
}
m_zsort = false;
2002-10-12 11:37:38 +00:00
}
else
{
for (i=0;i<numpolys;i++)
2002-10-12 11:37:38 +00:00
{
RAS_Polygon* poly = m_Polygons[i];
if (poly->IsVisible())
SchedulePoly(poly->GetVertexIndexBase(),poly->VertexCount(),
poly->GetMaterial()->GetPolyMaterial());
2002-10-12 11:37:38 +00:00
}
}
m_bModified = false;
m_MeshMod = true;
2002-10-12 11:37:38 +00:00
}
}