interactionAmbient_skinned.vertex 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 texcoord1 : TEXCOORD1;
  33. float4 texcoord2 : TEXCOORD2;
  34. float4 texcoord3 : TEXCOORD3;
  35. float4 texcoord4 : TEXCOORD4;
  36. float4 texcoord5 : TEXCOORD5;
  37. float4 texcoord6 : TEXCOORD6;
  38. float4 color : COLOR0;
  39. };
  40. void main( VS_IN vertex, out VS_OUT result ) {
  41. float4 vNormal = vertex.normal * 2.0 - 1.0;
  42. float4 vTangent = vertex.tangent * 2.0 - 1.0;
  43. float3 vBinormal = cross( vNormal.xyz, vTangent.xyz ) * vTangent.w;
  44. //--------------------------------------------------------------
  45. // GPU transformation of the normal / binormal / bitangent
  46. //
  47. // multiplying with 255.1 give us the same result and is faster than floor( w * 255 + 0.5 )
  48. //--------------------------------------------------------------
  49. const float w0 = vertex.color2.x;
  50. const float w1 = vertex.color2.y;
  51. const float w2 = vertex.color2.z;
  52. const float w3 = vertex.color2.w;
  53. float4 matX, matY, matZ; // must be float4 for vec4
  54. float joint = vertex.color.x * 255.1 * 3;
  55. matX = matrices[int(joint+0)] * w0;
  56. matY = matrices[int(joint+1)] * w0;
  57. matZ = matrices[int(joint+2)] * w0;
  58. joint = vertex.color.y * 255.1 * 3;
  59. matX += matrices[int(joint+0)] * w1;
  60. matY += matrices[int(joint+1)] * w1;
  61. matZ += matrices[int(joint+2)] * w1;
  62. joint = vertex.color.z * 255.1 * 3;
  63. matX += matrices[int(joint+0)] * w2;
  64. matY += matrices[int(joint+1)] * w2;
  65. matZ += matrices[int(joint+2)] * w2;
  66. joint = vertex.color.w * 255.1 * 3;
  67. matX += matrices[int(joint+0)] * w3;
  68. matY += matrices[int(joint+1)] * w3;
  69. matZ += matrices[int(joint+2)] * w3;
  70. float3 normal;
  71. normal.x = dot3( matX, vNormal );
  72. normal.y = dot3( matY, vNormal );
  73. normal.z = dot3( matZ, vNormal );
  74. normal = normalize( normal );
  75. float3 tangent;
  76. tangent.x = dot3( matX, vTangent );
  77. tangent.y = dot3( matY, vTangent );
  78. tangent.z = dot3( matZ, vTangent );
  79. tangent = normalize( tangent );
  80. float3 binormal;
  81. binormal.x = dot3( matX, vBinormal );
  82. binormal.y = dot3( matY, vBinormal );
  83. binormal.z = dot3( matZ, vBinormal );
  84. binormal = normalize( binormal );
  85. float4 modelPosition;
  86. modelPosition.x = dot4( matX, vertex.position );
  87. modelPosition.y = dot4( matY, vertex.position );
  88. modelPosition.z = dot4( matZ, vertex.position );
  89. modelPosition.w = 1.0;
  90. result.position.x = dot4( modelPosition, rpMVPmatrixX );
  91. result.position.y = dot4( modelPosition, rpMVPmatrixY );
  92. result.position.z = dot4( modelPosition, rpMVPmatrixZ );
  93. result.position.w = dot4( modelPosition, rpMVPmatrixW );
  94. float4 defaultTexCoord = float4( 0.0f, 0.5f, 0.0f, 1.0f );
  95. //calculate vector to light in R0
  96. float4 toLight = rpLocalLightOrigin - modelPosition;
  97. //textures 1 takes the base coordinates by the texture matrix
  98. result.texcoord1 = defaultTexCoord;
  99. result.texcoord1.x = dot4( vertex.texcoord.xy, rpBumpMatrixS );
  100. result.texcoord1.y = dot4( vertex.texcoord.xy, rpBumpMatrixT );
  101. //# texture 2 has one texgen
  102. result.texcoord2 = defaultTexCoord;
  103. result.texcoord2.x = dot4( modelPosition, rpLightFalloffS );
  104. //# texture 3 has three texgens
  105. result.texcoord3.x = dot4( modelPosition, rpLightProjectionS );
  106. result.texcoord3.y = dot4( modelPosition, rpLightProjectionT );
  107. result.texcoord3.z = 0.0f;
  108. result.texcoord3.w = dot4( modelPosition, rpLightProjectionQ );
  109. //# textures 4 takes the base coordinates by the texture matrix
  110. result.texcoord4 = defaultTexCoord;
  111. result.texcoord4.x = dot4( vertex.texcoord.xy, rpDiffuseMatrixS );
  112. result.texcoord4.y = dot4( vertex.texcoord.xy, rpDiffuseMatrixT );
  113. //# textures 5 takes the base coordinates by the texture matrix
  114. result.texcoord5 = defaultTexCoord;
  115. result.texcoord5.x = dot4( vertex.texcoord.xy, rpSpecularMatrixS );
  116. result.texcoord5.y = dot4( vertex.texcoord.xy, rpSpecularMatrixT );
  117. //# texture 6's texcoords will be the halfangle in texture space
  118. //# calculate normalized vector to light in R0
  119. toLight = normalize( toLight );
  120. //# calculate normalized vector to viewer in R1
  121. float4 toView = normalize( rpLocalViewOrigin - modelPosition );
  122. //# add together to become the half angle vector in object space (non-normalized)
  123. float4 halfAngleVector = toLight + toView;
  124. //# put into texture space
  125. result.texcoord6.x = dot3( tangent, halfAngleVector );
  126. result.texcoord6.y = dot3( binormal, halfAngleVector );
  127. result.texcoord6.z = dot3( normal, halfAngleVector );
  128. result.texcoord6.w = 1.0f;
  129. //# generate the vertex color, which can be 1.0, color, or 1.0 - color
  130. //# for 1.0 : env[16] = 0, env[17] = 1
  131. //# for color : env[16] = 1, env[17] = 0
  132. //# for 1.0-color : env[16] = -1, env[17] = 1
  133. result.color = ( swizzleColor( vertex.color ) * rpVertexColorModulate ) + rpVertexColorAdd;
  134. }