Jump to content

View more

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Heightmap finding closest vertices

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
2 replies to this topic

#1 ChobitsTheZero   Members   

117
Like
0Likes
Like

Posted 18 February 2017 - 09:53 AM

So I have rendered a terrain based on a heightmap. It's based on this tutorial that I followed - http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=8
 
Now I want to have my camera to be able to "walk" on the terrain. The getY() function gets the x and z coordinates of the camera, and I understand that with those I have to find the closest vertices of the terrain to decide which triangle the camera is over. The thing is i'm unsure of how to actually do that, I have looked for a lot of examples on google but haven't been able to find any concrete answers for it. Currently my code looks like this:

HM_SIZE_X and HM_SIZE_Y are both 6
 
    float Terrain::getY(int x, int z)
    {
    
    //return
    }
    
    void Terrain::createTerrain()
    {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    
    // Setup heightmap
    
    glGenVertexArrays(1, &uiVAOHeightmap); // Create one VAO
    glGenBuffers(1, &uiVBOHeightmapData); // One VBO for data
    glGenBuffers(1, &uiVBOIndices); // And finally one VBO for indices
    
    glBindVertexArray(uiVAOHeightmap);
    glBindBuffer(GL_ARRAY_BUFFER, uiVBOHeightmapData);
    
    float fHeights[HM_SIZE_X*HM_SIZE_Y] =
    {
    10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,
    5.0f, 5.0f,5.0f, 10.0f, 10.0f, 10.0f,
    5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f,
    5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 10.0f,
    10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,
    10.0f, 5.0f, 5.0f, 5.0f, 5.0f, 10.0f,
    };
    
    float fSizeX = 100.0f, fSizeZ = 100.0f;
    
    for (int i = 0; i<HM_SIZE_X*HM_SIZE_Y;i++)
    {
    for (int j = 0; j < HM_SIZE_X*HM_SIZE_Y; j++)
    {
    float column = float(i%HM_SIZE_X), row = float(i / HM_SIZE_X);
    vHeightmapData[i] = glm::vec3(
    -fSizeX / 2 + fSizeX*column / float(HM_SIZE_X - 1), // X Coordinate
    fHeights[i], // Y Coordinate (it's height)
    -fSizeZ / 2 + fSizeZ*row / float(HM_SIZE_Y - 1) // Z Coordinate
    );
    }
    }
    
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)*HM_SIZE_X*HM_SIZE_Y, vHeightmapData, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    
    glGenBuffers(1, &uiVBOIndices);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiVBOIndices);
    int iIndices[] =
    {
    0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 36,
    6, 12, 7, 13, 8, 14, 9, 15, 10, 16, 11, 17, 36,
    12, 18, 13, 19, 14, 20, 15, 21, 16, 22, 17, 23, 36,
    18, 24, 19, 25, 20, 26, 21, 27, 22, 28, 23, 29, 36,
    24, 30, 25, 31, 26, 32, 27, 33, 28, 34, 29, 35
    };
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(iIndices), iIndices, GL_STATIC_DRAW);
    glEnable(GL_PRIMITIVE_RESTART);
    glPrimitiveRestartIndex(HM_SIZE_X*HM_SIZE_Y);
    }


#2 Norman Barrows   Members   

7149
Like
1Likes
Like

Posted 18 February 2017 - 02:44 PM

You know where you vertices are.

 

You know the size of a quad.

 

You know where the camera is.

 

From there you can determine which quad the camera is in.   ( camera x - mesh x ) / quad size = quad x.   similar for z.

 

and from there you can determine which triangle of the quad the camera is in.

 

from that you can lerp down the two sides of the triangle, then lerp between those results to get the height of the triangle at the camera's location.


Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

 

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

 

 


#3 ChobitsTheZero   Members   

117
Like
0Likes
Like

Posted 20 February 2017 - 11:45 AM

You know where you vertices are.

 

You know the size of a quad.

 

You know where the camera is.

 

From there you can determine which quad the camera is in.   ( camera x - mesh x ) / quad size = quad x.   similar for z.

 

and from there you can determine which triangle of the quad the camera is in.

 

from that you can lerp down the two sides of the triangle, then lerp between those results to get the height of the triangle at the camera's location.

 

How would I determine which triangle of the quad the camera is in?






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.