Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Getting maximum and minimum vertex position


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
11 replies to this topic

#1 Migi0027   Crossbones+   -  Reputation: 2137

Like
0Likes
Like

Posted 03 April 2013 - 01:58 PM

Now this may sound really simple, but somehow it's been bugging me for now, here's the code (explanation at the end):

 

void UMesh::GenerateBoundingBox(vector<MESH_STRUCT> &vertices)
{
	float maxx = 0, maxy = 0, maxz = 0;
	float minx = 0, miny = 0, minz = 0;

	FOREACH(vertices.size())
	{
		if (maxx < vertices[i].X || i == 0)
			maxx = vertices[i].X;

		if (maxy < vertices[i].Y || i == 0)
			maxy = vertices[i].Y;

		if (maxz < vertices[i].Z || i == 0)
			maxz = vertices[i].Z;

		if (minx > vertices[i].X || i == 0)
			minx = vertices[i].X;

		if (miny > vertices[i].Y || i == 0)
			miny = vertices[i].Y;

		if (minz > vertices[i].Z || i == 0)
			minz = vertices[i].Z;
	}

	// Set params
	BoundingBox.omx = BoundingBox.maxx = maxx;
	BoundingBox.omy = BoundingBox.maxy = maxy;
	BoundingBox.omz = BoundingBox.maxz = maxz;
	BoundingBox.ommx = BoundingBox.minx = minx;
	BoundingBox.ommy = BoundingBox.miny = miny;
	BoundingBox.ommz = BoundingBox.minz = minz;

	AdaptBoundingBox();
}

void UMesh::AdaptBoundingBox()
{
	BoundingBox.maxx = BoundingBox.omx;
	BoundingBox.maxy = BoundingBox.omy;
	BoundingBox.maxz = BoundingBox.omz;
	BoundingBox.minx = BoundingBox.ommx;
	BoundingBox.miny = BoundingBox.ommy;
	BoundingBox.minz = BoundingBox.ommz;

	// Apply Scaling
	BoundingBox.maxx *= scalation.x;
	BoundingBox.maxy *= scalation.y;
	BoundingBox.maxz *= scalation.z;
	BoundingBox.minx *= scalation.x;
	BoundingBox.miny *= scalation.y;
	BoundingBox.minz *= scalation.z;

	// Calculate size
	BoundingBox.sizex = BoundingBox.maxx - BoundingBox.minx;
	BoundingBox.sizey = BoundingBox.maxy - BoundingBox.miny;
	BoundingBox.sizez = BoundingBox.maxz - BoundingBox.minz;
}

 

UMesh is just a class. The GenerateBoundingBox gets all the vertices and then tries to get the max xyz, and the min xyz out of it. The BoundingBox.omx or .ommx is just backups for later use, omx = origional max x, ommx = origional minimum x (but it doesn't matter). From my results (i have a tree), the values are actually bigger than the mesh itself. The tree actually has submeshes so what I do is that I combine all the vertices into one vector. The AdaptBoundingBox is also used when scaling as the bounding box also then has to change when the mesh is scaled.

 

Now what on earth did I do wrong? biggrin.png


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

Sponsor:

#2 belfegor   Crossbones+   -  Reputation: 2725

Like
0Likes
Like

Posted 03 April 2013 - 02:30 PM

You should first initialze min with some maximum float point value and max with minimum. and then do comparison in loop.

 

//float maxx = 0, maxy = 0, maxz = 0;
//float minx = 0, miny = 0, minz = 0;
float maxx = FLT_MIN, maxy = FLT_MIN, maxz = FLT_MIN;
float minx = FLT_MAX, miny = FLT_MAX, minz = FLT_MAX;

Edited by belfegor, 03 April 2013 - 02:31 PM.


#3 Migi0027   Crossbones+   -  Reputation: 2137

Like
0Likes
Like

Posted 03 April 2013 - 02:33 PM

Well I changed it, but nothing really happened, same results:

 

float maxx = FLT_MIN, maxy = FLT_MIN, maxz = FLT_MIN;
	float minx = FLT_MAX, miny = FLT_MAX, minz = FLT_MAX;

	FOREACH(vertices.size())
	{
		if (maxx < vertices[i].X)
			maxx = vertices[i].X;

		if (maxy < vertices[i].Y)
			maxy = vertices[i].Y;

		if (maxz < vertices[i].Z)
			maxz = vertices[i].Z;

		if (minx > vertices[i].X)
			minx = vertices[i].X;

		if (miny > vertices[i].Y)
			miny = vertices[i].Y;

		if (minz > vertices[i].Z)
			minz = vertices[i].Z;
	}

Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#4 Juliean   GDNet+   -  Reputation: 2768

Like
0Likes
Like

Posted 03 April 2013 - 02:47 PM

Wait a minute...

 

	FOREACH(vertices.size())

are you sure this is a working loop? Seems like a stupid question, but all you are passing is the size of the vertices, but I couldn't find any reference to this, is this some sort of compiler macro or something?



#5 Migi0027   Crossbones+   -  Reputation: 2137

Like
0Likes
Like

Posted 03 April 2013 - 02:58 PM

Ohh sorry, this equals:

 

#define FOREACH(Arrmax) for(int i = 0; i < Arrmax; i++) 


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#6 belfegor   Crossbones+   -  Reputation: 2725

Like
0Likes
Like

Posted 03 April 2013 - 03:04 PM

Do you know that if you move your object you must also move BB min and max? Are you moving/moved your tree or is it at "origin"?

 

How are you visualiasing BBox? Post some more code.


Edited by belfegor, 03 April 2013 - 03:05 PM.


#7 Migi0027   Crossbones+   -  Reputation: 2137

Like
0Likes
Like

Posted 03 April 2013 - 03:23 PM

When I use these values, I add the current position of the mesh:

 

// Can we see it?
if (ViewFrustum.CheckRectangle(mesh->position.x + (mesh->BoundingBox.sizex/2.0f), // the x center
						   mesh->position.y + (mesh->BoundingBox.sizey/2.0f),  // the y center
						   mesh->position.z + (mesh->BoundingBox.sizez/2.0f), // the z center
						   mesh->BoundingBox.sizex, // the x size
						   mesh->BoundingBox.sizey, // the y size
						   mesh->BoundingBox.sizez) == false) // the z size
{FontSystem.get(0)->_string = L"Tree Available: false";return;}
else
{FontSystem.get(0)->_string = L"Tree Available: true";}

 

The frustum culling is based on the rastertek tutorial: http://www.rastertek.com/dx11tut16.html


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#8 belfegor   Crossbones+   -  Reputation: 2725

Like
0Likes
Like

Posted 03 April 2013 - 03:34 PM

From what i can see the size(s) variables should be half extent of a bbox , so

 

 

void UMesh::AdaptBoundingBox(){
...
//BoundingBox.sizex = BoundingBox.maxx - BoundingBox.minx;
//BoundingBox.sizey = BoundingBox.maxy - BoundingBox.miny;
//BoundingBox.sizez = BoundingBox.maxz - BoundingBox.minz;
BoundingBox.sizex = (BoundingBox.maxx - BoundingBox.minx) * 0.5;
BoundingBox.sizey = (BoundingBox.maxy - BoundingBox.miny) * 0.5f;
BoundingBox.sizez = (BoundingBox.maxz - BoundingBox.minz) * 0.5f;
}
 
 
float bbCenter[3];
bbCenter[0] = (mesh->BoundingBox.maxx + mesh->BoundingBox.minx) * 0.5; // x
bbCenter[1] = (mesh->BoundingBox.maxy + mesh->BoundingBox.miny) * 0.5f;// y
bbCenter[2] = (mesh->BoundingBox.maxz + mesh->BoundingBox.minz) * 0.5f; // z
 
if (ViewFrustum.CheckRectangle(mesh->position.x + (bbCenter[0]), // the x center
                         mesh->position.y + (bbCenter[1]), // the y center
                         mesh->position.z + (bbCenter[2]), // the z center
                         mesh->BoundingBox.sizex, // the x size
                         mesh->BoundingBox.sizey, // the y size
                         mesh->BoundingBox.sizez) == false) // the z size
...

Edited by belfegor, 03 April 2013 - 03:35 PM.


#9 Migi0027   Crossbones+   -  Reputation: 2137

Like
0Likes
Like

Posted 03 April 2013 - 05:17 PM

It works, thanks!


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#10 mhagain   Crossbones+   -  Reputation: 8287

Like
0Likes
Like

Posted 03 April 2013 - 06:05 PM

These are the wrong way around:

 

		if (maxx < vertices[i].X)
			maxx = vertices[i].X;

		if (maxy < vertices[i].Y)
			maxy = vertices[i].Y;

		if (maxz < vertices[i].Z)
			maxz = vertices[i].Z;

		if (minx > vertices[i].X)
			minx = vertices[i].X;

		if (miny > vertices[i].Y)
			miny = vertices[i].Y;

		if (minz > vertices[i].Z)
			minz = vertices[i].Z;

 

When you init minx, for example, to FLT_MAX, you're never actually going to have vertices[i].x exceeding it.  You need to check minx < vertices[i].x and maxx > vertices[i].x, and likewise for y and z.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#11 belfegor   Crossbones+   -  Reputation: 2725

Like
0Likes
Like

Posted 03 April 2013 - 09:12 PM

Are you sure 100%?



#12 Juliean   GDNet+   -  Reputation: 2768

Like
0Likes
Like

Posted 04 April 2013 - 02:31 AM

No, it is okay that way. Well, almost, you are doing sort of "yoda conditions" which is kind of confusing. I had to lool at the code a few times go get it right. I'd suggest you rewrite these conditions to like

" if(vertices[i] > maxx)"

to increase readability and understandabity.




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