From 59096e47e51620587e29a49f0e9c7621e35c084b Mon Sep 17 00:00:00 2001 From: Natsirt867 Date: Mon, 25 Aug 2025 02:55:20 -0500 Subject: [PATCH] Updating changes, long overdue --- assets/cube.obj | 48 ++++ assets/f22.obj | 707 ++++++++++++++++++++++++++++++++++++++++++++++++ src/array.c | 40 +++ src/array.h | 14 + src/display.c | 21 +- src/display.h | 3 +- src/loader.c | 23 ++ src/loader.h | 0 src/main.c | 190 +++++++++---- src/mesh.c | 90 ++++++ src/mesh.h | 26 ++ src/triangle.c | 3 + src/triangle.h | 19 ++ src/vector.c | 133 ++++++++- src/vector.h | 25 +- 15 files changed, 1272 insertions(+), 70 deletions(-) create mode 100644 assets/cube.obj create mode 100644 assets/f22.obj create mode 100644 src/array.c create mode 100644 src/array.h create mode 100644 src/loader.c create mode 100644 src/loader.h create mode 100644 src/mesh.c create mode 100644 src/mesh.h create mode 100644 src/triangle.c create mode 100644 src/triangle.h diff --git a/assets/cube.obj b/assets/cube.obj new file mode 100644 index 0000000..4e3c3f9 --- /dev/null +++ b/assets/cube.obj @@ -0,0 +1,48 @@ + +# cube.obj +# + +mtllib cube.mtl +o cube + +v -1.000000 -1.000000 1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 1.000000 1.000000 +v 1.000000 1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +v 1.000000 1.000000 -1.000000 +v -1.000000 -1.000000 -1.000000 +v 1.000000 -1.000000 -1.000000 + +vt 1.000000 0.000000 +vt 0.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 + +vn 0.000000 0.000000 1.000000 +vn 0.000000 1.000000 0.000000 +vn 0.000000 0.000000 -1.000000 +vn 0.000000 -1.000000 0.000000 +vn 1.000000 0.000000 0.000000 +vn -1.000000 0.000000 0.000000 + +g cube +usemtl cube +s 1 +f 1/1/1 2/2/1 3/3/1 +f 3/3/1 2/2/1 4/4/1 +s 2 +f 3/1/2 4/2/2 5/3/2 +f 5/3/2 4/2/2 6/4/2 +s 3 +f 5/4/3 6/3/3 7/2/3 +f 7/2/3 6/3/3 8/1/3 +s 4 +f 7/1/4 8/2/4 1/3/4 +f 1/3/4 8/2/4 2/4/4 +s 5 +f 2/1/5 8/2/5 4/3/5 +f 4/3/5 8/2/5 6/4/5 +s 6 +f 7/1/6 1/2/6 5/3/6 +f 5/3/6 1/2/6 3/4/6 diff --git a/assets/f22.obj b/assets/f22.obj new file mode 100644 index 0000000..3f4bf9e --- /dev/null +++ b/assets/f22.obj @@ -0,0 +1,707 @@ +# Blender v2.79 (sub 0) OBJ File: 'fighters_0.blend' +# www.blender.org +mtllib f-22.mtl +o F-22 +v 0.885739 0.001910 -0.380334 +v 0.322026 0.035470 -0.433102 +v -0.907053 0.017201 -0.352922 +v 1.047128 0.048117 -0.183264 +v 0.362277 0.114239 -0.142520 +v 1.125941 0.131116 -0.101088 +v 1.199510 0.242528 -0.056765 +v 1.529867 0.098373 -0.067674 +v 1.566378 0.011323 -0.146393 +v 1.762624 0.050007 -0.049581 +v 1.789931 -0.007335 -0.103633 +v -1.226703 0.083896 -0.231401 +v -1.226703 0.083896 -0.043034 +v -1.304689 0.083896 -0.142520 +v -0.560223 0.071221 -0.299735 +v 0.779234 -0.185506 -0.231401 +v -0.408812 -0.168000 -0.231401 +v -1.226703 -0.023036 -0.043034 +v -1.226703 -0.023036 -0.231464 +v -1.304556 -0.023036 -0.142520 +v -0.442164 -0.009523 -0.406435 +v -0.875984 0.035470 -1.161559 +v -0.727243 0.035470 -1.316844 +v -0.494141 0.035470 -1.316844 +v -1.376274 0.035470 -0.874979 +v -1.417826 0.035470 -0.043034 +v 1.016889 0.247035 -0.041089 +v -0.910201 0.071705 -0.260759 +v -0.910201 0.114239 -0.142520 +v -1.347072 0.053931 -0.291132 +v -0.949411 0.058716 -0.324121 +v -1.086356 0.618995 -0.582384 +v -0.945513 0.618995 -0.582384 +v -0.820765 0.618995 -0.582384 +v -1.074394 0.035470 -0.526090 +v -1.347072 0.017010 -0.291132 +v -1.420792 0.035470 -0.231401 +v -1.704863 0.035470 -0.554642 +v -1.607890 0.035470 -0.874979 +v 1.142508 0.156204 -0.091107 +v 0.821684 0.218761 -0.028011 +v 1.453805 0.131564 -0.065163 +v 0.835981 0.180467 -0.042103 +v 0.885739 0.001910 0.380334 +v 0.322026 0.035470 0.433102 +v -0.907053 0.017201 0.352922 +v 1.047128 0.048117 0.183264 +v 0.362277 0.114239 0.142520 +v 1.125941 0.131116 0.101088 +v 1.199510 0.242528 0.056765 +v 1.529867 0.098373 0.067674 +v 1.566378 0.011323 0.146393 +v 1.762624 0.050007 0.049581 +v 1.789931 -0.007335 0.103633 +v -1.226703 0.083896 0.231401 +v -1.226703 0.083896 0.043034 +v -1.304689 0.083896 0.142520 +v -0.560223 0.071221 0.299735 +v 0.779234 -0.185506 0.231401 +v -0.408812 -0.168000 0.231401 +v -1.226703 -0.023036 0.043034 +v -1.226703 -0.023036 0.231464 +v -1.304556 -0.023036 0.142520 +v -0.442164 -0.009523 0.406435 +v -0.875984 0.035470 1.161559 +v -0.727243 0.035470 1.316844 +v -0.494141 0.035470 1.316844 +v -1.376274 0.035470 0.874979 +v -1.417826 0.035470 0.043034 +v 1.016889 0.247035 0.041089 +v -0.910201 0.071705 0.260759 +v -0.910201 0.114239 0.142520 +v -1.347072 0.053931 0.291132 +v -0.949411 0.058716 0.324121 +v -1.086356 0.618995 0.582384 +v -0.945513 0.618995 0.582384 +v -0.820765 0.618995 0.582384 +v -1.074394 0.035470 0.526090 +v -1.347072 0.017010 0.291132 +v -1.420792 0.035470 0.231401 +v -1.704863 0.035470 0.554642 +v -1.607890 0.035470 0.874979 +v 1.142508 0.156204 0.091107 +v 0.821684 0.218761 0.028011 +v 1.453805 0.131564 0.065163 +v 0.835981 0.180467 0.042103 +v -0.910201 0.094558 0.000000 +v 0.403590 0.158204 0.000000 +v 1.236021 0.268974 0.000000 +v 1.568094 0.113851 0.000000 +v 1.791648 0.062518 0.000000 +v 2.014232 -0.030625 0.000000 +v 1.566378 -0.120168 0.000000 +v 1.789931 -0.082458 0.000000 +v 0.919486 -0.169810 0.000000 +v -0.408812 -0.167592 0.000000 +v -1.226703 0.078200 0.000000 +v -1.226703 -0.023036 0.000000 +v -1.442453 0.035470 0.000000 +v 1.016889 0.266178 0.000000 +v 1.487051 0.151710 0.000000 +v 0.817113 0.231006 0.000000 +vt 0.302940 0.699066 +vt 0.442081 0.762279 +vt 0.260042 0.751449 +vt 0.780315 0.800162 +vt 0.431099 0.800162 +vt 0.239093 0.773292 +vt 0.316239 0.628907 +vt 0.320312 0.648438 +vt 0.269531 0.648438 +vt 0.268080 0.668033 +vt 0.209833 0.668033 +vt 0.152344 0.648438 +vt 0.218750 0.648438 +vt 0.234686 0.622186 +vt 0.122021 0.761249 +vt 0.069858 0.786983 +vt 0.062599 0.772615 +vt 0.121565 0.800162 +vt 0.131726 0.782173 +vt 0.002978 0.800162 +vt 0.062143 0.800162 +vt 0.687288 0.720490 +vt 0.780315 0.762279 +vt 0.117476 0.888175 +vt 0.176898 0.858183 +vt 0.176898 0.893135 +vt 0.057855 0.881984 +vt 0.117476 0.868207 +vt 0.314919 0.902915 +vt 0.348847 0.844988 +vt 0.350939 0.448650 +vt 0.338144 0.510748 +vt 0.289431 0.452822 +vt 0.329981 0.132766 +vt 0.645773 0.071258 +vt 0.645773 0.132766 +vt 0.452780 0.685040 +vt 0.669723 0.450134 +vt 0.731683 0.450134 +vt 0.771220 0.491410 +vt 0.823959 0.660323 +vt 0.507656 0.899553 +vt 0.357817 0.890633 +vt 0.386127 0.840816 +vt 0.701919 0.845469 +vt 0.896439 0.722777 +vt 0.916034 0.738294 +vt 0.864444 0.738294 +vt 0.445843 0.497454 +vt 0.469468 0.469031 +vt 0.469468 0.497454 +vt 0.495912 0.497454 +vt 0.654638 0.182832 +vt 0.668454 0.424826 +vt 0.451511 0.189920 +vt 0.654638 0.179292 +vt 0.778209 0.165068 +vt 0.822690 0.211097 +vt 0.864444 0.469031 +vt 0.864444 0.497454 +vt 0.916034 0.484582 +vt 0.863174 0.082019 +vt 0.863174 0.071258 +vt 0.913977 0.082019 +vt 0.864444 0.800162 +vt 0.864444 0.789083 +vt 0.809306 0.497454 +vt 0.809306 0.469031 +vt 0.860108 0.484582 +vt 0.915246 0.789083 +vt 0.921792 0.800162 +vt 0.822690 0.214637 +vt 0.769951 0.383550 +vt 0.730414 0.424826 +vt 0.834355 0.894697 +vt 0.919320 0.884002 +vt 0.141653 0.276624 +vt 0.107458 0.425551 +vt 0.038204 0.279948 +vt 0.178054 0.425551 +vt 0.140617 0.425551 +vt 0.883868 0.109141 +vt 0.863174 0.132783 +vt 0.780315 0.730850 +vt 0.904201 0.567586 +vt 0.790737 0.714008 +vt 0.991542 0.652734 +vt 0.385137 0.280077 +vt 0.361364 0.425551 +vt 0.394523 0.425551 +vt 0.431960 0.425551 +vt 0.501261 0.275352 +vt 0.895170 0.148643 +vt 0.914765 0.132766 +vt 0.902932 0.303834 +vt 0.990273 0.218686 +vt 0.964497 0.303834 +vt 0.965766 0.567586 +vt 0.885173 0.762279 +vt 0.151941 0.583698 +vt 0.239090 0.583579 +vt 0.234686 0.590248 +vt 0.316239 0.596969 +vt 0.431096 0.590779 +vt 0.321182 0.614322 +vt 0.319967 0.606876 +vt 0.431099 0.614322 +vt 0.143107 0.601016 +vt 0.131726 0.583028 +vt 0.321182 0.668033 +vt 0.121565 0.601016 +vt 0.131723 0.574876 +vt 0.292110 0.279948 +vt 0.247354 0.275352 +vt 0.710784 0.887594 +vt 0.920523 0.071258 +vt 0.495912 0.469031 +vt 0.445826 0.469031 +vt 0.292701 0.071258 +vt 0.390527 0.498466 +vt 0.143107 0.668033 +vt 0.302940 0.699066 +vt 0.260042 0.751449 +vt 0.442081 0.762279 +vt 0.239093 0.773292 +vt 0.316239 0.628907 +vt 0.269531 0.648438 +vt 0.320312 0.648438 +vt 0.218750 0.648438 +vt 0.152344 0.648438 +vt 0.234686 0.622186 +vt 0.122021 0.761249 +vt 0.062599 0.772615 +vt 0.069858 0.786983 +vt 0.131726 0.782173 +vt 0.687288 0.720490 +vt 0.780315 0.762279 +vt 0.117476 0.888175 +vt 0.176898 0.893135 +vt 0.314919 0.902915 +vt 0.350939 0.448650 +vt 0.338144 0.510748 +vt 0.329981 0.132766 +vt 0.645773 0.132766 +vt 0.452780 0.685040 +vt 0.731683 0.450134 +vt 0.669723 0.450134 +vt 0.823959 0.660323 +vt 0.771220 0.491410 +vt 0.507656 0.899553 +vt 0.386127 0.840816 +vt 0.357817 0.890633 +vt 0.701919 0.845469 +vt 0.896439 0.722777 +vt 0.864444 0.738294 +vt 0.916034 0.738294 +vt 0.445843 0.497454 +vt 0.469468 0.497454 +vt 0.469468 0.469031 +vt 0.495912 0.497454 +vt 0.654638 0.182832 +vt 0.451511 0.189920 +vt 0.668454 0.424826 +vt 0.654638 0.179292 +vt 0.822690 0.211097 +vt 0.778209 0.165068 +vt 0.864444 0.469031 +vt 0.916034 0.484582 +vt 0.864444 0.497454 +vt 0.863174 0.082019 +vt 0.913977 0.082019 +vt 0.864444 0.789083 +vt 0.809306 0.497454 +vt 0.860108 0.484582 +vt 0.809306 0.469031 +vt 0.915246 0.789083 +vt 0.822690 0.214637 +vt 0.769951 0.383550 +vt 0.730414 0.424826 +vt 0.834355 0.894697 +vt 0.919320 0.884002 +vt 0.141653 0.276624 +vt 0.038204 0.279948 +vt 0.107458 0.425551 +vt 0.140617 0.425551 +vt 0.178054 0.425551 +vt 0.883868 0.109141 +vt 0.863174 0.132783 +vt 0.780315 0.730850 +vt 0.904201 0.567586 +vt 0.790737 0.714008 +vt 0.991542 0.652734 +vt 0.385137 0.280077 +vt 0.394523 0.425551 +vt 0.361364 0.425551 +vt 0.501261 0.275352 +vt 0.431960 0.425551 +vt 0.895170 0.148643 +vt 0.914765 0.132766 +vt 0.902932 0.303834 +vt 0.964497 0.303834 +vt 0.990273 0.218686 +vt 0.965766 0.567586 +vt 0.885173 0.762279 +vt 0.151941 0.583698 +vt 0.234686 0.590248 +vt 0.239090 0.583579 +vt 0.316239 0.596969 +vt 0.319967 0.606876 +vt 0.131726 0.583028 +vt 0.131723 0.574876 +vt 0.292110 0.279948 +vt 0.247354 0.275352 +vt 0.710784 0.887594 +vt 0.495912 0.469031 +vt 0.445826 0.469031 +vt 0.390527 0.498466 +vn 0.0752 0.9554 -0.2856 +vn -0.0464 0.9582 -0.2821 +vn -0.0672 0.7335 -0.6764 +vn -0.1079 0.3076 -0.9454 +vn -0.1576 0.8951 -0.4170 +vn 0.3158 0.7660 -0.5600 +vn 0.1032 0.3081 -0.9457 +vn 0.0996 0.6507 -0.7527 +vn 0.1879 0.7196 -0.6685 +vn 0.2166 0.9180 -0.3323 +vn 0.3493 0.7254 -0.5931 +vn 0.3509 0.8385 -0.4170 +vn 0.0000 0.9645 -0.2639 +vn 0.0656 -0.7424 -0.6668 +vn 0.1839 -0.7958 -0.5769 +vn 0.0506 -0.6600 -0.7496 +vn 0.6508 -0.6744 -0.3487 +vn -0.0147 -0.9999 0.0018 +vn -0.0004 0.9652 -0.2616 +vn 0.0000 0.9994 -0.0351 +vn -0.0610 0.9980 -0.0190 +vn 0.0346 -0.6338 -0.7728 +vn -0.0101 -0.6854 -0.7281 +vn -0.2421 0.9703 0.0011 +vn -0.7517 -0.0009 -0.6595 +vn -0.7870 -0.0010 0.6169 +vn 0.0569 -0.9970 -0.0525 +vn -0.0625 -0.9970 -0.0448 +vn 0.0001 -0.0006 1.0000 +vn -0.0313 0.9991 -0.0300 +vn -0.1740 -0.9847 0.0000 +vn -0.2927 -0.9562 0.0000 +vn -0.0512 0.9901 0.1310 +vn 0.0000 0.0000 -1.0000 +vn -0.1931 0.9749 -0.1105 +vn -0.0670 -0.9975 -0.0209 +vn -0.0388 -0.9986 -0.0372 +vn -0.1372 -0.7744 -0.6176 +vn 0.0702 -0.4308 -0.8997 +vn 0.0000 -0.4186 -0.9082 +vn -0.1725 -0.9734 -0.1510 +vn -0.1596 -0.9872 0.0017 +vn -0.1795 -0.9737 0.1405 +vn 0.0048 0.9410 -0.3385 +vn 0.0000 0.9906 0.1368 +vn 0.0387 0.9987 -0.0335 +vn -0.0258 0.9948 -0.0985 +vn -0.0061 0.8987 -0.4386 +vn -0.0204 0.9946 -0.1018 +vn -0.1436 0.9816 0.1262 +vn 0.0000 0.5067 0.8621 +vn -0.0803 0.4857 0.8704 +vn -0.2881 -0.9557 -0.0602 +vn 0.0387 -0.9987 -0.0335 +vn -0.0421 -0.9990 -0.0128 +vn -0.0126 -0.9956 -0.0929 +vn -0.0646 -0.8840 -0.4629 +vn -0.0000 0.9995 -0.0316 +vn 0.0000 -0.9995 -0.0316 +vn -0.0766 0.9953 0.0600 +vn -0.0765 0.9948 -0.0671 +vn -0.0519 0.9893 0.1366 +vn -0.1094 0.4290 -0.8967 +vn 0.3481 0.8307 -0.4344 +vn -0.0681 0.4073 -0.9107 +vn -0.1215 0.3445 -0.9309 +vn -0.0116 0.9064 -0.4223 +vn -0.0132 0.9096 -0.4153 +vn 0.3865 0.8274 -0.4075 +vn -0.1436 -0.9816 0.1262 +vn 0.0970 0.4924 0.8649 +vn -0.0421 0.9990 -0.0128 +vn -0.0694 -0.4316 -0.8994 +vn -0.1182 -0.7472 -0.6540 +vn -0.2436 0.9615 0.1273 +vn -0.2588 -0.9545 -0.1481 +vn 0.0000 -0.9988 -0.0494 +vn -0.7875 0.0000 0.6163 +vn -0.7525 0.0004 -0.6586 +vn 0.0200 -0.7392 -0.6731 +vn 0.0355 0.9988 -0.0328 +vn -0.0017 -0.9976 0.0687 +vn 0.6468 -0.6647 -0.3739 +vn -0.0053 -0.7439 -0.6682 +vn 0.1353 -0.8022 -0.5815 +vn -0.0364 0.9403 -0.3383 +vn 0.2094 0.9120 -0.3527 +vn 0.1990 0.7019 -0.6839 +vn 0.1150 0.6924 -0.7123 +vn 0.3792 0.8117 -0.4444 +vn 0.0728 0.9469 -0.3132 +vn 0.0825 0.9589 -0.2714 +vn 0.0752 0.9554 0.2856 +vn -0.0464 0.9582 0.2821 +vn -0.0672 0.7335 0.6764 +vn -0.1079 0.3076 0.9454 +vn -0.1576 0.8951 0.4170 +vn 0.3158 0.7660 0.5600 +vn 0.1032 0.3081 0.9457 +vn 0.0996 0.6507 0.7527 +vn 0.1879 0.7196 0.6685 +vn 0.2166 0.9180 0.3323 +vn 0.3493 0.7254 0.5931 +vn 0.3509 0.8385 0.4170 +vn 0.0000 0.9645 0.2639 +vn 0.0656 -0.7424 0.6668 +vn 0.1839 -0.7958 0.5769 +vn 0.0506 -0.6600 0.7496 +vn 0.6508 -0.6744 0.3487 +vn -0.0147 -0.9999 -0.0018 +vn -0.0004 0.9652 0.2616 +vn 0.0000 0.9994 0.0351 +vn -0.0610 0.9980 0.0190 +vn 0.0346 -0.6338 0.7728 +vn -0.0101 -0.6854 0.7281 +vn -0.2421 0.9703 -0.0011 +vn -0.7517 -0.0009 0.6595 +vn -0.7870 -0.0010 -0.6169 +vn 0.0569 -0.9970 0.0525 +vn -0.0625 -0.9970 0.0448 +vn 0.0001 -0.0006 -1.0000 +vn -0.0313 0.9991 0.0300 +vn -0.0512 0.9901 -0.1310 +vn 0.0000 0.0000 1.0000 +vn -0.1931 0.9749 0.1105 +vn -0.0670 -0.9975 0.0209 +vn -0.0388 -0.9986 0.0372 +vn -0.1372 -0.7744 0.6176 +vn 0.0702 -0.4308 0.8997 +vn 0.0000 -0.4186 0.9082 +vn -0.1725 -0.9734 0.1510 +vn -0.1596 -0.9872 -0.0017 +vn -0.1795 -0.9737 -0.1405 +vn 0.0048 0.9410 0.3385 +vn 0.0000 0.9906 -0.1368 +vn 0.0387 0.9987 0.0335 +vn -0.0258 0.9948 0.0985 +vn -0.0061 0.8987 0.4386 +vn -0.0204 0.9946 0.1018 +vn -0.1436 0.9816 -0.1262 +vn 0.0000 0.5067 -0.8621 +vn -0.0803 0.4857 -0.8704 +vn -0.2881 -0.9557 0.0602 +vn 0.0387 -0.9987 0.0335 +vn -0.0421 -0.9990 0.0128 +vn -0.0126 -0.9956 0.0929 +vn -0.0646 -0.8840 0.4629 +vn -0.0000 0.9995 0.0316 +vn 0.0000 -0.9995 0.0316 +vn -0.0766 0.9953 -0.0600 +vn -0.0765 0.9948 0.0671 +vn -0.0519 0.9893 -0.1366 +vn -0.1094 0.4290 0.8967 +vn 0.3481 0.8307 0.4344 +vn -0.0681 0.4073 0.9107 +vn -0.1215 0.3445 0.9309 +vn -0.0116 0.9064 0.4223 +vn -0.0132 0.9096 0.4153 +vn 0.3865 0.8274 0.4075 +vn -0.1436 -0.9816 -0.1262 +vn 0.0970 0.4924 -0.8649 +vn -0.0421 0.9990 0.0128 +vn -0.0694 -0.4316 0.8994 +vn -0.1182 -0.7472 0.6540 +vn -0.2436 0.9615 -0.1273 +vn -0.2588 -0.9545 0.1481 +vn 0.0000 -0.9988 0.0494 +vn -0.7875 0.0000 -0.6163 +vn -0.7525 0.0004 0.6586 +vn 0.0200 -0.7392 0.6731 +vn 0.0355 0.9988 0.0328 +vn -0.0017 -0.9976 -0.0687 +vn 0.6468 -0.6647 0.3739 +vn -0.0053 -0.7439 0.6682 +vn 0.1353 -0.8022 0.5815 +vn -0.0364 0.9403 0.3383 +vn 0.2094 0.9120 0.3527 +vn 0.1990 0.7019 0.6839 +vn 0.1150 0.6924 0.7123 +vn 0.3792 0.8117 0.4444 +vn 0.0728 0.9469 0.3132 +vn 0.0825 0.9589 0.2714 +usemtl Material.001 +s off +f 1/1/1 5/2/1 4/3/1 +f 5/2/2 87/4/2 88/5/2 +f 4/3/3 88/5/3 6/6/3 +f 43/7/4 41/8/4 27/9/4 +f 41/8/5 100/10/5 27/9/5 +f 89/11/6 42/12/6 7/13/6 +f 7/13/7 42/12/7 40/14/7 +f 6/6/8 9/15/8 4/3/8 +f 9/15/9 10/16/9 11/17/9 +f 90/18/10 10/16/10 8/19/10 +f 11/17/11 10/16/11 92/20/11 +f 10/16/12 91/21/12 92/20/12 +f 15/22/13 29/23/13 5/2/13 +f 11/24/14 93/25/14 9/26/14 +f 11/24/15 92/27/15 94/28/15 +f 4/29/16 93/25/16 95/30/16 +f 16/31/17 4/32/17 95/33/17 +f 16/34/18 96/35/18 17/36/18 +f 5/2/19 2/37/19 15/22/19 +f 15/22/20 24/38/20 23/39/20 +f 15/22/21 22/40/21 35/41/21 +f 2/42/22 1/43/22 16/44/22 +f 17/45/23 2/42/23 16/44/23 +f 30/46/24 37/47/24 12/48/24 +f 12/49/25 20/50/25 14/51/25 +f 20/50/26 13/52/26 14/51/26 +f 21/53/27 24/54/27 2/55/27 +f 21/56/28 3/57/28 35/58/28 +f 19/59/29 12/60/29 37/61/29 +f 15/22/30 23/39/30 22/40/30 +f 18/62/31 96/35/31 98/63/31 +f 98/63/32 26/64/32 18/62/32 +f 97/65/33 87/4/33 13/66/33 +f 13/67/34 18/68/34 26/69/34 +f 97/65/35 26/70/35 99/71/35 +f 35/72/36 22/73/36 21/53/36 +f 22/73/37 23/74/37 21/53/37 +f 3/75/38 17/45/38 19/76/38 +f 31/77/39 34/78/39 15/79/39 +f 31/77/40 32/80/40 33/81/40 +f 20/82/41 19/83/41 17/36/41 +f 20/82/42 17/36/42 96/35/42 +f 18/62/43 20/82/43 96/35/43 +f 29/23/44 28/84/44 12/48/44 +f 87/4/45 5/2/45 29/23/45 +f 25/85/46 30/46/46 35/41/46 +f 31/86/47 15/22/47 35/41/47 +f 30/46/48 12/48/48 28/84/48 +f 30/46/49 31/86/49 35/41/49 +f 30/46/50 38/87/50 37/47/50 +f 28/88/51 34/89/51 33/90/51 +f 28/88/52 32/91/52 30/92/52 +f 36/93/53 19/83/53 37/94/53 +f 36/93/54 25/95/54 35/58/54 +f 36/93/55 38/96/55 39/97/55 +f 36/93/56 35/58/56 3/57/56 +f 3/57/57 19/83/57 36/93/57 +f 25/85/58 39/98/58 30/46/58 +f 36/93/59 39/97/59 25/95/59 +f 29/23/60 14/99/60 13/66/60 +f 14/99/61 29/23/61 12/48/61 +f 87/4/62 29/23/62 13/66/62 +f 42/100/7 6/101/7 40/102/7 +f 43/103/63 6/101/63 88/104/63 +f 102/105/5 41/106/5 88/107/5 +f 101/108/64 8/109/64 42/100/64 +f 27/9/65 7/13/65 40/14/65 +f 40/14/66 43/7/66 27/9/66 +f 41/8/5 102/110/5 100/10/5 +f 100/10/67 89/11/67 27/9/67 +f 89/11/68 7/13/68 27/9/68 +f 101/108/69 90/111/69 8/109/69 +f 43/103/63 40/102/63 6/101/63 +f 88/104/4 41/106/4 43/103/4 +f 42/100/7 8/112/7 6/101/7 +f 36/93/70 37/94/70 38/96/70 +f 28/88/51 33/90/51 32/91/51 +f 28/88/71 15/113/71 34/89/71 +f 30/46/72 39/98/72 38/87/72 +f 31/77/73 30/114/73 32/80/73 +f 31/77/40 33/81/40 34/78/40 +f 3/75/74 21/115/74 17/45/74 +f 97/65/75 13/66/75 26/70/75 +f 98/63/76 99/116/76 26/64/76 +f 21/53/77 23/74/77 24/54/77 +f 20/50/78 18/117/78 13/52/78 +f 12/49/79 19/118/79 20/50/79 +f 17/45/80 21/115/80 2/42/80 +f 15/22/81 2/37/81 24/38/81 +f 16/34/82 95/119/82 96/35/82 +f 16/31/83 1/120/83 4/32/83 +f 4/29/84 9/26/84 93/25/84 +f 11/24/85 94/28/85 93/25/85 +f 15/22/86 28/84/86 29/23/86 +f 90/18/87 91/21/87 10/16/87 +f 9/15/88 8/19/88 10/16/88 +f 6/6/89 8/19/89 9/15/89 +f 89/11/90 101/121/90 42/12/90 +f 4/3/91 5/2/91 88/5/91 +f 1/1/92 2/37/92 5/2/92 +f 44/122/93 47/123/93 48/124/93 +f 48/124/94 88/5/94 87/4/94 +f 47/123/95 49/125/95 88/5/95 +f 86/126/96 70/127/96 84/128/96 +f 84/128/97 70/127/97 100/10/97 +f 89/11/98 50/129/98 85/130/98 +f 50/129/99 83/131/99 85/130/99 +f 49/125/100 47/123/100 52/132/100 +f 52/132/101 54/133/101 53/134/101 +f 90/18/102 51/135/102 53/134/102 +f 54/133/103 92/20/103 53/134/103 +f 53/134/104 92/20/104 91/21/104 +f 58/136/105 48/124/105 72/137/105 +f 54/138/106 52/139/106 93/25/106 +f 54/138/107 94/28/107 92/27/107 +f 47/140/108 95/30/108 93/25/108 +f 59/141/109 95/33/109 47/142/109 +f 59/143/110 60/144/110 96/35/110 +f 48/124/111 58/136/111 45/145/111 +f 58/136/112 66/146/112 67/147/112 +f 58/136/113 78/148/113 65/149/113 +f 45/150/114 59/151/114 44/152/114 +f 60/153/115 59/151/115 45/150/115 +f 73/154/116 55/155/116 80/156/116 +f 55/157/117 57/158/117 63/159/117 +f 63/159/118 57/158/118 56/160/118 +f 64/161/119 45/162/119 67/163/119 +f 64/164/120 78/165/120 46/166/120 +f 62/167/121 80/168/121 55/169/121 +f 58/136/122 65/149/122 66/146/122 +f 61/170/31 98/63/31 96/35/31 +f 98/63/32 61/170/32 69/171/32 +f 97/65/123 56/172/123 87/4/123 +f 56/173/124 69/174/124 61/175/124 +f 97/65/125 99/71/125 69/176/125 +f 78/177/126 64/161/126 65/178/126 +f 65/178/127 64/161/127 66/179/127 +f 46/180/128 62/181/128 60/153/128 +f 74/182/129 58/183/129 77/184/129 +f 74/182/130 76/185/130 75/186/130 +f 63/187/131 60/144/131 62/188/131 +f 63/187/132 96/35/132 60/144/132 +f 61/170/133 96/35/133 63/187/133 +f 72/137/134 55/155/134 71/189/134 +f 87/4/135 72/137/135 48/124/135 +f 68/190/136 78/148/136 73/154/136 +f 74/191/137 78/148/137 58/136/137 +f 73/154/138 71/189/138 55/155/138 +f 73/154/139 78/148/139 74/191/139 +f 73/154/140 80/156/140 81/192/140 +f 71/193/141 76/194/141 77/195/141 +f 71/193/142 73/196/142 75/197/142 +f 79/198/143 80/199/143 62/188/143 +f 79/198/144 78/165/144 68/200/144 +f 79/198/145 82/201/145 81/202/145 +f 79/198/146 46/166/146 78/165/146 +f 46/166/147 79/198/147 62/188/147 +f 68/190/148 73/154/148 82/203/148 +f 79/198/149 68/200/149 82/201/149 +f 72/137/150 56/172/150 57/204/150 +f 57/204/151 55/155/151 72/137/151 +f 87/4/152 56/172/152 72/137/152 +f 85/205/99 83/206/99 49/207/99 +f 86/208/153 88/104/153 49/207/153 +f 102/105/97 88/107/97 84/209/97 +f 101/108/154 85/205/154 51/210/154 +f 70/127/155 83/131/155 50/129/155 +f 83/131/156 70/127/156 86/126/156 +f 84/128/97 100/10/97 102/110/97 +f 100/10/157 70/127/157 89/11/157 +f 89/11/158 70/127/158 50/129/158 +f 101/108/159 51/210/159 90/111/159 +f 86/208/153 49/207/153 83/206/153 +f 88/104/96 86/208/96 84/209/96 +f 85/205/99 49/207/99 51/211/99 +f 79/198/160 81/202/160 80/199/160 +f 71/193/141 75/197/141 76/194/141 +f 71/193/161 77/195/161 58/212/161 +f 73/154/162 81/192/162 82/203/162 +f 74/182/163 75/186/163 73/213/163 +f 74/182/130 77/184/130 76/185/130 +f 46/180/164 60/153/164 64/214/164 +f 97/65/165 69/176/165 56/172/165 +f 98/63/166 69/171/166 99/116/166 +f 64/161/167 67/163/167 66/179/167 +f 63/159/168 56/160/168 61/215/168 +f 55/157/169 63/159/169 62/216/169 +f 60/153/170 45/150/170 64/214/170 +f 58/136/171 67/147/171 45/145/171 +f 59/143/172 96/35/172 95/119/172 +f 59/141/173 47/142/173 44/217/173 +f 47/140/174 93/25/174 52/139/174 +f 54/138/175 93/25/175 94/28/175 +f 58/136/176 72/137/176 71/189/176 +f 90/18/177 53/134/177 91/21/177 +f 52/132/178 53/134/178 51/135/178 +f 49/125/179 52/132/179 51/135/179 +f 89/11/180 85/130/180 101/121/180 +f 47/123/181 88/5/181 48/124/181 +f 44/122/182 48/124/182 45/145/182 diff --git a/src/array.c b/src/array.c new file mode 100644 index 0000000..bd576aa --- /dev/null +++ b/src/array.c @@ -0,0 +1,40 @@ +#include +#include +#include "array.h" + +#define ARRAY_RAW_DATA(array) ((int*)(array) - 2) +#define ARRAY_CAPACITY(array) (ARRAY_RAW_DATA(array)[0]) +#define ARRAY_OCCUPIED(array) (ARRAY_RAW_DATA(array)[1]) + +void* array_hold(void* array, int count, int item_size) { + if (array == NULL) { + int raw_size = (sizeof(int) * 2) + (item_size * count); + int* base = (int*)malloc(raw_size); + base[0] = count; // capacity + base[1] = count; // occupied + return base + 2; + } else if (ARRAY_OCCUPIED(array) + count <= ARRAY_CAPACITY(array)) { + ARRAY_OCCUPIED(array) += count; + return array; + } else { + int needed_size = ARRAY_OCCUPIED(array) + count; + int double_curr = ARRAY_CAPACITY(array) * 2; + int capacity = needed_size > double_curr ? needed_size : double_curr; + int occupied = needed_size; + int raw_size = sizeof(int) * 2 + item_size * capacity; + int* base = (int*)realloc(ARRAY_RAW_DATA(array), raw_size); + base[0] = capacity; + base[1] = occupied; + return base + 2; + } +} + +int array_length(void* array) { + return (array != NULL) ? ARRAY_OCCUPIED(array) : 0; +} + +void array_free(void* array) { + if (array != NULL) { + free(ARRAY_RAW_DATA(array)); + } +} diff --git a/src/array.h b/src/array.h new file mode 100644 index 0000000..cad327f --- /dev/null +++ b/src/array.h @@ -0,0 +1,14 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#define array_push(array, value) \ + do { \ + (array) = array_hold((array), 1, sizeof(*(array))); \ + (array)[array_length(array) - 1] = (value); \ + } while (0); + +void* array_hold(void* array, int count, int item_size); +int array_length(void* array); +void array_free(void* array); + +#endif diff --git a/src/display.c b/src/display.c index cc58614..6d6ac38 100644 --- a/src/display.c +++ b/src/display.c @@ -86,6 +86,12 @@ void draw_pixel(int x, int y, uint32_t color){ } } +void draw_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color) { + draw_line(x0, y0, x1, y1, color); + draw_line(x1, y1, x2, y2, color); + draw_line(x2, y2, x0, y0, color); +}; + void draw_grid(void){ for (int y = 0; y < window_height; y += 10){ for (int x = 0; x < window_width; x += 10){ @@ -94,28 +100,27 @@ void draw_grid(void){ } } -void draw_line(int x0, int y0, int x1, int y1) { +void draw_line(int x0, int y0, int x1, int y1, uint32_t color) { int delta_x = (x1 - x0); int delta_y = (y1 - y0); - int side_length = abs(delta_x) >= abs(delta_y) ? abs(delta_x) : abs(delta_y); + int longest_side_length = abs(delta_x) >= abs(delta_y) ? abs(delta_x) : abs(delta_y); // Find how much we should increment in both x and y each step - float x_inc = delta_x / (float)side_length; - float y_inc = delta_y / (float)side_length; + float x_inc = delta_x / (float)longest_side_length; + float y_inc = delta_y / (float)longest_side_length; float current_x = x0; float current_y = y0; - for (int i = 0; i <=side_length; i++) { - draw_pixel(round(current_x), round(current_y), 0xFFFFFF00); + for (int i = 0; i <= longest_side_length; i++) { + draw_pixel(round(current_x), round(current_y), color); current_x += x_inc; current_y += y_inc; } } -void destroy_window(void){ - free(color_buffer); +void destroy_window(void){ SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); diff --git a/src/display.h b/src/display.h index 2777b2e..7ec6577 100644 --- a/src/display.h +++ b/src/display.h @@ -17,10 +17,11 @@ extern int window_width; extern int window_height; bool initialize_window(void); -void draw_line(int x0, int y0, int x1, int y1); +void draw_line(int x0, int y0, int x1, int y1, uint32_t color); void draw_pixel(int x, int y, uint32_t color); void draw_rect(int x, int y, int width, int height, uint32_t color); void draw_grid(void); +void draw_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color); void render_color_buffer(void); void clear_color_buffer(uint32_t color); void destroy_window(void); diff --git a/src/loader.c b/src/loader.c new file mode 100644 index 0000000..2052f7e --- /dev/null +++ b/src/loader.c @@ -0,0 +1,23 @@ +/* +#include +#include + +char load_obj_file(const char* file_name){ + FILE* fp; + + fp = fopen(file_name, "r"); + + if (!fp) { + perror("Error opening file"); + } + + char* faces_loaded[100]; + int faces_count = 0; + + char line[1024]; + while (fgets(line, sizeof(line), fp)) { + if (line[0] == 'f') { + faces_loaded[faces_count++] = strdup(line); + } + } +}*/ diff --git a/src/loader.h b/src/loader.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 29d0a61..f99bc75 100644 --- a/src/main.c +++ b/src/main.c @@ -4,15 +4,16 @@ #include #include "display.h" #include "vector.h" +#include "mesh.h" +#include "triangle.h" +#include "array.h" -#define N_POINTS (9 * 9 * 9) +// Array of triangles that should be rendered frame by frame +triangle_t* triangles_to_render = NULL; -// Declare an array of vectors/points -vec3_t cube_points[N_POINTS]; // 9x9x9 cube -vec2_t projected_points[N_POINTS]; - -vec3_t camera_position = { .x = 0, .y = 0, .z = -5}; -vec3_t cube_rotation = { .x = 0, .y = 0, .z = 0}; +// Global variables for execution status and game loop +vec3_t camera_position = { 0, 0, 0 }; +//vec3_t cube_rotation = { .x = 0, .y = 0, .z = 0}; float fov_factor = 640; @@ -21,6 +22,7 @@ float fov_factor = 640; bool is_running = false; int previous_frame_time = 0; +// Setup function to initialize variables and game objects void setup(void){ // Allocates the memory in bytes required to hold for color_buffer, using sizeof to allocate the # of bytes required color_buffer = (uint32_t*) malloc(sizeof(uint32_t) * window_width * window_height); @@ -32,20 +34,12 @@ void setup(void){ SDL_TEXTUREACCESS_STREAMING, window_width, window_height - ); + ); - int point_count = 0; - - // Start loading my array of vectors - // From -1 to 1 (in this 9x9x9 cube) - for (float x = -1; x <= 1; x += 0.25) { - for (float y = -1; y <= 1; y += 0.25){ - for (float z = -1; z <= 1; z += 0.25){ - vec3_t new_point = { .x = x, .y = y, .z = z }; - cube_points[point_count++] = new_point; - } - } - } + // Loads the cube values in the mesh data structure + //load_cube_mesh_data(); + //load_obj_file_data("./assets/cube.obj"); + load_obj_file_data("./assets/cube.obj"); } void process_input(void){ @@ -59,7 +53,7 @@ void process_input(void){ case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) is_running = false; - break; + break; } } @@ -67,13 +61,15 @@ void process_input(void){ * Function that receives a 3D vector and returns a 2D point */ -vec2_t project(vec3_t point){ +vec2_t project(vec3_t point) { vec2_t projected_point = { - .x = (fov_factor * point.x) / point.z, + .x = (fov_factor * point.x) / point.z, .y = (fov_factor * point.y) / point.z }; + return projected_point; } +// Loads the cube values in the mesh data structure} void update(void){ //while (!SDL_TICKS_PASSED(SDL_GetTicks(), previous_frame_time + FRAME_TARGET_TIME)); @@ -85,25 +81,87 @@ void update(void){ previous_frame_time = SDL_GetTicks(); - cube_rotation.x += 0.01; - cube_rotation.y += 0.01; - cube_rotation.z += 0.01; + // Initialize the array of triangles to render + triangles_to_render = NULL; - for (int i = 0; i < N_POINTS; i++) { - vec3_t point = cube_points[i]; + mesh.rotation.x += 0.01; + mesh.rotation.y += 0.01; + mesh.rotation.z += 0.02; + + // Loop all triangle faces of our mesh + int num_faces = array_length(mesh.faces); + for (int i = 0; i < num_faces; i++) { + face_t mesh_face = mesh.faces[i]; + + vec3_t face_vertices[3]; + face_vertices[0] = mesh.vertices[mesh_face.a - 1]; + face_vertices[1] = mesh.vertices[mesh_face.b - 1]; + face_vertices[2] = mesh.vertices[mesh_face.c - 1]; + + //triangle_t projected_triangle; - vec3_t transformed_point = vec3_rotate_x(point, cube_rotation.x); - transformed_point = vec3_rotate_y(transformed_point, cube_rotation.y); - transformed_point = vec3_rotate_z(transformed_point, cube_rotation.z); + vec3_t transformed_vertices[3]; - // Translate point away from the camera - transformed_point.z -= camera_position.z; + // Loop all three vertices of this current face and apply transformations + for (int j = 0; j < 3; j++) { + vec3_t transformed_vertex = face_vertices[j]; + + transformed_vertex = vec3_rotate_x(transformed_vertex, mesh.rotation.x); + transformed_vertex = vec3_rotate_y(transformed_vertex, mesh.rotation.y); + transformed_vertex = vec3_rotate_z(transformed_vertex, mesh.rotation.z); - // Project the current point - vec2_t projected_point = project(transformed_point); - - // Save the projected 2D vector in the array of projected points - projected_points[i] = projected_point; + // Translate the vertex (point) away from the camera (in z) + transformed_vertex.z += 5; + + // Save the transformed vertex in the array of transformed vertices + transformed_vertices[j] = transformed_vertex; + } + + // TODO: Check backface culling + vec3_t vector_a = transformed_vertices[0]; /* A */ + vec3_t vector_b = transformed_vertices[1]; /* / \ */ + vec3_t vector_c = transformed_vertices[2]; /* C---B */ + + // Get the vector subtraction of B-A and C-A + vec3_t vector_ab = vec3_sub(vector_b, vector_a); + vec3_t vector_ac = vec3_sub(vector_c, vector_a); + + // Compute the face normal (using cross product to find perpendicular) + vec3_t normal = vec3_cross(vector_ab, vector_ac); + + // Normalize the face normal vector + vec3_normalize(&normal); + + // Find the vector between a point in the triangle and the camera origin + vec3_t camera_ray = vec3_sub(camera_position, vector_a); + + // Calculate how aligned the camera ray is with the face normal (using dot product) + float dot_normal_camera = vec3_dot(normal, camera_ray); + + // If dot product is negative, continue and bypass this for-loop iteration + // We do not want to push a face that has a negative dot product + // This bypasses triangles that are looking away from the camera + if (dot_normal_camera < 0) { + continue; + } + + triangle_t projected_triangle; + + // Loop all three vertices to perform projection + for (int j = 0; j < 3; j++) { + // Project the current vertex + vec2_t projected_point = project(transformed_vertices[j]); + + // Scale and translate the projected points to the middle of the screen + projected_point.x += (window_width / 2); + projected_point.y += (window_height / 2); + + projected_triangle.points[j] = projected_point; + } + + // Save the projected triangle in the array of triangles to render + //triangles_to_render[i] = projected_triangle; + array_push(triangles_to_render, projected_triangle); } } @@ -112,25 +170,37 @@ void render(void){ draw_grid(); //Loop all projected points and render them - for (int i = 0; i < N_POINTS; i++){ - vec2_t projected_point = projected_points[i]; - draw_rect( - projected_point.x + (window_width / 2.0), - projected_point.y + (window_height / 2.0), - 4, - 4, - 0xFFFFFF00 - ); - draw_line(projected_point.x, - projected_point.y, - projected_point.x + projected_point.x, projected_point.y + projected_point.y); - } - - draw_line(75, 75, 25, 25); + int num_of_triangles = array_length(triangles_to_render); + for (int i = 0; i < num_of_triangles; i++){ + triangle_t triangle = triangles_to_render[i]; + + /* + // Draw vertex points + draw_rect(triangle.points[0].x, triangle.points[0].y, 3, 3, 0xFFFFFF00); + draw_rect(triangle.points[1].x, triangle.points[1].y, 3, 3, 0xFFFFFF00); + draw_rect(triangle.points[2].x, triangle.points[2].y, 3, 3, 0xFFFFFF00); + */ + + // draw unfilled triangle + draw_triangle( + triangle.points[0].x, triangle.points[0].y, // Vertex A + triangle.points[1].x, triangle.points[1].y, // Vertex B + triangle.points[2].x, triangle.points[2].y, // Vertex C + 0xFF00FF00); + + /* my implementation + draw_line(triangle.points[0].x, triangle.points[0].y, triangle.points[1].x, triangle.points[1].y, 0xFFFFFF00); + draw_line(triangle.points[1].x, triangle.points[1].y, triangle.points[2].x, triangle.points[2].y, 0xFFFFFF00); + draw_line(triangle.points[2].x, triangle.points[2].y, triangle.points[0].x, triangle.points[0].y, 0xFFFFFF00); + */ + + } + //draw_line(triangle.points[0].x, triangle.points[0].y, 25, 25); // draw_pixel(20, 20, 0xFFFFFF00); // draw_rect(300, 200, 300, 150, 0xFF998500); - + array_free(triangles_to_render); + render_color_buffer(); clear_color_buffer(0xFF000000); @@ -138,20 +208,28 @@ void render(void){ SDL_RenderPresent(renderer); } +// Free memory that was dynamically allocated +void free_resources(void) { + free(color_buffer); + array_free(mesh.faces); + array_free(mesh.vertices); +} + int main(void) { is_running = initialize_window(); setup(); - + while (is_running) { process_input(); update(); render(); } - destroy_window(); + destroy_window(); + free_resources(); return 0; } diff --git a/src/mesh.c b/src/mesh.c new file mode 100644 index 0000000..5524b22 --- /dev/null +++ b/src/mesh.c @@ -0,0 +1,90 @@ +#include +#include "mesh.h" +#include "array.h" +#include +#include "array.h" + +mesh_t mesh = { + .vertices = NULL, + .faces = NULL, + .rotation = { 0, 0, 0 } +}; + +vec3_t cube_vertices[N_CUBE_VERTICES] = { + { .x = -1, .y = -1, .z = -1 }, // 1 + { .x = -1, .y = 1, .z = -1 }, // 2 + { .x = 1, .y = 1, .z = -1 }, // 3 + { .x = 1, .y = -1, .z = -1 }, // 4 + { .x = 1, .y = 1, .z = 1 }, // 5 + { .x = 1, .y = -1, .z = 1 }, // 6 + { .x = -1, .y = 1, .z = 1 }, // 7 + { .x = -1, .y = -1, .z = 1 } // 8 +}; + + +face_t cube_faces[N_CUBE_FACES] = { + // front + { .a = 1, .b = 2, .c = 3 }, + { .a = 1, .b = 3, .c = 4 }, + // right + { .a = 4, .b = 3, .c = 5 }, + { .a = 4, .b = 5, .c = 6 }, + // back + { .a = 6, .b = 5, .c = 7 }, + { .a = 6, .b = 7, .c = 8 }, + // left + { .a = 8, .b = 7, .c = 2 }, + { .a = 8, .b = 2, .c = 1 }, + // top + { .a = 2, .b = 7, .c = 5 }, + { .a = 2, .b = 5, .c = 3 }, + // bottom + { .a = 6, .b = 8, .c = 1 }, + { .a = 6, .b = 1, .c = 4 } +}; + +void load_cube_mesh_data(void) { + for (int i = 0; i < N_CUBE_VERTICES; i++) { + vec3_t cube_vertex = cube_vertices[i]; + array_push(mesh.vertices, cube_vertex); + } + for (int i = 0; i < N_CUBE_FACES; i++) { + face_t cube_face = cube_faces[i]; + array_push(mesh.faces, cube_face); + } +}; + +void load_obj_file_data(char* filename) { + FILE* file; + file = fopen(filename, "r"); + + char line[1024]; + + // read several lines + while (fgets(line, 1024, file)) { + // vertex information + if (strncmp(line, "v ", 2) == 0) { + vec3_t vertex; + sscanf(line, "v %f %f %f", &vertex.x, &vertex.y, &vertex.z); + array_push(mesh.vertices, vertex); + } + // Face information + if (strncmp(line, "f ", 2) == 0) { + int vertex_indices[3]; + int texture_indices[3]; + int normal_indices[3]; + + sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", + &vertex_indices[0], &texture_indices[0], &normal_indices[0], + &vertex_indices[1], &texture_indices[1], &normal_indices[1], + &vertex_indices[2], &texture_indices[2], &normal_indices[2] + ); + face_t face = { + .a = vertex_indices[0], + .b = vertex_indices[1], + .c = vertex_indices[2] + }; + array_push(mesh.faces, face); + } + } +} diff --git a/src/mesh.h b/src/mesh.h new file mode 100644 index 0000000..5fb0745 --- /dev/null +++ b/src/mesh.h @@ -0,0 +1,26 @@ +#ifndef MESH_H +#define MESH_H + +#include "vector.h" +#include "triangle.h" + + +#define N_CUBE_VERTICES 8 +#define N_CUBE_FACES (6*2) // 6 cube faces, 2 triangles per face + +extern vec3_t cube_vertices[N_CUBE_VERTICES]; + +extern face_t cube_faces[N_CUBE_FACES]; + +// Define a struct for dynamic size meshes, with an array of vertices and faces +typedef struct { + vec3_t* vertices; // dynamic array of vertices + face_t* faces; // dynamic array of faces + vec3_t rotation; // rotation with x, y, and z values +} mesh_t; + +extern mesh_t mesh; + +void load_cube_mesh_data(void); +void load_obj_file_data(char* filename); +#endif diff --git a/src/triangle.c b/src/triangle.c new file mode 100644 index 0000000..4a4b16e --- /dev/null +++ b/src/triangle.c @@ -0,0 +1,3 @@ +#include "triangle.h" + +//TODO diff --git a/src/triangle.h b/src/triangle.h new file mode 100644 index 0000000..7e95d7f --- /dev/null +++ b/src/triangle.h @@ -0,0 +1,19 @@ +#ifndef TRIANGLE_H +#define TRIANGLE_H + +#include "vector.h" + +typedef struct { + int a; + int b; + int c; +} face_t; + +typedef struct { + vec2_t points[3]; +} triangle_t; + +#endif + + + diff --git a/src/vector.c b/src/vector.c index d71fccd..22e8f52 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,6 +1,133 @@ #include "vector.h" #include +//////////////////////////////////////////////////////////////////////////////// +// implementations of vector 2d functions +//////////////////////////////////////////////////////////////////////////////// +float vec2_length(vec2_t v) { + return sqrt(v.x * v.x + v.y * v.y); +} + +vec2_t vec2_add(vec2_t a, vec2_t b) { + vec2_t result = { + .x = a.x + b.x, + .y = a.y + b.y + }; + + return result; +} + +vec2_t vec2_sub(vec2_t a, vec2_t b) { + vec2_t result = { + .x = a.x - b.x, + .y = a.y - b.y, + }; + + return result; +} + +vec2_t vec2_mul(vec2_t v, float factor) { + vec2_t result = { + .x = v.x * factor, + .y = v.y * factor + }; + + return result; +} + +vec2_t vec2_div(vec2_t v, float factor) { + vec2_t result = { + .x = v.x / factor, + .y = v.y / factor + }; + + return result; +} + +// Dot product +float vec2_dot(vec2_t a, vec2_t b) { + return (a.x * b.x) + (a.y * b.y); +} + +void vec2_normalize(vec2_t* v) { + // Get length by squaring x and y, adding together and taking sqrt + float length = sqrt((v->x * v->x) + (v->y * v->y)); + v->x /= length; // directly modifies x and y no need to return + v->y /= length; +} + +//////////////////////////////////////////////////////////////////////////////// +// implementations of vector 3d functions +//////////////////////////////////////////////////////////////////////////////// +float vec3_length(vec3_t v) { + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); +} + +vec3_t vec3_add(vec3_t a, vec3_t b) { + vec3_t result = { + .x = a.x + b.x, + .y = a.y + b.y, + .z = a.z + b.z + }; + + return result; +} + +vec3_t vec3_sub(vec3_t a, vec3_t b) { + vec3_t result = { + .x = a.x - b.x, + .y = a.y - b.y, + .z = a.z - b.z + }; + + return result; +} + +vec3_t vec3_mul(vec3_t v, float factor) { + vec3_t result = { + .x = v.x * factor, + .y = v.y * factor, + .z = v.z * factor + }; + + return result; +} + +// Division +vec3_t vec3_div(vec3_t v, float factor) { + vec3_t result = { + .x = v.x / factor, + .y = v.y / factor, + .z = v.z / factor + }; + + return result; +} + +// Cross Product - helps to find perpendicular vector between two vectors +vec3_t vec3_cross(vec3_t a, vec3_t b) { + vec3_t result = { + .x = a.y * b.z - a.z * b.y, + .y = a.z * b.x - a.x * b.z, + .z = a.x * b.y - a.y * b.x + }; + return result; +} + +// Dot Product - helps to find how aligned two vectors are to eachother +float vec3_dot(vec3_t a, vec3_t b) { + return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); + +} + +// Normalize a vec3_t vector +void vec3_normalize(vec3_t* v) { + float length = sqrt((v->x * v->x) + (v->y * v->y) + (v->z * v->z)); + v->x /= length; + v->y /= length; + v->z /= length; +} + vec3_t vec3_rotate_x(vec3_t v, float angle) { vec3_t rotated_vector = { .x = v.x, @@ -28,6 +155,7 @@ vec3_t vec3_rotate_z(vec3_t v, float angle) { return rotated_vector; } +/* vec3_t cube_vertice[8] = { { .x = -1, .y = -1, .z = -1 }, // 1 { .x = -1, .y = 1, .z = -1 }, // 2 @@ -37,8 +165,9 @@ vec3_t cube_vertice[8] = { { .x = 1, .y = -1, .z = 1 }, // 6 { .x = -1, .y = 1, .z = 1 }, // 7 { .x = -1, .y = -1, .z = 1 } // 8 -}; +};*/ +/* face_t cube_faces[12] = { // front { .a = 1, .b = 2, .c = 3 }, @@ -58,4 +187,4 @@ face_t cube_faces[12] = { // bottom { .a = 6, .b = 8, .c = 1 }, { .a = 6, .b = 1, .c = 4 } -}; +};*/ diff --git a/src/vector.h b/src/vector.h index cbd44a2..da35a91 100644 --- a/src/vector.h +++ b/src/vector.h @@ -12,12 +12,31 @@ typedef struct { float z; } vec3_t; -typedef struct { - int a, b, c; -} face_t; +///////////////////////////////////////////////////////////////////////////////// +// Vector 2D Functions +//////////////////////////////////////////////////////////////////////////////// +float vec2_length(vec2_t v); +vec2_t vec2_add(vec2_t a, vec2_t b); +vec2_t vec2_sub(vec2_t a, vec2_t b); +vec2_t vec2_mul(vec2_t v, float factor); +vec2_t vec2_div(vec2_t v, float factor); +void vec2_normalize(vec2_t* v); + +//////////////////////////////////////////////////////////////////////////////// +// Vector 3D Functions +//////////////////////////////////////////////////////////////////////////////// +float vec3_length(vec3_t v); +vec3_t vec3_add(vec3_t a, vec3_t b); +vec3_t vec3_sub(vec3_t a, vec3_t b); +vec3_t vec3_mul(vec3_t v, float factor); +vec3_t vec3_div(vec3_t v, float factor); +vec3_t vec3_cross(vec3_t a, vec3_t b); vec3_t vec3_rotate_x(vec3_t v, float angle); vec3_t vec3_rotate_y(vec3_t v, float angle); vec3_t vec3_rotate_z(vec3_t v, float angle); +vec3_t vec3_cross(vec3_t a, vec3_t b); +float vec3_dot(vec3_t a, vec3_t b); +void vec3_normalize(vec3_t* v); #endif