Catmull-Clark Subdivision Algorithm with OpenGL

The Catmull-Clark subdivision algorithm is an algorithm used to form objects with smooth curves gradually. The procedure itself looks simple.

First we find the face-points which are the center points of the faces of the object. We find every face-point because we will use them later.

Second we find the edge-points for each edge. An edge-point is the average of the two end-points of that edge and the two neighboring face-points.

Finally we find the new vertex points where we change the previous vertices of the object. We get this by calculating the weighted average the neighboring face points, the original vertices and the touching edge mid-points which are not the same with the ones in the previous step. Edge mid-points are just the middle of the neighboring edges.
We calculate the weighted average with weights of 1, (n-3) and 2 in the order of face points, original vertices and edge mid-points where n is the number of touching faces and the number of touching edges to the original vertex.

After these calculations we get an object formed of quad literals. But OpenGL doesn’t support directly rendering quads, also we have a cube object which is formed with quads so we should form a way on how to handle these.

To parse the object first, I just formed every quad as 2 triangles following the order of the same direction clockwise so I could calculate the normals directed at the same direction. This was pretty easy and I could parse the cube successfully.

At this point I wanted to start forming the scene and do the flat shading. The rotations around two circles which two objects perform are just translation operations applied to each one with different “delta-angles” using the cosine and sine function in x-z and z-y planes.

I researched the way flat-shading works and what I found was not very easy to do. I thought of duplicating the vertices first. I already calculated the normals but the problem was in a cube setting, I needed every vertex to have 3 different normals so I could render the 3 different triangles it touches with different normals. So I needed to duplicate every original vertex twice and add 3 normals for each vertex. And I needed a specific order for them. Sadly I couldn’t get this done after trying for a long time. I decided to use the “flat” keyword in the object shaders to cancel the interpolation procedure. This provided sufficient results for me at the time especially for the cube. But the octahedron looked a bit of because by using the keyword “flat”, I just use the position and the normal of the first point of that face, so it is actually not the proper way to do this. I think in the octahedron some of the normals of the faces were directed backwards and it proved to be a problem here. This might have not been a problem for the higher stages but looked so obvious in the first one. But now I needed to make the actual algorithm itself.

I started implementing the algorithm looking at the cube as an example. The face-points and edge-points were easy to calculate and I thought I got the new vertices right but I could not get a reasonable output and this is the point where things went south. After calculating the points, I couldn’t figure out how to order them into faces properly. Even the first stage was so off. I tried to form a plan to render the object properly but could not come up with one. Even after working for a very long time at this and trying to debug the vertex calculations, I could not pull it off. The program produced very bad outputs and after the 3rd stage it crashed. At this point I lost the confidence that I could get it done. I got quite angry and decided not to submit the homework with the algorithm code.

So after long hours of work I was left with 3 moving objects that moved a certain way with mediocre flat shading that I could stop and see in different modes. The algorithm implementation was quite a disaster for me and overall this was a humbling experience. I am not proud of the way things worked out but I tried doing it for a long time even did not go to school for a couple of days to get it done so now I think I should do the research part really early on and form plans to make important things like forming faces after using the algorithm first. And maybe my skillset was not developed enough to get it done.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *