Skip to content

Instantly share code, notes, and snippets.

@ceberly
Created November 20, 2020 22:44
Show Gist options
  • Save ceberly/c4813311ffe1ce2efe405e87ad0154d9 to your computer and use it in GitHub Desktop.
Save ceberly/c4813311ffe1ce2efe405e87ad0154d9 to your computer and use it in GitHub Desktop.
Ray Tracing in One Weekend, but you can see it in html
# run this in the same directory as the other files
# then navigate to localhost:8000/
python -m http.server 8000
<html>
<head>
<style>
body {
background-color: rgba(0, 0, 0, 0.05);
margin: 1rem;
}
img {
/* adjust to taste */
height: 200px;
width: 400px;
}
</style>
</head>
<body>
<div>
<img src="out.png" />
</div>
</body>
</html>
#include <stdlib.h>
/* Get (https://twitter.com/nothings)'s image writer header lib */
/* https://github.com/nothings/stb/blob/master/stb_image_write.h */
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int main(void) {
int nx = 200;
int ny = 100;
unsigned char data[nx * ny * 3];
for (int j = ny - 1; j >= 0; j--) {
for (int i = 0; i < nx; i++) {
float r = i / (float)nx;
float g = j / (float)ny;
float b = 0.2;
unsigned char ir = (unsigned char)(255.99*r);
unsigned char ig = (unsigned char)(255.99*g);
unsigned char ib = (unsigned char)(255.99*b);
data[j * nx * 3 + (i * 3) + 0] = ir;
data[j * nx * 3 + (i * 3) + 1] = ig;
data[j * nx * 3 + (i * 3) + 2] = ib;
}
}
stbi_flip_vertically_on_write(1);
stbi_write_png("out.png", nx, ny, 3, data, 0);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment