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

  • SEARCH
  • Home
  • 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 7799035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:10:42+00:00 2026-06-02T00:10:42+00:00

I am currently making a method to load in a noisy heightmap, but lack

  • 0

I am currently making a method to load in a noisy heightmap, but lack the triangles to do so. I want to make an algorithm that will take an image, its width and height and construct a terrain node out of it.

Here’s what I have so far, in somewhat pseudo

Vertex* vertices = new Vertices[image.width * image.height];
Index* indices; // How do I judge how many indices I will have?
float scaleX = 1 / image.width;
float scaleY = 1 / image.height;
float currentYScale = 0;

for(int y = 0; y < image.height; ++y) {
    float currentXScale = 0;
    for (int x = 0; x < image.width; ++x) {
       Vertex* v = vertices[x * y];
       v.x = currentXScale;
       v.y = currentYScale;
       v.z = image[x,y]; 
       currentXScale += scaleX;
    }
    currentYScale += scaleY;
}

This works well enough to my needs, my only problem is this: How would I calculate the # of indices and their positions for drawing the triangles? I have somewhat familiarity with indices, but not how to programmatically calculate them, I can only do that statically.

  • 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-02T00:10:44+00:00Added an answer on June 2, 2026 at 12:10 am

    As far as your code above goes, using vertices[x * y] isn’t right – if you use that, then e.g. vert(2,3) == vert(3,2). What you want is something like vertices[y * image.width + x], but you can do it more efficiently by incrementing a counter (see below).

    Here’s the equivalent code I use. It’s in C# unfortunately, but hopefully it should illustrate the point:

    /// <summary>
    /// Constructs the vertex and index buffers for the terrain (for use when rendering the terrain).
    /// </summary>
    private void ConstructBuffers()
    {
        int heightmapHeight = Heightmap.GetLength(0);
        int heightmapWidth = Heightmap.GetLength(1);
        int gridHeight = heightmapHeight - 1;
        int gridWidth = heightmapWidth - 1;
    
        // Construct the individual vertices for the terrain.
        var vertices = new VertexPositionTexture[heightmapHeight * heightmapWidth];
    
        int vertIndex = 0;
        for(int y = 0; y < heightmapHeight; ++y)
        {
            for(int x = 0; x < heightmapWidth; ++x)
            {
                var position = new Vector3(x, y, Heightmap[y,x]);
                var texCoords = new Vector2(x * 2f / heightmapWidth, y * 2f / heightmapHeight);
                vertices[vertIndex++] = new VertexPositionTexture(position, texCoords);
            }
        }
    
        // Create the vertex buffer and fill it with the constructed vertices.
        this.VertexBuffer = new VertexBuffer(Renderer.GraphicsDevice, typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
        this.VertexBuffer.SetData(vertices);
    
        // Construct the index array.
        var indices = new short[gridHeight * gridWidth * 6];    // 2 triangles per grid square x 3 vertices per triangle
    
        int indicesIndex = 0;
        for(int y = 0; y < gridHeight; ++y)
        {
            for(int x = 0; x < gridWidth; ++x)
            {
                int start = y * heightmapWidth + x;
                indices[indicesIndex++] = (short)start;
                indices[indicesIndex++] = (short)(start + 1);
                indices[indicesIndex++] = (short)(start + heightmapWidth);
                indices[indicesIndex++] = (short)(start + 1);
                indices[indicesIndex++] = (short)(start + 1 + heightmapWidth);
                indices[indicesIndex++] = (short)(start + heightmapWidth);
            }
        }
    
        // Create the index buffer.
        this.IndexBuffer = new IndexBuffer(Renderer.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);
        this.IndexBuffer.SetData(indices);
    }
    

    I guess the key point is that given a heightmap of size heightmapHeight * heightmapWidth, you need (heightmapHeight - 1) * (heightmapWidth - 1) * 6 indices, since you’re drawing:

    • 2 triangles per grid square
    • 3 vertices per triangle
    • (heightmapHeight - 1) * (heightmapWidth - 1) grid squares in your terrain.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently making some system that will gather statistical reports from different sites, for
I'm making a method that load a graph from a file. It's very simple,
I'm currently making an iPhone app that has PDF viewing a crucial part of
I'm currently making a PHP-program that solves equations. I've divided the input equation into
currently making a SOAP request using Java's SOAPConnectionFactory and SOAPConnection 's .call() method, which
I am currently making a fullscreen game and I want to be able to
I'm making a small language that is very similar to hlsl but supports only
I am currently making a time-clock system that is inside of a custom-built application
I'm pretty new around WP/Visual C# and I'm currently making a project that involves
I am currently creating a sorting method that consists of values from an mysql

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.