# Weird values coming up in a loop.

This topic is 3473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on building a terrain engine and in the portion of code where I construct the vertices I'm dumping the values and getting some very strange output. Here's the code:
	Vector3* vertices;
int size = 256*256;

vertices = new Vector3[size];

int x,z;
x=z=0;
for (int i=0;i<size;i+=1)
{
vertices.x = x;
vertices.y = 0;
vertices.z = z;

error("%d = %d,%d,%d", i, vertices.x, vertices.y, vertices.z);

i++;

vertices.x = x;
vertices.y = 0;
vertices.z = z+1;

// Dump our vertices by triangle strip.
error("%d = %d,%d,%d", i, vertices.x, vertices.y, vertices.z);

x++;

if (x == 256)
{
z++;
x = 0;
}
}

The error() function is just a simple function that dumps whatever I print into a log.txt file. Now the weird thing is the first 2 values are correct. The x,y and z coordinates are all set to 0 as expected, but on the next iteration of the loop, the x and z values are fine but the y value is an absurdly high arbitrary number like 238573937. Any ideas as to why this would be?

##### Share on other sites
What does Vector3 look like? If x, y and z are floats or doubles, then what you're probably getting is a misinterpretation of binary values in your error() function.

##### Share on other sites
They're floats which could explain the misinterpretation, but isn't 0 always 0 no matter what type the variable is?

Also when I render I use glVertexPointer(3, GL_FLOAT, 0, NULL) as one would with a vertex buffer, but the Y values are messed up there as well and I actually ended up crashing my graphics card and having to restart my computer because the values were so out of wack.

##### Share on other sites
One interesting and not very well known property about variadic functions is that when you pass a float, it actually automatically gets promoted to a double. Which means that your first %d is getting i, the second %d is probably actually getting the first half of x, the second %d is getting the second half of x, and the last %d is getting the first half of y.

##### Share on other sites
Interesting... Thanks for the tip on that. I fixed my error printing function which has the values dumping correctly, but what about the rendering. I'm trying to build triangle strips for the terrain VBO, but as I said I'm getting all kind of weird values rendering. Am I setting up the vertices incorrectly for triangle strips?

##### Share on other sites
print it as %.2f instead of %d. You'll always get funk if you try to print floats as ints (i'm guessing b/c of what SiCrane posted, which I did not know but explains a lot [smile])

-me

##### Share on other sites
Take the magic numbers out of your program and replace them with a named constant. Then try reducing the size of your terrain to 4 by 4 rather than 256 by 256 and look at the resulting coordinates. Try drawing by hand, on paper, what the triangle strips would look like. In particular pay attention to what happens when you move from the end of one row to the beginning of the next.

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 33
• 13
• 12
• 10
• 12
• ### Forum Statistics

• Total Topics
632573
• Total Posts
3007138

×