Jump to content
  • Advertisement
Sign in to follow this  
Guest

Trouble with C++ and dynamic arrays

This topic is 3077 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

Guest
Hey,
Im working on some terrain in C++ and it works great so far. However i have the following problem.

I declare my indices statically(Must be known before runtime), However i want
to change this into a variable and the only other method is using a vector.
When i use a vector my program doesnt render the terrain. So anyhelp please?
Thanks:D

Here is a small fragment of my code!



short indices[(50-1)*(50-1)*6];

for (int x=0;x < width-1 ;x++)
{
for (int y=0; y < length-1 ;y++)
{
indices[(x+y*(width-1))*6+2] = x+y*width;
indices[(x+y*(width-1))*6+1] = (x+1)+y*width;
indices[(x+y*(width-1))*6] = (x+1)+(y+1)*width;

indices[(x+y*(width-1))*6+3] = (x+1)+(y+1)*width;
indices[(x+y*(width-1))*6+4] = x+y*width;
indices[(x+y*(width-1))*6+5] = x+(y+1)*width;

}
}
device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,
&indexBuffer,NULL);
VOID* pIndex;
indexBuffer->Lock(0, (width-1)*(length-1)*6*sizeof(short), (void**)&pIndex, 0);
memcpy(pIndex,&indices,sizeof(indices));
indexBuffer->Unlock();

Share this post


Link to post
Share on other sites
Advertisement
Guest
vector<short> indices((50-1)*(50-1)*6);

for (int x=0;x < width-1 ;x++)
{
for (int y=0; y < length-1 ;y++)
{
indices[(x+y*(width-1))*6+2] = x+y*width;
indices[(x+y*(width-1))*6+1] = (x+1)+y*width;
indices[(x+y*(width-1))*6] = (x+1)+(y+1)*width;

indices[(x+y*(width-1))*6+3] = (x+1)+(y+1)*width;
indices[(x+y*(width-1))*6+4] = x+y*width;
indices[(x+y*(width-1))*6+5] = x+(y+1)*width;

}
}
device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL);
VOID* pIndex;
indexBuffer->Lock(0, (width-1)*(length-1)*6*sizeof(short), (void**)&pIndex, 0);
memcpy(pIndex,&indices,sizeof(indices));
indexBuffer->Unlock();

Share this post


Link to post
Share on other sites
I think it has to do with the flag with which you create your index buffer.
D3DUSAGE_WRITEONLY is used for creating the index buffer in video memory as its fast for reading once its written. Hence, normally static data is placed in it. Whereas for variable data, you can use D3DUSAGE_DYNAMIC.. You can look in MSDN for further knowledge.

Share this post


Link to post
Share on other sites
Guest
Okayyyyyyy i tried

device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_DYNAMIC,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL);

and for some reason it i get a system.nullreferenceexception

Share this post


Link to post
Share on other sites
Here:

memcpy(pIndex,&indices,sizeof(indices));
^


You are copying the vector. You should copy the data instead.


memcpy(pIndex, &indices.front(), indices.size() * sizeof(indices[0]));

Share this post


Link to post
Share on other sites
Guest
Quote:
Original post by rozz666
Here:

memcpy(pIndex,&indices,sizeof(indices));
^


You are copying the vector. You should copy the data instead.


memcpy(pIndex, &indices.front(), indices.size() * sizeof(indices[0]));


Ahhhhhhhhh works perfectly! Thank you my good man!

Share this post


Link to post
Share on other sites
Guest
okay new problem...
I have now used what i have learnt for my vertices however it screws up the program


vector<CUSTOMVERTEX> terrainVertices(50*50);
for (int x=0;x< width;x++)
{
for (int y=0; y< length;y++)
{
terrainVertices[x+y*width].x=x;
terrainVertices[x+y*width].y=0;
terrainVertices[x+y*width].z=y;
terrainVertices[x+y*width].u=(float)x/30.0f;
terrainVertices[x+y*width].v=(float)y/30.0f;
}
}
device->CreateVertexBuffer(sizeof(terrainVertices),0,CUSTOMFVF,D3DPOOL_MANAGED,&vertexBuffer,NULL);

VOID* pVoid;
vertexBuffer->Lock(0,0,(void**)&pVoid,0);

memcpy(pVoid,&terrainVertices.front(),terrainVertices.size() * sizeof(terrainVertices[0]));

vertexBuffer->Unlock();

//Indices

//short indices[(50-1)*(50-1)*6];
vector<short> indices((50-1)*(50-1)*6);

for (int x=0;x < width-1 ;x++)
{
for (int y=0; y < length-1 ;y++)
{
indices[(x+y*(width-1))*6+2] = x+y*width;
indices[(x+y*(width-1))*6+1] = (x+1)+y*width;
indices[(x+y*(width-1))*6] = (x+1)+(y+1)*width;

indices[(x+y*(width-1))*6+3] = (x+1)+(y+1)*width;
indices[(x+y*(width-1))*6+4] = x+y*width;
indices[(x+y*(width-1))*6+5] = x+(y+1)*width;

}
}
device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL);
VOID* pIndex;
indexBuffer->Lock(0, (width-1)*(length-1)*6*sizeof(short), (void**)&pIndex, 0);

memcpy(pIndex, &indices.front(), indices.size() * sizeof(indices[0]));
indexBuffer->Unlock();







I changed the following around



device->CreateVertexBuffer(sizeof(terrainVertices),0,CUSTOMFVF,D3DPOOL_MANAGED,&vertexBuffer,NULL);

VOID* pVoid;
vertexBuffer->Lock(0,0,(void**)&pVoid,0);

memcpy(pVoid,&terrainVertices.front(),terrainVertices.size() * sizeof(terrainVertices[0]));

Help me once more? Thank you!:)

Share this post


Link to post
Share on other sites
Guest
oh yeah by the way i get System.NullReferenceException for my index buffer now for a wierd reason:S

Share this post


Link to post
Share on other sites
sizeof(terrainVertices) is the size of the class std::vector<CUSTOMVERTEX>. You want sizeof(CUSTOMVERTEX)*terrainVertices.size(). Just like you do in the memcpy actually.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!