summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--flake.lock27
-rw-r--r--flake.nix57
-rw-r--r--src/1/main.c62
4 files changed, 150 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..03f2f20
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
1.direnv
2.ccls
3.clang-format
4.ccls-cache
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..d24a185
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,27 @@
1{
2 "nodes": {
3 "nixpkgs": {
4 "locked": {
5 "lastModified": 1633083543,
6 "narHash": "sha256-thXKms0SvDirYb8/Hw0zqAE1TsuSFWd0y51mSzkPGLU=",
7 "owner": "NixOS",
8 "repo": "nixpkgs",
9 "rev": "92609f3d9bc3acffbdbe54fa1c591a885612aa73",
10 "type": "github"
11 },
12 "original": {
13 "owner": "NixOS",
14 "ref": "nixos-21.05",
15 "repo": "nixpkgs",
16 "type": "github"
17 }
18 },
19 "root": {
20 "inputs": {
21 "nixpkgs": "nixpkgs"
22 }
23 }
24 },
25 "root": "root",
26 "version": 7
27}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..21b787c
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,57 @@
1{
2 description = "A very basic flake";
3
4 inputs = {
5 nixpkgs.url = "github:NixOS/nixpkgs/nixos-21.05";
6 };
7
8 outputs =
9 { self
10 , nixpkgs
11 }:
12 let
13 allPrograms = [ 1 ];
14 supportedSystems = [ "x86_64-linux" ];
15 forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
16 nixpkgsFor = forAllSystems (system:
17 import nixpkgs { inherit system; }
18 );
19 in
20 {
21 devShell = forAllSystems
22 (system:
23 let
24 pkgs = nixpkgsFor."${system}";
25 in
26 with pkgs;
27 mkShell {
28 buildInputs = [ gcc glew glfw glm freeglut mesa_glu clang-tools ];
29 });
30
31 apps = forAllSystems
32 (system:
33 let
34 pkgs = nixpkgsFor."${system}";
35 execs = with builtins; map toString allPrograms;
36 mkApp = name: with pkgs; stdenv.mkDerivation {
37 name = "${name}";
38 src = ./src;
39 buildInputs = [ gcc glew glfw glm freeglut mesa_glu ];
40 unpackPhase = ''
41 true
42 '';
43 buildPhase = ''
44 gcc -o main $src/${name}/main.c -lglut -lGLU -lGL
45 '';
46 installPhase = ''
47 install -Dm755 main $out/bin/main
48 '';
49 };
50 in
51 with pkgs;
52 lib.genAttrs execs (p: {
53 type = "app";
54 program = "${mkApp p}/bin/main";
55 }));
56 };
57}
diff --git a/src/1/main.c b/src/1/main.c
new file mode 100644
index 0000000..f75eca5
--- /dev/null
+++ b/src/1/main.c
@@ -0,0 +1,62 @@
1#include <GL/glut.h>
2#include <stdlib.h>
3#include <time.h>
4
5typedef GLfloat point2[2];
6
7float rand_float() { return 1 * (float)rand() / (float)RAND_MAX; }
8
9void myInit() {
10 glClearColor(1.0, 1.0, 1.0, 1.0);
11 glColor3f(rand_float(), rand_float(), rand_float());
12 glMatrixMode(GL_PROJECTION);
13 glLoadIdentity();
14 gluOrtho2D(0.0, 500.0, 0.0, 500.0);
15 glMatrixMode(GL_MODELVIEW);
16}
17
18void triangle(point2 a, point2 b, point2 c) {
19 glBegin(GL_TRIANGLES);
20 glVertex2fv(a);
21 glVertex2fv(b);
22 glVertex2fv(c);
23 glEnd();
24}
25
26void divide_triangle(point2 a, point2 b, point2 c, int k) {
27 point2 ab, ac, bc;
28 int j;
29 if (k > 0) {
30 for (j = 0; j < 2; j++)
31 ab[j] = (a[j] + b[j]) / 2;
32 for (j = 0; j < 2; j++)
33 ac[j] = (a[j] + c[j]) / 2;
34 for (j = 0; j < 2; j++)
35 bc[j] = (b[j] + c[j]) / 2;
36
37 divide_triangle(a, ab, ac, k - 1);
38 divide_triangle(c, ac, bc, k - 1);
39 divide_triangle(b, bc, ab, k - 1);
40 } else {
41 triangle(a, b, c);
42 }
43}
44
45void display() {
46 glClear(GL_COLOR_BUFFER_BIT);
47 point2 v[3] = {{0.0, 0.0}, {250.0, 500.0}, {500.0, 0.0}};
48 divide_triangle(v[0], v[1], v[2], 8);
49 glFlush();
50}
51
52int main(int argc, char *argv[]) {
53 srand(time(0));
54 glutInit(&argc, argv);
55 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
56 glutInitWindowSize(500, 500);
57 glutInitWindowPosition(0, 0);
58 glutCreateWindow("Sierpinski Gasket");
59 glutDisplayFunc(display);
60 myInit();
61 glutMainLoop();
62}