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