blender/source/gameengine/Rasterizer/RAS_MeshObject.cpp

510 lines
11 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
*/
#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
RAS_MeshObject::RAS_MeshObject(Mesh* mesh, int lightlayer)
: m_lightlayer(lightlayer),
m_bModified(true),
m_bMeshModified(true),
m_mesh(mesh),
m_bDeformed(false)
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
for(it=m_Polygons.begin(); it!=m_Polygons.end(); it++)
delete (*it);
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()
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_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();
}
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_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
}
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);
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
if(!flat) {
/* 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
if(!flat) {
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
}
void RAS_MeshObject::AddMeshUser(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) {
/* always copy from the base slot, which is never removed
* since new objects can be created with the same mesh data */
RAS_MeshSlot *ms = it->m_bucket->CopyMesh(it->m_baseslot);
ms->m_clientObj = clientobj;
it->m_slots.insert(clientobj, ms);
2002-10-12 11:37:38 +00:00
}
}
void RAS_MeshObject::UpdateBuckets(void* clientobj,
double* oglmatrix,
bool useObjectColor,
const MT_Vector4& rgbavec,
bool visible,
bool culled)
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
ms->m_mesh = this;
ms->m_OpenGLMatrix = oglmatrix;
ms->m_bObjectColor = useObjectColor;
ms->m_RGBAcolor = rgbavec;
ms->m_bVisible = visible;
ms->m_bCulled = culled || !visible;
/* split if necessary */
ms->Split();
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
}
}