Jump to content
  • Advertisement
Sign in to follow this  
Makoy

newbie question: How to use vertex

This topic is 4998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I got this piece of code from codesampler.com. what confuses me is this vertex structure and createvertexbuffer functions. Can you guys tell me how does this work? I only put this in my code but I don't know the details of how this codes works. I hope that you will help me with this one guys. Thanks! Here are the codes that keeps me wondering:
struct Vertex
{
    float x, y, z;
    float tu, tv;
};

Vertex g_quadVertices[] =
{
	{-1.0f, 1.0f, 0.0f,  0.0f,0.0f },
	{ 1.0f, 1.0f, 0.0f,  1.0f,0.0f },
	{-1.0f,-1.0f, 0.0f,  0.0f,1.0f },
	{ 1.0f,-1.0f, 0.0f,  1.0f,1.0f }
};

g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY, 
                                      D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, 
                                      &Vertex_Buffer, NULL );
    void *pVertices = NULL;

    Vertex_Buffer->Lock( 0, sizeof(g_quadVertices), (void**)&pVertices, 0 );
    memcpy( pVertices, g_quadVertices, sizeof(g_quadVertices) );
    Vertex_Buffer->Unlock();

    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ), 
                                640.0f / 480.0f, 0.1f, 100.0f );

Share this post


Link to post
Share on other sites
Advertisement
See the top bit, defines a vertex structure that has 5 components the first 3 are the vertices x, y and z coordinates (in 3d space), the next 2 are its texture coordinates (if a texture was applied to the vertex, this would matter).

In the bottom bit, A Vertex Buffer (DirectX's array used to hold vertices), to render (draw the vertices, you must use a Vertex Buffer). The buffer is first locked (allows writing of vertex data into the Vertex Buffer), saying that the pVertices pointer holds the vertex data. Then the vertex data is copied into the pVertices pointer. Then the Vertex Buffer is unlocked, doing the opposite of the Lock() function.

The last two lines create a perspective projection matrix, this just sets up how we view the seen (where the camera we look through is situated, and how far it can see).

Hope that helps, I probably confused you further.

All the best,
Keith

Share this post


Link to post
Share on other sites
Yeah I got you thanks. What confuses me now is the parameters in the lock(), D3DXMatrixPerspectiveFovLH and memcopy. can you tell me a few details of those parameters? specially the (void**)&pVertices. Thank you very much!

Share this post


Link to post
Share on other sites
Okay

memcpy will copy a portion of data in memory to another location in memory so pVertices was NULL, before this line (ie. It was pointing to memory location 0, but after it is pointing to a new location in memory with the vertex data at that location.

When you set up a 3d scene you need to use a projection matrix, this sets up how close and far object appear on the screen. this is what D3DMATRIXPERSPECTIVEFOVLH(). Here is a quick explanation of the paramaters:

  • The first one is the projection matrix, result of the function
  • The next one is the field of view (almost always 45 degrees, however the function only understands radians so we must convert it).
  • The next one is the aspect ratio, this is the
  • The next one is the distance to the near plane (closest visible point)
  • The next one is the distance to the far plane (furthest visible point)


The &(**void)pVertices result means "A pointer to a Pointer to a void type", we must do this because the Lock function expects this paramater as a "void **ptr".

Lock function:

  • The first paramater The offset in byte from start of buffer to lock
  • The next one is the amount of the buffer to lock
  • The next one is the vertex data (void**)
  • The last one is a flag, whether to lock entire buffer or just a segment of it.


DirectX has loads of these ugly interfaces, once you get used to them there ok!

All the best,
Keith

[Edited by - khalligan on November 15, 2004 9:42:57 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!