Game Development Reference
In-Depth Information
shader in order to obtain a highly populated 3D grid representation of the scene.
Listing 7.1 shows how this is done for DirectX 11 in HLSL.
// vertex shader
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;
output.position = float4(input.position,1.0f);
output.texCoords = input.texCoords;
output.normal = input.normal;
return output;
}
// geometry shader
static float3 viewDirections[3] =
{
float3(0.0f,0.0f,-1.0f), // back to front
float3(-1.0f,0.0f,0.0f), // right to left
float3(0.0f,-1.0f,0.0f) // top to down
};
int GetViewIndex(in float3 normal)
{
float3x3 directionMatrix;
directionMatrix[0] = -viewDirections[0];
directionMatrix[1] = -viewDirections[1];
directionMatrix[2] = -viewDirections[2];
float3 dotProducts = abs(mul(directionMatrix ,normal));
float maximum = max (max(dotProducts.x,dotProducts.y), dotProducts.z);
int index;
if (maximum==dotProducts.x)
index = 0;
else if (maximum==dotProducts.y)
index = 1;
else
index = 2;
return index;
}
[maxvertexcount(3)]
void main(triangle VS_OUTPUT input[3],inout TriangleStream <GS_OUTPUT>
outputStream)
{
float3 faceNormal = normalize(input[0].normal+input[1].normal+
input[2].normal);
// Get view, at which the current triangle is most visible, in order to
// achieve highest possible rasterization of the primitive.
int viewIndex = GetViewIndex(faceNormal);
GS_OUTPUT output[3];
[unroll]
for ( int i=0;i<3;i++)
{
output[i].position = mul(constBuffer.gridViewProjMatrices[viewIndex],
input[i].position);
output[i].positionWS = input[i].position.xyz; // world-space position
output[i].texCoords = input[i].texCoords;
output[i].normal = input[i].normal;
}
 
Search Nedrilad ::




Custom Search