Sign in to follow this  
Kris2456

Random decleration problem

Recommended Posts

I have no idea why this is happening. What happens is, when i declare CVector3 PolyMap[1023*1023]; It makes the whole program have a segmentation fault. Even when i commented out everything in the function. It still gives me an error. Here is the code for the CalculateNormalMap() function:
void CIrisTerrain::CalculateNormalMap()
{
    int X, Y;
    int x,y,z;
    CVector3 normal,v1,v2,v3,v4;
    CVector3 pos[3];
    CVector3 PolyMap[1023*1023];
    //BLOODY THING WONT LET ME DO BIGGER THAN 416, BUGGER!
    //OK, NOW IT DOSNT WORK AT ALL!
    
    
    //Find values for polymap
    for(X=0;X<=1023;X++)
    {
        for(Y=0;Y<=1023;Y++)
        {
            //Vertex [0]
            x=X;
            z=Y;
            y=Height(X,Y); 
            pos[0].x=x;
            pos[0].y=y;
            pos[0].z=z;
            
            //Vertex [1]
            x=X;
            z=Y+1;
            y=Height(X,Y+1); 
            pos[1].x=x;
            pos[1].y=y;
            pos[1].z=z;
            
            //Vertex [2]
            x=X+1;
            z=Y;
            y=Height(X+1,Y); 
            pos[2].x=x;
            pos[2].y=y;
            pos[2].z=z;
            
            PolyMap[X+(Y*1023)] = Normal(pos);
        }
    }
    
    //Find values for NormalMap based on polyMap
    for(X=1;X<=1024;X++)
    {
        for(Y=1;Y<=1024;Y++)
        {
            v1.x = PolyMap[(X-1)+((Y-1)*1023)].x;
            v1.y = PolyMap[(X-1)+((Y-1)*1023)].y;
            v1.z = PolyMap[(X-1)+((Y-1)*1023)].z;
            
            v2.x = PolyMap[(X)+((Y-1)*1023)].x;
            v2.y = PolyMap[(X)+((Y-1)*1023)].y;
            v2.z = PolyMap[(X)+((Y-1)*1023)].z;

            v3.x = PolyMap[(X)+((Y)*1023)].x;
            v3.y = PolyMap[(X)+((Y)*1023)].y;
            v3.z = PolyMap[(X)+((Y)*1023)].z;
            
            v4.x = PolyMap[(X-1)+((Y)*1023)].x;
            v4.y = PolyMap[(X-1)+((Y)*1023)].y;
            v4.z = PolyMap[(X-1)+((Y)*1023)].z;
            
            normal = v1+v2+v3+v4;
            normal = Normalize(normal);
            NormalMap[X+(Y*1024)] = normal;
            
        }
    } 
}


It gives me no errors compiling.
Help!


Share this post


Link to post
Share on other sites
Ok, iadded that in. But im afraid it still refuses to work. Here is the new code:

void CIrisTerrain::CalculateNormalMap()
{
int X, Y;
int x,y,z;
CVector3 normal,v1,v2,v3,v4;
CVector3 pos[3];
CVector3* PolyMap = NULL;
PolyMap = new CVector3[1023*1023];
//DEBBUGGER JUST FREEZES ON THIS BIT. IT JUST SAYS THAT IT HAS A VALUE OF 0x00

//Find values for polymap
for(X=0;X<=1023;X++)
{
for(Y=0;Y<=1023;Y++)
{
//Vertex [0]
x=X;
z=Y;
y=Height(X,Y);
pos[0].x=x;
pos[0].y=y;
pos[0].z=z;

//Vertex [1]
x=X;
z=Y+1;
y=Height(X,Y+1);
pos[1].x=x;
pos[1].y=y;
pos[1].z=z;

//Vertex [2]
x=X+1;
z=Y;
y=Height(X+1,Y);
pos[2].x=x;
pos[2].y=y;
pos[2].z=z;

PolyMap[X+(Y*1023)] = Normal(pos);
}
}

//Find values for NormalMap based on polyMap
for(X=1;X<=1024;X++)
{
for(Y=1;Y<=1024;Y++)
{
v1 = PolyMap[(X-1)+((Y-1)*1023)];
//v1.y = PolyMap[(X-1)+((Y-1)*1023)]->y;
//v1.z = PolyMap[(X-1)+((Y-1)*1023)]->z;

v2 = PolyMap[(X)+((Y-1)*1023)];
//v2.y = PolyMap[(X)+((Y-1)*1023)]->y;
// v2.z = PolyMap[(X)+((Y-1)*1023)]->z;

v3 = PolyMap[(X)+((Y)*1023)];
// v3.y = PolyMap[(X)+((Y)*1023)]->y;
// v3.z = PolyMap[(X)+((Y)*1023)]->z;

v4 = PolyMap[(X-1)+((Y)*1023)];
// v4.y = PolyMap[(X-1)+((Y)*1023)]->y;
// v4.z = PolyMap[(X-1)+((Y)*1023)]->z;

normal = v1+v2+v3+v4;
normal = Normalize(normal);
NormalMap[X+(Y*1024)] = normal;

}
}

delete[] PolyMap;
}


I think its just not declaring it right. plz help!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Kris2456
...
CVector3 PolyMap[1023*1023];
...
for(X=0;X<=1023;X++)
for(Y=0;Y<=1023;Y++)
...
PolyMap[X+(Y*1023)] = Normal(pos);
...


Your map is 1023x1023, so X goes from 0 to 1022 and Y too.

But you let X go from 0 to 1023 (and Y too) and then access PolyMap[1023+1023*1023] which is not part of the array.

Share this post


Link to post
Share on other sites
Thx for that, but unfortunatly, even with the loops commented. The thing still tells me its wrong. As i said, the debugger just stops going to the next step when it says:
PolyMap = new CVector3[1023*1023];

Share this post


Link to post
Share on other sites
Whats also strange is, that when i try and acces elements of the array, it tells me this:

41 C:\Documents and Settings\Kris2456\My Documents\Programming\Engine\Terrain.cpp
base operand of `->' has non-pointer type `CVector3'

I got this from typing in: PolyMap[0]->x;

This is really getting on my nerves

Share this post


Link to post
Share on other sites
Quote:
Original post by Kris2456
Whats also strange is, that when i try and acces elements of the array, it tells me this:

41 C:\Documents and Settings\Kris2456\My Documents\Programming\Engine\Terrain.cpp
base operand of `->' has non-pointer type `CVector3'

I got this from typing in: PolyMap[0]->x;

This is really getting on my nerves


PolyMap is a pointer to an array of CVector3's. When you index the array you aren't getting a pointer to that element, you're accessing that element itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeMJH
PolyMap is a pointer to an array of CVector3's. When you index the array you aren't getting a pointer to that element, you're accessing that element itself.


Sorry, i didnt quite understand you there. If you could explain it more slowly. Heres a def of CVector3 if it helps:

typedef struct CVector3
{
CVector3(){}

CVector3(float X, float Y, float Z)
{
x=X;y=Y;z=Z;
}

//Operators
CVector3 operator+(CVector3 vector)
{
return CVector3(vector.x+x, vector.y+y, vector.z+z);
}

CVector3 operator-(CVector3 vector)
{
return CVector3(x - vector.x, y - vector.y, z - vector.z);
}

CVector3 operator*(float num)
{
return CVector3(x*num, y*num, z*num);
}

CVector3 operator/(float num)
{
return CVector3(x/num, y/num, z/num);
}

float x, y, z;
};

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