What is Computer Graphics?

Computer graphics is the creation, manipulation, and display of visual content using computers. It involves representing and rendering geometric data.

Key Applications

Games
Animation
CAD
Medical Imaging
VR & AR

Evolution Timeline

1960s: Sketchpad
Raster Graphics
OpenGL
GPU Era
VR / AR
Real-time Ray Tracing

World Coordinates

The mathematical coordinate space where the scene is defined.

Orthographic 2D Projection

Shows 2D graphics without making faraway objects look smaller. It keeps everything the same size regardless of depth.

Window

A selected rectangular region of the world space to be displayed.

Viewport

The sub-region of the screen/display where the window is mapped.

Screen Coordinates

The actual physical pixels on the device monitor.

Window-to-Viewport Mapping

To convert a point (x, y) in the world window to a point (sx, sy) in the screen viewport, we use proportional mapping:

(x - W_L) / (W_R - W_L) = (sx - V_L) / (V_R - V_L)

(y - W_B) / (W_T - W_B) = (sy - V_B) / (V_T - V_B)

Where W = Window, V = Viewport, L=Left, R=Right, T=Top, B=Bottom.

OpenGL Implementation

In OpenGL, the coordinate system origin (0,0) is natively at the lower-left corner. Setting up the mapping requires three tasks:

  1. Define World Window: gluOrtho2D(left, right, bottom, top); (preceded by glMatrixMode(GL_PROJECTION); glLoadIdentity();)
  2. Define Viewport: glViewport(left, bottom, width, height);
  3. Draw: Execute glBegin() and glEnd() calls which are then automatically mapped.

Interactive Mapping Demo

Drag the World Window to see how it affects the Viewport.

World Space
Window
★
➔
Screen / Viewport
★
Mapping calculations will appear here...

How it Works

DDA uses floating-point arithmetic to calculate the next pixel on a line by incrementally adding the slope to either x or y. It relies on two key mathematical functions:

  • Floor Function: Converts real values into integers (e.g., +8.5 to 8, -8.5 to -9).
  • Sign Function: Returns +1, 0, or -1 depending on whether the argument is >0, =0, or <0.

  • Compute dx = x2 - x1 and dy = y2 - y1
  • steps = max(|dx|, |dy|)
  • xInc = dx / steps, yInc = dy / steps

Characteristics

Advantages:

  • Simplest algorithm, no special skills required.
  • Faster than directly using the standard line equation.

Disadvantages:

  • Orientation dependence makes end point accuracy poor.
  • Time-consuming floating-point arithmetic.
  • Precision errors shift calculated points on long lines.

Interactive DDA Visualization

dx = -, dy = -, steps = -
xInc = -, yInc = -
Step x y Rounded (X,Y)

Complexity & Exam Tips

Time Complexity: O(N)
Where N is the number of steps (max of dx or dy).
Space Complexity: O(1)
Only a few variables are stored.
Advantages: Simple. Eliminates loop multiplication.
Disadvantages: Floating point arithmetic is slow. Rounding error.
Exam Tip: Examiners love asking why DDA is slower than Bresenham. Mention floating-point addition and the `round()` function overhead.

Decision Parameter

Bresenham's algorithm uses a decision parameter P to choose between the East (E) or North-East (NE) pixel.

HINT: Pk < 0 → just increment x
Pk ≥ 0 → increment both x and y
  • Initial P₀: 2dy - dx
  • If P < 0: Choose E pixel. P_{k+1} = P_k + 2dy
  • If P ≥ 0: Choose NE pixel. P_{k+1} = P_k + 2dy - 2dx

Solved Example: (2, 5) to (10, 8)

dy = 3, dx = 8, 2dy = 6, 2dx = 16
Initial P₀ = -2

Since P₀ < 0 → P₁ = -2 + 6 = 4. Pixel: (3, 5)

DDA vs Bresenham

Metric DDA Bresenham
Speed Slower Faster
Accuracy Rounding errors Exact
Arithmetic Floating-point Integer only

Interactive Bresenham Visualization

dx = -, dy = -
Current P = -
Chosen Pixel: -
k Pk (Xk+1, Yk+1)

Complexity & Exam Tips

Time Complexity: O(N)
N is dx (the driving axis).
Space Complexity: O(1)
Uses only integer variables.
Advantages: Fast, integer-only. No rounding errors. Exact pixels.
Disadvantages: More complex initial logic than DDA.
Exam Tip: Be ready to trace the Decision Parameter (Pk) table. If Pk is negative, only X increments. If Pk is positive, both X and Y increment!

Common Clipping Strategies

  • Check every point: Correct but slow.
  • Clip coordinates directly: Not accurate.
  • Compute intersections with boundaries: The correct method (used in Cohen-Sutherland).

Outcodes

Every point is assigned a 4-bit binary code (TBRL) indicating its position relative to the clipping window (Top, Bottom, Right, Left).

1001 | 1000 | 1010
------------------
0001 | 0000 | 0010
------------------
0101 | 0100 | 0110

Algorithm Steps

  1. Compute outcodes for P1 and P2.
  2. Trivially Accept: Both outcodes are 0000.
  3. Trivially Reject: Logical AND of outcodes != 0000.
  4. Clip: Find intersection with window boundary and repeat.

Interactive Cohen-Sutherland Visualization

Drag the line endpoints to see how the outcodes change and whether the line is accepted, rejected, or clipped.

Drag points to begin.
P1 Outcode: -
P2 Outcode: -
Status: Waiting...

Complexity & Exam Tips

Time Complexity: O(1) to O(K)
Very fast for trivial cases.
Space Complexity: O(1)
Outcodes are just 4 bits.
Advantages: Extremely fast for trivially accepted/rejected lines.
Disadvantages: Needs intersection math if not trivial.
Exam Tip: The Outcode sequence is always TBRL (Top, Bottom, Right, Left). Memorize that `AND != 0000` means trivial rejection.

Four Cases for Each Edge

When traversing from vertex V1 to V2 against a clipping boundary:

  • Inside → Inside: Add V2.
  • Inside → Outside: Add Intersection (I).
  • Outside → Outside: Add nothing.
  • Outside → Inside: Add Intersection (I) and V2.

Algorithm Flow

The entire polygon is clipped against the Left edge, producing a new polygon. This new polygon is then clipped against the Right edge, then Top, then Bottom.

Interactive Sutherland-Hodgman Visualization

Initial polygon loaded.
Waiting for edge clipping...

Complexity & Exam Tips

Time Complexity: O(N * E)
N vertices against E clip edges.
Space Complexity: O(N)
Intermediate polygons must be stored.
Advantages: Works well for convex clipping regions. Simple pipeline.
Disadvantages: Creates extraneous edges for concave polygons.
Exam Tip: Know the 4 intersection cases perfectly! Outside → Inside generates TWO points (Intersection + V2).

Phong Reflection Model

Combines three components to simulate light interacting with a surface:

  • Ambient: Constant background light scattered everywhere. Defined by coefficients kar, kag, kab (amounts of red, green, blue reflected).
  • Diffuse: Light scattered equally in all directions, representing a dull material appearance.
  • Specular: Highlight from reflected light, representing a shiny appearance.
Total = Ambient + Diffuse + Specular

Shading Models

  • Flat Shading: One normal per polygon. Blocky appearance.
  • Gouraud (Smooth): Normals at vertices, interpolate colors.
  • Phong Shading: Interpolate normals, compute lighting per pixel. Best quality.

Interactive Phong Shading Preview

Ambient
+
Diffuse
+
Specular

Final Combined Render

Complexity & Exam Tips

Time Complexity: O(P)
P is number of pixels for Phong shading.
Space Complexity: O(1)
Per-pixel calculation.
Advantages: Most realistic basic shading.
Disadvantages: Computationally expensive without GPU.
Exam Tip: Be able to write the full Phong equation: I = Ka*Ia + Kd*Id*(N.L) + Ks*Is*(R.V)^n

UV Coordinates

Textures use a 2D coordinate system (U, V) ranging from 0.0 to 1.0. These are mapped to the (X, Y, Z) vertices of 3D geometry.

(U=0, V=0) = Bottom-Left
(U=1, V=1) = Top-Right

OpenGL Workflow

  • glGenTextures() - Name the texture
  • glBindTexture() - Bind texture to its name
  • glTexParameteri() - Specify texture parameters
  • glTexEnvf() - Specify application mode
  • glTexImage2D() - Create the texture object
  • glEnable(GL_TEXTURE_2D) - Enable texture mapping

Interactive Texture Mapping Demo

1. Select a 2D Texture (UV Space)

(0,0) (1,0) U→ (0,1) V↑ (1,1)

2. Mapped onto 3D Object

Complexity & Exam Tips

Time Complexity: O(P)
Per-pixel texture lookup.
Space Complexity: O(T)
Where T is the size of the texture image in memory.
Advantages: Adds immense realism without geometry overhead.
Disadvantages: Can look pixelated up close (needs filtering).
Exam Tip: Always remember UV coordinates go from 0.0 to 1.0 regardless of actual image pixel size.

Elements of Realism

  • Geometry: High polygon counts for detail.
  • Lighting: Global illumination and shadows.
  • Materials: Physically Based Rendering (PBR).
  • Textures: High resolution and normal mapping.

Interactive Realism Configurator

Status: Immersive realism enabled. The scene feels lifelike.

Run Comparison

DDA Algorithm

Bresenham Algorithm

Why do they look different?

DDA uses round() which can pull the pixel selection in slightly different directions compared to Bresenham's exact integer Pk decision, especially on lines with specific fractional slopes. Bresenham is considered the mathematically "truer" representation of the ideal line.

DDA Line Algorithm

dx = x2 - x1
dy = y2 - y1
steps = max(|dx|, |dy|)

xInc = dx / steps
yInc = dy / steps

x = x + xInc
y = y + yInc
Plot( round(x), round(y) )

Bresenham's Line

dx = |x2 - x1|
dy = |y2 - y1|

Initial P₀ = 2dy - dx

If Pk < 0:
   Next P = Pk + 2dy
   x = x + 1

If Pk ≥ 0:
   Next P = Pk + 2dy - 2dx
   x = x + 1
   y = y + 1

Cohen-Sutherland Clipping

Outcode Sequence: TBRL
(Top, Bottom, Right, Left)

TOP    = 1000
BOTTOM = 0100
RIGHT  = 0010
LEFT   = 0001

Accept if: Code1 OR Code2 == 0000
Reject if: Code1 AND Code2 != 0000

Phong Reflection Model

I = Ambient + Diffuse + Specular

I = (Ka * Ia) + 
    (Kd * Id * (N·L)) + 
    (Ks * Is * (R·V)^n)

Ka, Kd, Ks = Material Constants
Ia, Id, Is = Light Intensities
n = Shininess
N = Normal, L = Light Dir
R = Reflection, V = View Dir

Q: Why is DDA less accurate than Bresenham?

A: DDA uses floating-point arithmetic. Successive additions of fractional numbers (xInc, yInc) cause rounding errors to accumulate, eventually pushing the plotted pixel off the ideal mathematical line. Bresenham uses exact integer mathematics.

Q: Why is Bresenham faster?

A: It relies entirely on integer addition, subtraction, and bit-shifting (multiplying by 2). It avoids expensive floating-point operations and the `round()` function.

Q: What is an outcode in Cohen-Sutherland?

A: A 4-bit binary code assigned to a point that describes its position relative to the viewing window (Top, Bottom, Right, Left). It allows for rapid bitwise AND/OR operations to accept or reject lines.

Q: Explain the difference between Flat, Gouraud, and Phong shading.

A:
- Flat: Calculates light once per polygon. Very blocky.
- Gouraud: Calculates light at vertices and interpolates colors across the polygon. Smoother, but misses specular highlights in the center of large polygons.
- Phong: Interpolates normal vectors across the polygon and calculates the full lighting equation for every single pixel. Most realistic, most expensive.

Q: What happens if the Aspect Ratio of the World Window doesn't match the Viewport?

A: The resulting image on the screen will be stretched or squashed (distorted). To maintain proportional geometry, `(Window Width / Window Height)` must equal `(Viewport Width / Viewport Height)`.

1. Generate Problem

Click two points on the grid to draw a line using DDA.

Algorithm Inspector

Enter values to instantly get the steps for exam verification.

Result will appear here...

Quick Exam Revision Notes

  • DDA vs Bresenham: Bresenham is faster, uses integers only.
  • Outcodes: Top(1000), Bottom(0100), Right(0010), Left(0001).
  • Phong Model: Ambient + Diffuse + Specular.
  • World to Viewport Mapping: Aspect ratio must match to avoid distortion.
  • Texture Mapping: Uses (U,V) coordinates from 0.0 to 1.0.

Knowledge Quiz

1. Which algorithm uses floating-point arithmetic?



2. In Cohen-Sutherland, a line is trivially rejected if the logical AND of its outcodes is:



Exam Readiness

0%

Formula: 40% Progress + 40% Challenge Accuracy + 20% Quiz Performance

Detailed Breakdown

Topics Completed: 0 / 16
Challenge Accuracy: 0%
Best Quiz Score: 0%

Best Topic: N/A
Weakest Topic: N/A