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.
There are a number of problems with the code you posted.
First of all, this won't work:
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:Then you can define your
zeme
like this: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:Then, inside your loop, initialise each entry like this:
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:
(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 yourCUSTOMVERTEX
has a structure like this (I'm guessing because you don't show it):Then, inside your loop, you could initialise each entry like this (you need to use whatever names are defined in your structure):
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.