Sign in to follow this  
shavitpriens

object flickering

Recommended Posts

shavitpriens    128

when i use this update code my box flickers and sometimes doesn't appear on the screen.

 

 
void Box::update(float dt)
{
 
D3DXVECTOR3 displacement = Velocity * dt;
 
if(Velocity != D3DXVECTOR3(0,0,0))
{
 
Vertex* v = 0;
mVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v );
 
for(int i = 0;i < mNumVertices;i++)
v[i].pos +=displacement;
 
mVB->Unmap();
}
}
 
 
I need help I don't know what to do, I tried to use D3D10Device::CopyResource but it didn't work it crushed the program ! 

Share this post


Link to post
Share on other sites
MrCodeSushi    1066

If you don't move the box, does it still flicker? Having constantly sending in new vertices in your dynamic vb may have some performance implications but i don't think the problem is in your update code

Edited by BrentChua

Share this post


Link to post
Share on other sites
shavitpriens    128

If you don't move the box, does it still flicker? Having constantly sending in new vertices in your dynamic vb may have some performance implications but i don't think the problem is in your update codI 

I have no flickers when the box doesn't move

Edited by shavitpriens

Share this post


Link to post
Share on other sites
mdias    823

D3D10_MAP_WRITE_DISCARD

 

D3D10_MAP_WRITE_DISCARD

Resource is mapped for writing; the previous contents of the resource will be undefined. The resource must have been created with write access (see D3D10_CPU_ACCESS_WRITE).

 

You either want to use D3D10_MAP_READ_WRITE (remember that reading from GPU memory is SLOW) or re-generate the vertices from scratch.

Edited by mdias

Share this post


Link to post
Share on other sites
MrCodeSushi    1066


D3D10_MAP_WRITE_DISCARD

 


D3D10_MAP_WRITE_DISCARD

Resource is mapped for writing; the previous contents of the resource will be undefined. The resource must have been created with write access (see D3D10_CPU_ACCESS_WRITE).

 

You either want to use D3D10_MAP_READ_WRITE (remember that reading from GPU memory is SLOW) or re-generate the vertices from scratch.

Oh wow totally missed that. mdias is right. The data in your Vertex* v is undefined. Your code v[i].pos+displacement adds a value to v[i] which is undefined. You should have the position value, along with the rest of the data in your vertex buffer, saved in system memory then use those values to assign it to v[].

 

So it should look something like this:

// This is stored somewhere in system memory
VertexData m_vertices;

Vertex* v = 0;
mVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v );
 
for(int i = 0;i < mNumVertices;i++)
{
   // Update position
   m_vertices[i].pos += displacement;

   // Replace new data
   v[i].pos = m_vertices[i].pos;
   // Also replace the rest of the attributes as well.. (color, normal, texCoord, etc...)
}

The reason why its probably flickering is that the api will give you a system memory with the values you initially assigned to the vb in one frame then the next frame the api will give you a totally different system memory with garbage values (resulting in a position with crazy values off the screen).

 

Thanks for the heads up mdias!

Edited by BrentChua

Share this post


Link to post
Share on other sites
shavitpriens    128

D3D10_MAP_WRITE_DISCARD

 

D3D10_MAP_WRITE_DISCARD

Resource is mapped for writing; the previous contents of the resource will be undefined. The resource must have been created with write access (see D3D10_CPU_ACCESS_WRITE).

 

You either want to use D3D10_MAP_READ_WRITE (remember that reading from GPU memory is SLOW) or re-generate the vertices from scratch.

 

Do I need to keep the vertices data in an array and then recreate a vertex buffer?

Share this post


Link to post
Share on other sites
shavitpriens    128

 



D3D10_MAP_WRITE_DISCARD

 


D3D10_MAP_WRITE_DISCARD

Resource is mapped for writing; the previous contents of the resource will be undefined. The resource must have been created with write access (see D3D10_CPU_ACCESS_WRITE).

 

You either want to use D3D10_MAP_READ_WRITE (remember that reading from GPU memory is SLOW) or re-generate the vertices from scratch.

Oh wow totally missed that. mdias is right. The data in your Vertex* v is undefined. Your code v[i].pos+displacement adds a value to v[i] which is undefined. You should have the position value, along with the rest of the data in your vertex buffer, saved in system memory then use those values to assign it to v[].

 

So it should look something like this:

// This is stored somewhere in system memory
VertexData m_vertices;

Vertex* v = 0;
mVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v );
 
for(int i = 0;i < mNumVertices;i++)
{
   // Update position
   m_vertices[i].pos += displacement;

   // Replace new data
   v[i].pos = m_vertices[i].pos;
   // Also replace the rest of the attributes as well.. (color, normal, texCoord, etc...)
}

The reason why its probably flickering is that the api will give you a system memory with the values you initially assigned to the vb in one frame then the next frame the api will give you a totally different system memory with garbage values (resulting in a position with crazy values off the screen).

 

Thanks for the heads up mdias!

 

if the data is undefined so why does the box still moving when i press a key ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this