📄 directx sdk 2006学习笔记2——顶点缓冲.mht
字号:
style=3D"COLOR: black">z</SPAN>, <SPAN style=3D"COLOR: =
black">rhw</SPAN>;=20
<SPAN style=3D"COLOR: green">// The transformed position for the=20
vertex</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">DWORD</SPAN><SPAN=20
style=3D"COLOR: =
black">color</SPAN>;<SPAN> =20
<SPAN style=3D"COLOR: green">// The vertex =
color</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft>};</DIV>
<DIV align=3Dleft>=E3=80=80=E3=80=80=E5=85=B6=E4=B8=AD=EF=BC=8Cx, =
y,=20
=
z=E6=98=AF=E9=A1=B6=E7=82=B9=E5=9C=A83=E7=BB=B4=E7=A9=BA=E9=97=B4=E7=9A=84=
=E6=9C=80=E7=BB=88=E4=BD=8D=E7=BD=AE=EF=BC=8Crhw=E6=98=AF3=E7=BB=B4=E7=9F=
=A9=E9=98=B5=E7=9A=84=E5=80=92=E6=95=B0=EF=BC=88=E4=B8=8D=E6=98=8E=E7=99=BD=
=E7=9A=84=E8=AF=9D=E6=89=BE=E6=9C=AC=E5=9B=BE=E5=BD=A2=E5=AD=A6=E7=9A=84=E4=
=B9=A6=E7=A0=94=E7=A9=B6=E4=B8=80=E4=B8=8B=EF=BC=89=EF=BC=8Ccolor=E8=87=AA=
=E7=84=B6=E5=B0=B1=E6=98=AF=E9=A1=B6=E7=82=B9=E7=9A=84=E9=A2=9C=E8=89=B2=E4=
=BA=86=E3=80=82=E7=94=B1=E4=BA=8E=E8=BF=99=E4=B8=AA=E7=BB=93=E6=9E=84=E6=98=
=AF=E8=87=AA=E5=AE=9A=E4=B9=89=E7=9A=84=EF=BC=8C=E6=89=80=E4=BB=A5=E6=88=91=
=E4=BB=AC=E9=9C=80=E8=A6=81=E5=91=8A=E8=AF=89D3D=E5=BA=94=E8=AF=A5=E5=A6=82=
=E4=BD=95=E8=AF=86=E5=88=AB=E8=BF=99=E4=B8=AA=E7=BB=93=E6=9E=84=EF=BC=8C=E8=
=BF=99=E5=B0=B1=E9=9C=80=E8=A6=81=E6=88=91=E4=BB=AC=E5=AE=9A=E4=B9=89=E4=B8=
=80=E4=B8=AA=E5=B8=B8=E9=87=8F=E4=BA=86=EF=BC=9A</DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft><SPAN=20
style=3D"FONT-SIZE: 11pt; COLOR: green">// Our custom FVF, which =
describes=20
our custom vertex structure</SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft><SPAN=20
style=3D"FONT-SIZE: 11pt; COLOR: blue">#define </SPAN><SPAN=20
style=3D"FONT-SIZE: 11pt; COLOR: =
black">D3DFVF_CUSTOMVERTEX</SPAN><STRONG><SPAN=20
style=3D"FONT-SIZE: 11pt"> (</SPAN></STRONG><SPAN=20
style=3D"FONT-SIZE: 11pt; COLOR: black">D3DFVF_XYZRHW</SPAN><SPAN=20
style=3D"FONT-SIZE: 11pt">|<SPAN=20
style=3D"COLOR: black">D3DFVF_DIFFUSE</SPAN>)</SPAN></DIV>
=
<DIV>=E3=80=80=E3=80=80=E8=BF=99=E4=B8=AAD3DFVF_CUSTOMVERTEX=E5=B0=B1=E6=98=
=AF=E7=94=A8=E6=9D=A5=E5=91=8A=E8=AF=89D3D=E4=B8=8A=E9=9D=A2=E9=82=A3=E4=B8=
=AACUSTOMVERTEX=E7=BB=93=E6=9E=84=E6=98=AF=E5=A6=82=E4=BD=95=E7=BB=84=E7=BB=
=87=E7=9A=84=EF=BC=9A=E9=A6=96=E5=85=88=E6=98=AFD3DFVF_XYZRHW=EF=BC=8C=E5=
=8D=B3=E9=A1=B6=E7=82=B9=E5=9C=A83=E7=BB=B4=E5=9D=90=E6=A0=87=E7=B3=BB=E7=
=9A=84=E6=9C=80=E7=BB=88=E4=BD=8D=E7=BD=AE=EF=BC=8CD3D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=E5=85=A5=E9=97=A8=E4=B8=80=E4=B9=A6=E4=B8=AD=E5=AF=B9=
=E5=A4=8D=E6=9D=82=E7=9A=84FVF=E4=B8=BE=E7=9A=84=E4=B8=80=E4=B8=AA=E4=BE=8B=
=E5=AD=90=EF=BC=8C=E4=BE=9B=E5=8F=82=E8=80=83</DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft><SPAN=20
style=3D"COLOR: blue">typedef </SPAN><SPAN style=3D"COLOR: =
blue">struct=20
</SPAN><SPAN style=3D"COLOR: black">SObjVertex</SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft>{</DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">FLOAT</SPAN><SPAN=20
style=3D"COLOR: black">x</SPAN>, <SPAN style=3D"COLOR: black">y, =
<SPAN=20
style=3D"COLOR: =
black">z</SPAN>; &nb=
sp; <SPAN=20
style=3D"COLOR: green">// position</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">FLOAT</SPAN><SPAN=20
style=3D"COLOR: black">nx</SPAN>, <SPAN style=3D"COLOR: black">ny, =
<SPAN=20
style=3D"COLOR: =
black">nz</SPAN>; &n=
bsp; =20
<SPAN style=3D"COLOR: green">// normal</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">DWORD</SPAN><SPAN=20
style=3D"COLOR: =
black">diffuse</SPAN>;<SPAN> &nb=
sp; =20
<SPAN style=3D"COLOR: green">// diffuse color</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">DWORD</SPAN><SPAN=20
style=3D"COLOR: =
black">specular</SPAN>;<SPAN> &n=
bsp; <SPAN=20
style=3D"COLOR: green">// specular color</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">FLOAT</SPAN><SPAN=20
style=3D"COLOR: black">tu</SPAN>, <SPAN=20
style=3D"COLOR: =
black">tv; &nb=
sp; =20
<SPAN style=3D"COLOR: green">// first pair of texture=20
coordinates</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">FLOAT</SPAN><SPAN=20
style=3D"COLOR: black">tu2</SPAN>, <SPAN style=3D"COLOR: =
black">tv2, <SPAN=20
style=3D"COLOR: =
black">tw2</SPAN>; =20
<SPAN style=3D"COLOR: green">// second pair of texture=20
coordinates</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">FLOAT</SPAN><SPAN=20
style=3D"COLOR: black">tu3</SPAN>, <SPAN=20
style=3D"COLOR: =
black">tv3; &n=
bsp; =20
<SPAN style=3D"COLOR: green">// third pair of texture=20
coordinates</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">FLOAT</SPAN><SPAN=20
style=3D"COLOR: black">tu4</SPAN>, <SPAN=20
style=3D"COLOR: =
black">tv4; &n=
bsp; =20
<SPAN style=3D"COLOR: green">// fourth pair of texture=20
coordinates</SPAN></SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft>} <SPAN=20
style=3D"COLOR: black">SObjVertex;</SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 21pt" align=3Dleft> </DIV>
<DIV style=3D"MARGIN-LEFT: 210pt; TEXT-INDENT: -189pt" =
align=3Dleft><SPAN=20
style=3D"COLOR: blue">const</SPAN> <SPAN style=3D"COLOR: =
black">DWORD</SPAN>=20
<SPAN style=3D"COLOR: black">gSObjVertexFVF</SPAN> =3D (<SPAN=20
style=3D"COLOR: black">D3DFVF_XYZ</SPAN> | <SPAN=20
style=3D"COLOR: black">D3DFVF_DIFFUSE</SPAN> | <SPAN=20
style=3D"COLOR: black">D3DFVF_SPECULAR</SPAN> |<SPAN=20
style=3D"COLOR: black">D3DFVF_NORMAL</SPAN> | <SPAN=20
style=3D"COLOR: black">D3DFVF_TEX4</SPAN> | <SPAN=20
style=3D"COLOR: black">D3DFVF_TEXCOORDSIZE2</SPAN>(0) | <SPAN=20
style=3D"COLOR: black">D3DFVF_TEXCOORDSIZE3(1)</SPAN> | =
<SPAN=20
style=3D"COLOR: black">D3DFVF_TEXCOORDSIZE2(2) | <SPAN=20
style=3D"COLOR: black">D3DFVF_TEXCOORDSIZE2</SPAN>(3) =
);</SPAN></DIV>
<DIV> </DIV>
=
<DIV>=E3=80=80=E3=80=80=E7=9C=8B=E5=AE=8C=E4=BA=86FVF=E9=A1=B6=E7=82=B9=E6=
=A0=BC=E5=BC=8F=E7=9A=84=E5=AE=9A=E4=B9=89=EF=BC=8C=E5=B0=B1=E5=8F=AF=E4=BB=
=A5=E7=9C=8B=E5=AE=83=E6=98=AF=E5=A6=82=E4=BD=95=E4=BD=BF=E7=94=A8=E7=9A=84=
=E3=80=82=E4=BD=BF=E7=94=A8=E6=96=B9=E6=B3=95=E9=9D=9E=E5=B8=B8=E7=AE=80=E5=
=8D=95=EF=BC=8C=E5=9C=A8OnCreateDevice=E4=B8=AD=E6=B7=BB=E5=8A=A0=E5=88=9B=
=E5=BB=BA=E5=92=8C=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=
=9C=A8OnFrameRender=E4=B8=AD=E6=B7=BB=E5=8A=A0=E6=B8=B2=E6=9F=93=E4=BB=A3=
=E7=A0=81=E5=B0=B1=E5=8F=AF=E4=BB=A5=E4=BA=86=EF=BC=8C=E5=85=B7=E4=BD=93=E5=
=A6=82=E4=B8=8B=EF=BC=9A</DIV>
<DIV><SPAN style=3D"COLOR: =
black">LPDIRECT3DVERTEXBUFFER9</SPAN><SPAN=20
style=3D"COLOR: black">g_pVB</SPAN><STRONG> =3D </STRONG><SPAN=20
style=3D"COLOR: black">NULL</SPAN>; <SPAN style=3D"COLOR: =
green">// Buffer to=20
hold vertices</SPAN></DIV>
<DIV align=3Dleft><SPAN style=3D"COLOR: black">HRESULT</SPAN><SPAN =
style=3D"COLOR: black">CALLBACK</SPAN><SPAN=20
style=3D"COLOR: black">OnCreateDevice</SPAN>( <SPAN=20
style=3D"COLOR: black">IDirect3DDevice9* <SPAN=20
style=3D"COLOR: black">pd3dDevice</SPAN>, <SPAN=20
style=3D"COLOR: blue">const</SPAN><SPAN=20
style=3D"COLOR: black">D3DSURFACE_DESC</SPAN>* <SPAN=20
style=3D"COLOR: black">pBackBufferSurfaceDesc</SPAN>, <SPAN=20
style=3D"COLOR: blue">void</SPAN>* <SPAN=20
style=3D"COLOR: black">pUserContext</SPAN><STRONG> =
)</STRONG></SPAN></DIV>
<DIV>{</DIV>
=
<DIV><SPAN> &n=
bsp; =20
=E2=80=A6</SPAN></DIV>
<DIV style=3D"TEXT-INDENT: 42pt" align=3Dleft><SPAN =
style=3D"COLOR: green">//=20
Initialize three vertices for rendering a triangle</SPAN></DIV>
<DIV align=3Dleft><SPAN> =
</SPAN><SPAN=20
style=3D"COLOR: black">CUSTOMVERTEX</SPAN> <SPAN=20
style=3D"COLOR: black">vertices</SPAN>[] =3D</DIV>
<DIV align=3Dleft><SPAN> { =
</SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 42pt; TEXT-INDENT: 42pt" =
align=3Dleft><SPAN=20
style=3D"COLOR: green">// x, y, z, rhw, color</SPAN></DIV>
<DIV=20
=
align=3Dleft><SPAN> =
=20
{ 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, },</SPAN></DIV>
<DIV=20
=
align=3Dleft><SPAN> =
=20
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },</SPAN></DIV>
<DIV=20
=
align=3Dleft><SPAN> =
=20
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },</SPAN></DIV>
<DIV align=3Dleft><SPAN> =20
}; </SPAN></DIV>
<DIV style=3D"TEXT-INDENT: 42pt" align=3Dleft><SPAN=20
style=3D"COLOR: blue">if</SPAN>( <SPAN style=3D"COLOR: =
black">FAILED( <SPAN=20
style=3D"COLOR: black">pd3dDevice</SPAN>-><SPAN=20
style=3D"COLOR: black">CreateVertexBuffer</SPAN>( </SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 42pt; TEXT-INDENT: 42pt" =
align=3Dleft>3*<SPAN=20
style=3D"COLOR: blue">sizeof(<SPAN=20
style=3D"COLOR: black">CUSTOMVERTEX</SPAN>),</SPAN></DIV>
<DIV=20
=
align=3Dleft><SPAN> =
=20
0, </SPAN><SPAN style=3D"COLOR: =
black">D3DFVF_CUSTOMVERTEX</SPAN>,</DIV>
<DIV=20
=
align=3Dleft><SPAN> =
=20
</SPAN><SPAN style=3D"COLOR: black">D3DPOOL_MANAGED</SPAN>, =
&<SPAN=20
style=3D"COLOR: black">g_pVB, <SPAN style=3D"COLOR: =
black">NULL</SPAN> ) )=20
)</SPAN></DIV>
<DIV align=3Dleft><SPAN> =
{</SPAN></DIV>
<DIV=20
=
align=3Dleft><SPAN> =
=20
</SPAN><SPAN style=3D"COLOR: blue">return</SPAN> <SPAN=20
style=3D"COLOR: black">E_FAIL</SPAN>;</DIV>
<DIV align=3Dleft><SPAN> =
}</SPAN></DIV>
<DIV align=3Dleft><SPAN> =
</SPAN><SPAN=20
style=3D"COLOR: green">// Now we fill the vertex buffer. To do =
this, we need=20
to Lock() </SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 42pt" align=3Dleft><SPAN =
style=3D"COLOR: green">//=20
</SPAN><SPAN style=3D"COLOR: green">the VB to</SPAN><SPAN=20
style=3D"COLOR: green">gain access to the vertices. This mechanism =
is=20
</SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 42pt" align=3Dleft><SPAN =
style=3D"COLOR: green">//=20
</SPAN><SPAN style=3D"COLOR: green">required becuase =
vertex</SPAN><SPAN=20
style=3D"COLOR: green">buffers may be in device =
memory.</SPAN></DIV>
<DIV =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">VOID</SPAN>* <SPAN=20
style=3D"COLOR: black">pVertices;</SPAN></DIV>
<DIV =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: blue">if</SPAN>( <SPAN=20
style=3D"COLOR: black">FAILED( <SPAN=20
style=3D"COLOR: black">g_pVB</SPAN>-><SPAN=20
style=3D"COLOR: black">Lock</SPAN>( 0, <SPAN=20
style=3D"COLOR: blue">sizeof</SPAN>(<SPAN=20
style=3D"COLOR: black">vertices</SPAN>), </SPAN></DIV>
<DIV style=3D"MARGIN-LEFT: 126pt; TEXT-INDENT: 42pt" =
align=3Dleft>(<SPAN=20
style=3D"COLOR: blue">void**)&<SPAN=20
style=3D"COLOR: black">pVertices</SPAN>, 0 ) ) )</SPAN></DIV>
<DIV=20
=
align=3Dleft><STRONG><SPAN> &nbs=
p; =20
</SPAN></STRONG><SPAN style=3D"COLOR: blue">return</SPAN><SPAN=20
style=3D"COLOR: black">E_FAIL</SPAN>;</DIV>
<DIV =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: black">memcpy</SPAN>( <SPAN=20
style=3D"COLOR: black">pVertices, <SPAN=20
style=3D"COLOR: black">vertices</SPAN>, <SPAN=20
style=3D"COLOR: blue">sizeof</SPAN>(<SPAN=20
style=3D"COLOR: black">vertices</SPAN>) );</SPAN></DIV>
<DIV =
align=3Dleft><STRONG><SPAN> =20
</SPAN></STRONG><SPAN style=3D"COLOR: =
black">g_pVB</SPAN>-><SPAN=20
style=3D"COLOR: black">Unlock();</SPAN></DIV>
<DIV align=3Dleft>}</DIV>
=
<DIV>=E3=80=80=E3=80=80=E4=B8=BA=E4=BA=86=E7=BB=9F=E4=B8=80=E5=92=8C=E5=BC=
=BA=E8=B0=83=E7=B2=BE=E5=BA=A6=EF=BC=8CD3D=E9=87=87=E7=94=A8=E4=BA=86floa=
t=E4=BD=9C=E4=B8=BA=E5=85=B6=E4=B8=BB=E8=A6=81=E7=9A=84=E6=95=B0=E5=80=BC=
=E7=B1=BB=E5=9E=8B=E3=80=82=E4=B8=8A=E9=9D=A2=E7=9A=84=E7=A8=8B=E5=BA=8F=E8=
=B0=83=E7=94=A8=E4=BA=86CreateVertexBuffer=E5=92=8CLPDIRECT3DVERTEXBUFFER=
9->=20
=
Lock=E5=92=8C=E3=80=80Unlock=E5=87=BD=E6=95=B0=E3=80=82=E5=8F=A6=E5=A4=96=
=E9=9C=80=E8=A6=81=E5=9C=A8OnFrameRender=E4=B8=AD=E7=9A=84BeginScene=E5=92=
=8CEndScene=E4=B8=AD=E8=B0=83=E7=94=A8=E5=A6=82=E4=B8=8B=E4=BB=A3=E7=A0=81=
</DIV>
<DIV style=3D"MARGIN-LEFT: 31.5pt" align=3Dleft><SPAN=20
style=3D"COLOR: black">pd3dDevice</SPAN>-><SPAN=20
styl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -