---
title: "Lighting in OpenGL"
subject: "Computer Graphics"
chapter: 9
key: "semester_5/computer_graphics:opengl-lighting"
source: https://www.bctengineer.com/q/semester_5/computer_graphics/opengl-lighting
site: BCT Engineering
---

# Lighting in OpenGL

Computer Graphics · Chapter 9: Introduction to Open GL

- Asked in: 2079 Baisakh, 4 marks; 2075 Chaitra, 3 marks (asked 2 times)
- Video: [OpenGL Tutorial 9 - Lighting](https://www.youtube.com/watch?v=ZbszezwNSZU) — Victor Gordan
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_5/computer_graphics/opengl-lighting
- Whole chapter: https://www.bctengineer.com/md/semester_5/computer_graphics/chapter-9

## Question

How can we use lighting in OpenGL (how is lighting applied to a polygon surface)?

## Short answer

To apply lighting, enable GL_LIGHTING and a light, set its position and color components (ambient, diffuse, specular), define the surface’s material properties (ambient/diffuse/specular colors and shininess), and specify a unit normal for each polygon or vertex before its vertices. OpenGL then shades the polygon according to the Blinn-Phong reflection model.

## Answer

OpenGL lighting is based on the Phong illumination model (ambient + diffuse + specular); fixed-function OpenGL actually uses the Blinn–Phong form, which computes the specular term with the halfway vector H. To use it, we must specify the lights, the material of the surfaces and the surface normals.

**Steps**
1. Enable lighting, a light and depth testing:
```c
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);          // at least 8 lights: GL_LIGHT0 ... GL_LIGHT7
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);      // Gouraud shading (GL_FLAT for flat)
```
2. Define the light properties:
```c
GLfloat pos[]     = {1.0f, 1.0f, 1.0f, 0.0f};  // w = 0: directional, w = 1: point
GLfloat ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
GLfloat diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specular[]= {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightfv(GL_LIGHT0, GL_AMBIENT,  ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE,  diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
```
3. Define the material of the polygon (reflection coefficients):
```c
GLfloat mat_d[] = {0.8f, 0.2f, 0.2f, 1.0f};
GLfloat mat_s[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_d);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_s);
glMaterialf (GL_FRONT, GL_SHININESS, 50.0f);   // exponent n
```
4. Give the unit normal of each polygon (or vertex) before its vertices, since lighting uses N:
```c
glBegin(GL_POLYGON);
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0, 0, 0); glVertex3f(1, 0, 0);
    glVertex3f(1, 1, 0); glVertex3f(0, 1, 0);
glEnd();
```

OpenGL then calculates the colour of each vertex using the ambient, diffuse and specular terms and interpolates it over the polygon (Gouraud shading when GL_SMOOTH is used). If `glEnable(GL_COLOR_MATERIAL)` is used, `glColor` sets the material colour.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. On which model is OpenGL lighting based?
   A. Ray tracing only
   B. Phong illumination model (ambient + diffuse + specular)
   C. Flat colour only
   D. Radiosity only
   Answer: B. OpenGL uses ambient, diffuse and specular terms. (Key point: Lighting model)

2. Which calls enable lighting and the first light?
   A. glLightfv alone
   B. glShadeModel(GL_FLAT)
   C. glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0)
   D. glBegin and glEnd
   Answer: C. Lighting and each light must be enabled. (Key point: Enabling)

3. In the position array {x,y,z,w}, what does w = 0 mean?
   A. A point light
   B. A spotlight
   C. The light is off
   D. A directional light
   Answer: D. w = 1 gives a point light. (Key point: Light position)

4. Why must glNormal3f be given before the vertices?
   A. Lighting calculations use the surface normal N
   B. It sets the colour
   C. It sets the viewing volume
   D. It starts the polygon
   Answer: A. Without normals the lighting is wrong. (Key point: Normals)

5. What does glShadeModel(GL_SMOOTH) give?
   A. Flat shading
   B. Gouraud shading
   C. Phong shading
   D. No shading
   Answer: B. GL_FLAT gives flat shading. (Key point: Smooth shading)

## Related in Computer Graphics

- [Wire-frame model and polygon table](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Awireframe-model-polygon-table) — Computer Graphics, chapter 6
- [Spatial orientation / normal of a polygon](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Aspatial-orientation-polygon-calculation) — Computer Graphics, chapter 6, asked 5×
- [Spatial orientation and polygon tables](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Aspatial-orientation-surface-significance) — Computer Graphics, chapter 6
- [Polygon table and normal of S1](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Apolygon-table-surface-normal-s1) — Computer Graphics, chapter 6
- [Model curved surface using polygons](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acurved-surface-polygons-tables) — Computer Graphics, chapter 6

## Related in other subjects

- [Dielectric stress maximum at conductor surface](https://www.bctengineer.com/question.md?key=semester_4%2Fpower_system_analysis_i%3Adielectric-stress-max-surface) — Power System Analysis I, chapter 2, asked 2×
- [Control of sub-surface water and seepage](https://www.bctengineer.com/question.md?key=semester_6%2Ftransportation_engineering_i%3Asubsurface-drainage-measures) — Transportation Engineering I, chapter 4, asked 15×
