//变换矩阵 float4x4 matWVP; //纹理 texture tex; //输入的顶点形式 struct VS_INPUT { float3 pos :POSITION; float4 diff :COLOR0; float2 tex :TEXCOORD0; }; //输出的顶点形式 struct VS_OUTPUT { float4 pos :POSITION; float4 diff :COLOR0; float2 tex :TEXCOORD0; }; //声明名为VS的顶点着色器 VS_OUTPUT VS(VS_INPUT In) { VS_OUTPUT Out;
Out.pos = mul(float4(In.pos,1),matWVP); //计算坐标的位置 Out.diff= In.diff; //输入颜色变为输出颜色 Out.tex = float2(In.tex.x, 1.0f - In.tex.y);//纹理的Y坐标颠倒
return Out; } //纹理编译状态 sampler Sampler = sampler_state { Texture = <tex>; MipFilter= LINEAR; MinFilter= LINEAR; MagFilter= LINEAR; }; //声明名为PS的像素着色器 float4 PS(VS_OUTPUT In) : COLOR0 { return tex2D(Sampler, In.tex) * In.diff; } //声明MyShader技巧 technique MyShader { pass P0//最初的号pass { Lighting = FALSE;
Sampler[0] = (Sampler); //shaders VertexShader = compile vs_1_1 VS(); PixelShader = compile ps_1_1 PS(); } } |
LPDIRECT3DVERTEXDECLARATION9 g_pDecl; LPD3DXEFFECT g_pEffect; |
//----------------------------------------------------------------------------- //初始化顶点着色器 //----------------------------------------------------------------------------- HRESULT InitVS() { ::D3DVERTEXELEMENT9 decl[18]; //使用FVF自动代入顶点的声明值 D3DXDeclaratorFromFVF(D3DFVF_CUSTOMVERTEX,decl); //顶点声明值生成g_pDecl g_pd3dDevice->CreateVertexDeclaration(decl, &g_pDecl); //读取FX文件生成Effect界面 LPD3DXBUFFER pCompilationErrors; if(FAILED(D3DXCreateEffectFromFile(g_pd3dDevice, "BasicHLSL.fx", NULL, NULL, 0, NULL, &g_pEffect, &pCompilationErrors))) { MessageBox(NULL,(LPCSTR)pCompilationErrors->GetBufferPointer(),NULL,NULL); return E_FAIL; } else { //将纹理和矩阵值传给ID3DXEffect g_pEffect->SetTexture("tex", pTexture); matWVP = matWorld * matView * matProj; g_pEffect->SetMatrix("matWVP", &matWVP); return S_OK; } } |
//设置顶点声明值和顶点 g_pd3dDevice->SetVertexDeclaration(g_pDecl); g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CustomVertex) ); //设定FX输出使用的方法 g_pEffect->SetTechnique("MyShader"); UINT nPass; //开始使用FX的输出 g_pEffect->Begin(&nPass, D3DXFX_DONOTSAVESTATE); //使用号渲染通道,即P0 g_pEffect->BeginPass(0); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); g_pEffect->EndPass(); //结束使用FX的输出 g_pEffect->End(); |