CG programming for non-programmers, lesson 24

Sometimes values only make sense within a certain range. For example, in an election it doesn’t make sense, when talking about voters, to speak of fewer than zero percent, or more than one hundred percent.

Similarly, when we mix two things, it often doesn’t make sense to talk about a mix parameter of less than zero, or greater than one.

For such reasons, it is often useful to clamp a value to some useful range. In lesson 24 we introduce the clamp(value,lo,hi) function, which makes sure that value can’t be lower than lo or higher than hi.

In this case, we are applying the clamp function to control the gradation in fog thickness from the top to the bottom of our image. We want to have lighter fog at the top of the image, and heavier fog at the bottom.

But we still want to mix in a sensible quantity of fog. And in this case, “sensible” means the mixing parameter needs to be between 0.0 and 1.0.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 23

For lesson 23 we learn how to mix colors. Actually, we can use it to mix pretty much anything. We are just using color as an example.

The mix(a,b,t) function uses its third argument t to create a mixture of a and b. When t is 0.0 the result is a. When t is 1.0 the result is b. When t is anything in between we get a mixture of a and b.

Here we are using it to create a mysterious fog cover over our moon at night. The shape of the fog cover is created by a noise texture, and the value of that noise texture is controlled by x, y, z and uTime. The use of uTime in defining the noise texture is what makes the fog animate.

You can see this lesson by CLICKING HERE.

Intermission

After 22 straight posts about computer graphics for non-programmers, it seems like a good idea to take a deep breath before plunging back in. Consider this an intermission.

Coincidentally, this is my first week of the semester teaching undergraduate computer graphics, and I am about to cover much the same material — albeit for students who have already been programming for at least a few years. Their prior programming experience will allow me to cover a lot more ground, so it’s not quite the same thing.

Meanwhile, I’ve been grappling with the question of how to write a really great story for immersive virtual reality. I have seen quite a few VR pieces by now, but to my mind none of them has been completely successful on the fundamental level of narrative storytelling.

I guess I’m taking that as a challenge. Wish me luck! 🙂

CG programming for non-programmers, lesson 22

For lesson 22 we make just a few small changes from lesson 20, but the resulting visual difference is dramatic.

We throw some of our handy noise function onto the sphere, tint our sphere ever so slightly yellow, and place it all over a deep blue background.

Suddenly we see not a mathematical sphere, but a moon in the night sky.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 21

Sometimes other people come up with cool shader ideas and all I can say is “wow, totally awesome.”

In his comment on yesterday’s lesson, Sam Umbach came up with such an idea, and posted the code in his comment.

So for lesson 21 I am just going to stand back and share his work with the rest of you. Hopefully others will be inspired to create their own interesting shader ideas.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 20

In lesson 20 we finally move from 2D graphics to 3D graphics. Specifically we go from flat disks to spheres.

To do this we just use some high school math. For a sphere of radius r, you probably learned in high school that for any point (x,y,z) on the sphere surface the following is true:

      x2 + y2 + z2 = r2

From there it is pretty easy to show that if you already know x,y and r, you can find z by:

      z = sqrt(r2 - x2 - y2)

We use this formula to create a sphere, which we will do cool things with in coming lessons.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 19

Computer Graphics programming isn’t just about getting the technical stuff right. It’s also about using your eye, looking mindfully at the world around you, being inspired by the beautiful patterns in nature, and drawing upon what you see as inspiration.

In lesson 19 we start with the code we developed in earlier lessons, and then tweak things a bit to bring out a desired aesthetic quality. In particular, we change our undulating disk just enough so that it suggests the reflection of the Moon over water at night.

Note that we don’t need to include all of the details. As long as we properly suggest the real thing — through good choice of color, shape and movement — the mind of our viewer will do the rest.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 18

Everything we are doing so far is on a flat plane. vPos.x and vPos.y vary, but vPos.z is always zero.

Yet the noise function actually takes a 3D point as its argument, and we can use this fact to create animated textures. If we animate the z coordinate of its argument, we end up essentially sliding the noise function in z over time.

You can think of this as taking different planar slices through some solid textured material. As you slide your plane through the texture, you uncover different layers of the texture, so the texture you see will change over time.

For lesson 18 we do just this. By animating the z coordinate of our argument to the noise function, we end up varying the value of the noise over time. The result is that the texture that is causing our disk to look bumpy will animate.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 17

For lesson 17 we will apply the noise function to something quite different: Changing the shape of something. Here we start to see that noise can be used pretty much anywhere, to make things look a little less synthetic.

One way I like to think of it is that noise is kind of like salt. You wouldn’t eat salt by itself. But if you sprinkle just the right amount on your food, the food tastes better.

You can see this lesson by CLICKING HERE.

CG programming for non-programmers, lesson 16

Computer graphics gets boring if everything becomes too regular. So it’s good to introduce a little randomness, to mix things up a bit and make the image look more natural.

But it should be controlled randomness, much in the way the arrangement of the bristles in a paintbrush allow an artist to add controlled randomness into a painting, while still maintaining aesthetic control.

To do this we can use procedural noise. Noise can be used in all sorts of ways. In lesson 16 we are using it in a very basic way: to create a simple texture that doesn’t look too regular.

You can see this lesson by CLICKING HERE.