CREATING WELL-FORMED VERTEX DATA

Associating a Vertex Buffer with a Stream

The next step, after we've created a vertex buffer, is getting ready to render the buffer. You'll typically be setting up the stream sources that go with a particular vertex buffer and then rendering the object with a render primitive call.

 // DirectX 8! // Set vertex stream 0 with our vertex buffer g_pD3DDevice->SetStreamSource(      0,             // Stream number to set (start at 0)      pVertexBuffer, // the vertex buffer handle      0              // stride      ); // We can set a vertex shader or an FVF code here // so (for now) we show it using our FVF code g_pD3DDevice->SetVertexShader( D3DFVF_MYCUSTOMVERTEX ); // render it g_pD3DDevice->DrawPrimitive(      D3DPT_TRIANGLELIST.    // our type of primitive      0,                     // starting index into vertex buffer      ArraySize/3,           // how many triangles = # primitives/3      ); 

So you can see there's the setup of the vertex buffer, which should happen only at initialization time. Then there's the preparation for the actual rendering where you'll be associating vertex buffers with streams, which will probably happen many times while you are rendering a scene. In the SetVertexShader() function call in the example, I used the FVF format flag. There really isn't a vertex shader in this case but simply a call to the rendering engine so that it knows the layout of the vertex information.

This was a change introduced in DirectX 8. A "real" vertex shader (consisting of code written in the shader language) would be written for a particular FVF and would know how to access the vertex stream for the correct data, even doing something like remapping the texture coordinate information in the example given earlier. This should give you an idea of the potential power of writing your own shaders. In DirectX 9, if you want to use the FFP, you'll have to set the vertex format using the SetFVF() function.

The actual pipeline from vertex data to rendering is illustrated in Figure 5.1.

click to expand
Figure 5.1: Vertex streams get loaded into vertex registers by a render call.

The input to a vertex shader is one or more vertex streams plus any other state information (like textures). The output is a new vertex position in clip coordinates and any other information that the shader provides, like vertex color, texture coordinates, fog values, etc.



Real-Time Shader Programming(c) Covering Directx 9. 0
Real-Time Shader Programming (The Morgan Kaufmann Series in Computer Graphics)
ISBN: 1558608532
EAN: 2147483647
Year: 2005
Pages: 104
Authors: Ron Fosner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net