interaction_skinned.vertex 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "global.inc"
  21. uniform matrices_ubo { float4 matrices[408]; };
  22. struct VS_IN {
  23. float4 position : POSITION;
  24. float2 texcoord : TEXCOORD0;
  25. float4 normal : NORMAL;
  26. float4 tangent : TANGENT;
  27. float4 color : COLOR0;
  28. float4 color2 : COLOR1;
  29. };
  30. struct VS_OUT {
  31. float4 position : POSITION;
  32. float4 texcoord0 : TEXCOORD0;
  33. float4 texcoord1 : TEXCOORD1;
  34. float4 texcoord2 : TEXCOORD2;
  35. float4 texcoord3 : TEXCOORD3;
  36. float4 texcoord4 : TEXCOORD4;
  37. float4 texcoord5 : TEXCOORD5;
  38. float4 texcoord6 : TEXCOORD6;
  39. float4 color : COLOR0;
  40. };
  41. void main( VS_IN vertex, out VS_OUT result ) {
  42. float4 vNormal = vertex.normal * 2.0 - 1.0;
  43. float4 vTangent = vertex.tangent * 2.0 - 1.0;
  44. float3 vBinormal = cross( vNormal.xyz, vTangent.xyz ) * vTangent.w;
  45. //--------------------------------------------------------------
  46. // GPU transformation of the normal / binormal / bitangent
  47. //
  48. // multiplying with 255.1 give us the same result and is faster than floor( w * 255 + 0.5 )
  49. //--------------------------------------------------------------
  50. const float w0 = vertex.color2.x;
  51. const float w1 = vertex.color2.y;
  52. const float w2 = vertex.color2.z;
  53. const float w3 = vertex.color2.w;
  54. float4 matX, matY, matZ; // must be float4 for vec4
  55. float joint = vertex.color.x * 255.1 * 3;
  56. matX = matrices[int(joint+0)] * w0;
  57. matY = matrices[int(joint+1)] * w0;
  58. matZ = matrices[int(joint+2)] * w0;
  59. joint = vertex.color.y * 255.1 * 3;
  60. matX += matrices[int(joint+0)] * w1;
  61. matY += matrices[int(joint+1)] * w1;
  62. matZ += matrices[int(joint+2)] * w1;
  63. joint = vertex.color.z * 255.1 * 3;
  64. matX += matrices[int(joint+0)] * w2;
  65. matY += matrices[int(joint+1)] * w2;
  66. matZ += matrices[int(joint+2)] * w2;
  67. joint = vertex.color.w * 255.1 * 3;
  68. matX += matrices[int(joint+0)] * w3;
  69. matY += matrices[int(joint+1)] * w3;
  70. matZ += matrices[int(joint+2)] * w3;
  71. float3 normal;
  72. normal.x = dot3( matX, vNormal );
  73. normal.y = dot3( matY, vNormal );
  74. normal.z = dot3( matZ, vNormal );
  75. normal = normalize( normal );
  76. float3 tangent;
  77. tangent.x = dot3( matX, vTangent );
  78. tangent.y = dot3( matY, vTangent );
  79. tangent.z = dot3( matZ, vTangent );
  80. tangent = normalize( tangent );
  81. float3 binormal;
  82. binormal.x = dot3( matX, vBinormal );
  83. binormal.y = dot3( matY, vBinormal );
  84. binormal.z = dot3( matZ, vBinormal );
  85. binormal = normalize( binormal );
  86. float4 modelPosition;
  87. modelPosition.x = dot4( matX, vertex.position );
  88. modelPosition.y = dot4( matY, vertex.position );
  89. modelPosition.z = dot4( matZ, vertex.position );
  90. modelPosition.w = 1.0;
  91. result.position.x = dot4( modelPosition, rpMVPmatrixX );
  92. result.position.y = dot4( modelPosition, rpMVPmatrixY );
  93. result.position.z = dot4( modelPosition, rpMVPmatrixZ );
  94. result.position.w = dot4( modelPosition, rpMVPmatrixW );
  95. float4 defaultTexCoord = float4( 0.0f, 0.5f, 0.0f, 1.0f );
  96. //calculate vector to light in R0
  97. float4 toLight = rpLocalLightOrigin - modelPosition;
  98. //--------------------------------------------------------------
  99. //result.texcoord0 is the direction to the light in tangent space
  100. result.texcoord0.x = dot3( tangent, toLight );
  101. result.texcoord0.y = dot3( binormal, toLight );
  102. result.texcoord0.z = dot3( normal, toLight );
  103. result.texcoord0.w = 1.0f;
  104. //textures 1 takes the base coordinates by the texture matrix
  105. result.texcoord1 = defaultTexCoord;
  106. result.texcoord1.x = dot4( vertex.texcoord.xy, rpBumpMatrixS );
  107. result.texcoord1.y = dot4( vertex.texcoord.xy, rpBumpMatrixT );
  108. //# texture 2 has one texgen
  109. result.texcoord2 = defaultTexCoord;
  110. result.texcoord2.x = dot4( modelPosition, rpLightFalloffS );
  111. //# texture 3 has three texgens
  112. result.texcoord3.x = dot4( modelPosition, rpLightProjectionS );
  113. result.texcoord3.y = dot4( modelPosition, rpLightProjectionT );
  114. result.texcoord3.z = 0.0f;
  115. result.texcoord3.w = dot4( modelPosition, rpLightProjectionQ );
  116. //# textures 4 takes the base coordinates by the texture matrix
  117. result.texcoord4 = defaultTexCoord;
  118. result.texcoord4.x = dot4( vertex.texcoord.xy, rpDiffuseMatrixS );
  119. result.texcoord4.y = dot4( vertex.texcoord.xy, rpDiffuseMatrixT );
  120. //# textures 5 takes the base coordinates by the texture matrix
  121. result.texcoord5 = defaultTexCoord;
  122. result.texcoord5.x = dot4( vertex.texcoord.xy, rpSpecularMatrixS );
  123. result.texcoord5.y = dot4( vertex.texcoord.xy, rpSpecularMatrixT );
  124. //# texture 6's texcoords will be the halfangle in texture space
  125. //# calculate normalized vector to light in R0
  126. toLight = normalize( toLight );
  127. //# calculate normalized vector to viewer in R1
  128. float4 toView = normalize( rpLocalViewOrigin - modelPosition );
  129. //# add together to become the half angle vector in object space (non-normalized)
  130. float4 halfAngleVector = toLight + toView;
  131. //# put into texture space
  132. result.texcoord6.x = dot3( tangent, halfAngleVector );
  133. result.texcoord6.y = dot3( binormal, halfAngleVector );
  134. result.texcoord6.z = dot3( normal, halfAngleVector );
  135. result.texcoord6.w = 1.0f;
  136. // for joint transformation of the tangent space, we use color and
  137. // color2 for weighting information, so hopefully there aren't any
  138. // effects that need vertex color...
  139. result.color = float4( 1.0f, 1.0f, 1.0f, 1.0f );
  140. //# generate the vertex color, which can be 1.0, color, or 1.0 - color
  141. //# for 1.0 : env[16] = 0, env[17] = 1
  142. //# for color : env[16] = 1, env[17] = 0
  143. //# for 1.0-color : env[16] = -1, env[17] = 1
  144. // result.color = ( swizzleColor( vertex.color ) * rpVertexColorModulate ) + rpVertexColorAdd;
  145. }