Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

c++ - Opengl object extrusion

I've got a 3d box drawn in opengl, can someone explain how to extrude objects in opengl? do i just translate further back in the z axis for each box?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That approach could work, but you have to do some calculations on how much to translate for each step. I'd recommend generating the extruded geometry in a smarter way, though.

For example, you shouldn't draw the box caps (floor and ceiling) for in-between boxes. You also have to make sure the sides touch perfectly or you will get artifacts.

I recommend using a path to determine the planes where each set of vertices goes. The path should consist of a series of points and an orientation vector for each point, that determines how much to rotate around the direction vector. With that, you can calculate the 4 ring vertices very easily, just using basic vector math.

So, for example, you start with the cap [(0.5,0.5,0),(-0.5,0.5,0),(-0.5,-0.5,0),(0.5,-0.5,0)] and move it along the path (first is center point, second is right vector) [(0,0,0),(1,0,0)],[(0,5,10),(1,1,0)],[(10,5,12),(0,1,0)]

Now, you first calculate all three orientation vectors. The normal is the difference between the current and next point, so (0,5,10) - (0,0,0) = (0,5,10). The right vector must be projected onto the plane defined by the normal, so we calculate the up vector first using the cross product: (0,5,10) x (1,0,0) = (0,10,-5). And as a last step, we calculate the projected right vector, which is the cross product between normal and up: (0,5,10) x (-2,4,-2) = (-125,0,0). All three vectors must be normalized then, and if you put them side by side, you will get a nice transformation matrix that you apply to the cap vectors, yielding the 4 vertices for the current step:

|-1  0        0       |   |0.5|   |-0.5     |
| 0  0.894427 0.447214| * |0.5| = | 0.447213| etc.
| 0 -0.447214 0.894427|   |0  |   | 0.223607|

(I've probably messed up with the signs a bit, you might have to swap the cross product factors to get the right results)

Then you repeat the same procedure for each step on the path and draw the 4 ring quads each time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...