|
很简单的一个代码:
- //uniform float4x4 uModelViewProj;
- void main( float4 Position : POSITION,
- float4 Normal : NORMAL,
- out float4 oPosition : POSITION,
- out float4 oColor : COLOR
- , uniform float4x4 uModelViewProj )
- {
- int a = 100;
- oPosition = mul( Position, uModelViewProj );
- oColor.x = 1.0;
- oColor.y = 0.0;
- oColor.z = 0.0;
- oColor.w = 0.5;
- }
复制代码
如果uModelViewProj放在参数里面,则什么都看不到。如果放在全局,把参数中的uModelViewProj的注掉,那么就正常了。
产生顶点的程序代码:
- HRESULT InitGeometry()
- {
- //创顶点缓冲区
- if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX),
- 0, D3DFVF_CUSTOMVERTEX,
- D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
- {
- return E_FAIL;
- }
- //填充顶点缓冲区
- CUSTOMVERTEX* pVertices;
- if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
- return E_FAIL;
- for( DWORD i=0; i<50; i++ )
- {
- FLOAT theta = (2*D3DX_PI*i)/(50-1);
- pVertices[2*i+0].position = D3DXVECTOR3( sinf(theta),-1.0f, cosf(theta) );
- pVertices[2*i+0].normal = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) );
- pVertices[2*i+1].position = D3DXVECTOR3( sinf(theta), 1.0f, cosf(theta) );
- pVertices[2*i+1].normal = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) );
- }
- g_pVB->Unlock();
- LPD3DXBUFFER pCode = NULL;
- LPD3DXBUFFER pErrorMsg = NULL;
- if( FAILED( ::D3DXCompileShaderFromFile( L"SimpleLight_v.cg", NULL, NULL,
- "main", "vs_2_0", D3DXSHADER_SKIPOPTIMIZATION|D3DXSHADER_DEBUG,
- &pCode,
- &pErrorMsg, &g_pConstantTable_VS ) )
- )
- {
- ::MessageBoxA( NULL, (const char*)pErrorMsg->GetBufferPointer(), "error", 0 );
- ::PostQuitMessage(0);
- return FALSE;
- }
- if( FAILED( g_pd3dDevice->CreateVertexShader(
- (DWORD*)pCode->GetBufferPointer(), &g_pVertexShader) ) )
- {
- ::MessageBoxA( NULL, "Error2!!!", "error", 0 );
- ::PostQuitMessage(0);
- return FALSE;
- }
- assert( g_pConstantTable_VS != NULL );
- assert( g_pVertexShader != NULL );
- pCode->Release();
- //设置变换矩阵
- SetMatrices();
- return S_OK;
- }
复制代码
设置矩阵的代码;
- VOID SetMatrices()
- {
- //建立并设置世界矩阵
- D3DXMATRIX matWorld;
- D3DXMatrixIdentity( &matWorld );
- //建立并设置观察矩阵
- D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
- D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
- D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
- D3DXMATRIX matView;
- D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
- //建立并设置投影矩阵
- D3DXMATRIX matProj;
- D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
- assert( g_pConstantTable_VS != NULL );
- D3DXMATRIXA16 matModelViewProj = matWorld*matView*matProj;
- g_pConstantTable_VS->SetMatrix( g_pd3dDevice, "uModelViewProj", &matModelViewProj );
- }
复制代码
效果区别图:
|
|