下面给出一个完整的使用vs 和ps的例子,例子没做什么,只是让大家知道一个shader和direcetx是怎么结合起来的,看例子:
vertex shader://first_vs.txt
matrix WVPMatrix;
vector color;
struct VS_INPUT
...{
vector position : POSITION;
};
struct VS_OUTPUT
...{
vector position : POSITION;
vector color : COLOR;
};
VS_OUTPUT main(VS_INPUT input)
...{
VS_OUTPUT output = (VS_OUTPUT)0;
output.position = mul(input.position, WVPMatrix);
output.color = color;
return output;
}
//顶点程序没做什么,只是设置了顶点的位置坐标,然后输出一个color到片段程序
pixel shader//first_ps.txt
struct Output ...{
vector color : COLOR;
};
Output ps_main(vector color : COLOR)
...{
Output OUT;
float4 _color=...{1,0,0,1};
OUT.color = _color;
return OUT;
}
//简单的输出黄色
下面是一个directx程序,创建一个三角形,然后用上面的两个程序渲染~~看代码
#include "d3dUtility.h"
#define ReleaseCOM(x)
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
IDirect3DVertexBuffer9* Triangle = 0; // vertex buffer to store
// our triangle data.
//pointer about the shader
IDirect3DVertexShader9* BasicShader = 0;//vs pointer
IDirect3DPixelShader9* PixelShader = 0;//ps pointer
ID3DXConstantTable* BasicConstTable = 0;//vs constant table pointer
ID3DXConstantTable* PixelConstTable = 0;//ps constant table pointer
D3DXHANDLE WVPMatrixHandle = 0;//matrix handle
D3DXHANDLE ColorHandle = 0;//color handle
//
// Classes and Structures
//
struct Vertex
...{
Vertex()...{}
Vertex(float x, float y, float z)
...{
_x = x; _y = y; _z = z;
}
float _x, _y, _z;
DWORD _color;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ|D3DFVF_DIFFUSE;
//
// Framework Functions
//
bool Setup()
...{
//
// Create the vertex buffer.
//
ID3DXBuffer* shaderBuffer = 0;
ID3DXBuffer* errorBuffer = 0;
Device->CreateVertexBuffer(
3 * sizeof(Vertex), // size in bytes
D3DUSAGE_WRITEONLY, // flags
Vertex::FVF, // vertex format
D3DPOOL_MANAGED, // managed memory pool
&Triangle, // return create vertex buffer
0); // not used - set to 0
//
// Fill the buffers with the triangle data.
//
Vertex* vertices;
Triangle->Lock(0, 0, (void**)&vertices, 0);
vertices[0] = Vertex(-1.0f, 0.0f, 2.0f);
vertices[1] = Vertex( 0.0f, 1.0f, 2.0f);
vertices[2] = Vertex( 1.0f, 0.0f, 2.0f);
vertices[0]._color=D3DCOLOR_XRGB(250,0,0);
Triangle->Unlock();
//
// Set the projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj, // result
D3DX_PI * 0.5f, // 90 - degrees
(float)Width / (float)Height, // aspect ratio
1.0f, // near plane
1000.0f); // far plane
Device->SetTransform(D3DTS_PROJECTION, &proj);
//创建vertex shader
HRESULT hr = D3DXCompileShaderFromFile("first_vs.txt",
0,
0,
"main", // entry point function name
"vs_1_1",
D3DXSHADER_DEBUG,
&shaderBuffer,
&errorBuffer,
&BasicConstTable);
// output any error messages
if(errorBuffer)
...{
MessageBox(0, (char*)errorBuffer->GetBufferPointer(), 0, 0);
ReleaseCOM(errorBuffer);
exit(0);
}
if(FAILED(hr))
...{
MessageBox(0, "D3DXCompileShaderFromFile() - FAILED", 0, 0);
return false;
}
hr = Device->CreateVertexShader((DWORD*)shaderBuffer->GetBufferPointer(), &BasicShader);
if(FAILED(hr))
...{
MessageBox(0, "CreateVertexShader - FAILED", 0, 0);
return false;
}
ReleaseCOM(shaderBuffer);
//创建pixel shader
HRESULT hr2 = D3DXCompileShaderFromFile("first_ps.txt",
0,
0,
"ps_main", // entry point function name
"ps_1_1",
D3DXSHADER_DEBUG,
&shaderBuffer,
&errorBuffer,
&PixelConstTable);
// output any error messages
if(errorBuffer)
...{
MessageBox(0, (char*)errorBuffer->GetBufferPointer(), 0, 0);
ReleaseCOM(errorBuffer);
exit(0);
}
if(FAILED(hr2))
...{
MessageBox(0, "D3DXCompileShaderFromFile() - FAILED", 0, 0);
return false;
}
hr2 = Device->CreatePixelShader((DWORD*)shaderBuffer->GetBufferPointer(), &PixelShader);
if(FAILED(hr2))
...{
MessageBox(0, "CreateVertexShader - FAILED", 0, 0);
return false;
}
ReleaseCOM(shaderBuffer);
WVPMatrixHandle = BasicConstTable->GetConstantByName(0, "WVPMatrix");ColorHandle = BasicConstTable->GetConstantByName(0, "color");
BasicConstTable->SetDefaults(Device);
//set light false so we can see the triangle's color
Device->SetRenderState(D3DRS_LIGHTING, false);
return true;
}
void Cleanup()
...{
d3d::Release<IDirect3DVertexBuffer9*>(Triangle);
ReleaseCOM(BasicShader);
ReleaseCOM(BasicConstTable);
ReleaseCOM(PixelShader);
ReleaseCOM(PixelConstTable);
}
bool Display(float timeDelta)
...{
if( Device )
...{
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(153,153,153), 1.0f, 0);
Device->BeginScene();
Device->SetStreamSource(0, Triangle, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
D3DXMATRIX matWorld, matView, matProj;
Device->GetTransform(D3DTS_WORLD, &matWorld);
Device->GetTransform(D3DTS_VIEW, &matView);
Device->GetTransform(D3DTS_PROJECTION, &matProj);
D3DXMATRIX matWVP = matWorld * matView * matProj;
//set the globals 设置shader程序的全局变量,这里为了显示有和没有pixel shader的区别,先设置了一个颜色,然后用ps修改
BasicConstTable->SetMatrix(Device, WVPMatrixHandle, &matWVP);
D3DXVECTOR4 color(1.0f, 1.0f, 0.0f, 1.0f);
BasicConstTable->SetVector(Device, ColorHandle, &color);
//use the shader 使用shader
Device->SetVertexShader(BasicShader);
Device->SetPixelShader(PixelShader);
// Draw one triangle.
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
...{
switch( msg )
...{
case WM_DESTROY:
::PostQuitMessage(0);
break;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
...{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
...{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
...{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}
运行程序,毫无新意,就是一个黄色的三角形
大部分都是有注释的,能看懂的吧,很早写的程序了,肯定很乱的,凑合吧
这里为了显示有没有ps的区别,先是用vs把三角形的颜色设成绿色,然后用ps改成黄色(吃饱撑的,哈哈)你可是注释掉关于ps的那部分代码,一样能工作的 去掉vs 同样能的,哈哈
前面说了,这个程序只是向你展示怎么用,怎么结合,下节就不会给出directx程序了,只给出shader,自己运行吧 出错就是你进步的开始