• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Migi0027

Getting maximum and minimum vertex position

11 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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;
	}
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0