Skip to the content.

Ray Casting

Vision Process

In human vision process is as follow

  1. Lights drop on objects
  2. Then it is reflected (scattered) in all directions
  3. Reflected light rays are received by the eye and image is formed.

The same is for the pinhole camera model

Ideally

So how we simulate this in computer graphics ? Keep in mind that the objective is to get a 2D image of the scene.

Ray Casting

Basic Idea The process is reversed where rays are sent from the camera position instead of receive it at the camera position.

Pseudo code for ray casting

for each pixel:
    Send a ray through the scene
    Find the interection with objects 
    Calculate the color at this intersection 
    Assign this color to that pixel where ray was sent from

Ray Tracing

Sample of generated images with ray tracing (source)

Lighting and materials

Classical lighting model

Enabling lightening in OpenGL

// Here we have a white light source
float light_ambient[] = {1.0, 1.0, 1.0, 1.0};
float light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
float light_specular[] = {1.0, 1.0, 1.0, 1.0};
// This is the light position
float light_position[] = {0.0, 0.0, 4.0, 1.0};
// Setting light source properties and enabling it
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);

glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);

Defining material properties.

GLfloat material_ambient[] = {0.1, 0.01, 0.01, 1.0},
        material_diffuse[] = {0.3, 0.01, 0.01, 1.0},
        material_specular[] = {1, 0.01, 0.01, 1.0},
        material_shininess = 30;

 glMaterialfv(GL_FRONT, GL_AMBIENT, material_ambient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);
  glMaterialf(GL_FRONT, GL_SHININESS, material_shininess);

Red light sphere Demo

This is an example of a solid sphere

No light sources

Only ambient is set

Ambient, diffuse

Ambient, diffuse, and specular

Note

The overall appearance of the object is determined by both material and light properties.

Shading models

There are two types of shading:

glShadeModel(GL_FLAT);

By default, shading is smooth.

Demo

Demos are here.

References:

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLight.xml