Jump to content


Weird values in a loop.


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

#1 SteveH

    New Member

  • Members
  • Pip
  • 9 posts

Posted 17 March 2009 - 09:31 PM

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?

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 17 March 2009 - 09:35 PM

Aren't your vectors floats? If so, you need to use %f in printf instead of %d.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 SteveH

    New Member

  • Members
  • Pip
  • 9 posts

Posted 17 March 2009 - 09:37 PM

That only explains the weird values in the error function though, it doesn't account for the fact that when I render the Y values are messed up there as well. Theoretically, since I'm defaulting all the Y values to 0 I should just be rendering a flat grid, but instead it looks more like an abstract spider web and actually crashed my graphics card earlier because the Y values were so out of wack.

#4 SteveH

    New Member

  • Members
  • Pip
  • 9 posts

Posted 17 March 2009 - 09:55 PM

Ok, the values are dumping correctly now that I fixed the error() function. I'm trying to construct a blank terrain grid built of triangle strips, but I'm wondering if I'm building the vertices correctly. Can anyone see anything wrong with the way I'm setting them up?

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 17 March 2009 - 10:41 PM

The code you posted looks correct to me. I'd advise stepping through everything with a debugger and just inspecting the values. Do this in your draw function as well as in your setup function. There could be some problem elsewhere in the code like an array being overwritten that is causing the memory to get corrupted.
reedbeta.com - developer blog, OpenGL demos, and other projects





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users