# How can I bend an object in OpenGL?

Is there a way one could bend an object, like a cylinder or a plane using OpenGL?

I'm an OpenGL beginner (I'm using OpenGL ES 2.0, if that matters, although I suspect, math matters most in this case, so it's somehow version independent), I understand the basics: translate, rotate, matrix transformations, etc. I was wondering if there is a technique which allows you to actually change the geometry of your objects (in this case by bending them)?

Any links, tutorials or other references are welcomed!

-

What you'd need to do to "bend" an object is to apply some appropriate combination of translation/rotation/scaling to only some subset of the vertices of the object in question -- probably weighting that transformation accordingly.

Doing this is a pleasant-looking fashion will necessitate using more vertices than may be otherwise strictly necessary to describe the original mesh. For example, a simple cylinder may only have vertices at either end, but if you want that cylinder to bend in the middle, you'll need vertices there as well.

If all you are interested in is very specific, one-off deformations of very simple shapes (like your aforementioned plane) you could probably just do this manually. For example, decide on the "bend axis" for your plane and compute appropriate transformations for each row of plane vertices to achieve the appropriate rotation about that bend axis depending on their distance from it, and then apply that to the geometry (possibly on the CPU as it would be the most straightforward, at least initially). But such a technique would not scale well and would be very tedious.

Since it can be so cumbersome to correctly figure out how to weight a transformation to each individual vertex in a set to achieve the desired deformation (especially for more practical and complicated objects), many games use a technique called skeletal animation. At a high level this involves associating a separate set of geometry (the "skeleton") with your mesh. That geometry represents the bones of your object, and you define (often in modeling tools) a mapping between vertices of the real model and the bones of the skeleton -- which bones are associated with each vertex, and how much that bone affects the deformation of that vertex. Then the skeleton is animated directly, and the animation is transferred to the real model via the relationship between the bones and their vertices.

Typically in this technique, the mapping between skeleton and real mesh is defined at asset production time. The animations can also be built then, or they can be built procedurally at runtime. The application of the animation transformations to the real mesh occurs at runtime. Generally no new vertices are generated at runtime, which means the range of good-looking deformations is implicitly fixed in the mesh when it is originally created.

In theory, one could also apply tessellation and subdivision to the mesh at runtime based on analysis of the transformations in the animation, and thus produce the appropriate vertices to create an animation that was not necessarily planned for. I'm not aware, offhand, of any papers on this subject.

-

For the actual math of warping, this can get very complicated, why don't you start here?.

I'll now talk about how you can apply this, assuming you've already got the math for how you will do your deformations down.

2 ways:

1) Every frame, visit every vertex in the cylinder model and offset it in some way.

2) Offset the vertices in the vertex shader as you are rendering. OpenGL ES 2.0 supports vertex shaders. So you're in luck, in a way.

Do you want to only warp the object as it appears on the screen, or do you want to warp the object so every object in the game knows that it has been deformed?

If you warp the object in the vertex shader, then that happens only immediately prior to display/rasterization, so this means none of the game objects will know about the deformation. This is fine if you're simply making a screen saver, or if the deformations are so small that they have no impact on the collidable shape of the object.

When you warp the geometry in the vertex shader __the original vertices of the model actually don't move__, (they only appear to move).

## Deformed

This is from chapter 6 of the cg tutorial, (program #14).

The shader code to produce this deformation is something like

``````// choose a displacement amount
float displacement = scaleFactor * 0.5 * sin(position.y * frequency * time) + 1 ;

// displace it in the direction of the normal
float4 displacementDirection = float4(normal.x, normal.y, normal.z, 0) ;

// move the vertex
float4 newPosition = position + displacement * displacementDirection ;

// transform (as normal)
oPosition = mul(modelViewProj, newPosition) ;
``````

So you can see, this happens RIGHT BEFORE display (vertex shader happens on GPU) so CPU code has practically no way to access the transformed vertex position (there are ways to send data from the GPU to the CPU (primarily via textures) but we won't go there).

-

What you're trying to achieve is basically a subset of the topic of mesh deformation. But since you're a begginer, I'm afraid this type of information might be a bit too strange for now. I'll try to lay down the basic notions though.

In order to do this you'll need two things:

1. Your mesh needs to have enough vertices for you to transform. For instance if it's a plane, you'll need to make sure it's actually created as a sort of grid, with many vertices for you to deform. Same thing with a cylinder. Might be possible to achieve this at runtime with tessellation but I don't know much about the subject.
2. You need to use a vertex shader to deform the vertices. Basically the vertex shader takes care of calculating the final transformed position of each vertex, so you can take the chance to apply any deformations to them.

I'm not sure how helpful it will be, but take a look at this OpenGL resource, as it seems to be right the sort of thing you need:

http://www.ozone3d.net/tutorials/mesh_deformer_p3.php#part_3