Skip to content

Instantly share code, notes, and snippets.

@nixterrimus
Created May 12, 2021 21:24
Show Gist options
  • Save nixterrimus/45f5de076e07ba087dffe012cc2f7730 to your computer and use it in GitHub Desktop.
Save nixterrimus/45f5de076e07ba087dffe012cc2f7730 to your computer and use it in GitHub Desktop.
import * as fs from "fs"
import * as path from "path"
import * as v from "./util/vector"
type OutputImage = {
width: number,
height: number,
maxColor: number
}
function colorOutput(image: OutputImage, color: v.Vector3): string {
const r = Math.round(color.x * image.maxColor)
const g = Math.round(color.y * image.maxColor)
const b = Math.round(color.z * image.maxColor)
return (`${r} ${g} ${b}\n`)
}
function OutputImageByAspectRatio(aspectRatio: number, width: number): OutputImage{
return {
width: width,
height: Math.round(width * aspectRatio),
maxColor: 2000
}
}
function main(){
const image = OutputImageByAspectRatio(16/9, 400)
var output = `P3
${image.width} ${image.height}
${image.maxColor}
`
console.log("Rendering")
for (var row=0; row<image.height; row++){
console.log(`\tRendering row: ${row} of ${image.height}`)
for (var col=0; col<image.height; col++){
const color = v.Color(
row / (image.width-1),
col / (image.height-1),
(0.25 * image.maxColor))
output += colorOutput(image, color)
}
}
const scriptName = path.basename(__filename);
const outputBaseName = scriptName.split(".")[0]
const outputName = `output/${outputBaseName}.ppm`
console.log(`Writing to: ${outputName}`);
fs.writeFileSync(outputName, output);
}
main();
@nixterrimus
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment