Results 1 to 11 of 11

Thread: Terrain

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Terrain

    I have a greyscale heightmap *.bmp file and I want to be able to display it in my 3d window.

    I know how to set up a scene and use meshes but i have never tried to do a terrain before.

    What are the steps I need to take? What classes do i need to use?
    I don't live here any more.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Terrain

    First of read it into a double or single array, in the latter on you have to remember the width on your own.

    Then traverse through it one and set a width of lets say 4.0f. Then use that for all changes in the X, and Z coordinat and use the Y coodinat from the BMP file as the vertices coordinates. Do this back and forth, not starting from the same end everytime. That can cause you problems when you are starting to texture the terrain.

    When it comes to texturing you have several options. You can stretch one tecture all over the whole thing. Or you can use one texture for every quad (two triangles) or you can use more then one texture, and blend them togheter, giving each area on the terrain a difrent texture, so you can see gravel, grass, snow, and so on.

    Did that answer your vague question? If not, aske again.

  3. #3
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Terrain

    I know you explained to me before this whole left right, right left...
    I think that just confuse's people as it is not necessary.

    On a 128x128 bitmap.
    I gather vertices for each point going from left to right, down 1 row, left to right, and so on.

    Then I create the indices going from left to right as well, I will show you the snippet and you will notice that textures coords are being calculated correctly.

    As you can see it is currently used for a QuadTree but the function was originally used for loading the entire heightmap. Flawlessly.

    PHP Code:
    //--------------------------
    //Fill Vertices of a certain node -- used for leaves
    //Can be easily modified to work as one function for a whole heightmap
    //--------------------------
    void QuadTree::FillVertices(int iNodeIdVector2D *BoundingCoords)
    {
        
    Vector2D vSize BoundingCoords[3] - BoundingCoords[0];

        
    m_pNodes[iNodeId].iNumVertices = (int)((vSize.x+1) * (vSize.y+1));
        
    m_pNodes[iNodeId].Vertices = new D3DVERTEX[m_pNodes[iNodeId].iNumVertices];
        
    memset(m_pNodes[iNodeId].Vertices0m_pNodes[iNodeId].iNumVertices);

        
    m_pNodes[iNodeId].iNumIndices = (int)(((vSize.x) * (vSize.y))*6);
        
    m_pNodes[iNodeId].Indices = new unsigned short[m_pNodes[iNodeId].iNumIndices];
        
    memset(m_pNodes[iNodeId].Indices0m_pNodes[iNodeId].iNumIndices);

        
    //VertexJump defines the pitch at which you jump through the array 'rows'
        
    int iVertexJump = (int)(vSize.x+1.0f);
        
    int CurrVertex=0;
        
    int Index 0;

        
    //Fill the vertices
        
    for (int y=0<= vSize.yy++)
        {
            for (
    int x=0<= vSize.xx++)
            {
                
    CurrVertex y*iVertexJump x;
                
    Index = (int)(((BoundingCoords[0].y) * (m_vMapSize.1.0f)) + (BoundingCoords[0].x));

                
    AddVertice(iNodeId,
                           
    CurrVertex,
                           (float)(
    x+BoundingCoords[0].x) * m_iScaleX,
                           
    m_fHeightTable[Index],
                           (float)(
    y+BoundingCoords[0].y) * m_iScaleX,
                           
    m_fTileU * ((float)(x+BoundingCoords[0].x) * m_fTexDelta 0.5f),
                           
    m_fTileV * (1.0f - (float)(y+BoundingCoords[0].y) * m_fTexDelta 0.5f)
                          );
            }
        }

        
    //Fill the indices
        
    int idx=0;
        for (
    y=0vSize.yy++)
        {
            for (
    int x=0vSize.xx++)
            {
                
    CurrVertex y*(iVertexJump)+x;

                
    m_pNodes[iNodeId].Indices[idx++] = CurrVertex+(iVertexJump);
                
    m_pNodes[iNodeId].Indices[idx++] = CurrVertex+1;
                
    m_pNodes[iNodeId].Indices[idx++] = CurrVertex;
                
                
    m_pNodes[iNodeId].Indices[idx++] = CurrVertex+1;
                
    m_pNodes[iNodeId].Indices[idx++] = CurrVertex+(iVertexJump);
                
    m_pNodes[iNodeId].Indices[idx++] = CurrVertex+(iVertexJump)+1;
            }
        }


    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  4. #4

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Terrain

    Halsafar said that using a mesh would be slow because of frustrum culling. Is there a class specifically for showing a terrain?

    I don't know how to organise the height values in a way that they can be drawn. i have tried using indexed Trianglelists but I can't get it working (haven't got any code at the moment).

    What class is traditionally used to contain a terrain??? I'm sure i'm going about this all wrong.

    *cries*

    I don't live here any more.

  5. #5

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Terrain

    ok Just this second read halsafar's thread

    Isn't there a built-in function for all this? this is a pain in the arse.
    I don't live here any more.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Terrain

    3D programmin is a pain in the ass.


    And if you are not going back and forth as I said, then you have to end every horizontal line of the terrain, before you draw a new one, to not cause artifacts. If you do end it every time, then it will be slower then not ending it for every horizontal line.



    ØØ

  7. #7
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Terrain

    Ah, see this is where we are mis communicating.
    I use Indexed TriangleLists, not Strips.

    As for what class to use: Load a Bmp, Fill a 1D array full the pixel value (GetPixel) (tranverse through the 1D array as shown in my above func, since the 1D array represents a 2D bitmap), Now write a function like the one I posted to read in the vertice, each pixel requires a vertice (top-left corner) all the way from the beginning to the end of each row then column.

    The write a function to generate the indices -- now a note to remember -- a 128x128 bmp it is a good idea to make it 129x129 bitmap or (a root of 2, plus 1).

    Calculating tileU and tileV coords can be a rather complex process, and complications arise if you want to add cliff textures or different textures but NoteMe covered the idea on that above.

    Rendering is easy, render a TRIANGLELIST, or a LINELIST to see the terrain is beautiful wireframe. Use a plain indentity matrix with no rotations.
    My function above generates the triangles in Counter-Clockwise order so that works for D3D, but in OpenGL you will need to use CW.

    This method is beautiful as writing a function to aid frustum culling is a simple task -- the Quad Tree -- which is what you'll want to touch on once you get your entire heightmap rendered.

    Read these aricles (none are dx specific, but this topic is ranged for all libraries):
    http://www.qeradiant.com/manual/Terr...ight_maps.html
    http://nehe.gamedev.net/data/lessons....asp?lesson=34
    http://home.planet.nl/~monstrous/terrain.html
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Terrain

    Even if you are uses indexes I can't see how you manage to not get the artifacts if you are not:


    • Going back and fort when you are making the vertex buffer
    • Or going back and fort when you use the index buffer
    • Or stop at each end.


    Becasuse if you don't you will use the last vertices on the end of one horizontal line on the start of the next one, dragging a triangel all the way back to the start or the next line.


    BTW Wooossy, while looking for someting else (don't ask why I just woke up at this time of the night starting too look for soemthing), I triped over a new DX page I have never seen before. Covering your exact topic (guess it isn't in your prefered lanugage though, but they have a PDF too (even if I didn't look at it))

    http://www.spheregames.com/articles.asp


    Lots of paranteses thre..


    ØØ

  9. #9

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Terrain

    My latest success. I designed the rather ugly terrain in Gamespace Light.

    Attached Images Attached Images  
    I don't live here any more.

  10. #10
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Terrain

    Yes NoteMe, if you are using QUADS then that would be case. If you using a TRIANGLELIST then I em just specifing the triangles and I know it is being specifed properly, every 3 indices is a triangle. the function goes row by row.


    Impliment the fnction I posted.
    Last edited by Halsafar; Feb 19th, 2005 at 07:28 PM.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Terrain

    You don't have QUADS in DX.......And you are using Indecis. Read my posts.




    ØØ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width