OgreTangentSpaceCalc.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef _OgreTangentSpaceCalc_H_
#define _OgreTangentSpaceCalc_H_
#include "OgrePrerequisites.h"
#include "OgreRenderOperation.h"
#include "OgreVector2.h"
#include "OgreVector3.h"
#include "OgreVertexIndexData.h"
#include "OgreHeaderPrefix.h"
namespace Ogre
{
class _OgreExport TangentSpaceCalc
{
public:
TangentSpaceCalc();
virtual ~TangentSpaceCalc();
typedef std::pair<size_t, size_t> VertexSplit;
struct IndexRemap
{
size_t indexSet;
size_t faceIndex;
VertexSplit splitVertex;
IndexRemap() {} // to keep container happy
IndexRemap(size_t i, size_t f, const VertexSplit& s) : indexSet(i), faceIndex(f), splitVertex(s) {}
};
typedef list<IndexRemap>::type IndexRemapList;
typedef list<VertexSplit>::type VertexSplits;
struct Result
{
VertexSplits vertexSplits;
IndexRemapList indexesRemapped;
};
void clear();
void setVertexData(VertexData* v_in);
void addIndexData(IndexData* i_in, RenderOperation::OperationType opType = RenderOperation::OT_TRIANGLE_LIST);
void setStoreParityInW(bool enabled) { mStoreParityInW = enabled; }
bool getStoreParityInW() const { return mStoreParityInW; }
void setSplitMirrored(bool split) { mSplitMirrored = split; }
bool getSplitMirrored() const { return mSplitMirrored; }
void setSplitRotated(bool split) { mSplitRotated = split; }
bool getSplitRotated() const { return mSplitRotated; }
Result build(VertexElementSemantic targetSemantic = VES_TANGENT,
unsigned short sourceTexCoordSet = 0, unsigned short index = 1);
protected:
VertexData* mVData;
typedef vector<IndexData*>::type IndexDataList;
typedef vector<RenderOperation::OperationType>::type OpTypeList;
IndexDataList mIDataList;
OpTypeList mOpTypes;
bool mSplitMirrored;
bool mSplitRotated;
bool mStoreParityInW;
struct VertexInfo
{
Vector3 pos;
Vector3 norm;
Vector2 uv;
Vector3 tangent;
Vector3 binormal;
// Which way the tangent space is oriented (+1 / -1) (set on first time found)
int parity;
// What index the opposite parity vertex copy is at (0 if not created yet)
size_t oppositeParityIndex;
VertexInfo() : tangent(Vector3::ZERO), binormal(Vector3::ZERO),
parity(0), oppositeParityIndex(0) {}
};
typedef vector<VertexInfo>::type VertexInfoArray;
VertexInfoArray mVertexArray;
void extendBuffers(VertexSplits& splits);
void insertTangents(Result& res,
VertexElementSemantic targetSemantic,
unsigned short sourceTexCoordSet, unsigned short index);
void populateVertexArray(unsigned short sourceTexCoordSet);
void processFaces(Result& result);
void calculateFaceTangentSpace(const size_t* vertInd, Vector3& tsU, Vector3& tsV, Vector3& tsN);
Real calculateAngleWeight(size_t v0, size_t v1, size_t v2);
int calculateParity(const Vector3& u, const Vector3& v, const Vector3& n);
void addFaceTangentSpaceToVertices(size_t indexSet, size_t faceIndex, size_t *localVertInd,
const Vector3& faceTsU, const Vector3& faceTsV, const Vector3& faceNorm, Result& result);
void normaliseVertices();
void remapIndexes(Result& res);
template <typename T>
void remapIndexes(T* ibuf, size_t indexSet, Result& res)
{
for (IndexRemapList::iterator i = res.indexesRemapped.begin();
i != res.indexesRemapped.end(); ++i)
{
IndexRemap& remap = *i;
// Note that because this is a vertex split situation, and vertex
// split is only for some faces, it's not a case of replacing all
// instances of vertex index A with vertex index B
// It actually matters which triangle we're talking about, so drive
// the update from the face index
if (remap.indexSet == indexSet)
{
T* pBuf;
pBuf = ibuf + remap.faceIndex * 3;
for (int v = 0; v < 3; ++v, ++pBuf)
{
if (*pBuf == remap.splitVertex.first)
{
*pBuf = (T)remap.splitVertex.second;
}
}
}
}
}
};
}
#include "OgreHeaderSuffix.h"
#endif
Ogre::TangentSpaceCalc::mIDataList
IndexDataList mIDataList
Definition: OgreTangentSpaceCalc.h:189
Ogre::TangentSpaceCalc::mSplitRotated
bool mSplitRotated
Definition: OgreTangentSpaceCalc.h:192
OgreVector3.h
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::list
Definition: OgrePrerequisites.h:505
Ogre::TangentSpaceCalc::VertexSplits
list< VertexSplit >::type VertexSplits
Definition: OgreTangentSpaceCalc.h:74
Ogre::VertexData
Summary class collecting together vertex source information.
Definition: OgreVertexIndexData.h:49
Ogre::TangentSpaceCalc::VertexInfo
Definition: OgreTangentSpaceCalc.h:196
Ogre::TangentSpaceCalc::mVData
VertexData * mVData
Definition: OgreTangentSpaceCalc.h:186
Ogre::TangentSpaceCalc::IndexRemap::splitVertex
VertexSplit splitVertex
The old and new vertex index.
Definition: OgreTangentSpaceCalc.h:65
Ogre::TangentSpaceCalc::Result
The result of having built a tangent space basis.
Definition: OgreTangentSpaceCalc.h:77
Ogre::Vector2
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::TangentSpaceCalc::IndexDataList
vector< IndexData * >::type IndexDataList
Definition: OgreTangentSpaceCalc.h:187
Ogre::IndexData
Summary class collecting together index data source information.
Definition: OgreVertexIndexData.h:242
Ogre::TangentSpaceCalc::IndexRemap::IndexRemap
IndexRemap()
Definition: OgreTangentSpaceCalc.h:67
Ogre::TangentSpaceCalc::getSplitRotated
bool getSplitRotated() const
Sets whether or not to split vertices when tangent space rotates more than 90 degrees around a vertex...
Definition: OgreTangentSpaceCalc.h:156
Ogre::TangentSpaceCalc::VertexInfo::oppositeParityIndex
size_t oppositeParityIndex
Definition: OgreTangentSpaceCalc.h:206
Ogre::TangentSpaceCalc::IndexRemap::IndexRemap
IndexRemap(size_t i, size_t f, const VertexSplit &s)
Definition: OgreTangentSpaceCalc.h:68
Ogre::TangentSpaceCalc::IndexRemap
Information about a remapped index.
Definition: OgreTangentSpaceCalc.h:58
Ogre::TangentSpaceCalc::Result::indexesRemapped
IndexRemapList indexesRemapped
A list of indexes which were affected by splits.
Definition: OgreTangentSpaceCalc.h:86
Ogre::TangentSpaceCalc::getStoreParityInW
bool getStoreParityInW() const
Gets whether to store tangent space parity in the W of a 4-component tangent or not.
Definition: OgreTangentSpaceCalc.h:115
Ogre::TangentSpaceCalc::mVertexArray
VertexInfoArray mVertexArray
Definition: OgreTangentSpaceCalc.h:212
Ogre::TangentSpaceCalc::remapIndexes
void remapIndexes(T *ibuf, size_t indexSet, Result &res)
Definition: OgreTangentSpaceCalc.h:230
OgreVertexIndexData.h
Ogre::Vector3
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
Ogre::TangentSpaceCalc::VertexInfo::VertexInfo
VertexInfo()
Definition: OgreTangentSpaceCalc.h:208
OgrePrerequisites.h
Ogre::TangentSpaceCalc::setSplitMirrored
void setSplitMirrored(bool split)
Sets whether or not to split vertices when a mirrored tangent space transition is detected (matrix pa...
Definition: OgreTangentSpaceCalc.h:131
Ogre::TangentSpaceCalc::VertexInfo::norm
Vector3 norm
Definition: OgreTangentSpaceCalc.h:199
Ogre::TangentSpaceCalc::VertexInfo::tangent
Vector3 tangent
Definition: OgreTangentSpaceCalc.h:201
Ogre::TangentSpaceCalc::setSplitRotated
void setSplitRotated(bool split)
Sets whether or not to split vertices when tangent space rotates more than 90 degrees around a vertex...
Definition: OgreTangentSpaceCalc.h:152
Ogre::TangentSpaceCalc::setStoreParityInW
void setStoreParityInW(bool enabled)
Sets whether to store tangent space parity in the W of a 4-component tangent or not.
Definition: OgreTangentSpaceCalc.h:112
Ogre::TangentSpaceCalc::VertexInfo::binormal
Vector3 binormal
Definition: OgreTangentSpaceCalc.h:202
Ogre::TangentSpaceCalc::mOpTypes
OpTypeList mOpTypes
Definition: OgreTangentSpaceCalc.h:190
OgreRenderOperation.h
Ogre::VertexElementSemantic
VertexElementSemantic
Vertex element semantics, used to identify the meaning of vertex buffer contents. ...
Definition: OgreHardwareVertexBuffer.h:99
Ogre::TangentSpaceCalc::VertexInfo::parity
int parity
Definition: OgreTangentSpaceCalc.h:204
Ogre::TangentSpaceCalc::mStoreParityInW
bool mStoreParityInW
Definition: OgreTangentSpaceCalc.h:193
Ogre::RenderOperation::OperationType
OperationType
The rendering operation type to perform.
Definition: OgreRenderOperation.h:48
Ogre::TangentSpaceCalc::IndexRemapList
list< IndexRemap >::type IndexRemapList
List of indexes that were remapped (split vertices).
Definition: OgreTangentSpaceCalc.h:72
Ogre::VES_TANGENT
Tangent (X axis if normal is Z)
Definition: OgreHardwareVertexBuffer.h:117
OgreHeaderSuffix.h
Ogre::TangentSpaceCalc::VertexInfo::uv
Vector2 uv
Definition: OgreTangentSpaceCalc.h:200
Ogre::TangentSpaceCalc::VertexInfoArray
vector< VertexInfo >::type VertexInfoArray
Definition: OgreTangentSpaceCalc.h:211
Ogre::RenderOperation::OT_TRIANGLE_LIST
A list of triangles, 3 vertices per triangle.
Definition: OgreRenderOperation.h:56
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::vector
Definition: OgrePrerequisites.h:491
OgreHeaderPrefix.h
OgreVector2.h
Ogre::TangentSpaceCalc::mSplitMirrored
bool mSplitMirrored
Definition: OgreTangentSpaceCalc.h:191
Ogre::TangentSpaceCalc::VertexInfo::pos
Vector3 pos
Definition: OgreTangentSpaceCalc.h:198
Ogre::TangentSpaceCalc::IndexRemap::indexSet
size_t indexSet
Index data set (can be >0 if more than one index data was added)
Definition: OgreTangentSpaceCalc.h:61
Ogre::TangentSpaceCalc
Class for calculating a tangent space basis.
Definition: OgreTangentSpaceCalc.h:49
Ogre::TangentSpaceCalc::getSplitMirrored
bool getSplitMirrored() const
Gets whether or not to split vertices when a mirrored tangent space transition is detected...
Definition: OgreTangentSpaceCalc.h:136
Ogre::TangentSpaceCalc::OpTypeList
vector< RenderOperation::OperationType >::type OpTypeList
Definition: OgreTangentSpaceCalc.h:188
Ogre::TangentSpaceCalc::IndexRemap::faceIndex
size_t faceIndex
The position in the index buffer that&#39;s affected.
Definition: OgreTangentSpaceCalc.h:63
Ogre::TangentSpaceCalc::Result::vertexSplits
VertexSplits vertexSplits
A list of vertex indices which were split off into new vertices because of mirroring.
Definition: OgreTangentSpaceCalc.h:83
Ogre::TangentSpaceCalc::VertexSplit
std::pair< size_t, size_t > VertexSplit
Definition: OgreTangentSpaceCalc.h:55

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.