This lab adds some sparkle to the cylinder you developed in the last lab. You'll shade the cylinder using the Blinn-Phong illumination model. Additionally, you'll get some practice reading in a file.
Your first challenge is to load in a configuration file like this:
{
"albedo": [1, 1, 0],
"position": [0, 0, -6],
"ambientFactor": 0.2,
"diffuseColor": [1, 0.5, 1],
"specularColor": [1, 1, 1],
"shininess": 99
}
The format of this file is JSON (JavaScript Object Notation), which is a plain text format for describing integers, floats, booleans, strings, null, arrays, and key-value pairs. This particular JSON file describes the lighting and position properties of your cylinder. Follow these steps to pull this file into your renderer.
fetch
call. If the file is plain text, you read it with code like this:
async function readTextFromFile(file) {
const text = await fetch(file).then(response => response.text());
return text;
}
But the file is JSON, so it must be parsed into a real JavaScript object. Instead of calling the text
method, you call json
:
async function readObjectFromFile(file) {
const object = await fetch(file).then(response => response.json());
return object;
}
The actual fetching of the file takes place in the background. Sometimes you need the fetched content before you can do anything else. In that case, then you can effectively force the browser to block using the await
keyword. Awaiting is only legal if the function is marked async
.
The initialize
function might read in a JSON file like this:
function initialize() {
const configuration = await readObjectFromFile('configuration.json');
// ...
}
position
property to translate the cylinder. Use a translation matrix, but make sure the current rotation happens first.
Your second challenge is to shade the the cylinder using the Blinn-Phong illumination model. Follow these steps: