My most amazing error yet! [complicated]

This is a discussion on My most amazing error yet! [complicated] within the C++ Programming forums, part of the General Programming Boards category; You are getting this code from the same book I have. You are destroying the memory. Plain and simple and ...

  1. #31
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    You are getting this code from the same book I have. You are destroying the memory. Plain and simple and this is your problem for the most part.

    My guess is that if you have destroyed the memory for the array, you have probably fragged it elsewhere too.

    Here is the values you will need:
    Code:
    ...
    BYTE *pTempMap=new BYTE[iWidth*iDepth];
    BYTE *pHeightMap=new WORD[iWidth*iDepth];
    
    int handle=_open("Heightmap.dat",_O_RDONLY);
    if (handle==-1)
    {
      ::MessageBox(0,TEXT("CTerrain::Create() - Failed to load heightmap",0,0);
      return;
    }
    DWORD dwSizeBytes=iWidth*iDepth*sizeof(WORD);
    
    _read(handle,pTempMap,dwSizeBytes);
    _close(handle);
    
    int iNumVertsPerCol=iWidth;
    int iNumVertsPerRow=iDepth;
    int iNumVerts=iWidth*iDepth;
    
    int iNumCellsPerCol=iNumVertsPerCol-1;
    int iNumCellsPerRow=iNumVertsPerRow-1;
    int iNumCells=iNumVertsPerCol*iNumVertsPerRow;
    int iNumTris=iNumCells*2;
    int iNumIndices=iNumTris*3;
    
    for (int i=0;i<iNumVerts;i++)
    {
      pHeightMap[i]=pTempMap[i]*iScale;
    }
    
    delete [] pTempMap;
    ...

    I have more source for the terrain but I have done far more than the book ever showed so it may confuse you. My version has a quadtree, separates the heightmap from the terrain class (they are both sep objects), uses a sky scattering shader, and employs billboards for trees on the terrain.

  2. #32
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    I said it before, but I'll say again.....

    you set numvertices to
    numVertices = numVertsPerRow * numVertsPerCol;

    then in readRawFile you do this:

    Code:
    std::vector<BYTE> in(numVertices);											
    std::ifstream inFile(fname, std::ios_base::binary);
    if(inFile == NULL) return false;
    inFile.read((char*)&in[0], in.size());
    So suppose you have 10 vertices. Then you make a vector 10 bytes in size and read 10 bytes from file. Is that correct??

  3. #33
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    The book sticks the vertices in a vector but there is no need. Once terrain is loaded into any system you won't be adding to it or removing it, you will simply cache in 'chunks' and cache out 'chunks'. These chunks will be quad tree partitioned areas of terrain either using a restrictive quad-tree or a simpler form of a quad tree. But to do one simple area of terrain you only need an array. I don't recommend specifically loading from a RAW file into a std::vector.

    Also you can store the height and image data in the same file if you use the alpha component of the DWORD to store the height value and the first 3 BYTES of the DWORD for the RGB components.

    Code:
    struct RGBH
    {
      BYTE uRed;
      BYTE uGreen;
      BYTE uBlue;
      BYTE uHeight;
    };
    To use negative heights just convert the uHeight into a signed byte or a char and then multiply by scale. So now you can load height map and color map in one array-based read.

    The sample in the book compiles as is and runs just fine. I would recommend going to www.moon-labs.com for more info if you need help.

  4. #34
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Quote Originally Posted by Bubba
    You are getting this code from the same book I have. You are destroying the memory. Plain and simple and this is your problem for the most part.
    I figured that it was that D: I just need to find where it's happening.

    Quote Originally Posted by Bubba
    Code:
    int handle=_open("Heightmap.dat",_O_RDONLY);
    I have seen _open before, is that a function that you wrote? Also, I'm assuming becuase of the way they used RAW's, there's no real difference between using a raw and a dat in this instance?

    I'm comparing the rest of my values to what you have now...they're a little different though.I think that you're using width and depth differently...

    Quote Originally Posted by Laserve
    I said it before, but I'll say again.....

    you set numvertices to
    numVertices = numVertsPerRow * numVertsPerCol;

    then in readRawFile you do this:


    Code:
    std::vector<BYTE> in(numVertices);											
    std::ifstream inFile(fname, std::ios_base::binary);
    if(inFile == NULL) return false;
    inFile.read((char*)&in[0], in.size());
    So suppose you have 10 vertices. Then you make a vector 10 bytes in size and read 10 bytes from file. Is that correct??
    Yes, the vertices are computed using these BYTE's later.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PGO is amazing
    By cyberfish in forum Tech Board
    Replies: 8
    Last Post: 02-12-2009, 11:30 PM
  2. When done right, PC games are amazing
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2008, 05:32 PM
  3. Amazing Pool Tricks
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-07-2005, 07:50 PM
  4. Hiking in the Rockies..Amazing!
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-11-2005, 09:36 AM
  5. It's amazing...
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-18-2002, 01:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21