I'm having some trouble with a function that creates/recreates my display list. This is called once at the beginning of the application and once every time a different terrain hex is selected. However, every time this function runs, the application eats up ~2400k more. Each time I run the function I delete the old display list and create a new one using these three lines:
Hi! What does [font=CourierNew, monospace][size=2]createNewMap()[/font][size=2][font="Arial"] do?[/font]
[size=2][font="Arial"]
[/font]
[size=2][font="Arial"]If it contains a "new" statement, remember to "delete" when you're done with the object.[/font]
[size=2][font="Arial"]I advice to avoid using new unless you absolutely have to, -there's typically an easier way.[/font]
Hi! What does [font="CourierNew, monospace"]createNewMap()[/font][font="Arial"] do?[/font]
[font="Arial"]If it contains a "new" statement, remember to "delete" when you're done with the object.[/font]
[font="Arial"]I advice to avoid using new unless you absolutely have to, -there's typically an easier way.[/font]
createNewMap is a boolean that is used to determine if I need to get new perlin noise values used for the heightmap. There is no 'new' statement used.
[quote name='SuperVGA' timestamp='1305552668' post='4811416']
Hi! What does [font="CourierNew, monospace"]createNewMap()[/font][font="Arial"] do?[/font]
[font="Arial"]If it contains a "new" statement, remember to "delete" when you're done with the object.[/font]
[font="Arial"]I advice to avoid using new unless you absolutely have to, -there's typically an easier way.[/font]
createNewMap is a boolean that is used to determine if I need to get new perlin noise values used for the heightmap. There is no 'new' statement used.
[/quote]
My bad, i meant [color=#660066][font=CourierNew, monospace][size=2]GetHeight()[/font][color=#660066][size=2][font="Arial"], Also what does [/font][color=#1C2837][size=2][color=#000000][font=CourierNew, monospace]hexMap[/font][color=#666600][font=CourierNew, monospace][[/font][color=#000000][font=CourierNew, monospace]x[/font][color=#666600][font=CourierNew, monospace]][[/font][color=#000000][font=CourierNew, monospace]y[/font][color=#666600][font=CourierNew, monospace]].[/font][color=#000000][font=CourierNew, monospace]drawRenderMode[/font][color=#666600][font="CourierNew, monospace"]()[/font][font="Arial"] do?[/font]
The posted code looks fine, the problem is where stuff is created and destroyed. No new-statements : good for you!
My bad, i meant [color="#660066"][font="CourierNew, monospace"]GetHeight()[/font][color="#660066"][font="Arial"], Also what does [/font][color="#1C2837"][color="#000000"][font="CourierNew, monospace"]hexMap[/font][color="#666600"][font="CourierNew, monospace"][[/font][color="#000000"][font="CourierNew, monospace"]x[/font][color="#666600"][font="CourierNew, monospace"]][[/font][color="#000000"][font="CourierNew, monospace"]y[/font][color="#666600"][font="CourierNew, monospace"]].[/font][color="#000000"][font="CourierNew, monospace"]drawRenderMode[/font][color="#666600"][font="CourierNew, monospace"]()[/font][font="Arial"] do?[/font]
The posted code looks fine, the problem is where stuff is created and destroyed. No new-statements : good for you!
[/quote]
PerlinNoise.cpp
double PerlinNoise::GetHeight(double x, double y) const
{
return amplitude * Total(x, y);
}
double PerlinNoise::Total(double i, double j) const
{
//properties of one octave (changing each loop)
double t = 0.0f;
double _amplitude = 1;
double freq = frequency;
for(int k = 0; k < octaves; k++)
{
t += GetValue(j * freq + randomseed, i * freq + randomseed) * _amplitude;
_amplitude *= persistence;
freq *= 2;
}
return t;
}
double PerlinNoise::GetValue(double x, double y) const
{
int Xint = (int)x;
int Yint = (int)y;
double Xfrac = x - Xint;
double Yfrac = y - Yint;
//find the noise values of the four corners
double x0y0 = 0.0625*(n01+n02+n03+n04) + 0.125*(n05+n06+n07+n08) + 0.25*(n09);
double x1y0 = 0.0625*(n07+n12+n08+n14) + 0.125*(n09+n16+n02+n04) + 0.25*(n06);
double x0y1 = 0.0625*(n05+n06+n23+n24) + 0.125*(n03+n04+n09+n28) + 0.25*(n08);
double x1y1 = 0.0625*(n09+n16+n28+n34) + 0.125*(n08+n14+n06+n24) + 0.25*(n04);
//interpolate between those values according to the x and y fractions
double v1 = Interpolate(x0y0, x1y0, Xfrac); //interpolate in x direction (y)
double v2 = Interpolate(x0y1, x1y1, Xfrac); //interpolate in x direction (y+1)
double fin = Interpolate(v1, v2, Yfrac); //interpolate in y direction
GLvoid GameUtils::calcNormal(float v[3][3], float normal[3])
{
float v1[3],v2[3]; // Vector 1 (x,y,z) & Vector 2 (x,y,z)
static const int x = 0; // Define X Coord
static const int y = 1; // Define Y Coord
static const int z = 2; // Define Z Coord
// Finds The Vector Between 2 Points By Subtracting
// The x,y,z Coordinates From One Point To Another.
// Calculate The Vector From Point 1 To Point 0
v1[x] = v[0][x] - v[1][x]; // Vector 1.x=Vertex[0].x-Vertex[1].x
v1[y] = v[0][y] - v[1][y]; // Vector 1.y=Vertex[0].y-Vertex[1].y
v1[z] = v[0][z] - v[1][z]; // Vector 1.z=Vertex[0].y-Vertex[1].z
// Calculate The Vector From Point 2 To Point 1
v2[x] = v[1][x] - v[2][x]; // Vector 2.x=Vertex[0].x-Vertex[1].x
v2[y] = v[1][y] - v[2][y]; // Vector 2.y=Vertex[0].y-Vertex[1].y
v2[z] = v[1][z] - v[2][z]; // Vector 2.z=Vertex[0].z-Vertex[1].z
// Compute The Cross Product To Give Us A Surface Normal
normal[x] = v1[y]*v2[z] - v1[z]*v2[y]; // Cross Product For Y - Z
normal[y] = v1[z]*v2[x] - v1[x]*v2[z]; // Cross Product For X - Z
normal[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Cross Product For X - Y
normalize(normal); // Normalize The Vectors
}
GLvoid GameUtils::normalize(float vector[3])
{
float length; // Holds Unit Length
// Calculates The Length Of The Vector
length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2]));
if(length == 0.0f) // Prevents Divide By 0 Error By Providing
length = 1.0f; // An Acceptable Value For Vectors To Close To 0.
vector[0] /= length; // Dividing Each Element By
vector[1] /= length; // The Length Results In A
vector[2] /= length; // Unit Normal Vector.
glDeleteLists (or any other glDelete* function) may not delete the object immediately. All that is required is that the object name be available for reuse immediately; the actual deletion may be placed in the command buffer (in which case it will occur when it's turn comes around) and memory associated with the list may be queued for deletion at a later time, may be retained in case the driver needs to reuse it again shortly, etc.
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.
...interesting. Thank you for the link. I'll put this on the back burner for now and address it again if it starts to cause out of memory errors. Thanks all!