views:

767

answers:

6
+2  Q: 

Hole in a Polygon

I need to create a 3D model of a cube with a circular hole punched at the center of one face passing completely through the cube to the opposite side. I am able to generate the vertices for the faces and for the holes.

Four of the faces (untouched by the hole) can be modeled as a single triangle strip. The inside of the hole can also be modeled as a single triangle strip.

How do I generate the index buffer for the faces with the holes? Is there a standard algorithm(s) to do this?

I am using Direct3D but ideas from elsewhere are welcome.

+1  A: 

Modern hardware usually can't render concave polygons correctly.
Specifically, there usually isn't even a way to define a polygon with a hole.
You'll need to find a triangulation of the plane around the hole somehow. The best way is probably to create triangles from a vertex of the hole to the nearest vertices of the rectangular face. This will probably create some very thin triangles. if that's not a problem then you're done. if it is then you'll need some mesh fairing/optimization algorithm to create nice looking triangles.

shoosh
+2  A: 

Is alpha blending out of the question? If not, just texture the sides with holes using a texture that has transparency in the middle. You have to do more rendering of polygons since you can't take advantage of drawing front-to-back and ignoring covered faces, but it might be faster than having a lot tiny triangles.

Sydius
Great idea! But in my case, things are a little more complicated and I will have to use a mesh.
Vulcan Eager
+4  A: 
MizardX
MizardX, I did not fully understand "2. For each vertex, you project it onto the surrounding square with (x/M,y/M), where M is max(abs(x),abs(y))." And how did you generate the image?
Vulcan Eager
+3  A: 

You want to look up Tessellation which is the area of math that deals with what MizardX is showing.Folks in 3D Graphcs have to deal with this all the time and there are a variety of tessellation algorithms to take a face with a hole and calculate the triangles needed to render it.

RS Conley
+1  A: 

I'm imagining 4 triangle fans coming from the 4 corners of the square.

Jimmy
A: 

Just a thought -

If you're into cheating (as done many times in games), you can always construct a regular cube but have the texture for the two faces you desire with a hole (alpha = 0), you can then either clip it in the shader, or blend it (in which case you need to render with Z sort). You get the inside hole by constructing an inner cylinder facing inwards and with no caps.

Of course this will only work if the geometry is not important to you.

Adi