Knowledge / Computer Science and Engineering / Computer graphics basics

Computer graphics basics

Computer Science and Engineering (B.E./B.Tech) · Theory and architecture · free study notes for exams, placements and interviews

In short

Computer graphics is the study of creating, storing, transforming and displaying pictures with a computer - from drawing a single line of pixels to rendering a shaded 3D scene. It rests on three pillars: scan-conversion algorithms (line, circle, fill), geometric transformations in homogeneous coordinates, and the viewing pipeline (clipping, projection, hidden-surface removal, shading). It is a full university paper in most CSE curricula, a regular GATE/UGC-NET source of 2-3 mark numericals on Bresenham and transformation matrices, and it is tested directly in interviews at gaming and visual-computing employers (Ubisoft Pune, Rockstar India, Zynga, Nvidia Bengaluru, Qualcomm, AMD, Samsung R&D, Adobe, Autodesk, ISRO visualisation groups) and indirectly in any front-end, CAD or embedded-display role.

Explained

Display hardware, raster fundamentals and colour

Scan conversion - lines, circles and ellipses

Filling, polygons and clipping

Two-dimensional transformations and homogeneous coordinates

Three dimensions - transformations, projection and curves

Hidden surfaces, illumination and shading

Key points

Formulas and examples

Frame buffer size (bytes) = (horizontal pixels × vertical pixels × bits per pixel) / 8
Memory bandwidth for refresh (bytes/s) = frame buffer size × refresh rate (Hz)
DDA: slope m = (y2 − y1)/(x2 − x1); for |m| ≤ 1, x(k+1) = x(k) + 1, y(k+1) = y(k) + m
Bresenham (|m| ≤ 1): p0 = 2Δy − Δx; if p(k) < 0 → p(k+1) = p(k) + 2Δy, y unchanged; else p(k+1) = p(k) + 2Δy − 2Δx, y increases by 1
Midpoint circle: p0 = 1 − r; if p(k) < 0 → p(k+1) = p(k) + 2x(k+1) + 1; else p(k+1) = p(k) + 2x(k+1) + 1 − 2y(k+1)
2D translation: x' = x + tx, y' = y + ty (tx, ty in the same length units as x, y)
2D scaling about origin: x' = sx·x, y' = sy·y (sx, sy dimensionless; sx = sy is uniform scaling)
2D rotation about origin by θ (anticlockwise): x' = x cos θ − y sin θ, y' = x sin θ + y cos θ
Homogeneous rotation matrix R(θ) = [[cos θ, −sin θ, 0], [sin θ, cos θ, 0], [0, 0, 1]]
Rotation about pivot (xp, yp): M = T(xp, yp) · R(θ) · T(−xp, −yp)
Window-to-viewport: xv = xvmin + (xw − xwmin)·sx, yv = yvmin + (yw − ywmin)·sy, with sx = (xvmax − xvmin)/(xwmax − xwmin), sy = (yvmax − yvmin)/(ywmax − ywmin)
Perspective projection onto z = 0 with centre of projection at z = −d: xp = x/(1 + z/d), yp = y/(1 + z/d)
Bezier curve: P(u) = Σ(i = 0 to n) Pi · BEZ(i,n)(u), where BEZ(i,n)(u) = C(n,i)·ui·(1 − u)n − i, 0 ≤ u ≤ 1
Phong illumination: I = ka·Ia + kd·Il·(N·L) + ks·Il·(R·V)ns (k values are reflection coefficients 0-1; N, L, R, V are unit vectors; ns is the specular exponent)
Plane equation for culling: Ax + By + Cz + D = 0; normal N = (A, B, C); back face if N · Vview ≥ 0

Example 1 - Frame buffer and bandwidth. A 1920 × 1080 display at 24 bits per pixel, refreshed at 60 Hz.

Example 2 - Bresenham line from (20, 10) to (30, 18). Δx = 10, Δy = 8, m = 0.8 ≤ 1, so step x. p₀ = 2Δy − Δx = 16 − 10 = 6. Increments: 2Δy = 16, 2Δy − 2Δx = 16 − 20 = −4.

kpₖnext pixelpₖ₊₁
06(21, 11)6 − 4 = 2
12(22, 12)2 − 4 = −2
2−2(23, 12)−2 + 16 = 14
314(24, 13)14 − 4 = 10
410(25, 14)10 − 4 = 6
56(26, 15)6 − 4 = 2
62(27, 16)2 − 4 = −2
7−2(28, 16)−2 + 16 = 14
814(29, 17)14 − 4 = 10
910(30, 18)-

Pixels: (20,10), (21,11), (22,12), (23,12), (24,13), (25,14), (26,15), (27,16), (28,16), (29,17), (30,18) - 11 pixels, y rising by 8 over 10 x-steps, as the slope requires.

Example 3 - Midpoint circle, r = 10, first octant. p₀ = 1 − 10 = −9, start (0, 10).

kpₖchosen (x, y)pₖ₊₁
0−9(1, 10)−9 + 2(1) + 1 = −6
1−6(2, 10)−6 + 2(2) + 1 = −1
2−1(3, 10)−1 + 2(3) + 1 = 6
36(4, 9)6 + 2(4) + 1 − 2(9) = −3
4−3(5, 9)−3 + 2(5) + 1 = 8
58(6, 8)8 + 2(6) + 1 − 2(8) = 5
65(7, 7)x ≥ y, stop

Octant pixels: (0,10), (1,10), (2,10), (3,10), (4,9), (5,9), (6,8), (7,7); the other 48 points follow by reflecting x and y.

Example 4 - Rotating a triangle 90° anticlockwise about the point (2, 2). Vertices A(4, 2), B(6, 2), C(4, 5). With θ = 90°, cos θ = 0, sin θ = 1, so the composite gives x' = 2 − (y − 2) = 4 − y and y' = 2 + (x − 2) = x.

Example 5 - Window-to-viewport mapping. Window (xwmin, ywmin) = (0, 0) to (10, 10); viewport (0, 0) to (400, 300) device pixels. Map the world point (6, 4).

Example 6 - Perspective projection. Centre of projection at z = −d with d = 50, projection plane z = 0. A point P(30, 20, 100).

AspectDDABresenham
ArithmeticFloating pointInteger only
Operations per pixel1 add + round1 add + compare
AccuracyRound-off accumulatesExact decision variable
Speed and hardware fitSlowerFaster, easy in hardware
AspectGouraud shadingPhong shading
Interpolated quantityVertex intensitiesVertex normals
Highlight qualityMisses highlights inside a polygonRenders highlights correctly
CostLowerHigher (shading per pixel)
Mach bandsReduced but visibleLargely removed
AspectBezierB-spline (and NURBS)
DegreeFixed by control point count (n points → degree n−1)Independent of control point count
ControlGlobalLocal
Conic sectionsApproximateExact with NURBS weights
Typical useFonts, vector illustration, animation pathsCAD/CAM surfaces, product design
/* Bresenham line, |m| <= 1, x1 < x2, integer only */
void bres_line(int x1,int y1,int x2,int y2){
    int dx = x2-x1, dy = y2-y1;
    int sy = (dy >= 0) ? 1 : -1;          /* handle downward lines */
    dy = (dy >= 0) ? dy : -dy;
    int p = 2*dy - dx, x = x1, y = y1;
    setpixel(x,y);
    while (x < x2) {
        x++;
        if (p < 0) p += 2*dy;
        else { y += sy; p += 2*dy - 2*dx; }
        setpixel(x,y);
    }
}

Where it is used

Interview questions

Common mistakes

Related topics

Parallel computing and GPUs · Computer organisation and architecture · Linear algebra and matrices · Image processing and computer vision · Numerical methods and interpolation · Data structures (trees for BSP and spatial indexing)

Open in the Knowledge reader →Find jobs →