Saturday, 15 February 2014

Lighting in Games

So in my last blog I mentioned that next I was going to focus on lighting. Now I'm not going to focus on why lighting in games is important since I looked at lighting's importance in an older blog, which (if any of you are interested in) is located HERE. It talks a little bit about why lighting is important and also what can be effected or changed when lighting is involved. This blog however is going to focus on some types of lighting that you can do and how you can achieve some of them.

So there are numerous different types of lights and different methods of lighting that a lot of industries, not just the gaming industry uses. Light is an absolutely crucial aspect of games and entertainment, and really can make or break the atmosphere of the movie or game. Different lights can give you different effects and different combinations of different types of lights can make your game look absolutely outstanding. Now since there are so many different types of lights I'm only going to focus on few.

TYPES OF LIGHTS AND TECHNIQUES

When lighting objects there are a few different methods to do so, 3 important ones to know about are Per-vertex, per-pixel and per-face lighting.

  • Per-Face Lighting


    •  Per-Face Lighting, also referred to as flat shading, is a lighting technique that shades and lights each polygon of an 3D object based on the angle between the polygon's surface normal and the direction of the source.
    • In addition, it also takes into consideration the respective colours of the polygons and the intensity of the light source.
    • In this type of lighting all polygons are coloured with only one colour meaning that all other polygons have a differentiation between themselves and their adjacent polygons. This means that almost every polygon has its own unique colour depending on the intensity and location of the light source.

  • Per-Vertex Lighting

    •  Also known as Gouraud Shading, Per-Vertex lighting calculates the lighting at each vertex, Linear Interpolating between vertices. This gives the a very polygonal appearance. 
    • Basically this type of lighting estimates the surface normal of each vertex in a 3D polygonal model, which is generated by averaging the surface normals of the polygons that meet at each vertex.
    • Using these estimates lighting is then computed based on a reflection model are then done in order to determine the intensity of each colour at the vertices.
  • Per-Pixel Lighting


    • Also known as per-fragment lighting, Per-pixel lighting calculates the lighting at each pixel in a 3D model, this gives a more more realistic look to the light on the model.
    • This type of lighting is able to be combined with things like normal mapping and bump mapping, which allows for really interesting effects and more realistic lighting.
    • Per-pixel lighting is the commonly used lighting in video games as it allows for greater realism and detail.
  •  Lambert

    • Lambert, named after Johann Lambertian who developed the idea, is a lighting technique that specifies how light illuminates a surface.
    • Essentially this light model states that on objects light is emitted evenly across all angles. This means that different points or areas of an object will look the same no matter what angle the are viewed from.
    • To calculate Lambert you take the dot product of the surface normal vector and a normalized light-direction vector pointing from the surface to the light source. Then multiple that number by the colour and the intensity of the light.
  • Phong

    •  Phong Lighting or Phong Shading, named after Bui Tuong Phong who first published the method in 1973, is an interpolation technique for surface shading and is also known as Phong Interpolation or normal-vector interpolation shading.
    • It interpolates surface normals across rasterized polygons and computes pixel colour based on the interpolated normals and a reflection model.
    • Phong Lighting is used for surfaces that have specular highlights, which are the direct reflection of the light off of the object.
    • This model is made up of two components: the diffuse light you get from lambert and the light's reflection, which is found by determining the angle we are viewing the object at and compare it to the angle the light is hitting the object at the same time.
  • Blinn-Phong

    •  Also known as Blinn-Phong reflection, this is a modified phong reflection model, developed by Jim Blinn, that uses a halfway vector between the viewer and the light-source vector instead of the reflection.
    • The Blinn-Phong model, while less efficient than the Phong model in most cases, is faster in  the case where the viewer and light are treated to be at infinity, which is the case for most directional lights.
    • When using directional lights, Phong-Blinn lighting allows for each light to have its own computation which can then be used for the entire frame or while the light and viewpoint remain in the relatively same position.
  • Specular

    •  Specular Lighting, also known as specular highlight, is the bright spot of light that appears on shiny objects or models  and uses the highlight to give visual clues for light source locations.

    • It is commonly used to give light reflection off of metallic surfaces such as mirrors and highly polished/ reflective metallic surfaces. In addition, it is used for reflecting sunlight off water and can add a degree of photo realism to most 3D scenes. 
  • Diffuse


    •  Diffuse Lighting, also known as diffuse reflection, is the reflection of light from a surface that an incident ray is reflected at many angles rather than just one in the case of specular lighting.
    • In order to determine diffuse lighting we need to calculate the angle between the surface ans the light, the diffuse intensity of the light and the diffuse reflectance of the material.
  •  Ambient

    •  Ambient Lighting is a fixed-intensity and fixed colour light source that effects all objects in the scene equally. All objects in the scene are brightened with the specified intensity and colour.
    • This type of lighting is mainly used to provide the scene with a basic view of the different objects in it, and is considered to be the simplest type of lighting to implement.
    • In order to represent how exposed each point of the scene is, ambient light is combined with ambient occlusion (which I will focus on in a later blog), which produces a similar effect to an overcast day.

 

 

LIGHT SOURCES

There are numerous different types of light sources, however in this blog I will be briefly going over only a few of them.
  • Spotlight Lighting

    •  This is a type of light source that originates from a single point and spreads outward in a cone shape, illuminating only a specific area which is determined by the diameter of the of the light.
  • Directional Lighting

    •  This is a light that illuminates all objects equally from any given direction, like an area light of infinite size and distance from the scene. Anything in the direct path of this light source is illuminate while those objects that aren't in the light's path are not seen.
  • Point Lighting 

    •  A point light is a light source that originates from a single point, and spreads outward in all directions.
  • Area Lighting

    •  This type of light originates from a small area on a single plane, which is a more accurate type of light source than point light.
  • Volumetric Lighting

    •  This light source originates from a small volume, which is an enclosed lighting object within that space. With Volumetric light shading is interpolated based on how the angle of these light sources reach the objects in a scene.
So my next blog is going to go over more in detail the following lighting techniques and will give a few tutorials on how to achieve a few of these effects in Photoshop and Maya.

Lighting Effects
  • Radiosity
  • Global Illumination
  • HDR/Bloom 

Monday, 3 February 2014

Shaders



Hello and welcome people of the internet to this blog of well stuff, yeah stuff that’s the best way to describe this blog, since I talk about stuff and every once in a while I rant about stuff. SO YEAH, welcome to the blog that talks about stuff, the stuff that will be focused on in this blog will be a little something called shaders. This blog will cover a brief overview of what shaders are some of the types of shaders and examples of what shaders can do and where you can use them.
http://eggtion.net/wp-content/uploads/2012/07/overview.jpg
So you may be wondering what shaders are, and why the hell should you care about shaders, well I am going to answer those questions in just one moment. 

WHAT ARE SHADERS?

http://upload.wikimedia.org/wikipedia/en/e/e6/Example_of_a_Shader.png
Now shaders, in the field of computer graphics, are the name for a computer program that are used to produce special effects and do post processing. Shaders can be used to create anything from shadows to reflective glass. They are one of the most flexible ways to calculate the rendering effects on graphics hardware, most of which are primarily coded for the GPU (Otherwise known as the graphics processing unit). Programmers use shaders in order to program the programmable GPU rendering pipeline (which you can learn more about HERE) in order to get around and exceed the fixed functionality of the pipeline. 
images/pipeline.png
GPU Graphics Pipeline
By using shaders we are able to use customizable effects and alter things such as position, hue, saturation, brightness, and contrast of all pixels, vertices and textures at the drop of a hat. 

TYPE OF SHADERS

 

There are a lot of different types of shaders, however I will only be focusing on three types of shaders in this blog, which are The Vertex Shader, Pixel Shader and the Geometry shader.

VERTEX SHADER

http://img.tomshardware.com/us/2002/01/16/vertex_shaders_and_pixel_shaders/materialsfinal.jpg

Vertex shaders are one of the main shaders used in computer graphics which are run once per vertex depending on the graphics processor. Their purpose is to transform the 3D position of each vector into the 2D coordinate that will appear on the screen. The vertex shader is responsible for the manipulation of position, colour and texture coordinates, and can enable powerful control over the position, movement, lighting, and colour of any 3D scene. 

The following are just a few simple examples of some vertex shader code:

#version 120
void main()
{
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

This is a simple pass through shader that passes the model view projection matrix and the vertex through to a position.  The next example is a draw attribute vertex shader:

#version 120
varying vec3 passVertex;
varying vec3 passNormal;
varying vec2 passTexcoord;
void main()
{
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            passVertex = gl_Vertex.xyz;
            passNormal = gl_Normal;
            passTexcoord = gl_MultiTexCoord0.st;
}

PIXEL SHADER

http://www.andrespagella.com/img/articles/understanding-css-custom-filters/fig10.png

The pixel shader, otherwise known as the fragment shader, computes colour and other attributes on a per pixel bases rather than on the vertices like the vertex shader does. The fragment shader is responsible for changing the look and feel of colors, applying lighting value, translucency, shadows, bump mapping, among other effects. The fragment shader also has the ability to alter the depth of the fragment (for Z-buffering) or output for multiple target rendering. Now while the pixel shader cannot produce very complex effects it does have the knowledge of the screen coordinates being drawn and can sample the screen and nearby pixels, which can enable a large variety  of 2D post processing effects such as blur, edge detection and cell shading. 

The following are just a few simple examples of some Pixel (fragment) shader code:

#version 120
void main()
{
//this sets all targets to the same colour
            gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);
}

The is a simple fragment shader pass through that sets all targets to the same color, while the following code is an example of a simple fragment shader that draws the attributes:

#version 120
varying vec3 passVertex;
varying vec3 passNormal;
varying vec2 passTexcoord;
void main()
{
            //vec3 colour = passVertex*0.5 + 0.5;
            vec3 colour = passNormal*0.5 + 0.5;
            //vec3 colour = vec3(passTexcoord, 0.0);
            gl_FragColor = vec4(colour, 1.0);
}

GEOMETRY SHADER

http://www.pcper.com/images/reviews/319/dx10_03.jpg

The last shader I will be talking briefly about in this blog will be the Geometry shader, which is a relatively new shader in the OpenGL language that has the ability to generate new graphics primitives, such as points, lines and triangles, from the ones that were sent to the beginning of the GPU pipeline. Geometry shaders are executed after the vertex shader and take the whole primitive as well as additional information as its input. This type of shader is an optional shader and does not have to be used.

Now I mentioned a few post processing techniques which I will discuss more in a later blog, my next blog will focus on lighting.