Thread: VertexBuffer (D3D) question

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    VertexBuffer (D3D) question

    I noticed on all the tutorials you create the vertexbuffer the number of vetices times the size of the class why is this so why woudent you create the VertexBuffer just the size of the class holding your points to draw?
    so i made a program to understand why you would need all this extra memory but i still cant firgure it out.

    Code:
    #include <iostream>
    using namespace std;
    
    class VertexBuffer
    {
    public:
    	float x,y,z;
    };
    int main()
    {
    	VertexBuffer VBuffer[5] =
    	{
    		{1,2,1},
    		{2,2,2},
    		{0,1,0},
    		{3,1,3},
    		{9,5,1},
    	};
    
    	cout << "buffer size : " << sizeof(VBuffer) <<endl;
    	cout << "Seven times Buffer size : " << sizeof(VBuffer)*5 <<endl;
    
    	cin.get();
    }
    Please explain this to me is there something im not seeing or is this just a waste of memory.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Extend your investigation a little and it should hopefully become a little clearer:
    Code:
    #include <iostream>
    using namespace std;
    
    class VertexBuffer
    {
    public:
        float x,y,z;
    };
    int main()
    {
        VertexBuffer VBuffer[5] =
        {
            {1,2,1},
            {2,2,2},
            {0,1,0},
            {3,1,3},
            {9,5,1},
        };
        
        cout << "buffer size (VBuffer) : " << sizeof(VBuffer) <<"\n";
        cout << "five times Buffer size (VBuffer * 5) : " << sizeof(VBuffer)*5 <<
          "\n\n";
        
        cout << "buffer size (VBuffer[0]) : " << sizeof(VBuffer[0]) <<"\n";
        cout << "five times Buffer size (VBuffer[0] * 5) : " << 
          sizeof(VBuffer[0])*5 << "\n\n";
        
        cout << "buffer size (VertexBuffer) : " << sizeof(VertexBuffer) <<"\n";
        cout << "five times Buffer size (VertexBuffer * 5) : " << 
          sizeof(VertexBuffer)*5 <<endl;
    
        cin.get();
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The vertex buffer is not just coordinates and not just floats so you cannot just allocate how many vertices you have.

    Code:
    struct Vertex
    {
      float x,y,z;
      D3DCOLOR diffuse;
      float u,v;
    
      static const DWORD FVF;
      Vertex(float _x,float _y,float _z,float _u,float _v):x(_x),y(_y),z(_z),u(_u),v(_v) {}
    };
    This is a vertex structure. It defines one vertex in 3D space. But it also has with it texture coords and a constructor as well as a flexible vertex format DWORD that tells Direct3D what to expect in the data stream and in what order to expect it. This is so it can properly use your data.

    So let's say we have 10 vertices in space of the type we just mentioned. If you only allocate 10 in your call to CreateVertexBuffer() you are way off. You need way more than 10 bytes because each vertex is sizeof(Vertex) bytes in size. Hence you have 10*sizeof(Vertex). This will allocate the correct amount of memory for 10 vertices of type Vertex.
    You are not only allocating space for a standard data type like float or integer you are allocating space for an aggregated data type that is composed of several other data types and/or functions/function pointers.

    The total size of all the data types and information inside of the Vertex structure is retrieved by using sizeof(Vertex).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM