Sign in to follow this  
HemoGloben

Pointer members...Dynamic...

Recommended Posts

I was working on a loose Terrain class, quick rough draft, when I ran into a problem.
class CTerrain
{
    public:
        
        
        CTerrain()
        {
        }
        ~CTerrain();
        
        bool GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength);
        bool drawTerrain();
    private:
        float * terrainMap[][];
        int width;
        int length;  
};
CTerrain::~CTerrain()
{
    delete terrainMap;
}    
bool CTerrain::GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength)
{
        width=mapWidth;
        length=mapLength;
        
        terrainMap = new float array [width][height];
        
         for(int x=width;x>=0;x--)
         {
            for(int z=length;z>=0;z--)
            {
                terrainMap [x][z] =(float)(
                    ((float)heightMap[((x)*width*3)+(z*3)]/255.0f)+
                    ((float)heightMap[((x)*width*3)+(z*3)+1]/255.0f)+
                    ((float)heightMap[((x)*width*3)+(z*3)+2]/255.0f));
            }
        }  
        return true;
}    

bool CTerrain::drawTerrain()
{
    for(int x=width;x>=0;x--)
    {
        glBegin(GL_LINE_STRIP);
        for(int z=length;z>=0;z--)
        {
            glVertex3f( (float)x, terrainMap[x][z], (float)z);
            glVertex3f( (float)(x+1), terrainMap[x+1][z], (float)z);
        }
        glEnd;
    }
    return true;
}
I need away to dynamically allocate the pointer(and it's fine on the heap, not really gonna go over 256 at the moment size wise), but obviously what I've done isn't legal. Good way around this? Thanks.

Share this post


Link to post
Share on other sites
class CTerrain
{
public:


CTerrain()
{
terrainMap = 0;
width = 0;
height = 0;
}
~CTerrain();

bool GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength);
bool drawTerrain();
private:
float** terrainMap;
int width;
int length;
void Destroy();
};
void CTerrain::Destroy()
{
if(terrainMap)
{
for(int x=0;x<width)
{
delete[] terrainMap[x];
}
delete terrainMap;
terrainMap = 0;
}
width = 0;
height = 0;
}
CTerrain::~CTerrain()
{
Destroy();
}
bool CTerrain::GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength)
{
Destroy();
width=mapWidth;
length=mapLength;

terrainMap = new float*[width];

for(int x=width;x>=0;x--)
{
terrainMap[x] = new float[height];
for(int z=length;z>=0;z--)
{
terrainMap [x][z] =(float)(
((float)heightMap[((x)*width*3)+(z*3)]/255.0f)+
((float)heightMap[((x)*width*3)+(z*3)+1]/255.0f)+
((float)heightMap[((x)*width*3)+(z*3)+2]/255.0f));
}
}
return true;
}

bool CTerrain::drawTerrain()
{
for(int x=width;x>=0;x--)
{
glBegin(GL_LINE_STRIP);
for(int z=length;z>=0;z--)
{
glVertex3f( (float)x, terrainMap[x][z], (float)z);
glVertex3f( (float)(x+1), terrainMap[x+1][z], (float)z);
}
glEnd;
}
return true;
}


Share this post


Link to post
Share on other sites
Thanks, never would've thought of that. (Probably because I'd never seen it).

So, it creates a...pointer to an array of what?

terrainMap*[width];

That confuses me.

Hmm, I'll have to research that.

Thanks again though.

Share this post


Link to post
Share on other sites
Quote:
Original post by HemoGloben
Thanks, never would've thought of that. (Probably because I'd never seen it).

So, it creates a...pointer to an array of what?

terrainMap*[width];

That confuses me.

Hmm, I'll have to research that.

Thanks again though.

terrainMap = new float*[width];
Creates a new array of width float*s. So if width is 4, you get an array of 4 float pointers. Then you initialise each pointer by setting it to point to an array of floats. So you have an array of pointers to floats. And each pointer points to an array fo floats.

Share this post


Link to post
Share on other sites
Hmm. I missed a few of those lines when I first read through the code. I had rtouble implementing it last night. Finally got it to compile, and it crashed the second I opened it. Maybe I'll post the entire code later(on school PC's at the moment).

Thanks for the help though, and I'll probably conver to container classes a little later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this