BVB Source Codes

CRYENGINE Show TangentSpaceCalculation.h Source code

Return Download CRYENGINE: download TangentSpaceCalculation.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. //
  4. // Used with permission to distribute for non-commercial purposes.
  5. //
  6. //
  7. //      File:TangentSpaceCalculation.h
  8. //  Description: calculated the tangent space base vector for a given mesh
  9. //  Dependencies: none
  10. //  Documentation: "How to calculate tangent base vectors.doc"
  11. //
  12. //      History:
  13. //      - 12/07/2002: Created by Martin Mittring as part of CryEngine
  14. //  - 08/18/2003: MM improved stability (no illegal floats) with bad input data
  15. //  - 08/19/2003: MM added check for input data problems (DebugMesh() is deactivated by default)
  16. //  - 10/02/2003: MM removed rundundant code
  17. //  - 10/01/2004: MM added errorcodes (NAN texture coordinates)
  18. //  - 05/21/2005: MM made proxy interface typesafe
  19. //  - 22/09/2012: Bogdan refactored and added Mikkelsen's Tangent Basis algorithm to have better support for mirrored geometry
  20. //
  21. //////////////////////////////////////////////////////////////////////
  22.  
  23. #ifndef TANGENTSPACECALCULATION_H
  24. #define TANGENTSPACECALCULATION_H
  25.  
  26. #pragma once
  27.  
  28. enum eCalculateTangentSpaceErrorCode
  29. {
  30.         CALCULATE_TANGENT_SPACE_NO_ERRORS,
  31.         BROKEN_TEXTURE_COORDINATES,
  32.         VERTICES_SHARING_COORDINATES,
  33.         ALL_VERTICES_ON_THE_SAME_VECTOR,
  34.         MEMORY_ALLOCATION_FAILED
  35. };
  36.  
  37. class ITriangleInputProxy
  38. {
  39. public:
  40.         virtual ~ITriangleInputProxy(){}
  41.  
  42.         virtual uint32 GetTriangleCount() const = 0;
  43.         virtual void   GetTriangleIndices(const uint32 indwTriNo, uint32 outdwPos[3], uint32 outdwNorm[3], uint32 outdwUV[3]) const = 0;
  44.         virtual void   GetPos(const uint32 indwPos, Vec3& outfPos) const = 0;
  45.         virtual void   GetUV(const uint32 indwPos, Vec2& outfUV) const = 0;
  46.         virtual void   GetNorm(const uint32 indwTriNo, const uint32 indwVertNo, Vec3& outfNorm) const = 0;
  47. };
  48.  
  49. class CTangentSpaceCalculation
  50. {
  51. public:
  52.         //! /param inInput - the normals are only used as smoothing input - we calculate the normals ourself
  53.         eCalculateTangentSpaceErrorCode CalculateTangentSpace(const ITriangleInputProxy& inInput, bool bUseCustomNormals, bool bIgnoreDegeneracies, string& errorMessage);
  54.  
  55.         size_t                          GetBaseCount();
  56.         void                            GetTriangleBaseIndices(const uint32 indwTriNo, uint32 outdwBase[3]);
  57.  
  58.         //! returns a orthogonal base (perpendicular and normalized)
  59.         void GetBase(const uint32 indwPos, float* outU, float* outV, float* outN);
  60.  
  61. private:
  62.  
  63.         struct CBase33
  64.         {
  65.                 CBase33();
  66.                 CBase33(const Vec3& Uval, const Vec3& Vval, const Vec3& Nval);
  67.  
  68.                 Vec3 u;
  69.                 Vec3 v;
  70.                 Vec3 n; // part of the tangent base but can be used also as vertex normal
  71.         };
  72.  
  73.         struct CVec3PredicateLess
  74.         {
  75.                 bool operator()(const Vec3& first, const Vec3& second) const;
  76.         };
  77.  
  78.         struct CBase33PredicateLess
  79.         {
  80.                 bool operator()(const CBase33& first, const CBase33& second) const;
  81.         };
  82.  
  83.         struct CBaseIndex
  84.         {
  85.                 // position index in the positions stream
  86.                 uint32 m_posIndex;
  87.                 // normal index in the vertex normals stream
  88.                 uint32 m_normIndex;
  89.         };
  90.  
  91.         struct CBaseIndexOrder : public std::binary_function<CBaseIndex, CBaseIndex, bool>
  92.         {
  93.                 bool operator()(const CBaseIndex& a, const CBaseIndex& b) const;
  94.         };
  95.  
  96.         struct CTriBaseIndex
  97.         {
  98.                 uint32 p[3]; //!< index in m_BaseVectors
  99.         };
  100.  
  101.         // [dwTriangleCount]
  102.         std::vector<CTriBaseIndex> m_trianglesBaseAssigment;
  103.         // [0..] generated output data
  104.         std::vector<CBase33>       m_baseVectors;
  105.  
  106.         eCalculateTangentSpaceErrorCode CalculateTangentSpaceMikk(const ITriangleInputProxy& inInput, string& errorMessage);
  107.  
  108.         CBase33& GetBase(std::multimap<CBaseIndex, uint32, CBaseIndexOrder>& inMap, const uint32 indwPosNo, const uint32 indwNormNo);
  109.         uint32   AddUV2Base(std::multimap<CBaseIndex, uint32, CBaseIndexOrder>& inMap, const uint32 indwPosNo, const uint32 indwNormNo, const Vec3& inU, const Vec3& inV, const Vec3& inNormN, bool bIgnoreDegeneracies);
  110.         void     AddNormal2Base(std::multimap<CBaseIndex, uint32, CBaseIndexOrder>& inMap, const uint32 indwPosNo, const uint32 indwNormNo, const Vec3& inNormal);
  111.         Vec3     Rotate(const Vec3& vFrom, const Vec3& vTo, const Vec3& vInput);
  112.         void     DebugMesh(const ITriangleInputProxy& inInput) const;
  113.         float    CalcAngleBetween(const Vec3& invA, const Vec3& invB);
  114. };
  115.  
  116. #endif
  117.  
downloadTangentSpaceCalculation.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top