bool StencilShadow::Initialize() { // Create a vertex shader DWORD dwVertexDecl[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // v0 = Position D3DVSD_REG( 1, D3DVSDT_FLOAT1 ), // v1 = Extrusion determinant D3DVSD_END() }; if(!( CreateVertexShader(Sys_RemapPath("base\\media\\shadow.xvu"), dwVertexDecl, &m_dwVertexShaderShadow))) return false; return true; }
//----------------------------------------------------------------------------- // Name: XBUtil_DeclaratorFromFVF() // Desc: Create a vertex declaration from an FVF. Registers are assigned as // follows: // v0 = Vertex position // v1 = Vertex blend weights // v2 = Vertex normal // v3 = Vertex diffuse color // v4 = Vertex specular color // // v5 = Vertex fog (no FVF code) // // v6 = Vertex pointsize (no FVF code) // // v7 = Vertex back diffuse color (no FVF code) // // v8 = Vertex back specular color (no FVF code) // v9-v12 = Vertex texture coords //----------------------------------------------------------------------------- HRESULT XBUtil_DeclaratorFromFVF( DWORD dwFVF, DWORD Declaration[MAX_FVF_DECL_SIZE] ) { // Start the declaration DWORD decl = 0; Declaration[decl++] = D3DVSD_STREAM(0); // Handle position DWORD dwPositionFVF = ( dwFVF & D3DFVF_POSITION_MASK ); if( dwPositionFVF == D3DFVF_XYZRHW ) Declaration[decl++] = D3DVSD_REG( 0, D3DVSDT_FLOAT4 ); else Declaration[decl++] = D3DVSD_REG( 0, D3DVSDT_FLOAT3 ); // Handle blend weights if( dwPositionFVF == D3DFVF_XYZB1 ) Declaration[decl++] = D3DVSD_REG( 1, D3DVSDT_FLOAT1 ); if( dwPositionFVF == D3DFVF_XYZB2 ) Declaration[decl++] = D3DVSD_REG( 1, D3DVSDT_FLOAT2 ); if( dwPositionFVF == D3DFVF_XYZB3 ) Declaration[decl++] = D3DVSD_REG( 1, D3DVSDT_FLOAT3 ); if( dwPositionFVF == D3DFVF_XYZB4 ) Declaration[decl++] = D3DVSD_REG( 1, D3DVSDT_FLOAT4 ); // Handle normal, diffuse, and specular if( dwFVF & D3DFVF_NORMAL ) Declaration[decl++] = D3DVSD_REG( 2, D3DVSDT_FLOAT3 ); if( dwFVF & D3DFVF_DIFFUSE ) Declaration[decl++] = D3DVSD_REG( 3, D3DVSDT_D3DCOLOR ); if( dwFVF & D3DFVF_SPECULAR ) Declaration[decl++] = D3DVSD_REG( 4, D3DVSDT_D3DCOLOR ); // Handle texture coordinates DWORD dwNumTextures = (dwFVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT; for( DWORD i=0; i<dwNumTextures; i++ ) { DWORD dwTexCoordSize = ( dwFVF & (0x00030000<<(i*2)) ); DWORD dwNumTexCoords = 0; if( dwTexCoordSize == D3DFVF_TEXCOORDSIZE1(i) ) dwNumTexCoords = D3DVSDT_FLOAT1; if( dwTexCoordSize == D3DFVF_TEXCOORDSIZE2(i) ) dwNumTexCoords = D3DVSDT_FLOAT2; if( dwTexCoordSize == D3DFVF_TEXCOORDSIZE3(i) ) dwNumTexCoords = D3DVSDT_FLOAT3; if( dwTexCoordSize == D3DFVF_TEXCOORDSIZE4(i) ) dwNumTexCoords = D3DVSDT_FLOAT4; Declaration[decl++] = D3DVSD_REG( 9 + i, dwNumTexCoords ); } // End the declarator Declaration[decl++] = D3DVSD_END(); return S_OK; }
HRESULT IDX8PixelVertexShaderImp::LoadAndCompileShader() { // VertexShader Instructions DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(1, D3DVSDT_FLOAT3), D3DVSD_END() }; HRESULT hr; TCHAR *vertexShaderPath = FindMapFile(map->GetVertexShaderFile()); if(!vertexShaderPath) { return E_FAIL; } hr = D3DXAssembleShaderFromFile(vertexShaderPath , 0, NULL, &pCode, NULL); if (dwVertexShader) { pd3dDevice->DeleteVertexShader(dwVertexShader); } if(FAILED(hr)) { return E_FAIL; } hr = pd3dDevice->CreateVertexShader(dwDecl, (LPDWORD)pCode->GetBufferPointer(), &dwVertexShader, 0); return hr; }
HRESULT IDX8PixelVertexShaderImp::InitValid(MNMesh *mnmesh, INode *node) { HRESULT hr = S_OK; DebugPrint("DX8 Vertex Shader Init\n"); // VertexShader Instructions DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(1, D3DVSDT_FLOAT3), D3DVSD_END() }; //Declaration.Append(4, &Decl[0]); // VertexShader Constants Constants.SetCount(20); Constants[CV_ZERO] = D3DXVECTOR4(0.0f,0.0f,0.0f,0.0f); Constants[CV_ONE] = D3DXVECTOR4(1.0f,1.0f,1.0f,1.0f); Constants[CV_HALF] = D3DXVECTOR4(0.5f,0.5f,0.5f,0.5f); // pd3dDevice->SetRenderState(D3DRS_SOFTWAREVERTEXPROCESSING, true); pd3dDevice->SetVertexShader(dwVertexShader); pd3dDevice->SetVertexShaderConstant(CV_ZERO, Constants[CV_ZERO], 1); pd3dDevice->SetVertexShaderConstant(CV_ONE, Constants[CV_ONE], 1); pd3dDevice->SetVertexShaderConstant(CV_HALF, Constants[CV_HALF], 1); map->SetInitVS(false); return hr; }
HRESULT SampleShaderPluginVertexShader::InitValid(Mesh *mesh, INode *node) { HRESULT hr = S_OK; m_pINode = node; // Create a vertex shader for doing the effect LPD3DXBUFFER *ppCode = &pCode; LPD3DXBUFFER pBuffer = NULL; //! VertexShader Declarations //! VertexShader Constants Constants.SetCount(20); //! set up the material vertex color ... D3DMATERIAL8 mtrl; ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) ); mtrl.Diffuse.r = mtrl.Ambient.r = m_MtlColor.r; mtrl.Diffuse.g = mtrl.Ambient.g = m_MtlColor.g; mtrl.Diffuse.b = mtrl.Ambient.b = m_MtlColor.b; mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; pd3dDevice->SetMaterial( &mtrl ); pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff ); /*! * D3DXAssembleShader * Assembles an ASCII description of a shader into binary form, where the shader source is in memory. * * @param pSrcData : [in] Pointer to the source code. * @param SrcDataLen : [in] Size of the source code, in bytes. * @param Flags : [in] A combination of the following flags, specifying assembly options. * D3DXASM_DEBUG Inserts debugging information as comments in the assembled shader. * D3DXASM_SKIPVALIDATION Do not validate the generated code against known capabilities and constraints. This option is recommended only when assembling a shader you know will function (that is, the shader has been assembled before without this option.) * @param ppConstants : [out] Returns a pointer to an ID3DXBuffer interface, representing the returned constant declarations. These constants are returned as a vertex shader declaration fragment. It is up to the application to insert the contents of this buffer into their declaration. For pixel shaders this parameter is meaningless because constant declarations are included in the assembled shader. This parameter is ignored if it is NULL. * @param ppCompiledShader : [out] Returns a pointer to an ID3DXBuffer interface, representing the returned compiled object code. This parameter is ignored if it is NULL. * @param ppCompilationErrors : [out] Returns a pointer to an ID3DXBuffer interface, representing the returned ASCII error messages. This parameter is ignored if it is NULL. * * @return HRESULT : HRESULT D3DXAssembleShader( LPCVOID pSrcData, UINT SrcDataLen, DWORD Flags, LPD3DXBUFFER* ppConstants, LPD3DXBUFFER* ppCompiledShader, LPD3DXBUFFER* ppCompilationErrors ); */ //! Specify the vertex format that the vertex shader will be using for doing the effect DWORD dwVertexFormatDefinition[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // v0 = Position D3DVSD_REG( 1, D3DVSDT_FLOAT3 ), // v1 = Normal D3DVSD_END() }; #if 0 TCHAR *vertexShaderPath = FindMapFile("ambient.njv"); hr = D3DXAssembleShaderFromFile(vertexShaderPath , 0, NULL, &pCode, NULL); #else hr = D3DXAssembleShader( SurfaceShader , sizeof(SurfaceShader)-1 , 0 , NULL , &pCode , NULL ); #endif hr = pd3dDevice->CreateVertexShader(dwVertexFormatDefinition,(LPDWORD)pCode->GetBufferPointer(), &dwVertexShader, 0); initDone = true; return hr; }
// // Globals // //_____________________________________________________________________________ DWORD gVertexDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), // Position D3DVSD_REG(1, D3DVSDT_FLOAT3), // Normal D3DVSD_REG(2, D3DVSDT_FLOAT3), // S D3DVSD_REG(3, D3DVSDT_FLOAT2), // Texture1 D3DVSD_REG(4, D3DVSDT_FLOAT2), // Texture2 D3DVSD_REG(5, D3DVSDT_FLOAT2), // Texture3 D3DVSD_REG(6, D3DVSDT_FLOAT2), // Texture4 D3DVSD_END() }; int gVIndex[3]; //_____________________________________________________________________________ // // Functions // //_____________________________________________________________________________ //_____________________________________ // // Default constructor // //_____________________________________
HRESULT RacorX5::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); VERTICES cp[9]; cp[0].vPosition = D3DXVECTOR3(-1, 1, 0); cp[0].uv = D3DXVECTOR2(0.0f, 0.0f); cp[1].vPosition = D3DXVECTOR3(0, 1, 0); cp[1].uv = D3DXVECTOR2(0.5f, 0.0f); cp[2].vPosition = D3DXVECTOR3(1, 1, 0); cp[2].uv = D3DXVECTOR2(1.0f, 0.0f); cp[3].vPosition = D3DXVECTOR3(-1, 0, 0); cp[3].uv = D3DXVECTOR2(0.0f, 0.5f); cp[4].vPosition = D3DXVECTOR3(0, 0, -2); cp[4].uv = D3DXVECTOR2(0.5f, 0.5f); cp[5].vPosition = D3DXVECTOR3(1, 0, 0); cp[5].uv = D3DXVECTOR2(1.0f, 0.5f); cp[6].vPosition = D3DXVECTOR3(-1, -1, 0); cp[6].uv = D3DXVECTOR2(0.0f, 1.0f); cp[7].vPosition = D3DXVECTOR3(0, -1, 0); cp[7].uv = D3DXVECTOR2(0.5f, 1.0f); cp[8].vPosition = D3DXVECTOR3(1, -1, 0); cp[8].uv = D3DXVECTOR2(1.0f, 1.0f); m_spBPatch->m_dwULevel = 4; m_spBPatch->m_dwVLevel = 4; m_spBPatch->Create(cp, 3, 3); m_spBPatch->RestoreDeviceObjects(m_spDevice.get()); DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(3, D3DVSDT_FLOAT3), D3DVSD_REG(7, D3DVSDT_FLOAT2), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), dwDecl, L"point_light.vso", &m_dwVSH); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0); return E_FAIL; } IDirect3DTexture8* backgroud; hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("ShaderX.tga"), &backgroud); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0); return E_FAIL; } m_spBackground.reset(backgroud, [](IDirect3DTexture8* background){ background->Release(); }); m_spDevice->SetViewport(&m_Viewport); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE); m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); m_spDevice->SetVertexShaderConstant(SPEC_POWER, D3DXVECTOR4(0,10,25,50),1); //D3DXVECTOR3 vLight(0, 0, 1); //m_spDevice->SetVertexShaderConstant(LIGHT_POSITION, vLight, 1); D3DXCOLOR matDiffuse(0.9f, 0.9f, 0.9f, 1.0f); m_spDevice->SetVertexShaderConstant(DIFFUSE_COLOR, &matDiffuse, 1); D3DXVECTOR4 Attenuation = { 1.0f, 0.0f, 0.0f, 0.0f }; m_spDevice->SetVertexShaderConstant(LIGHT_ATTENUATION, Attenuation, 1); return S_OK; }
HRESULT RacorX8::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); //CreateSphere(); LoadXFile("sphere.x"); IDirect3DVertexBuffer8* vbNormal; hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbNormal); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0); return E_FAIL; } hr = CreateNormal<ShaderVertex, SimpleVertex>(m_spVB.get(), vbNormal); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateNormal failed!", L"Error", 0); return E_FAIL; } m_spVBNormal.reset(vbNormal, [](IDirect3DVertexBuffer8* vbNormal){vbNormal->Release(); }); IDirect3DVertexBuffer8* vbTangent; hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbTangent); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0); return E_FAIL; } hr = CreateTangent<ShaderVertex, SimpleVertex>(m_spVB.get(), vbTangent); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateTangent failed!", L"Error", 0); return E_FAIL; } m_spVBTangent.reset(vbTangent, [](IDirect3DVertexBuffer8* vbTangent){vbTangent->Release(); }); //DWORD dwDecl[MAX_FVF_DECL_SIZE]; //ZeroMemory(dwDecl, sizeof dwDecl); //pSphere->GetDeclaration(dwDecl); DWORD decl[] = { D3DVSD_STREAM(0), D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3), D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2), D3DVSD_REG(D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"specular.vso", &m_dwVSH); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"specular.pso", &m_dwPSH); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump.pso", &m_dwPSHBump); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } if (m_bPS14Avaliable) { hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump14.pso", &m_dwPSHBump14); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } } DWORD declLine[] = { D3DVSD_STREAM(0), D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), declLine, L"line.vso", &m_dwVSHLine); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"line.pso", &m_dwVPHLine); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } IDirect3DTexture8* color_map; hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earth.bmp"), &color_map); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0); return E_FAIL; } m_spColorMap.reset(color_map, [](IDirect3DTexture8* color_map){ color_map->Release(); }); IDirect3DTexture8* heightMap; hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earthbump.bmp"), &heightMap); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0); return E_FAIL; } m_spHeightMap.reset(heightMap, [](IDirect3DTexture8* heightMap) { heightMap->Release(); }); D3DSURFACE_DESC desc; m_spHeightMap->GetLevelDesc(0, &desc); IDirect3DTexture8* normalMap; hr = D3DXCreateTexture(m_spDevice.get(), desc.Width, desc.Height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &normalMap); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0); return E_FAIL; } m_spNormalMap.reset(normalMap, [](IDirect3DTexture8* normalMap){ normalMap->Release(); }); hr = D3DXComputeNormalMap(m_spNormalMap.get(), m_spHeightMap.get(), NULL, 0, D3DX_CHANNEL_RED, 10); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXComputeNormalMap failed!", L"Error", 0); return E_FAIL; } IDirect3DTexture8* power; hr = D3DXCreateTexture(m_spDevice.get(), 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &power); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0); return E_FAIL; } m_spPower.reset(power, [](IDirect3DTexture8* power){ power->Release(); }); FLOAT fPower = 16.0f; hr = D3DXFillTexture(power, LightEval, &fPower); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXFillTexture failed!", L"Error", 0); return E_FAIL; } m_spDevice->SetViewport(&m_Viewport); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); //m_spDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); //m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); //m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE); //m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); //m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); //m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); //m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); //m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); //m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); //m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); //m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); D3DXVECTOR4 vLight(0.0f, 0.0f, 1.0f, 0.0f); D3DXVec4Normalize(&vLight, &vLight); m_spDevice->SetVertexShaderConstant(12, &vLight, 1); D3DXVECTOR4 half(0.5f, 0.5f, 0.5f, 0.5f); m_spDevice->SetVertexShaderConstant(33, &half, 1); return S_OK; }
bool InitVertexShader() { #if DIRECTX_VERSION == 8 // shader decl DWORD decl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // D3DVSDE_POSITION D3DVSD_REG(1, D3DVSDT_FLOAT3 ), // D3DVSDE_NORMAL D3DVSD_REG(2, D3DVSDT_D3DCOLOR ), // D3DVSDE_DIFFUSE D3DVSD_REG(3, D3DVSDT_FLOAT2 ), // D3DVSDE_TEXCOORD0 D3DVSD_END() }; #else D3DVERTEXELEMENT9 decl[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 }, { 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, { 0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; #endif LPD3DXBUFFER pCode; LPD3DXBUFFER ppCompilationErrors; HRESULT res; #ifdef _DEBUG #if DIRECTX_VERSION == 8 LPD3DXBUFFER ppConstants; res = D3DXAssembleShaderFromFile("shader.vsh", D3DXASM_DEBUG, &ppConstants, &pCode, &ppCompilationErrors); #else res = D3DXAssembleShaderFromFileA("D:\\n64developing\\RiceVideo\\shaderdx9.vsh", NULL, NULL, D3DXSHADER_DEBUG, &pCode, &ppCompilationErrors); //res = D3DXAssembleShaderFromFileA("D:\\n64developing\\RiceVideo\\shaderdx9-2.vsh", NULL, NULL, D3DXSHADER_DEBUG, &pCode, &ppCompilationErrors); #endif #else #if DIRECTX_VERSION == 8 LPD3DXBUFFER ppConstants; res = D3DXAssembleShader(shaderstr, strlen(shaderstr), 0, &ppConstants, &pCode, &ppCompilationErrors); #else res = D3DXAssembleShader(shaderstr2, strlen(shaderstr2), NULL, NULL, D3DXSHADER_DEBUG, &pCode, &ppCompilationErrors); #endif #endif if( CDXGraphicsContext::IsResultGood(res,true) ) { #if DIRECTX_VERSION == 8 res = g_pD3DDev->CreateVertexShader( decl, (DWORD*)pCode->GetBufferPointer(), &gVertexShader, 0 ); #else res = g_pD3DDev->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(), &gVertexShader ); //FILE *fp = fopen("D:\\n64developing\\RiceVideo\\shaderdx9.vso","rb"); //BYTE buf[4000]; //int num = fread(buf, 1, 4000, fp); //res = g_pD3DDev->CreateVertexShader( (DWORD*)buf, &gVertexShader ); //fclose(fp); #endif pCode->Release(); if( !CDXGraphicsContext::IsResultGood(res,true) ) return false; res = g_pD3DDev->SetVertexShader( gVertexShader ); InitVertexShaderConstants(); return true; } else { if( ppCompilationErrors ) { char* str = (char*)(ppCompilationErrors->GetBufferPointer()); TRACE0(str); } return false; } }
bool CD3D8ShaderMaterialRenderer::createVertexShader(const char* vtxsh, E_VERTEX_TYPE type) { if (!vtxsh) return true; // compile shader #if defined( _IRR_XBOX_PLATFORM_) return false; #else LPD3DXBUFFER code = 0; LPD3DXBUFFER errors = 0; #ifdef _IRR_D3D_NO_SHADER_DEBUGGING // compile shader without debug info D3DXAssembleShader(vtxsh, (UINT)strlen(vtxsh), 0, 0, &code, &errors); #else // compile shader and emitt some debug informations to // make it possible to debug the shader in visual studio static int irr_dbg_file_nr = 0; ++irr_dbg_file_nr; char tmp[32]; sprintf(tmp, "irr_d3d8_dbg_shader_%d.vsh", irr_dbg_file_nr); FILE* f = fopen(tmp, "wb"); fwrite(vtxsh, strlen(vtxsh), 1, f); fflush(f); fclose(f); D3DXAssembleShaderFromFile(tmp, D3DXASM_DEBUG, 0, &code, &errors); #endif if (errors) { // print out compilation errors. os::Printer::log("Vertex shader compilation failed:", ELL_ERROR); os::Printer::log((c8*)errors->GetBufferPointer(), ELL_ERROR); if (code) code->Release(); errors->Release(); return false; } DWORD* decl = 0; DWORD dwStdDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), // position 0 D3DVSD_REG(1, D3DVSDT_FLOAT3), // normal 1 D3DVSD_REG(2, D3DVSDT_D3DCOLOR ),// color 2 D3DVSD_REG(3, D3DVSDT_FLOAT2 ), // tex1 3 D3DVSD_REG(4, D3DVSDT_FLOAT2 ), // tex2 4 D3DVSD_END() }; DWORD dwTngtDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0 , D3DVSDT_FLOAT3), // position 0 D3DVSD_REG(1 , D3DVSDT_FLOAT3), // normal 1 D3DVSD_REG(2 , D3DVSDT_D3DCOLOR ),// color 2 D3DVSD_REG(3 , D3DVSDT_FLOAT2 ), // tex1 3 D3DVSD_REG(4, D3DVSDT_FLOAT3 ), // tangent 4 D3DVSD_REG(5, D3DVSDT_FLOAT3 ), // binormal 5 D3DVSD_END() }; if (type == EVT_TANGENTS) decl = dwTngtDecl; else decl = dwStdDecl; if (FAILED(pID3DDevice->CreateVertexShader(decl, (DWORD*)code->GetBufferPointer(), &VertexShader, 0))) { os::Printer::log("Could not create vertex shader.", ELL_ERROR); code->Release(); return false; } code->Release(); return true; #endif }
HRESULT HelloShadowVolume::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_dpps, &device); if (FAILED(hr)) { MessageBox(0, L"CreateDevice failed", 0, 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); D3DVIEWPORT8 viewport = { 0, 0, m_iWidth, m_iHeight }; m_spDevice->SetViewport(&viewport); D3DXVECTOR3 eye(0.0f, 0.0f, 30.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&m_mtView, &eye, &target, &up); D3DXMatrixPerspectiveFovLH(&m_mtProj, 0.2*D3DX_PI, (float)m_iWidth / (float)m_iHeight, 1.0f, 100.f); m_cPlaneTint = { 0.7f, 0.6f, 0.4f, 1.0f }; ID3DXMesh* plane; //D3DXCreatePolygon(m_spDevice.get(), 2.0f, 4, &plane, NULL); CreatePlane(m_spDevice.get(), 15.0f, 10, &plane); //D3DXCreateSphere(m_spDevice.get(), 1.0f,20,20, &plane, NULL); IDirect3DVertexBuffer8* vb; IDirect3DIndexBuffer8* ib; plane->GetVertexBuffer(&vb); plane->GetIndexBuffer(&ib); m_spPlaneVB.reset(vb, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spPlaneIB.reset(ib, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwPlaneNumVertices = plane->GetNumVertices(); m_dwPlaneNumFaces = plane->GetNumFaces(); plane->Release(); DWORD decl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(3, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"plane.vso", &m_dwPlaneVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"plane.pso", &m_dwPlanePSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } D3DXMATRIX Rx, Tz; D3DXMatrixRotationX(&Rx, D3DX_PI*0.5f); D3DXMatrixTranslation(&Tz, 0.0f, -3.0f, 0.0f); m_mtPlaneWorld = Rx * Tz; ID3DXMesh* occluder; CreateOccluder(m_spDevice.get(), &occluder); IDirect3DVertexBuffer8* vbOccluder; IDirect3DIndexBuffer8* ibOccluder; occluder->GetVertexBuffer(&vbOccluder); occluder->GetIndexBuffer(&ibOccluder); m_spOccluderVB.reset(vbOccluder, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spOccluderIB.reset(ibOccluder, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwOccluderNumVertices = occluder->GetNumVertices(); m_dwOccluderNumFaces = occluder->GetNumFaces(); occluder->Release(); hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"occluder.vso", &m_dwOccluderVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"occluder.pso", &m_dwOccluderPSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } m_cOccluderTint = { 0.3f, 0.0f, 0.8f, 1.0f }; D3DXMATRIX Rz, T; D3DXMatrixTranslation(&T, 5.1f * cosf(0.5), 0.0f, 5.1f * sinf(0.5)); D3DXMatrixIdentity(&m_mtVolumeWorld); D3DXMatrixRotationZ(&Rz, 0.5f); m_mtOccluderWorld = T * Rz; ID3DXMesh* volume; CreateVolume(m_spDevice.get(), &volume); IDirect3DVertexBuffer8* vbVolume; IDirect3DIndexBuffer8* ibVolume; volume->GetVertexBuffer(&vbVolume); volume->GetIndexBuffer(&ibVolume); m_spVolumeVB.reset(vbVolume, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spVolumeIB.reset(ibVolume, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwVolumeNumVertices = volume->GetNumVertices(); m_dwVolumeNumFaces = volume->GetNumFaces(); volume->Release(); hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"volume.vso", &m_dwVolumeVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"volume.pso", &m_dwVolumePSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } m_cVolumeTint = { 0.7f, 0.0f, 0.0f, 1.0f }; D3DXMATRIX Sx; D3DXMatrixIdentity(&m_mtVolumeWorld); D3DXMatrixScaling(&Sx, 6.0f, 1.0f, 1.0f); D3DXMatrixRotationZ(&Rz, 0.5f); m_mtVolumeWorld = Sx * Rz; return S_OK; }
HRESULT CRacorX::RestoreDeviceObjects() { IDirect3DDevice8* device; HRESULT hr = m_spD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device); if (FAILED(hr)) { OutputDebugString(L"Create Device Failed! Error:\n"); switch (hr) { case D3DERR_DEVICELOST: OutputDebugString(L"D3DERR_DEVICELOST\n"); break; case D3DERR_INVALIDCALL: OutputDebugString(L"D3DERR_INVALIDCALL\n"); break; case D3DERR_NOTAVAILABLE: OutputDebugString(L"D3DERR_NOTAVAILABLE\n"); break; case D3DERR_OUTOFVIDEOMEMORY: OutputDebugString(L"D3DERR_OUTOFVIDEOMEMORY\n"); break;; default: OutputDebugString(L"Unknown\n"); break; } return S_FALSE; } m_spDevice.reset(device, [](IDirect3DDevice8* device){ device->Release(); }); DWORD dwDecl0[] = { D3DVSD_STREAM(0), D3DVSD_REG(0,D3DVSDT_FLOAT3), //D3DVSD_REG(5,D3DVSDT_D3DCOLOR), /*D3DVSD_CONST(0, 4), *(DWORD*)&m_mtWorld[0], *(DWORD*)&m_mtWorld[1], *(DWORD*)&m_mtWorld[2], *(DWORD*)&m_mtWorld[3], *(DWORD*)&m_mtWorld[4], *(DWORD*)&m_mtWorld[5], *(DWORD*)&m_mtWorld[6], *(DWORD*)&m_mtWorld[7], *(DWORD*)&m_mtWorld[8], *(DWORD*)&m_mtWorld[9], *(DWORD*)&m_mtWorld[10], *(DWORD*)&m_mtWorld[11], *(DWORD*)&m_mtWorld[12], *(DWORD*)&m_mtWorld[13], *(DWORD*)&m_mtWorld[14], *(DWORD*)&m_mtWorld[15], D3DVSD_CONST(0, 1), *(DWORD*)&m_fMaterial[0], *(DWORD*)&m_fMaterial[1], *(DWORD*)&m_fMaterial[2], *(DWORD*)&m_fMaterial[3],*/ D3DVSD_END() }; IDirect3DVertexBuffer8* vb; m_spDevice->CreateVertexBuffer( 4 * sizeof (Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &vb); m_spVB.reset(vb, [](IDirect3DVertexBuffer8* vb){ vb->Release(); }); Vertex* vertices = 0; m_spVB->Lock(0, 0, reinterpret_cast<BYTE**>(&vertices), 0); vertices[0] = { -100.0f, -100.0f, 0.0f, }; vertices[1] = { 100.0f, -100.0f, 0.0f, }; vertices[2] = { 100.0f, 100.0f, 0.0f, }; vertices[3] = { -100.0f, 100.0f, 0.0f, }; /* vertices[0] = { -1.0f, -1.0f, 0.2f, }; vertices[1] = { 1.0f, -1.0f, 0.2f, }; vertices[2] = { 1.0f, 1.0f, 0.2f, }; vertices[3] = { -1.0f, 1.0f, 0.2f, }; */ m_spVB->Unlock(); IDirect3DIndexBuffer8* ib; m_spDevice->CreateIndexBuffer( 6 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ib); m_spIB.reset(ib, [](IDirect3DIndexBuffer8* ib){ ib->Release(); }); WORD *indices = 0; m_spIB->Lock(0, 0, reinterpret_cast<BYTE**>(&indices), 0); indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; m_spIB->Unlock(); const char vsh[] = "vs.1.1 \n" \ "dp4 oPos.x, v0, c0 \n"\ "dp4 oPos.y, v0, c1 \n"\ "dp4 oPos.z, v0, c2 \n"\ "dp4 oPos.w, v0, c3 \n"\ "mov oD0, c4\n"; /* const char vsh[] = "vs.1.1 \n" \ "mov oPos, v0 \n" \ "mov oD0, c4 \n"; */ ID3DXBuffer* pVBuffer; ID3DXBuffer* pErrors; HRESULT rc = D3DXAssembleShader(reinterpret_cast<LPCVOID>(vsh), sizeof(vsh) - 1, 0, NULL, &pVBuffer, &pErrors); if (FAILED(rc)) { OutputDebugString(L"Failed to assemble the vertex shader, error:\n"); OutputDebugStringA(reinterpret_cast<CHAR*>(pErrors->GetBufferPointer())); OutputDebugString(L"\n"); } rc = m_spDevice->CreateVertexShader(dwDecl0, (DWORD*)pVBuffer->GetBufferPointer(), &m_dwVertexShader, 0); if (FAILED(rc)) { OutputDebugString(L"Failed to create vertex shader, error:\n"); WCHAR szBuffer[512] = { 0 }; D3DXGetErrorString(rc, szBuffer, sizeof(szBuffer)); OutputDebugString(szBuffer); OutputDebugString(L"\n"); } m_spDevice->SetViewport(&m_Viewport); //m_spDevice->SetTransform(D3DTS_VIEW, &m_mtView); //m_spDevice->SetTransform(D3DTS_PROJECTION, &m_mtProj); m_spDevice->SetRenderState(D3DRS_ZENABLE, true); m_spDevice->SetRenderState(D3DRS_LIGHTING, false); //m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); return S_OK; }
//----------------------------------------------------------------------------- // Name: RestoreDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::RestoreDeviceObjects() { // Restore the fonts m_pFont->RestoreDeviceObjects(); HRESULT hr = S_OK; D3DLIGHT8 light; m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 2.0f ); if (m_pdeSelected != NULL) SetProjectionMatrix(); m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE ); m_pd3dDevice->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW ); m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR ); m_pd3dDevice->SetRenderState( D3DRS_COLORVERTEX, FALSE ); // Create vertex shader for the indexed skinning DWORD dwIndexedVertexDecl1[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh D3DVSD_REG( 2, D3DVSDT_D3DCOLOR ), // Blend indices // D3DVSD_REG( 2, D3DVSDT_UBYTE4 ), // Blend indices D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal D3DVSD_REG( 4, D3DVSDT_FLOAT2 ), // Tex coords D3DVSD_END() }; DWORD dwIndexedVertexDecl2[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh D3DVSD_REG( 1, D3DVSDT_FLOAT1 ), // Blend weights D3DVSD_REG( 2, D3DVSDT_D3DCOLOR ), // Blend indices // D3DVSD_REG( 2, D3DVSDT_UBYTE4 ), // Blend indices D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal D3DVSD_REG( 4, D3DVSDT_FLOAT2 ), // Tex coords D3DVSD_END() }; DWORD dwIndexedVertexDecl3[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh D3DVSD_REG( 1, D3DVSDT_FLOAT2 ), // Blend weights D3DVSD_REG( 2, D3DVSDT_D3DCOLOR ), // Blend indices // D3DVSD_REG( 2, D3DVSDT_UBYTE4 ), // Blend indices D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal D3DVSD_REG( 4, D3DVSDT_FLOAT2 ), // Tex coords D3DVSD_END() }; DWORD dwIndexedVertexDecl4[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh D3DVSD_REG( 1, D3DVSDT_FLOAT3 ), // Blend weights D3DVSD_REG( 2, D3DVSDT_D3DCOLOR ), // Blend indices // D3DVSD_REG( 2, D3DVSDT_UBYTE4 ), // Blend indices D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal D3DVSD_REG( 4, D3DVSDT_FLOAT2 ), // Tex coords D3DVSD_END() }; DWORD* dwIndexedVertexDecl[] = {dwIndexedVertexDecl1, dwIndexedVertexDecl2, dwIndexedVertexDecl3, dwIndexedVertexDecl4}; LPD3DXBUFFER pCode; DWORD bUseSW = D3DUSAGE_SOFTWAREPROCESSING; if (m_d3dCaps.VertexShaderVersion >= D3DVS_VERSION(1, 1)) { bUseSW = 0; } for (DWORD i = 0; i < 4; ++i) { // Assemble the vertex shader file if( FAILED( hr = D3DXAssembleShaderFromResource(NULL, MAKEINTRESOURCE(IDD_SHADER1 + i), 0, NULL, &pCode, NULL ) ) ) return hr; // Create the vertex shader if( FAILED( hr = m_pd3dDevice->CreateVertexShader( dwIndexedVertexDecl[i], (DWORD*)pCode->GetBufferPointer(), &(m_dwIndexedVertexShader[i]) , bUseSW ) ) ) { return hr; } pCode->Release(); } ZeroMemory( &light, sizeof(light) ); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = 1.0; light.Diffuse.g = 1.0; light.Diffuse.b = 1.0; light.Specular.r = 0; light.Specular.g = 0; light.Specular.b = 0; light.Ambient.r = 0.25; light.Ambient.g = 0.25; light.Ambient.b = 0.25; light.Direction = D3DXVECTOR3( 0.0f, 0.0f, -1.0f); hr = m_pd3dDevice->SetLight(0, &light ); if (FAILED(hr)) return E_FAIL; hr = m_pd3dDevice->LightEnable(0, TRUE); if (FAILED(hr)) return E_FAIL; // Set Light for vertex shader D3DXVECTOR4 vLightDir( 0.0f, 0.0f, 1.0f, 0.0f ); m_pd3dDevice->SetVertexShaderConstant(1, &vLightDir, 1); return S_OK; }
namespace NxXbox { // Vertex color attenuation, 4 sets of tex coords. static DWORD WeightedMeshVertexShaderVertColUV4Decl[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( VSD_REG_POS, D3DVSDT_FLOAT3 ), // Position. D3DVSD_REG( VSD_REG_WEIGHTS, D3DVSDT_NORMPACKED3 ), // Weights. D3DVSD_REG( VSD_REG_INDICES, D3DVSDT_SHORT4 ), // Indices. D3DVSD_REG( VSD_REG_NORMAL, D3DVSDT_NORMPACKED3 ), // Normals. D3DVSD_REG( VSD_REG_COLOR, D3DVSDT_D3DCOLOR ), // Diffuse color. D3DVSD_REG( VSD_REG_TEXCOORDS0, D3DVSDT_FLOAT2 ), // Texture coordinates 0. D3DVSD_REG( VSD_REG_TEXCOORDS1, D3DVSDT_FLOAT2 ), // Texture coordinates 1. D3DVSD_REG( VSD_REG_TEXCOORDS2, D3DVSDT_FLOAT2 ), // Texture coordinates 2. D3DVSD_REG( VSD_REG_TEXCOORDS3, D3DVSDT_FLOAT2 ), // Texture coordinates 3. D3DVSD_END() }; // Billboards. static DWORD BillboardVSDecl[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position (actually pivot position). D3DVSD_REG( 1, D3DVSDT_FLOAT3 ), // Normal (actually position of point relative to pivot). D3DVSD_REG( 2, D3DVSDT_D3DCOLOR ), // Diffuse color. D3DVSD_REG( 3, D3DVSDT_FLOAT2 ), // Texture coordinates 0. D3DVSD_REG( 4, D3DVSDT_FLOAT2 ), // Texture coordinates 1. D3DVSD_REG( 5, D3DVSDT_FLOAT2 ), // Texture coordinates 2. D3DVSD_REG( 6, D3DVSDT_FLOAT2 ), // Texture coordinates 3. D3DVSD_END() }; // Particles. static DWORD ParticleFlatVSDecl[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_D3DCOLOR ), // Diffuse color (start) D3DVSD_REG( 1, D3DVSDT_D3DCOLOR ), // Diffuse color (end) D3DVSD_REG( 2, D3DVSDT_SHORT2 ), // Indices. D3DVSD_END() }; // New, Ps2 style particles using PointSprites. static DWORD NewParticleFlatVSDecl[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT4 ), // Random 4-element 'R' vector. D3DVSD_REG( 1, D3DVSDT_FLOAT2 ), // Time and color interpolator. D3DVSD_REG( 2, D3DVSDT_D3DCOLOR ), // Diffuse color (start) D3DVSD_REG( 3, D3DVSDT_D3DCOLOR ), // Diffuse color (end) D3DVSD_END() }; // Shadow buffer, static geom. static DWORD ShadowBufferStaticGeomVSDecl[] = { D3DVSD_STREAM( 0 ), D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position. D3DVSD_REG( 1, D3DVSDT_D3DCOLOR ), // Diffuse color. D3DVSD_REG( 2, D3DVSDT_FLOAT2 ), // Texture coordinates 0. D3DVSD_REG( 3, D3DVSDT_FLOAT2 ), // Texture coordinates 1. D3DVSD_REG( 4, D3DVSDT_FLOAT2 ), // Texture coordinates 2. D3DVSD_END() }; /******************************************************************/ /* */ /* */ /******************************************************************/ DWORD GetVertexShader( bool vertex_colors, bool specular, uint32 max_weights_used ) { Dbg_Assert( max_weights_used > 0 ); if( vertex_colors ) { if( max_weights_used == 1 ) { return ( specular ) ? WeightedMeshVS_VXC_Specular_1Weight : WeightedMeshVS_VXC_1Weight; } else if( max_weights_used == 2 ) { return ( specular ) ? WeightedMeshVS_VXC_Specular_2Weight : WeightedMeshVS_VXC_2Weight; } else if( max_weights_used == 3 ) { return ( specular ) ? WeightedMeshVS_VXC_Specular_3Weight : WeightedMeshVS_VXC_3Weight; } } Dbg_Assert( 0 ); return 0; } /******************************************************************/ /* */ /* */ /******************************************************************/ void CreateWeightedMeshVertexShaders( void ) { static bool created_shaders = false; if( !created_shaders ) { created_shaders = true; if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_1WeightVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_1Weight, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_2WeightVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_2Weight, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_3WeightVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_3Weight, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_Specular_1WeightVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_Specular_1Weight, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_Specular_2WeightVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_Specular_2Weight, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_Specular_3WeightVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_Specular_3Weight, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_1Weight_UVTransformVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_1Weight_UVTransform, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_2Weight_UVTransformVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_2Weight_UVTransform, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_3Weight_UVTransformVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_3Weight_UVTransform, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVertexShader_SBWriteVertexShader, // Defined in the header file from xsasm. &WeightedMeshVertexShader_SBWrite, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_1Weight_SBPassThruVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_1Weight_SBPassThru, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_2Weight_SBPassThruVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_2Weight_SBPassThru, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( WeightedMeshVertexShaderVertColUV4Decl, dwWeightedMeshVS_VXC_3Weight_SBPassThruVertexShader, // Defined in the header file from xsasm. &WeightedMeshVS_VXC_3Weight_SBPassThru, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( BillboardVSDecl, dwBillboardScreenAlignedVSVertexShader, // Defined in the header file from xsasm. &BillboardScreenAlignedVS, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( ParticleFlatVSDecl, dwParticleFlatVSVertexShader, // Defined in the header file from xsasm. &ParticleFlatVS, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( ParticleFlatVSDecl, dwParticleNewFlatVSVertexShader, // Defined in the header file from xsasm. &ParticleNewFlatVS, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( NewParticleFlatVSDecl, dwParticleNewFlatPointSpriteVSVertexShader, // Defined in the header file from xsasm. &ParticleNewFlatPointSpriteVS, 0 )) { exit( 0 ); } if( D3D_OK != D3DDevice_CreateVertexShader( ShadowBufferStaticGeomVSDecl, dwShadowBufferStaticGeomVSVertexShader, // Defined in the header file from xsasm. &ShadowBufferStaticGeomVS, 0 )) { exit( 0 ); } } } /******************************************************************/ /* */ /* */ /******************************************************************/ void setup_weighted_mesh_vertex_shader( void *p_root_matrix, void *p_bone_matrices, int num_bone_matrices ) { XGMATRIX dest_matrix; XGMATRIX inverse_view_matrix; XGMATRIX temp_matrix; XGMATRIX projMatrix; XGMATRIX viewMatrix; XGMATRIX worldMatrix; // Projection matrix. XGMatrixTranspose( &projMatrix, &EngineGlobals.projection_matrix ); // View matrix. XGMatrixTranspose( &viewMatrix, &EngineGlobals.view_matrix ); viewMatrix.m[3][0] = 0.0f; viewMatrix.m[3][1] = 0.0f; viewMatrix.m[3][2] = 0.0f; viewMatrix.m[3][3] = 1.0f; // World space transformation matrix. (3x3 rotation plus 3 element translation component). worldMatrix.m[0][0] = ((float*)p_root_matrix )[0]; worldMatrix.m[0][1] = ((float*)p_root_matrix )[1]; worldMatrix.m[0][2] = ((float*)p_root_matrix )[2]; worldMatrix.m[0][3] = ((float*)p_root_matrix )[3]; worldMatrix.m[1][0] = ((float*)p_root_matrix )[4]; worldMatrix.m[1][1] = ((float*)p_root_matrix )[5]; worldMatrix.m[1][2] = ((float*)p_root_matrix )[6]; worldMatrix.m[1][3] = ((float*)p_root_matrix )[7]; worldMatrix.m[2][0] = ((float*)p_root_matrix )[8]; worldMatrix.m[2][1] = ((float*)p_root_matrix )[9]; worldMatrix.m[2][2] = ((float*)p_root_matrix )[10]; worldMatrix.m[2][3] = ((float*)p_root_matrix )[11]; worldMatrix.m[3][0] = 0.0f; worldMatrix.m[3][1] = 0.0f; worldMatrix.m[3][2] = 0.0f; worldMatrix.m[3][3] = 1.0f; // Calculate composite world->view->projection matrix. XGMatrixMultiply( &temp_matrix, &viewMatrix, &worldMatrix ); XGMatrixMultiply( &dest_matrix, &projMatrix, &temp_matrix ); // Switch to 192 constant mode, removing the lock on the reserved constants c-38 and c-37. // D3DDevice_SetShaderConstantMode( D3DSCM_192CONSTANTS | D3DSCM_NORESERVEDCONSTANTS ); // Load up the combined world, camera & projection matrix. D3DDevice_SetVertexShaderConstantFast( VSCONST_REG_TRANSFORM_OFFSET, (void*)&dest_matrix, VSCONST_REG_TRANSFORM_SIZE ); D3DDevice_SetVertexShaderConstantFast( VSCONST_REG_WORLD_TRANSFORM_OFFSET, (void*)&worldMatrix, VSCONST_REG_WORLD_TRANSFORM_SIZE ); // We want to transform the light directions by the inverse of the world transform - this means we don't have to transform // the normal by the world transform for every vertex in the vertex shader. However, the function D3DXVec3TransformNormal // (used below) does the inverse transform for us, so need to actually figure the inverse... // XGMATRIX inverse_world_transform = worldMatrix; // D3DXMatrixInverse( &inverse_world_transform, NULL, &worldMatrix ); float directional_light_color[24]; CopyMemory( directional_light_color, EngineGlobals.directional_light_color, sizeof( float ) * 24 ); XGVec3TransformNormal((XGVECTOR3*)&directional_light_color[0], (XGVECTOR3*)&EngineGlobals.directional_light_color[0], &worldMatrix ); XGVec3TransformNormal((XGVECTOR3*)&directional_light_color[8], (XGVECTOR3*)&EngineGlobals.directional_light_color[8], &worldMatrix ); XGVec3TransformNormal((XGVECTOR3*)&directional_light_color[16], (XGVECTOR3*)&EngineGlobals.directional_light_color[16], &worldMatrix ); XGVec3Normalize((XGVECTOR3*)&directional_light_color[0], (XGVECTOR3*)&directional_light_color[0] ); XGVec3Normalize((XGVECTOR3*)&directional_light_color[8], (XGVECTOR3*)&directional_light_color[8] ); XGVec3Normalize((XGVECTOR3*)&directional_light_color[16], (XGVECTOR3*)&directional_light_color[16] ); // Load up the directional light data. D3DDevice_SetVertexShaderConstantFast( VSCONST_REG_DIR_LIGHT_OFFSET, (void*)directional_light_color, 6 ); // Load up the ambient light color. D3DDevice_SetVertexShaderConstantFast( VSCONST_REG_AMB_LIGHT_OFFSET, (void*)EngineGlobals.ambient_light_color, 1 ); // Calculate and load up the model-relative camera position. EngineGlobals.model_relative_cam_position = XGVECTOR3( EngineGlobals.cam_position.x - worldMatrix.m[0][3], EngineGlobals.cam_position.y - worldMatrix.m[1][3], EngineGlobals.cam_position.z - worldMatrix.m[2][3] ); XGVec3TransformNormal( &EngineGlobals.model_relative_cam_position, &EngineGlobals.model_relative_cam_position, &worldMatrix ); float specular_attribs[4] = { EngineGlobals.model_relative_cam_position.x, EngineGlobals.model_relative_cam_position.y, EngineGlobals.model_relative_cam_position.z, 0.0f }; D3DDevice_SetVertexShaderConstantFast( VSCONST_REG_CAM_POS_OFFSET, (void*)&specular_attribs, 1 ); // Safety check here to limit number of bones to that available. if( num_bone_matrices > 55 ) { num_bone_matrices = 55; } DWORD* p_bone_element = (DWORD*)p_bone_matrices; // Begin state block to set vertex shader constants for bone transforms. DWORD *p_push; EngineGlobals.p_Device->BeginState(( num_bone_matrices * ( 12 + 1 )) + 3, &p_push ); // 1 here isn't the parameter for SET_TRANSFORM_CONSTANT_LOAD; rather, it's the number of dwords written to that register. p_push[0] = D3DPUSH_ENCODE( D3DPUSH_SET_TRANSFORM_CONSTANT_LOAD, 1 ); // Here is the actual parameter for SET_TRANSFORM_CONSTANT_LOAD. Always add 96 to the constant register. p_push[1] = VSCONST_REG_MATRIX_OFFSET + 96; p_push += 2; while( num_bone_matrices > 0 ) { // A 3x4 matrix is 12 dwords. You can encode a maximum of 32 dwords to D3DPUSH_SET_TRANSFORM_CONSTANT before needing another D3DPUSH_ENCODE. p_push[0] = D3DPUSH_ENCODE( D3DPUSH_SET_TRANSFORM_CONSTANT, 12 ); p_push[1] = p_bone_element[0]; p_push[2] = p_bone_element[4]; p_push[3] = p_bone_element[8]; p_push[4] = p_bone_element[12]; p_push[5] = p_bone_element[1]; p_push[6] = p_bone_element[5]; p_push[7] = p_bone_element[9]; p_push[8] = p_bone_element[13]; p_push[9] = p_bone_element[2]; p_push[10] = p_bone_element[6]; p_push[11] = p_bone_element[10]; p_push[12] = p_bone_element[14]; --num_bone_matrices; p_bone_element += 16; p_push += 13; } EngineGlobals.p_Device->EndState( p_push ); // Load up the replacement registers for c-38 and c-37 // The z value is 2^24 - to take 1.0f to the max z buffer value for a 24bit z buffer. static float homogenous_to_screen_reg[8] = { 320.0f, -240.0f, 1.6777215e7f, 0.0f, 320.53125f, 240.53125f, 0.0f, 0.0f }; if( EngineGlobals.is_orthographic ) { homogenous_to_screen_reg[0] = 128.0f; homogenous_to_screen_reg[1] = -128.0f; homogenous_to_screen_reg[2] = 1.6777215e7f; homogenous_to_screen_reg[4] = 128.53125f; homogenous_to_screen_reg[5] = 128.53125f; } else { homogenous_to_screen_reg[0] = (float)EngineGlobals.viewport.Width * 0.5f; homogenous_to_screen_reg[1] = (float)EngineGlobals.viewport.Height * -0.5f; homogenous_to_screen_reg[2] = ( EngineGlobals.zstencil_depth == 16 ) ? 65535.0f : 1.6777215e7f; homogenous_to_screen_reg[4] = (float)NxXbox::EngineGlobals.viewport.X + ((float)NxXbox::EngineGlobals.viewport.Width * 0.5f ) + 0.53125f; homogenous_to_screen_reg[5] = (float)NxXbox::EngineGlobals.viewport.Y + ((float)NxXbox::EngineGlobals.viewport.Height * 0.5f ) + 0.53125f; } D3DDevice_SetVertexShaderConstantFast( 94, (void*)homogenous_to_screen_reg, 2 ); } /******************************************************************/ /* */ /* */ /******************************************************************/ void startup_weighted_mesh_vertex_shader( void ) { // Switch to 192 constant mode, removing the lock on the reserved constants c-38 and c-37. D3DDevice_SetShaderConstantMode( D3DSCM_192CONSTANTS | D3DSCM_NORESERVEDCONSTANTS ); // Flag the custom pipeline is in operation. EngineGlobals.custom_pipeline_enabled = true; } /******************************************************************/ /* */ /* */ /******************************************************************/ void shutdown_weighted_mesh_vertex_shader( void ) { // Switch back to 96 constant mode. D3DDevice_SetShaderConstantMode( D3DSCM_96CONSTANTS ); // Flag the custom pipeline is no longer in operation. EngineGlobals.custom_pipeline_enabled = false; } } // namespace NxXbox