Jump to content

Recommended Posts

Posted

I use this code to create the normals for my terrain model.  How would I create the Tangents and Bi-normals?  I can't use the surface->UpdateTangentsAndBinormals() function, as I want the code to able to update normals for specific vertices only.

How exactly do the tangents and bi-normals relate to the normal and how do they effect normal mapping?

Vec3 _cPos = _getTerrainVertexPosition(x, z);
Vec3 _lPos = _getTerrainVertexPosition(x - 1, z);
Vec3 _rPos = _getTerrainVertexPosition(x + 1, z);
Vec3 _uPos = _getTerrainVertexPosition(x, z + 1);
Vec3 _dPos = _getTerrainVertexPosition(x, z - 1);

//==================================================================================================//
//											TOP LEFT												//
//==================================================================================================//
_U.x = _uPos.x - _lPos.x;
_U.y = _uPos.y - _lPos.y;
_U.z = _uPos.z - _lPos.z;

_V.x = _cPos.x - _lPos.x;
_V.y = _cPos.y - _lPos.y;
_V.z = _cPos.z - _lPos.z;

_topLeft.x = (_U.y * _V.z) - (_U.z * _V.y);
_topLeft.y = (_U.z * _V.x) - (_U.x * _V.z);
_topLeft.z = (_U.x * _V.y) - (_U.y * _V.x);

//==================================================================================================//
//											TOP RIGHT												//
//==================================================================================================//
_U.x = _cPos.x - _rPos.x;
_U.y = _cPos.y - _rPos.y;
_U.z = _cPos.z - _rPos.z;

_V.x = _uPos.x - _rPos.x;
_V.y = _uPos.y - _rPos.y;
_V.z = _uPos.z - _rPos.z;

_topRight.x = (_U.y * _V.z) - (_U.z * _V.y);
_topRight.y = (_U.z * _V.x) - (_U.x * _V.z);
_topRight.z = (_U.x * _V.y) - (_U.y * _V.x);

//==================================================================================================//
//										BOTTOM LEFT													//
//==================================================================================================//
_U.x = _dPos.x - _cPos.x;
_U.y = _dPos.y - _cPos.y;
_U.z = _dPos.z - _cPos.z;

_V.x = _lPos.x - _cPos.x;
_V.y = _lPos.y - _cPos.y;
_V.z = _lPos.z - _cPos.z;

_bottomLeft.x = (_U.y * _V.z) - (_U.z * _V.y);
_bottomLeft.y = (_U.z * _V.x) - (_U.x * _V.z);
_bottomLeft.z = (_U.x * _V.y) - (_U.y * _V.x);

//==================================================================================================//
//										BOTTOM RIGHT												//
//==================================================================================================//
_U.x = _rPos.x - _cPos.x;
_U.y = _rPos.y - _cPos.y;
_U.z = _rPos.z - _cPos.z;

_V.x = _dPos.x - _cPos.x;
_V.y = _dPos.y - _cPos.y;
_V.z = _dPos.z - _cPos.z;

_bottomRight.x = (_U.y * _V.z) - (_U.z * _V.y);
_bottomRight.y = (_U.z * _V.x) - (_U.x * _V.z);
_bottomRight.z = (_U.x * _V.y) - (_U.y * _V.x);

_normal.x = (_topLeft.x + _topRight.x + _bottomLeft.x + _bottomRight.x) / 4.0f;
_normal.y = (_topLeft.y + _topRight.y + _bottomLeft.y + _bottomRight.y) / 4.0f;
_normal.z = (_topLeft.z + _topRight.z + _bottomLeft.z + _bottomRight.z) / 4.0f;

//==================================================================================================//
//										NORMALIZE													//
//==================================================================================================//
float _vectorLength = sqrt((_normal.x * _normal.x) + (_normal.y * _normal.y) + (_normal.z * _normal.z));
_normal.x = _normal.x / _vectorLength;
_normal.y = _normal.y / _vectorLength;
_normal.z = _normal.z / _vectorLength;

 

Posted

Tangents and binormals are a vector describing the way the texture mapping lies. You can call UpdateTangentsAndBinormals() once and thereafter you can change the normals and the tangents / binormals will be unaffected.

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...