c++graphicsheightmap

Changing a heightmap so it show 16 hieghtmaps on a 4x4 grid


Change the program to display 16 identical heightmaps arranged in a 4 x 4 grid. The edges of the heightmaps should be side by side in X and Z coordinates. However, they will not touch in the Y direction because the heights will be different.

C++

The below code is what I have already, i am just not too sure how to make it show the 16 identical heightmaps arranged in a 4x4 grid. I know it has to do with the squares on the height map, but i am very confused.

 const int HEIGHTMAP_SIZE = 12;
 float heights[HEIGHTMAP_SIZE + 1][HEIGHTMAP_SIZE + 1];  

initDisplay();
    for (unsigned int x = 0; x <= HEIGHTMAP_SIZE; x++)
    {
        for (unsigned int z = 0; z <= HEIGHTMAP_SIZE; z++)
        {
            heights[x][z] = (x % 2) * 0.5f -
            z * z * 0.05f;
        }
    }
    //TextureManager::activate("rainbow.bmp");
    initHeightmapDisplayList();

 void initHeightmapHeights()
 {
    for (unsigned int x0 = 0; x0 < HEIGHTMAP_SIZE; x0++)
    {
        unsigned int x1 = x0 + 1;
        float tex_x0 = (float)(x0) / HEIGHTMAP_SIZE;
        float tex_x1 = (float)(x1) / HEIGHTMAP_SIZE;
        glBegin(GL_TRIANGLE_STRIP);
        for (unsigned int z = 0; z <= HEIGHTMAP_SIZE; z++)
        {
            float tex_z = (float)(z) / HEIGHTMAP_SIZE;
            glTexCoord2d(tex_x1, tex_z);
            glVertex3d(x1, heights[x1][z], z);
            glTexCoord2d(tex_x0, tex_z);
            glVertex3d(x0, heights[x0][z], z);
        }
        glEnd();
    }
 }  

 void initHeightmapDisplayList()
 {
    heightmap_list.begin();
    glEnable(GL_TEXTURE_2D);

    TextureManager::activate("ground.bmp");
    glColor3d(1.0, 1.0, 1.0);
    initHeightmapHeights();
    glDisable(GL_TEXTURE_2D);
    heightmap_list.end();
 }

Solution

  • I suspect that your TextureManager already has a way of doing this without direct calls to OpenGL. What you should do is make the texture repeat itself.

    glTexParamteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    

    By then scaling your texture coordinates up by a factor of 4, you would obtain a 4x4 grid of the original texture.