SetStreamSource的解惑
HRESULT SetStreamSource(
[in] UINT StreamNumber,
[in] IDirect3DVertexBuffer9 *pStreamData,
[in] UINT OffsetInBytes,
[in] UINT Stride
);
这个是DirectX中SetStreamSource函数的原型声明,文档上说的很简单,就是把顶点缓存和设备数据流绑定,剩下的就不大清楚了。
1 参数解释
StreamNumber:这个是设备数据流的序号,一般程序是用的都是单流,此时设置参数为0。如果想试用多个数据流,就得使用顶点声明,其实FVF最终也会在内部转换为顶点声明。
pStreamData:这个是顶点缓存。
OffsetInBytes:这个偏移量是相对顶点缓存的偏移量,绘制函数才绘制的时候也是从相对于顶点缓存的此偏移量开始计算的。所以它并不是顶点缓存的起始位置相对于数据流起始位置的偏移量。后面的程序可以解释。
Stride:这个跨度自然就是单个顶点的大小了,绘图函数也是根据这个跨度进行取点的。
2 示例代码
bool InitObject() { intnVertexNum = 6; HRESULT hRe = NULL; hRe = g_pDevice->CreateVertexBuffer(nVertexNum*sizeof(D3DVertex),0, D3DFVF_VERTEX,D3DPOOL_MANAGED,&g_pVertex,NULL); if(FAILED(hRe) ) { MessageBox(NULL,TEXT("Failed to create Vertex buffer"),TEXT("ERROR"),MB_OK); returnfalse; } D3DCOLOR dcol =D3DCOLOR_XRGB(255,255,255); D3DVertex *Vertexs = NULL; g_pVertex->Lock(0,0,(void**)&Vertexs,0); Vertexs[0] =D3DVertex(320.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0)); Vertexs[1] =D3DVertex(420.0f,350.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0)); Vertexs[2] =D3DVertex(220.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0)); Vertexs[3] =D3DVertex(320.0f,200.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0)); Vertexs[4] =D3DVertex(420.0f,400.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0)); Vertexs[5] = D3DVertex(220.0f,200.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0)); g_pVertex->Unlock(); ///--------------------------------------------------------------------- nVertexNum = 4; hRe = g_pDevice->CreateVertexBuffer(nVertexNum*sizeof(D3DVertex),0, D3DFVF_VERTEX,D3DPOOL_MANAGED,&g_pVertex2,NULL); if(FAILED(hRe) ) { MessageBox(NULL,TEXT("Failed to create Vertex buffer"),TEXT("ERROR"),MB_OK); returnfalse; } D3DVertex *Vertexs2 = NULL; g_pVertex2->Lock(0,0,(void**)&Vertexs2,0); Vertexs2[0] =D3DVertex(440.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0)); Vertexs2[1] =D3DVertex(540.0f,350.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0)); Vertexs2[2] =D3DVertex(340.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0)); Vertexs2[3] = D3DVertex(600.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0)); g_pVertex2->Unlock(); D3DXVECTOR3 vPosition(0.0f, 0.0f,-5.0f); D3DXVECTOR3 vTarget(0.0f, 0.0f, 0.0f); D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f); D3DXMATRIX vView; D3DXMatrixLookAtLH(&vView,&vPosition,&vTarget,&vUp); g_pDevice->SetTransform(D3DTS_VIEW, &vView); D3DXMATRIX mProj; //D3DXMatrixOrthoLH(&vProj,(FLOAT)g_nWidth,(FLOAT)g_nHeight,1.0f,1000.0f); D3DXMatrixPerspectiveFovLH(&mProj,D3DX_PI* 0.5f, (float)g_nWidth/ (float)g_nHeight, 1.0f,1000.0f); g_pDevice->SetTransform(D3DTS_PROJECTION,&mProj); g_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW); g_pDevice->SetRenderState(D3DRS_LIGHTING,TRUE); return true; } void RenderScene() { DWORD DDXCLEAR =D3DCLEAR_TARGET|D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER; g_pDevice->Clear(0,NULL,DDXCLEAR,D3DCOLOR_XRGB(0,0,0),1.0f,0); g_pDevice->BeginScene(); HRESULT hr; hr =g_pDevice->SetStreamSource(0,g_pVertex,0,2*sizeof(D3DVertex)); //g_pDevice->SetStreamSource(0,g_pVertex2,3*sizeof(D3DVertex),sizeof(D3DVertex)); g_pDevice->SetFVF(D3DFVF_VERTEX); g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); hr =g_pDevice->SetStreamSource(0,g_pVertex2,sizeof(D3DVertex),sizeof(D3DVertex) ); g_pDevice->SetFVF(D3DFVF_VERTEX); g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); g_pDevice->EndScene(); g_pDevice->Present(NULL,NULL,NULL,NULL); }
3 结果
4 总结
<1>SetStreamSource函数的作用是将顶点缓存与数据流绑定,并在其中指定绘图的起始点相对顶点缓存的偏移量,数据流的序号。
<2>一个程序可以有多个顶点缓存。
<3>数据流设置完毕后,还要设置FVF,其实这个最终还要在内部转换为顶点的声明。
<4>SetStreamSource中的OffsetInBytes是说的绘图开始点距离顶点缓存起始位置的偏移量。
时间: 2024-10-28 20:59:40