Skip to content

112. Lighting in 3D – introduction

September 24, 2013

This is the first in a series of posts on how to create impressive lighting effects in 3D using Codea.

What sort of effects?

by damny

by spacemonkey

by spacemonkey

Disclaimer – I am not a lighting expert, so forgive me any errors in what follows.

What you need to know, and where we are headed

You don’t need any special knowledge to read this post, but once we get deeper, you’ll need to know about shaders and meshes. In other words, this is not for new Codea users, unless you have some training in this stuff (and if you do, you may know more than me!).

Lighting involves math, trigonometry, matrices and visualising objects in 3D (which probably explains why I have struggled so much to understand it- I am terrible at solving 3D puzzles!). It also has a lot of confusing language and names.

However – I believe it is possible for the average person to understand, so I am going to try to walk you through it, in the way I wish someone had done for me.

The basic lighting model

Computers don’t attempt to model lighting perfectly accurately, because it’s too hard. The most common approach, and the one I’ll explain, is Phong reflection. I understand it is popular because it can be modelled¬†efficiently by computers.

There are three types of light we need to model, as shown below.
(Source: Wikipedia)

Ambient light

Ambient light is the general light level, not coming from any special direction, or stronger in one direction than another. No matter which angle you look at, the light is about the same. The sun’s rays are a good example, because they get scattered so much, they seem to come from everywhere. Low ambient light means everything is dark, and high ambient light makes everything brightly lit.

So if you’ve been drawing graphics without any special lighting effects, you’ve been using ambient lighting.

However, there is one little thing that we will add in our ambient light here, and that is that it can have different colours, not just white. This is the same as adding a colour tint to our scene.

Ambient light is very simple to model, as you will see.

Diffuse light

Diffuse light is directional. Outside, the sun makes one side of a building brighter than the other sides, and that brighter light is diffuse. Think of it as adding to the ambient light, so it makes one side of your scene brighter (it doesn’t make the other side darker, though).

Diffuse light is produced by a light source that shines on our scene. By moving our light source around, we can change the effect of the diffuse light. The light can be any colour you want.

The effect is usually strongest if a light shines straight at a surface, and is weaker if it shines at an angle to the surface (because the light spreads over a bigger area). This is important, as you will see.

Specular light

Specular light is the shiny bright reflection of the light source off an object. To see this, you need to be at exactly the right angle, and its brightness depends on how shiny the object is. So this is the most complicated of the three types of light.

The image below shows the connection between the three types of light. Specular light is the bright spot reflecting the actual light source (if it is visible), diffuse light is the effect of a light source, and ambient light is the general light level without any light sources.

So you’ve got that? One more time to fix it in your mind.

  • Ambient = background (all around) light level
  • Diffuse = reflection created by a light source
  • Specular = bright shiny part of the reflection

How we model lighting in Codea with shaders

Lighting largely needs to be modelled pixel by pixel to look realistic.

Codea uses the OpenGL ES graphics engine, which allows us to write “shaders” that can manipulate mesh vertices (vertex shader) and individual pixels (fragment shader).¬† You will need a basic understanding of shaders to understand this series of posts.

If you don’t know what they are, and you want to master lighting, I suggest you have a look at the posts I wrote on shaders, starting here. They are not nearly as scary as they may sound.

Each time Codea draws (up to 60 times per second), it runs the vertex shader for each vertex in your mesh (36 for a simple cube!), and the fragment shader for each pixel (I think only for the visible pixels that actually get drawn, but that’s still a lot of pixels). So while we get best results from doing our lighting at pixel level in the fragment shader, we need to try to do as much work as possible in Codea or in the vertex shader.

Where to from here

First I will need to explain “normals”. They are very important.

Then I’m going to explain the formulae for the three types of light (don’t be nervous, I’m sure you will understand them).

Then, I’ll cover what was for me the most difficult part – the different “spaces”, object, world and eye, and how we move between them. I’m hoping I can make this much easier for you to understand than it was for me.

Finally, I’ll put it all together in a lighting library that you can use in your own projects.

And maybe I’ll have some bonus extras – in my exploration, I saw some interesting stuff I haven’t had a chance to look at yet.

From → 3D, Shaders

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: