Computer Graphics II - Bezier Surfaces

First Homework - Smooth Surfaces using Bézier Equations

Hello everyone, in this series of posts, we'll be talking about the homeworks of the class CENG469 of 2022-2023 and my stories behind their implementations.



For the first homework, I was tasked with implementing bezier surface equations in OpenGL, which is an industry standard graphics library, used from video games to scientific visualization applications. 

Why is this useful? You ask. Or why you'd even bother with equations in the first place. Or even, you may be wondering what you can create with it, instead of pondering on why or how. Well, I hope that you will understand my perspective by the end of this journey you are coming along with me. Let's begin!

Bezier & It's surfaces

What is even a Bezier? It's not a what, its who. And its Bézier, a guy who used to be an engineer in Renault. He was a leader in the evolution of the design and manufacturing principles that used mathematics, and helped people realize the power of computer aided design and modeling. He is the holder of the patent of Bezier Curves and Bezier surfaces. So we may say that we are making a small homage to his work, by actualizing his equations in this homework.

Pierre Étienne Bézier (1910-1999)

Bezier surfaces themselves are a popular type of parametric surfaces in computer graphics. They can be used to create a wide variety of complex shapes, even including the famous Utah Teapot:

 

But for the span of this homework, we will draw only a handful of sample surfaces, and test a couple of test inputs. Hold on, it will get technical.

First Steps

So the first thing I had to do in this homework was to understand how to work with the given example inputs. The inputs themselves were given in a text format, specifying multiple lights, their positions and their color, and a set of numbers that specifies the shape of the surface, mainly the control points of the bezier surfaces. I was asked to draw multiple rectangles and write an interactive program to be able to rotate and change the shape of the surface by interacting with it. But first, I had to come up with solutions to certain design problems.

Lights

We can have up to 5 light sources in this homework. It is a basic concept, and may be the most commonly encountered concept in computer graphics, but it doesn't mean that we don't get to choose our way of handling it. Upon researching it further, I have found that there are multiple ways of implementing multiple lights, like rendering the scene for each light and adding the results (having multiple passes), or writing the shader code to calculate the color values using multiple lights, or even rendering a completely different image, and using color data of that image as a data table and rendering lights using only that image. For this homework however, I have went for supporting up to 5 light sources in the shader, since it was the easiest one of them all, given that we knew for a fact that it will be a maximum of 5. 

Vertices

 If you want to draw a model, you will read from a file all of the vertices and triangles that object has. But for this homework, all we did was to draw simple rectangles, if you disregard the fact that their heights can change by the inputs. So this meant that I could get away with having only necessary amounts of vertices, and not bother changing their positions in CPU, but let GPU handle all of that stuff. So what I did was first decide to handle only 1 patch of surfaces at a given time, and figure how to do that, and repeat drawing each patch until we have drawn everything.

2 Patches drawn separately

A single patch can have at most 80*80*2 triangles, so all I had to do was send that amount of triangles to the GPU, and it can do the rest of the calculations in the vertex shader.

In the vertex shader, there was a pre-defined variable called gl_vertexID, that held information about which vertex it was currently processing. So by drawing each patch separately, I could reset this id to start from 0, and given by the value it has, could offset the x,y coordinate of that vertex. The resulting drawing looked like the figure above.

Surface Equations in the Vertex Shader

Now that we have the x,y coordinates in the vertex shader, it is the time to use the input heights given in the homework, which I passed to the shaders using uniforms. Luckily, we had example matlab code that uses bernstein polynomials, which is just an equation that can help us find the bezier surface. 

For each x,y position, the equation calculates a height, using each of the 16 control points that belongs to that patch of surface. 
 
 
Next, we need to calculate each points normal vector so that we can make use of the lights in the scene.

Calculating Normals

How do you even start? Well, we know that the normal vector should be, well, normal to the surface. Or let's say they are perpendicular vectors that have a unit length of 1. We could use different equations that involve matrix multiplications to find these normals for each vertex, but since we have already implemented a solution using berstein polynomials, I have decided to just take the partial derivates of these equations by hand, and by taking it in both directions, x and y, we can just multiply them using cross product and normalize the resulting vector. Voila!

Surfaces, shaded correctly now that they have normal vectors.

Kidding, it is never that easy with computer graphics

Well, there's no need to say that all of these came with their own problems, and weird bugs, since we are doing computer graphics after all. But let's put those problems aside for now, and just enjoy the resulting, albeit sometimes horrific images that we got along the way.











Our final result!

So in conclusion, we have learnt the intricacies of drawing figures using bezier equations. Particularly, let's say we had some fun with playing with geometry and surfaces, and bezier equations were just a tool that helped us along the way.

Thanks, and see you in the next homework!



Yorumlar

Bu blogdaki popüler yayınlar

Computer Graphics II - HDR & Physically Based Rendering & Image Based Lighting & Shadows

Computer Graphics II - Clouds