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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
(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.