www.xbdev.net
xbdev - software development
Friday February 20, 2026
Home | Contact | Support | Computer Graphics Powerful and Beautiful ...
     
 

Physically-Based
Rendering

Lights and Rays ...

 



[TOC] Chapter 4: Camera


The camera is important because it determines how a scene is presented to the viewer, shaping their perception of space, depth, and composition. By defining the perspective and field of view, it controls what is visible and how it appears, ensuring the visual output aligns with the desired artistic or functional intent.

Camera Model


The camera model simulates how a physical camera works by defining how rays are cast into a 3D scene and how these rays intersect with objects to form an image. The camera acts as the viewer's eye, projecting rays from its position through each pixel of the image plane and into the 3D world to determine what is visible from that point of view.

The essential elements of a camera model in ray tracing are:

Position: Where the camera is placed in the 3D space.
Orientation: The direction the camera is facing (usually given by an eye or view direction).
Image Plane: A 2D plane where the scene will be projected.
Field of View (FoV): The angular extent of the scene that is visible through the camera.
Aspect Ratio: The width-to-height ratio of the image.

A simple pinhole camera model can be described by casting rays from the camera origin through the image plane. The rays follow this equation:

\[
\text{Ray}(t) = \text{cameraPosition} + t \cdot \text{rayDirection}
\]

Example Code: Simple Pinhole Camera Model in JavaScript


class Camera {
    
constructor(cameraPositionlookAtupVectorfovaspectRatio) {
        
this.position cameraPosition;
        
this.aspectRatio aspectRatio;
        
this.fov fov;

        const 
normalize(subtract(cameraPositionlookAt)); // View direction
        
const normalize(cross(upVectorw)); // Right direction
        
const cross(wu); // Up direction

        
const halfHeight Math.tan(fov 2);
        const 
halfWidth aspectRatio halfHeight;

        
this.lowerLeftCorner subtract(
            
subtract(subtract(this.positionscale(uhalfWidth)),
                
scale(vhalfHeight)),
            
w
        
);
        
this.horizontal scale(uhalfWidth);
        
this.vertical scale(vhalfHeight);
    }

    
getRay(uv) {
        return new 
Ray(
            
this.position,
            
add(
                
add(this.lowerLeftCornerscale(this.horizontalu)),
                
subtract(scale(this.verticalv), this.position)
            )
        );
    }
}


Projective Camera Models


Projective camera models introduce perspective projection, which mimics how a real camera or the human eye perceives depth. In a projective camera model, parallel lines converge at a vanishing point, creating the illusion of depth.

A perspective projection matrix is often used to transform 3D points into a normalized 2D image plane with the following matrix equation:

\[
P_{\text{perspective}} =
\begin{bmatrix}
\frac{1}{\tan(\frac{fov}{2}) \cdot \text{aspect}} & 0 & 0 & 0 \\
0 & \frac{1}{\tan(\frac{fov}{2})} & 0 & 0 \\
0 & 0 & \frac{z_{\text{far}} + z_{\text{near}}}{z_{\text{near}} - z_{\text{far}}} & \frac{2 \cdot z_{\text{far}} \cdot z_{\text{near}}}{z_{\text{near}} - z_{\text{far}}} \\
0 & 0 & -1 & 0
\end{bmatrix}
\]

This matrix defines how a point in 3D is transformed into 2D coordinates on the screen. The focal length and field of view affect how the depth is perceived.

Example Code: Projective Camera Model in JavaScript


ript
class ProjectiveCamera {
    
constructor(fovaspectnearfar) {
        
this.projectionMatrix = [
            [
/ (aspect Math.tan(fov 2)), 000],
            [
0Math.tan(fov 2), 00],
            [
00, (near far) / (near far), (near far) / (near far)],
            [
00, -10]
        ];
    }

    
project(point) {
        const [
xyzw] = multiplyMatrixAndPoint(this.projectionMatrixpoint);
        return [
ww];
    }
}


Environment Camera


An environment camera captures the entire surrounding scene in a spherical (or sometimes cubic) format. This type of camera is typically used in rendering environments, such as generating 360-degree panoramas or applying environment mapping techniques in shaders.

For an environment camera, rays are projected outward in all directions, effectively capturing a 360-degree view. In a ray-tracing context, this means that the ray direction is determined based on the pixels position on a sphere surrounding the camera.

Spherical Coordinates

To generate rays for an environment camera, we can use spherical coordinates. The direction of the ray is defined by two angles: (azimuthal angle) and (polar angle).

\[
x = \sin(\phi) \cos(\theta)
\]
\[
y = \cos(\phi)
\]
\[
z = \sin(\phi) \sin(\theta)
\]

Example Code: Environment Camera in JavaScript


class EnvironmentCamera {
    
constructor(position) {
        
this.position position;
    }

    
getRay(uv) {
        const 
theta Math.PI u// Azimuth angle
        
const phi Math.PI v// Polar angle

        
const direction = new Vector3(
            
Math.sin(phi) * Math.cos(theta),
            
Math.cos(phi),
            
Math.sin(phi) * Math.sin(theta)
        );

        return new 
Ray(this.positiondirection);
    }
}


Realistic Cameras


Realistic cameras aim to simulate the physical attributes of real-world cameras, such as lens distortion, aperture size, and depth of field. These cameras go beyond simple pinhole models by incorporating lens systems that refract light according to real-world optics principles. The key parameters are:

Aperture: Determines how much light is allowed through, affecting the exposure and depth of field.
Lens System: A series of lenses that can bend and focus light.
Focal Length: The distance between the lens and the image plane, affecting the magnification and field of view.

The equation for depth of field, influenced by the aperture and focal length, is:

\[
b = \frac{f \cdot s}{s - f}
\]
Where:
- \( b \) is the image distance (where the image is focused).
- \( f \) is the focal length.
- \( s \) is the subject distance (distance from the camera to the object being photographed).

Example Code: Realistic Camera in JavaScript


class RealisticCamera {
    
constructor(lensRadiusfocalDistance) {
        
this.lensRadius lensRadius;
        
this.focalDistance focalDistance;
    }

    
getRay(uvsensorPoint) {
        const 
lensPoint randomPointInDisk(this.lensRadius);
        const 
focusPoint scale(subtract(sensorPointlensPoint), this.focalDistance);
        const 
direction normalize(subtract(focusPointlensPoint));
        return new 
Ray(lensPointdirection);
    }
}

function 
randomPointInDisk(radius) {
    
let xy;
    do {
        
Math.random() - 1;
        
Math.random() - 1;
    } while (
>= 1);
    return new 
Vector3(radiusradius0);
}








Ray-Tracing with WebGPU kenwright WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright wgsl webgpugems shading language cookbook kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.