Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:38:19+00:00 2026-06-06T13:38:19+00:00

I have a terrain mesh where just the Z value for each vertex needs

  • 0

I have a terrain mesh where just the Z value for each vertex needs to be updated every frame. My current method looks like this:

int stepping = CustomVertex.PositionNormalTextured.StrideSize / 4;

//ZPtr points to the Z value of the first PositionNormalTextured in the mesh.  
//This way we don't have to dereference ->Z for each vertex.
float* ZPtr = &(((CustomVertex.PositionNormalTextured*)
    TerrainMesh.LockVertexBuffer(LockFlags.NoOverwrite).InternalDataPointer)->Z);

float* DPtr = TerrainHeight; //point to begin scanning result
float* EndPtr = DPtr + TerrainMesh.NumberVertices; //point to stop scanning result

do { *ZPtr = *DPtr; ZPtr += stepping; } while (++DPtr < EndPtr); //copy data
TerrainMesh.UnlockVertexBuffer(); //unlock

Here, TerrainHeight is a float array created with Marshal.AllocHGlobal representing terrain height. Basically it scans across the entire TerrainHeight array and copies each value to the Z value of the respective PositionNormalTextured in the mesh. I used LockFlags.NoOverwrite to avoid creating a new copy of the array, although that doesn’t seem any faster than LockFlags.Discard.

It takes as long or longer to update the mesh than it does to compute the new terrain in the CPU, which leads me to believe there should be a faster way. I’ve been having trouble finding info on Google about this. Is there a better way to update the vertexbuffer? In case it matters, the size of the mesh is user set and may include over a million vertices (this is accomplished with multiple meshes), but the default settings is 32k verts which is the max for a single D3D mesh.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:38:22+00:00Added an answer on June 6, 2026 at 1:38 pm

    It appears you don’t understand the ramifications of the Discard and NoOverwrite flags. Have a read of the section Using Dynamic Vertex and Index Buffers under Performance Optimizations in the DirectX SDK help. Assuming you’re using a dynamic vertex buffer, then Discard means “I’m replacing the whole buffer” and NoOverwrite means “I’m writing to an unused portion of the buffer and I promise not to change any part I’ve already used”.

    With either flag, you’d have to write every component of your terrain vertices, even the ones that haven’t changed in your new frame.

    If you don’t use a dynamic vertex buffer, then you might experience a stall when you attempt to lock the vertex buffer for the next frame, if your GPU is still using it. In that case you’ll need to use multiple vertex buffers, and lock, update, unlock and render with a different buffer each frame that the terrain height changes. You will also have to initialize all of these buffers with all your terrain vertex data.

    I’d suggest separating the z values for your mesh into their own vertex buffer – assuming that you’re not updating x and y in position, your normal (which might be incorrect if z changes) or your texture coordinates. That way you can use your PositionNormalTextured (without position z) vertex buffer untouched every frame, and fill your dynamic z-position buffer each frame, from the beginning, using the Discard flag, without the stride to each Z value. Since the stride is gone, you can do that with a flat memory copy.

    You’ll supply your vertex shader with the z-position values with SetStreamSource( 1, ZPositionVB, ... ). You’ll need to adjust your vertex declaration to read the z-position values from stream 1, and your vertex shader to combine the z-position values before transformation.

    Apologies if some of this is a bad fit for C#.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a terrain map which i would like to represent some data in.
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have a terrain mesh stored in a VBO. The mesh is a grid
I have code like this that sets the map type to Terrain View: map
I have a terrain data which contains elevations as shown below And each file
In simple game, where map (terrain) is divided in grid cell I have soldier
have written this little class, which generates a UUID every time an object of
In my model I have Terrain.php /** * @ORM\Entity * @ORM\HasLifecycleCallbacks() */ class Terrain
I have coded a 3d-terrain engine with simplistic model engine on top of it.
I have a program which draws some terrain and simulates water flowing over it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.