Jump to content

  • Log In with Google      Sign In   
  • Create Account


Weird values coming up in a loop.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 stevo86   Members   -  Reputation: 150

Like
0Likes
Like

Posted 17 March 2009 - 10:17 AM

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[i].x = x;
		vertices[i].y = 0;
		vertices[i].z = z;

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

		i++;

		vertices[i].x = x;
		vertices[i].y = 0;
		vertices[i].z = z+1;

		// Dump our vertices by triangle strip.
		error("%d = %d,%d,%d", i, vertices[i].x, vertices[i].y, vertices[i].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?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 17 March 2009 - 10:21 AM

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.

#3 stevo86   Members   -  Reputation: 150

Like
0Likes
Like

Posted 17 March 2009 - 10:23 AM

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.

#4 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 17 March 2009 - 10:28 AM

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.

#5 stevo86   Members   -  Reputation: 150

Like
0Likes
Like

Posted 17 March 2009 - 10:33 AM

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?

#6 Palidine   Members   -  Reputation: 1275

Like
0Likes
Like

Posted 17 March 2009 - 10:34 AM

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

#7 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 17 March 2009 - 12:04 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS