Opengl object extrusion

前端 未结 2 1008
时光取名叫无心
时光取名叫无心 2020-12-18 17:32

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?

相关标签:
2条回答
  • 2020-12-18 17:50

    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.

    0 讨论(0)
  • 2020-12-18 18:10

    how to extrude objects in opengl

    You don't. OpenGL is a drawing API, not a geometry processing library. Look at libraries like GTS http://gts.sourceforge.net/

    0 讨论(0)
提交回复
热议问题