Advertisement
mrDIMAS

Deferred Shading v4

May 15th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.42 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Shader.h"
  4.  
  5. class DeferredRenderer
  6. {
  7. private:
  8.   struct QuadVertex
  9.   {
  10.     float x, y, z;
  11.     float tx, ty;
  12.  
  13.     QuadVertex( float ax, float ay, float az, float atx, float aty )
  14.     {
  15.       x = ax;
  16.       y = ay;
  17.       z = az;
  18.  
  19.       tx = atx;
  20.       ty = aty;
  21.     }
  22.   };
  23.  
  24.   IDirect3DTexture9 * depthMap;
  25.   IDirect3DTexture9 * normalMap;
  26.   IDirect3DTexture9 * diffuseMap;
  27.  
  28.   IDirect3DSurface9 * positionSurface;
  29.   IDirect3DSurface9 * normalSurface;
  30.   IDirect3DSurface9 * diffuseSurface;
  31.   IDirect3DSurface9 * backSurface;
  32.  
  33.   VertexShader * vertexShaderPassOne;
  34.   PixelShader * pixelShaderPassOne;
  35.  
  36.   VertexShader * vertexShaderPassTwo;
  37.   PixelShader * pixelShaderPassTwo;
  38.  
  39.   IDirect3DVertexBuffer9 * screenQuad;
  40.   IDirect3DVertexDeclaration9 * vertexDeclaration;
  41.  
  42.   D3DXHANDLE v1World;
  43.   D3DXHANDLE v1Proj;
  44.   D3DXHANDLE v1View;
  45.  
  46.   D3DXHANDLE v2World;
  47.   D3DXHANDLE v2Proj;
  48.   D3DXHANDLE v2View;
  49.  
  50.   D3DXHANDLE p2LightPos;
  51.   D3DXHANDLE p2LightRange;
  52.   D3DXHANDLE p2CameraPos;
  53.   D3DXHANDLE p2InvViewProj;
  54.  
  55.  
  56.  
  57.   DWORD icosphereFVF;
  58.   DWORD icosphereStride;
  59.   DWORD icosphereVertexCount;
  60.   DWORD icosphereFaceCount;
  61.   IDirect3DVertexBuffer9 * icosphereVB;
  62.   IDirect3DIndexBuffer9 * icosphereIB;
  63. public:
  64.  
  65.   void CreateScreenQuad()
  66.   {
  67.     g_device->CreateVertexBuffer( 6 * sizeof( QuadVertex ), D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_TEX1, D3DPOOL_DEFAULT, &screenQuad, 0 );
  68.  
  69.     QuadVertex vertices[ ] = { QuadVertex( -0.5, -0.5, 0, 0, 0 ), QuadVertex( g_width - 0.5, 0 - 0.5, 0, 1, 0 ), QuadVertex ( 0 - 0.5, g_height - 0.5, 0, 0, 1 ),
  70.       QuadVertex( g_width - 0.5, 0 - 0.5, 0, 1, 0 ), QuadVertex( g_width - 0.5, g_height - 0.5, 0, 1, 1 ), QuadVertex ( 0 - 0.5, g_height - 0.5, 0, 0, 1 ) };
  71.  
  72.     void * data = 0;
  73.  
  74.     screenQuad->Lock( 0, 0, &data, 0 );
  75.     memcpy( data, vertices, sizeof( QuadVertex ) * 6 );
  76.     screenQuad->Unlock( );
  77.  
  78.     D3DVERTEXELEMENT9 quadVertexDeclation[ ] =
  79.     {
  80.       { 0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  81.       { 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
  82.       D3DDECL_END()
  83.     };
  84.  
  85.     g_device->CreateVertexDeclaration( quadVertexDeclation, &vertexDeclaration );
  86.   }
  87.  
  88.   void CreateRenderTargets()
  89.   {
  90.     D3DCAPS9 caps;  g_device->GetDeviceCaps( &caps );
  91.  
  92.     if( caps.NumSimultaneousRTs < 4 )
  93.       MessageBoxA( 0, "Your video card does not support MRT.", 0, MB_OK | MB_ICONERROR );
  94.  
  95.     if( FAILED( D3DXCreateTexture( g_device, g_width, g_height, 0, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &depthMap )))
  96.       MessageBoxA( 0, "Failed to create 'Depth Map' texture.", 0, MB_OK | MB_ICONERROR );
  97.     if( FAILED( D3DXCreateTexture( g_device, g_width, g_height, 0, D3DUSAGE_RENDERTARGET, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &normalMap )))
  98.       MessageBoxA( 0, "Failed to create 'Normal Map' texture.", 0, MB_OK | MB_ICONERROR );
  99.     if( FAILED( D3DXCreateTexture( g_device, g_width, g_height, 0, D3DUSAGE_RENDERTARGET, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &diffuseMap )))
  100.       MessageBoxA( 0, "Failed to create 'Diffuse Map' texture.", 0, MB_OK | MB_ICONERROR );
  101.  
  102.     depthMap->GetSurfaceLevel( 0, &positionSurface );
  103.     normalMap->GetSurfaceLevel( 0, &normalSurface );
  104.     diffuseMap->GetSurfaceLevel( 0, &diffuseSurface );
  105.  
  106.     g_device->GetRenderTarget( 0, &backSurface );
  107.  
  108.     ID3DXMesh * icosphere = 0;
  109.     D3DXCreateSphere( g_device, 1.0, 6, 6, &icosphere, 0 );
  110.  
  111.     void * dataSrc = 0;
  112.     void * dataDest = 0;
  113.     icosphereFVF = icosphere->GetFVF();
  114.     icosphereStride = icosphere->GetNumBytesPerVertex();
  115.     icosphereFaceCount = icosphere->GetNumFaces();
  116.     icosphereVertexCount = icosphere->GetNumVertices();
  117.  
  118.     // copy vertex buffer
  119.     int size = icosphere->GetNumBytesPerVertex() * icosphere->GetNumVertices();
  120.     g_device->CreateVertexBuffer( size, D3DUSAGE_WRITEONLY, icosphereFVF, D3DPOOL_DEFAULT, &icosphereVB, 0 );
  121.     icosphereVB->Lock( 0, 0, &dataDest, 0 );
  122.     icosphere->LockVertexBuffer( 0, &dataSrc );
  123.     memcpy( dataDest, dataSrc, size );
  124.     icosphere->UnlockVertexBuffer();
  125.     icosphereVB->Unlock();
  126.  
  127.     // copy index buffer
  128.     size = icosphere->GetNumFaces() * sizeof( short ) * 3;
  129.     g_device->CreateIndexBuffer( size, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &icosphereIB, 0 );
  130.     icosphereIB->Lock( 0, 0, &dataDest, 0 );
  131.     icosphere->LockIndexBuffer( 0, &dataSrc );
  132.     memcpy( dataDest, dataSrc, size );
  133.     icosphere->UnlockIndexBuffer();
  134.     icosphereIB->Unlock();
  135.  
  136.     icosphere->Release();
  137.   }
  138.  
  139.   void InitPassOneShaders()
  140.   {
  141.     // PASS 1 - Filling the G-Buffer
  142.     string vertexSourcePassOne =
  143.       "float4x4 g_world;\n"
  144.       "float4x4 g_view;\n"
  145.       "float4x4 g_projection;\n"
  146.  
  147.       "struct VS_INPUT {\n"
  148.       "  float4 position : POSITION;\n"
  149.       "  float4 normal   : NORMAL;\n"
  150.       "  float2 texCoord : TEXCOORD0;\n"
  151.       "};\n"
  152.  
  153.       "struct VS_OUTPUT {\n"
  154.       "  float4 position : POSITION;\n"
  155.       "  float4 screenPosition : TEXCOORD0;\n"
  156.       "  float3 normal : TEXCOORD1;\n"
  157.       "  float2 diffuseMapCoord : TEXCOORD2;\n"
  158.       "};\n"
  159.  
  160.       "VS_OUTPUT main(VS_INPUT input) {\n"
  161.       "  VS_OUTPUT output;\n"
  162.       "  float4x4 worldViewProj = mul(mul(g_world, g_view), g_projection);\n"
  163.       "  output.position = mul(input.position, worldViewProj);\n"
  164.       "  output.normal = normalize(mul(input.normal, (float3x3)g_world));\n"
  165.       "  output.screenPosition = output.position;\n"
  166.       "  output.diffuseMapCoord = input.texCoord;\n"
  167.       "  return output;\n"
  168.       "};\n";
  169.  
  170.     vertexShaderPassOne = new VertexShader( vertexSourcePassOne );
  171.  
  172.     v1World = vertexShaderPassOne->GetConstantTable()->GetConstantByName( 0, "g_world" );
  173.     v1Proj = vertexShaderPassOne->GetConstantTable()->GetConstantByName( 0, "g_projection" );
  174.     v1View = vertexShaderPassOne->GetConstantTable()->GetConstantByName( 0, "g_view" );
  175.  
  176.     string pixelSourcePassOne =
  177.  
  178.       "texture diffuseMap;\n"
  179.  
  180.       "sampler diffuseSampler : register(s0) = sampler_state\n"
  181.       "{\n"
  182.       "   texture = <diffuseMap>;\n"
  183.       "};\n"
  184.  
  185.       "struct PS_INPUT {\n"
  186.       "  float4 screenPos : TEXCOORD0;\n"
  187.       "  float3 normal : TEXCOORD1;\n"      
  188.       "  float2 diffuseMapCoord : TEXCOORD2;\n"
  189.       "};\n"
  190.  
  191.       "struct MRT_OUTPUT {\n"
  192.       "  float4 depth : COLOR0;\n"
  193.       "  float4 normal : COLOR1;\n"
  194.       "  float4 diffuseMap : COLOR2;\n"
  195.       "};\n"
  196.  
  197.       "MRT_OUTPUT main( PS_INPUT input )\n"      
  198.       "{\n"
  199.       "   MRT_OUTPUT output;\n"
  200.       "   float3 normal = normalize( input.normal );\n"
  201.       "   float depth = input.screenPos.z / input.screenPos.w;\n"
  202.       "   output.depth = float4( depth, depth, depth, 1.0 );\n"
  203.       "   output.normal = float4( normal.x, normal.y, normal.z, 1.0 );\n"
  204.       "   output.diffuseMap = tex2D( diffuseSampler, input.diffuseMapCoord );\n"
  205.       "   return output;"
  206.       "};\n"
  207.       ;
  208.  
  209.     pixelShaderPassOne = new PixelShader( pixelSourcePassOne );
  210.   }
  211.  
  212.   DeferredRenderer()
  213.   {
  214.     CreateScreenQuad();
  215.     CreateRenderTargets();      
  216.     InitPassOneShaders();
  217.     InitPassTwoShaders();
  218.   }
  219.  
  220.   void InitPassTwoShaders()
  221.   {
  222.     // PASS 2 - Final render
  223.     string vertexSourcePassTwo =
  224.       "float4x4 g_world;\n"
  225.       "float4x4 g_view;\n"
  226.       "float4x4 g_projection;\n"
  227.       "struct VS_INPUT {\n"
  228.       "  float4 position : POSITION;\n"
  229.       "  float3 texcoord : TEXCOORD0;\n"
  230.       "};\n"
  231.  
  232.       "struct VS_OUTPUT {\n"
  233.       "  float4 position : POSITION;\n"
  234.       "  float3 texcoord : TEXCOORD0;\n"
  235.       "};\n"
  236.  
  237.       "VS_OUTPUT main(VS_INPUT input) {\n"
  238.       "  VS_OUTPUT output;\n"
  239.       "  float4x4 worldViewProj = mul(mul(g_world, g_view), g_projection);\n"
  240.       "  output.position   = mul(input.position, worldViewProj);\n"
  241.       "  output.texcoord   = input.texcoord;\n"
  242.       "  return output;\n"
  243.       "};\n";
  244.  
  245.     vertexShaderPassTwo = new VertexShader( vertexSourcePassTwo );
  246.  
  247.     v2World = vertexShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "g_world" );
  248.     v2Proj = vertexShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "g_projection" );
  249.     v2View = vertexShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "g_view" );
  250.  
  251.     string pixelSourcePassTwo =
  252.      
  253.       "texture depthMap;\n"
  254.       "texture normalMap;\n"
  255.       "texture diffuseMap;\n"
  256.  
  257.       "sampler depthSampler : register(s0) = sampler_state\n"
  258.       "{\n"
  259.       "   texture = <depthMap>;\n"
  260.       "};\n"
  261.  
  262.       "sampler normalSampler : register(s1) = sampler_state\n"
  263.       "{\n"
  264.       "   texture = <normalMap>;\n"
  265.       "};\n"    
  266.  
  267.       "sampler diffuseSampler : register(s2) = sampler_state\n"
  268.       "{\n"
  269.       "   texture = <diffuseMap>;\n"
  270.       "};\n"
  271.  
  272.       // light props
  273.       "float3 lightPos;\n"
  274.       "float lightRange;\n"
  275.  
  276.       // camera props
  277.       "float3 cameraPosition;\n"
  278.          
  279.       "float4x4 invViewProj;\n"
  280.       "float4 main( float3 texcoord : TEXCOORD0 ) : COLOR0\n"
  281.       "{\n"
  282.  
  283.       "   float4 diffuseTexel = tex2D( diffuseSampler, texcoord.xy );\n"
  284.       "   float depth = tex2D( depthSampler, texcoord.xy ).r;\n"
  285.       "   float3 n = tex2D( normalSampler, texcoord.xy ).xyz;\n"
  286.            
  287.       "   float4 screenPosition;\n"
  288.       "   screenPosition.x = texcoord.x * 2.0f - 1.0f;\n"
  289.       "   screenPosition.y = -(texcoord.y * 2.0f - 1.0f);\n"
  290.       "   screenPosition.z = depth;\n"
  291.       "   screenPosition.w = 1.0f;\n"
  292.  
  293.       "   float4 p = mul( screenPosition, invViewProj );\n"
  294.       "   p /= p.w;\n"  
  295.            
  296.       "   float3 lightDirection = lightPos - p;"
  297.       "   float3 l = normalize( lightDirection );\n"
  298.  
  299.       // specular
  300.  
  301.       "   float3 v = normalize( cameraPosition - p );\n"
  302.       "   float3 r = reflect( -v, n );\n"      
  303.       "   float spec = pow( saturate( dot( l, r ) ), 20.0 );\n"
  304.  
  305.       // diffuse
  306.       "   float diff = saturate(dot( l, n ));\n"
  307.  
  308.       "   float falloff = pow( lightRange, 2 ) / pow( dot( lightDirection, lightDirection ), 2 );\n"
  309.  
  310.       "   float o = clamp( falloff, 0, 2 ) * (  diff + spec );\n"
  311.  
  312.       "   return float4( diffuseTexel.x * o, diffuseTexel.y * o, diffuseTexel.z * o, diffuseTexel.w );\n"
  313.       "};\n";
  314.  
  315.  
  316.     pixelShaderPassTwo = new PixelShader( pixelSourcePassTwo );
  317.  
  318.     p2LightPos = pixelShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "lightPos" );
  319.     p2LightRange = pixelShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "lightRange" );
  320.     p2CameraPos = pixelShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "cameraPosition" );
  321.     p2InvViewProj  = pixelShaderPassTwo->GetConstantTable()->GetConstantByName( 0, "invViewProj" );
  322.   }
  323.  
  324.   ~DeferredRenderer()
  325.   {
  326.     if( depthMap )
  327.       depthMap->Release();
  328.     if( normalMap )
  329.       normalMap->Release();
  330.     if( diffuseMap )
  331.       diffuseMap->Release();
  332.  
  333.     if( positionSurface )
  334.       positionSurface->Release();
  335.     if( normalSurface )
  336.       normalSurface->Release();
  337.     if( diffuseSurface )
  338.       diffuseSurface->Release();
  339.   }
  340.  
  341.   void Begin()
  342.   {
  343.     g_device->SetRenderTarget( 0, positionSurface );
  344.     g_device->SetRenderTarget( 1, normalSurface );
  345.     g_device->SetRenderTarget( 2, diffuseSurface );
  346.  
  347.     g_device->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , D3DCOLOR_XRGB( 0, 0, 0 ), 1.0, 0 );
  348.     g_device->BeginScene( );
  349.  
  350.     vertexShaderPassOne->Bind();
  351.     pixelShaderPassOne->Bind();
  352.  
  353.     g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE );
  354.   }
  355.  
  356.   void RenderMesh( Mesh * mesh )
  357.   {
  358.     g_device->SetStreamSource( 0, mesh->vb, 0, sizeof( Vertex ));
  359.     g_device->SetIndices( mesh->ib );
  360.  
  361.     D3DXMATRIX view; g_device->GetTransform( D3DTS_VIEW, &view );
  362.     D3DXMATRIX proj; g_device->GetTransform( D3DTS_PROJECTION, &proj );
  363.     D3DXMATRIX world; GetD3DMatrixFromBulletTransform( mesh->parent->globalTransform, world );
  364.  
  365.     vertexShaderPassOne->GetConstantTable()->SetMatrix( g_device, v1View, &view );
  366.     vertexShaderPassOne->GetConstantTable()->SetMatrix( g_device, v1Proj, &proj );  
  367.     vertexShaderPassOne->GetConstantTable()->SetMatrix( g_device, v1World, &world );
  368.  
  369.     g_device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, mesh->vertexCount, 0, mesh->faceCount );
  370.   }
  371.  
  372.   void End()
  373.   {
  374.     g_device->EndScene();
  375.  
  376.     g_device->SetRenderTarget( 0, backSurface );
  377.     g_device->SetRenderTarget( 1, 0 );
  378.     g_device->SetRenderTarget( 2, 0 );
  379.  
  380.     g_device->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_STENCIL, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0, 0 );
  381.     g_device->BeginScene( );
  382.    
  383.     IDirect3DStateBlock9 * state;
  384.     g_device->CreateStateBlock( D3DSBT_ALL, &state );
  385.  
  386.     g_device->SetRenderState( D3DRS_LIGHTING, FALSE );    
  387.    
  388.  
  389.     D3DXMATRIX ortho; D3DXMatrixOrthoOffCenterLH ( &ortho, 0, g_width, g_height, 0, 0, 1024 );
  390.     D3DXMATRIX identity; D3DXMatrixIdentity( &identity );
  391.  
  392.     vertexShaderPassTwo->GetConstantTable()->SetMatrix( g_device, v2World, &identity );
  393.     vertexShaderPassTwo->GetConstantTable()->SetMatrix( g_device, v2View, &identity );
  394.     vertexShaderPassTwo->GetConstantTable()->SetMatrix( g_device, v2Proj, &ortho );
  395.  
  396.     g_device->SetTexture( 0, depthMap );
  397.     g_device->SetTexture( 1, normalMap );
  398.     g_device->SetTexture( 2, diffuseMap );  
  399.  
  400.     g_device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE );
  401.     g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE );
  402.     g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE );
  403.     g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE );
  404.  
  405.     float temp[ 3 ];
  406.  
  407.     btVector3 cameraPos = g_camera->base->globalTransform.getOrigin();
  408.  
  409.     temp[ 0 ] = cameraPos.x();
  410.     temp[ 1 ] = cameraPos.y();
  411.     temp[ 2 ] = cameraPos.z();
  412.  
  413.     pixelShaderPassTwo->GetConstantTable()->SetFloatArray( g_device, p2CameraPos, temp, 3 );
  414.  
  415.     D3DXMATRIX view; g_device->GetTransform( D3DTS_VIEW, &view );
  416.     D3DXMATRIX proj; g_device->GetTransform( D3DTS_PROJECTION, &proj );
  417.  
  418.     D3DXMATRIX viewProj;
  419.     D3DXMatrixMultiply( &viewProj, &view, &proj );
  420.  
  421.     D3DXMATRIX invViewProj;
  422.     D3DXMatrixInverse( &invViewProj, 0, &viewProj );
  423.  
  424.     pixelShaderPassTwo->GetConstantTable()->SetMatrix( g_device, p2InvViewProj, &invViewProj );
  425.  
  426.    
  427.     g_device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
  428.  
  429.     g_device->SetRenderState(D3DRS_STENCILREF, 0x0 );
  430.     g_device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
  431.     g_device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
  432.  
  433.     g_device->SetRenderState( D3DRS_STENCILENABLE, TRUE );
  434.     g_device->SetRenderState( D3DRS_TWOSIDEDSTENCILMODE, TRUE );
  435.  
  436.     for( int i = 0; i < g_lights.size(); i++ )
  437.     {
  438.       Light * light = g_lights.at( i );      
  439.  
  440.       btVector3 lightPos = light->base->globalTransform.getOrigin();
  441.  
  442.       temp[ 0 ] = lightPos.x();
  443.       temp[ 1 ] = lightPos.y();
  444.       temp[ 2 ] = lightPos.z();            
  445.  
  446.       float scl = sqrt( 5 * light->radius );
  447.       D3DXMATRIX scale; D3DXMatrixScaling( &scale, scl,scl,scl );
  448.       D3DXMATRIX trans; D3DXMatrixTranslation( &trans, lightPos.x(), lightPos.y(), lightPos.z() );
  449.       D3DXMATRIX world; D3DXMatrixMultiply( &world, &scale, &trans );          
  450.          
  451.  
  452.       // use default shaders
  453.       g_device->SetVertexShader( 0 );
  454.       g_device->SetPixelShader( 0 );
  455.      
  456.       g_device->SetTransform( D3DTS_WORLD, &world );
  457.       g_device->SetTransform( D3DTS_VIEW, &view );
  458.       g_device->SetTransform( D3DTS_PROJECTION, &proj );      
  459.      
  460.       // disable draw into color buffer
  461.       g_device->SetRenderState( D3DRS_COLORWRITEENABLE, 0x00000000 );
  462.  
  463.       g_device->SetRenderState( D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP );
  464.       g_device->SetRenderState( D3DRS_STENCILPASS, D3DSTENCILOP_KEEP );
  465.  
  466.       g_device->SetStreamSource( 0, icosphereVB, 0, icosphereStride );
  467.       g_device->SetIndices( icosphereIB );
  468.       g_device->SetFVF( icosphereFVF );
  469.  
  470.       g_device->SetRenderState( D3DRS_STENCILFUNC, D3DCMP_ALWAYS );
  471.       g_device->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_DECR );
  472.  
  473.       g_device->SetRenderState( D3DRS_CCW_STENCILFUNC, D3DCMP_ALWAYS );
  474.       g_device->SetRenderState( D3DRS_CCW_STENCILZFAIL, D3DSTENCILOP_INCR );
  475.  
  476.       // draw a sphere bounds light into stencil buffer
  477.       g_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  478.       g_device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, icosphereVertexCount, 0, icosphereFaceCount );
  479.      
  480.       vertexShaderPassTwo->Bind();
  481.       pixelShaderPassTwo->Bind();
  482.  
  483.       pixelShaderPassTwo->GetConstantTable()->SetFloatArray( g_device, p2LightPos, temp, 3 );
  484.       pixelShaderPassTwo->GetConstantTable()->SetFloat( g_device, p2LightRange, light->radius );
  485.      
  486.       // enable draw into color buffer
  487.       g_device->SetRenderState( D3DRS_COLORWRITEENABLE, 0xFFFFFFFF );
  488.      
  489.       /////////////////////////////////////////////////////////////////////////////
  490.       // draw a screen quad
  491.       g_device->SetStreamSource( 0, screenQuad, 0, sizeof( QuadVertex ));
  492.       g_device->SetVertexDeclaration( vertexDeclaration );
  493.  
  494.       // disable culling at all
  495.       g_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  496.  
  497.       g_device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_NOTEQUAL);
  498.       g_device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_ZERO ); // fill stencil with zeros
  499.  
  500.       g_device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );      
  501.     }    
  502.  
  503.     state->Apply();
  504.     state->Release();
  505.  
  506.     g_device->SetTexture( 0, 0 );
  507.     g_device->SetTexture( 1, 0 );
  508.     g_device->SetTexture( 2, 0 );
  509.  
  510.  
  511.   }
  512. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement