Create struct for vertex buffer on DX9

805 views Asked by At

I have a huge problem in my DirectX project. I need to make struct for vertexbuffer..

CUSTOMVERTEX vertices[] =
{
    { 2.5f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
    { 0.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
    { -2.5f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
};

But how do I do that in a loop? I need to make huge field of triangles(3d terrain). So, I tried something like this:

void Field_Generation::Field_Generate(int X, int Y, int Z)
{
int zeme[X][Y];
int n=0;

for (int i=0;i < X;i++)
{
    for (int j=0;i < Y;j++)
    {
        zeme[i][j] = rand() % Z;
    }
}

for (int i=0;i < X;i++)
{
    for (int j=0;i < Y;j++)
    {
        int color1= rand() % 255;
        int color2= rand() % 255;
        int color3= rand() % 255;

        n++;
    CUSTOMVERTEX verticles[n] = {{i,j,zeme[i][j],D3DCOLOR_XRGB(color1,color2,color3),}};


    }
}
}

But this ain't working for me. :( I can't figure out how to do that. Help please. Thank you.

2

There are 2 answers

0
Roger Rowland On BEST ANSWER

There are a number of problems with the code you posted.

First of all, this won't work:

int zeme[X][Y];

You can't create a dynamic array this way. Although there are various ways to do it, I would recommend using std::vector so at the top of your source make sure you include the right header:

#include <vector>
using namespace std;

Then you can define your zeme like this:

vector<vector<int> > zeme(X, vector<int>(Y, 0));

Now you can access your elements using zeme[x][y] as you do already.

Next you need to create an array to hold your custom vertex data, and again you can use a std::vector. This should be defined as a class member or a global variable rather than being defined inside your function. Something like:

std::vector<CUSTOMVERTEX> m_pVertices;

Then, inside your loop, initialise each entry like this:

CUSTOMVERTEX pVertex = {i, j, zeme[i][j], D3DCOLOR_XRGB(color1,color2,color3) };
m_pVertices.push_back(pVertex);

Later, when you need to pass a pointer to your array of vertices to DirectX for drawing, use this syntax to get a pointer to the start of the data:

CUSTOMVERTEX *pData = &m_pVertices[0];

(Note that in C++11 there are alternative ways of handling all of the above)

Another alternative, if you already have an array of CUSTOMVERTEX is to initialise each component inside the loop. So, assuming your CUSTOMVERTEX has a structure like this (I'm guessing because you don't show it):

struct CUSTOMVERTEX 
{
    float x, y, z;
    D3DCOLOR nCol;
};

Then, inside your loop, you could initialise each entry like this (you need to use whatever names are defined in your structure):

verticles[n].x = i;
verticles[n].y = j;
verticles[n].z = zeme[i][j];
verticles[n].nCol = D3DCOLOR_XRGB(color1,color2,color3);

So, you assign each value to a member of the struct rather than trying to assign the whole struct in one hit like I did in the first example.

0
zdd On

You can set the value of a struct by setting each of it's component, like this.

struct Person
{
    int age;
    string name;
};

Person person[2];

person[0].age = 20;
person[0].name = "zdd";

person[1].age = 30;
person[1].name = "ddz";