This commit is contained in:
2026-04-03 12:34:06 +02:00
parent cd77088dbf
commit 85dbded287
25 changed files with 2711 additions and 165 deletions

26
src/gameObjs.h Normal file
View File

@@ -0,0 +1,26 @@
#include "models/plane.h"
#include "renderer.h"
typedef struct {
mat4x4 transform;
vec3 velocity;
model model;
} gameObj;
gameObj rocket = {
{{1., 0., 0., 0.}, {0., 1., 0., 0.}, {0., 0., 1., 0.}, {0., -1., -10., 0.}},
{0.0, 0.0, 0.0},
testModel};
const gameObj *allGameObjs[] = {&rocket};
int vertBufferSize() {
int count = sizeof(allGameObjs) / sizeof(void *);
int result = 0;
for (int i = 0; i < count; i++) {
if (result < allGameObjs[i]->model.vert_size) {
result = allGameObjs[i]->model.vert_size;
}
}
return result * sizeof(vec4);
}

View File

@@ -19,6 +19,7 @@
#define REPEAT_4(FN) REPEAT_3(FN) FN(3)
#define add(i) r[i] = a[i] + b[i];
#define set(i) r[i] = v;
#define sub(i) r[i] = a[i] - b[i];
#define scale(i) r[i] = v[i] * s;
#define add_scale(i) r[i] = v[i] * s;
@@ -29,6 +30,9 @@
#define LINMATH_H_DEFINE_VEC(n) \
typedef float vec##n[n]; \
LINMATH_H_FUNC void vec##n##_set(vec##n r, float const v) { \
REPEAT_##n(set); \
} \
LINMATH_H_FUNC void vec##n##_add(vec##n r, vec##n const a, vec##n const b) { \
REPEAT_##n(add); \
} \
@@ -87,6 +91,7 @@ LINMATH_H_DEFINE_VEC(2)
LINMATH_H_DEFINE_VEC(3)
LINMATH_H_DEFINE_VEC(4)
#undef set
#undef add
#undef sub
#undef scale
@@ -124,6 +129,13 @@ LINMATH_H_FUNC void vec4_reflect(vec4 r, vec4 const v, vec4 const n) {
}
typedef vec4 mat4x4[4];
LINMATH_H_FUNC void mat4x4_getPos(vec3 r, mat4x4 const M) {
r[0] = M[3][0];
r[1] = M[3][1];
r[2] = M[3][2];
}
LINMATH_H_FUNC void mat4x4_identity(mat4x4 M) {
int i, j;
for (i = 0; i < 4; ++i)

View File

@@ -1,5 +1,5 @@
#include "gameObjs.h"
#include "linmath.h"
#include "models/plane.h"
#include "renderer.h"
#include <signal.h>
#include <stdint.h>
@@ -102,14 +102,7 @@ int main(void) {
render_target target = {WIDTH, HEIGHT, malloc(PIX_COUNT * 4)};
clearTarget(&target);
// model m;
// m = create_model((vec3[]){{-2.0f, -2.0f, -10.0f},
// {-2.0f, 2.0f, -10.0f},
// {2.0f, 2.0f, -10.0f},
// {2.0f, -2.0f, -10.0f}},
// (int[]){0, 2, 1, 0, 3, 2}, 4, 6);
// vec3_print(m.verts[0]);
vec4 *buffer = malloc(sizeof(vec4) * testModel.vert_size);
vec4 *buffer = malloc(vertBufferSize());
mat4x4 projMat = {};
mat4x4 viewMat = {};
mat4x4 drawMat = {};
@@ -126,12 +119,14 @@ int main(void) {
clock_t now = clock();
clock_t renderC;
float deltaTime; // in seconds
while (!stop) {
now = clock();
printf("total time: %fms\n",
((float)(now - start)) / (0.001 * CLOCKS_PER_SEC));
printf("render time: %fms\n", ((float)renderC) / (0.001 * CLOCKS_PER_SEC));
deltaTime = ((float)(now - start)) / CLOCKS_PER_SEC;
start = now;
printf("total time: %fms\n", deltaTime * 1000);
printf("render time: %fms\n", ((float)renderC) / (0.001 * CLOCKS_PER_SEC));
if (read(STDIN_FILENO, &lastKey, 1) == -1) {
perror("read failed");
@@ -141,7 +136,7 @@ int main(void) {
switch (lastKey) {
case 'w':
mat4x4_translate_in_place(viewMat, 0, 0, -0.2);
rocket.velocity[1] = (rocket.velocity[1] * 0.9 + 20 * 0.1);
break;
case 's':
mat4x4_translate_in_place(viewMat, 0, 0, 0.2);
@@ -156,11 +151,17 @@ int main(void) {
lastKey = 0;
rocket.velocity[1] +=
(-4.0f + 0.5 * powf(MIN(rocket.velocity[1], 0.0), 2.0f)) * deltaTime;
mat4x4_translate_in_place(rocket.transform, rocket.velocity[0] * deltaTime,
rocket.velocity[1] * deltaTime,
rocket.velocity[2] * deltaTime);
// mat4x4_translate_in_place(viewMat, 0, 0, .1);
mat4x4_mul(drawMat, projMat, viewMat);
mat4x4_mul(drawMat, projMat, rocket.transform);
clearTarget(&target);
renderC = clock();
render(&target, &testModel, &drawMat, buffer);
render(&target, &rocket.model, &drawMat, buffer);
renderC = clock() - renderC;
base64encode(&target, encodeBuff);

View File

@@ -1,2 +1,355 @@
#include "../renderer.h"
const model testModel = {(vec3[]){{1.000000,1.000000,-11.000000},{1.000000,-1.000000,-11.000000},{1.000000,1.000000,-9.000000},{1.000000,-1.000000,-9.000000},{-1.000000,1.000000,-11.000000},{-1.000000,-1.000000,-11.000000},{-1.000000,1.000000,-9.000000},{-1.000000,-1.000000,-9.000000}},(int[]){4,2,0,2,7,3,6,5,7,1,7,5,0,3,1,4,1,5,4,6,2,2,6,7,6,4,5,1,3,7,0,2,3,4,0,1},8,36};
#include "../renderer.h"
const model testModel = {
(vec3[]){
{0.698513, 0.757563, 0.000000}, {0.044645, 2.825787, 0.000000},
{0.656388, 0.757563, 0.238906}, {0.041953, 2.825787, 0.015270},
{0.535092, 0.757563, 0.448996}, {0.034200, 2.825787, 0.028697},
{0.349257, 0.757563, 0.604930}, {0.022323, 2.825787, 0.038664},
{0.121296, 0.757563, 0.687901}, {0.007753, 2.825787, 0.043967},
{-0.121296, 0.757563, 0.687901}, {-0.007753, 2.825787, 0.043967},
{-0.349257, 0.757563, 0.604930}, {-0.022323, 2.825787, 0.038664},
{-0.535092, 0.757563, 0.448996}, {-0.034200, 2.825787, 0.028697},
{-0.656388, 0.757563, 0.238906}, {-0.041953, 2.825787, 0.015270},
{-0.698513, 0.757563, -0.000000}, {-0.044645, 2.825787, 0.000000},
{-0.656388, 0.757563, -0.238906}, {-0.041953, 2.825787, -0.015270},
{-0.535092, 0.757563, -0.448996}, {-0.034200, 2.825787, -0.028697},
{-0.349257, 0.757563, -0.604930}, {-0.022323, 2.825787, -0.038664},
{-0.121296, 0.757563, -0.687901}, {-0.007753, 2.825787, -0.043967},
{0.121296, 0.757563, -0.687901}, {0.007753, 2.825787, -0.043967},
{0.349257, 0.757563, -0.604930}, {0.022323, 2.825787, -0.038664},
{0.535092, 0.757563, -0.448996}, {0.034200, 2.825787, -0.028697},
{0.656388, 0.757563, -0.238906}, {0.041953, 2.825787, -0.015269},
{0.544639, 2.240545, 0.000000}, {0.511793, 2.240545, 0.186277},
{0.409479, 2.128906, 0.371044}, {0.290178, 2.227410, 0.470774},
{0.100778, 2.227410, 0.539710}, {-0.100778, 2.227410, 0.539710},
{-0.290178, 2.227410, 0.470774}, {-0.409479, 2.128906, 0.371044},
{-0.511793, 2.240545, 0.186277}, {-0.544639, 2.240545, -0.000000},
{-0.511793, 2.240545, -0.186277}, {-0.417217, 2.240545, -0.350087},
{-0.272319, 2.240545, -0.471671}, {-0.094576, 2.240545, -0.536364},
{0.094576, 2.240545, -0.536364}, {0.272319, 2.240545, -0.471671},
{0.417218, 2.240545, -0.350087}, {0.511793, 2.240545, -0.186277},
{0.694426, 1.474881, 0.252751}, {0.527394, 1.631239, 0.497527},
{0.367433, 1.488016, 0.632126}, {0.127608, 1.488016, 0.719415},
{-0.127608, 1.488016, 0.719415}, {-0.367433, 1.488016, 0.632126},
{-0.527394, 1.631239, 0.497527}, {-0.694426, 1.474881, 0.252751},
{-0.738993, 1.474881, -0.000000}, {-0.694426, 1.474881, -0.252751},
{-0.566102, 1.474881, -0.475016}, {-0.369497, 1.474881, -0.639987},
{-0.128325, 1.474881, -0.727766}, {0.128325, 1.474881, -0.727766},
{0.369497, 1.474881, -0.639987}, {0.566102, 1.474881, -0.475016},
{0.694426, 1.474881, -0.252751}, {0.738993, 1.474881, 0.000000},
{0.410385, -0.154204, 0.000000}, {0.385635, -0.154204, 0.140360},
{0.314373, -0.154204, 0.263790}, {0.205192, -0.154204, 0.355403},
{0.071263, -0.154204, 0.404150}, {-0.071263, -0.154204, 0.404150},
{-0.205192, -0.154204, 0.355403}, {-0.314373, -0.154204, 0.263790},
{-0.385635, -0.154204, 0.140360}, {-0.410385, -0.154204, -0.000000},
{-0.385635, -0.154204, -0.140360}, {-0.314373, -0.154204, -0.263790},
{-0.205192, -0.154204, -0.355403}, {-0.071263, -0.154204, -0.404150},
{0.071263, -0.154204, -0.404150}, {0.205192, -0.154204, -0.355403},
{0.314373, -0.154204, -0.263790}, {0.385635, -0.154204, -0.140360},
{0.237705, 2.675272, 0.000000}, {0.223370, 2.675272, 0.081300},
{0.182093, 2.675272, 0.152794}, {0.118853, 2.675272, 0.205859},
{0.041277, 2.675272, 0.234094}, {-0.041277, 2.675272, 0.234094},
{-0.118853, 2.675272, 0.205859}, {-0.182093, 2.675272, 0.152794},
{-0.223370, 2.675272, 0.081300}, {-0.237705, 2.675272, -0.000000},
{-0.223370, 2.675272, -0.081300}, {-0.182093, 2.675272, -0.152794},
{-0.118853, 2.675272, -0.205859}, {-0.041277, 2.675272, -0.234094},
{0.041277, 2.675272, -0.234094}, {0.118853, 2.675272, -0.205859},
{0.182093, 2.675272, -0.152794}, {0.223370, 2.675272, -0.081300},
{0.046326, 2.839588, 0.000000}, {-0.023163, 2.839588, -0.040120},
{-0.023163, 2.839588, 0.040120}, {0.000000, 3.137896, 0.000000},
{-0.272319, 2.240545, 0.471671}, {-0.417218, 2.141971, 0.350087},
{-0.094576, 2.240545, 0.536364}, {0.094576, 2.240545, 0.536364},
{0.272319, 2.240545, 0.471671}, {0.417217, 2.141971, 0.350087},
{0.566102, 1.618174, 0.475016}, {-0.566102, 1.618174, 0.475016},
{0.369497, 1.474881, 0.639987}, {0.128325, 1.474881, 0.727766},
{-0.128325, 1.474881, 0.727766}, {-0.369497, 1.474881, 0.639987},
{0.237002, 2.117707, 0.625731}, {0.257333, 1.652925, 0.728921},
{0.082310, 2.117707, 0.682034}, {0.089371, 1.652925, 0.766453},
{-0.082310, 2.117707, 0.682034}, {-0.089371, 1.652925, 0.766453},
{-0.237002, 2.117707, 0.625731}, {-0.257333, 1.652925, 0.728921},
{-0.334441, 2.019176, 0.544276}, {0.334441, 2.019176, 0.544276},
{0.369362, 1.735177, 0.671046}, {-0.369362, 1.735177, 0.671046},
{0.096339, 2.201041, 0.616476}, {-0.096339, 2.201041, 0.616476},
{0.489408, 1.656222, 0.539235}, {0.391442, 2.102530, 0.455240},
{0.118417, 1.527654, 0.730721}, {-0.118417, 1.527654, 0.730721},
{-0.489408, 1.656222, 0.539235}, {-0.391442, 2.102530, 0.455240},
{0.277396, 2.201041, 0.550577}, {-0.340969, 1.527654, 0.655392},
{-0.277396, 2.201041, 0.550577}, {0.340969, 1.527654, 0.655392}},
(int[]){
1, 1, 91, 91, 90, 90, 91, 91, 5, 5, 92, 92, 5, 5,
93, 93, 92, 92, 7, 7, 94, 94, 93, 93, 9, 9, 95, 95,
94, 94, 11, 11, 96, 96, 95, 95, 96, 96, 15, 15, 97, 97,
15, 15, 98, 98, 97, 97, 98, 98, 19, 19, 99, 99, 19, 19,
100, 100, 99, 99, 100, 100, 23, 23, 101, 101, 23, 23, 102, 102,
101, 101, 25, 25, 103, 103, 102, 102, 27, 27, 104, 104, 103, 103,
104, 104, 31, 31, 105, 105, 31, 31, 106, 106, 105, 105, 33, 33,
107, 107, 106, 106, 35, 35, 90, 90, 107, 107, 4, 4, 73, 73,
2, 2, 53, 53, 71, 71, 70, 70, 52, 52, 70, 70, 69, 69,
51, 51, 69, 69, 68, 68, 50, 50, 68, 68, 67, 67, 49, 49,
67, 67, 66, 66, 65, 65, 49, 49, 66, 66, 64, 64, 48, 48,
65, 65, 46, 46, 64, 64, 63, 63, 62, 62, 46, 46, 63, 63,
44, 44, 62, 62, 61, 61, 119, 119, 44, 44, 61, 61, 136, 136,
124, 124, 144, 144, 142, 142, 132, 132, 143, 143, 141, 141, 127, 127,
129, 129, 137, 137, 126, 126, 136, 136, 138, 138, 133, 133, 134, 134,
37, 37, 118, 118, 54, 54, 71, 71, 37, 37, 54, 54, 0, 0,
54, 54, 2, 2, 54, 54, 4, 4, 2, 2, 4, 4, 120, 120,
6, 6, 120, 120, 8, 8, 6, 6, 121, 121, 10, 10, 8, 8,
122, 122, 12, 12, 10, 10, 123, 123, 14, 14, 12, 12, 14, 14,
61, 61, 16, 16, 16, 16, 62, 62, 18, 18, 18, 18, 63, 63,
20, 20, 63, 63, 22, 22, 20, 20, 64, 64, 24, 24, 22, 22,
65, 65, 26, 26, 24, 24, 66, 66, 28, 28, 26, 26, 67, 67,
30, 30, 28, 28, 68, 68, 32, 32, 30, 30, 69, 69, 34, 34,
32, 32, 34, 34, 71, 71, 0, 0, 75, 75, 83, 83, 87, 87,
14, 14, 78, 78, 12, 12, 24, 24, 83, 83, 22, 22, 34, 34,
88, 88, 32, 32, 10, 10, 76, 76, 8, 8, 20, 20, 81, 81,
18, 18, 30, 30, 86, 86, 28, 28, 6, 6, 74, 74, 4, 4,
16, 16, 79, 79, 14, 14, 2, 2, 72, 72, 0, 0, 26, 26,
84, 84, 24, 24, 0, 0, 89, 89, 34, 34, 12, 12, 77, 77,
10, 10, 22, 22, 82, 82, 20, 20, 32, 32, 87, 87, 30, 30,
8, 8, 75, 75, 6, 6, 18, 18, 80, 80, 16, 16, 28, 28,
85, 85, 26, 26, 107, 107, 36, 36, 53, 53, 106, 106, 53, 53,
52, 52, 105, 105, 52, 52, 51, 51, 104, 104, 51, 51, 50, 50,
103, 103, 50, 50, 49, 49, 102, 102, 49, 49, 48, 48, 101, 101,
48, 48, 47, 47, 100, 100, 47, 47, 46, 46, 99, 99, 46, 46,
45, 45, 98, 98, 45, 45, 44, 44, 97, 97, 44, 44, 113, 113,
112, 112, 97, 97, 113, 113, 95, 95, 112, 112, 114, 114, 94, 94,
114, 114, 115, 115, 116, 116, 94, 94, 115, 115, 92, 92, 116, 116,
117, 117, 37, 37, 92, 92, 117, 117, 90, 90, 37, 37, 36, 36,
108, 108, 5, 5, 3, 3, 108, 108, 7, 7, 5, 5, 110, 110,
7, 7, 108, 108, 110, 110, 11, 11, 9, 9, 110, 110, 13, 13,
11, 11, 110, 110, 15, 15, 13, 13, 110, 110, 17, 17, 15, 15,
110, 110, 19, 19, 17, 17, 109, 109, 19, 19, 110, 110, 109, 109,
23, 23, 21, 21, 109, 109, 25, 25, 23, 23, 109, 109, 27, 27,
25, 25, 109, 109, 29, 29, 27, 27, 109, 109, 31, 31, 29, 29,
108, 108, 31, 31, 109, 109, 108, 108, 35, 35, 33, 33, 108, 108,
1, 1, 35, 35, 108, 108, 3, 3, 1, 1, 110, 110, 9, 9,
7, 7, 109, 109, 21, 21, 19, 19, 108, 108, 33, 33, 31, 31,
110, 110, 108, 108, 111, 111, 109, 109, 110, 110, 111, 111, 108, 108,
109, 109, 111, 111, 42, 42, 113, 113, 43, 43, 41, 41, 112, 112,
42, 42, 40, 40, 114, 114, 41, 41, 40, 40, 116, 116, 115, 115,
39, 39, 117, 117, 116, 116, 38, 38, 118, 118, 117, 117, 43, 43,
119, 119, 60, 60, 56, 56, 118, 118, 55, 55, 57, 57, 120, 120,
56, 56, 58, 58, 121, 121, 57, 57, 58, 58, 123, 123, 122, 122,
59, 59, 119, 119, 123, 123, 130, 130, 135, 135, 131, 131, 128, 128,
131, 131, 129, 129, 126, 126, 129, 129, 127, 127, 125, 125, 126, 126,
127, 127, 134, 134, 124, 124, 125, 125, 141, 141, 131, 131, 145, 145,
146, 146, 132, 132, 130, 130, 147, 147, 134, 134, 125, 125, 144, 144,
133, 133, 139, 139, 145, 145, 135, 135, 142, 142, 137, 137, 130, 130,
128, 128, 140, 140, 125, 125, 127, 127, 57, 57, 147, 147, 140, 140,
41, 41, 146, 146, 137, 137, 59, 59, 142, 142, 60, 60, 39, 39,
139, 139, 38, 38, 56, 56, 138, 138, 147, 147, 42, 42, 143, 143,
146, 146, 58, 58, 145, 145, 59, 59, 55, 55, 139, 139, 138, 138,
41, 41, 136, 136, 40, 40, 57, 57, 141, 141, 58, 58, 60, 60,
143, 143, 43, 43, 40, 40, 144, 144, 39, 39, 1, 1, 3, 3,
91, 91, 91, 91, 3, 3, 5, 5, 5, 5, 7, 7, 93, 93,
7, 7, 9, 9, 94, 94, 9, 9, 11, 11, 95, 95, 11, 11,
13, 13, 96, 96, 96, 96, 13, 13, 15, 15, 15, 15, 17, 17,
98, 98, 98, 98, 17, 17, 19, 19, 19, 19, 21, 21, 100, 100,
100, 100, 21, 21, 23, 23, 23, 23, 25, 25, 102, 102, 25, 25,
27, 27, 103, 103, 27, 27, 29, 29, 104, 104, 104, 104, 29, 29,
31, 31, 31, 31, 33, 33, 106, 106, 33, 33, 35, 35, 107, 107,
35, 35, 1, 1, 90, 90, 4, 4, 74, 74, 73, 73, 53, 53,
36, 36, 71, 71, 52, 52, 53, 53, 70, 70, 51, 51, 52, 52,
69, 69, 50, 50, 51, 51, 68, 68, 49, 49, 50, 50, 67, 67,
65, 65, 48, 48, 49, 49, 64, 64, 47, 47, 48, 48, 46, 46,
47, 47, 64, 64, 62, 62, 45, 45, 46, 46, 44, 44, 45, 45,
62, 62, 119, 119, 113, 113, 44, 44, 136, 136, 126, 126, 124, 124,
142, 142, 135, 135, 132, 132, 141, 141, 140, 140, 127, 127, 137, 137,
128, 128, 126, 126, 138, 138, 139, 139, 133, 133, 37, 37, 117, 117,
118, 118, 71, 71, 36, 36, 37, 37, 0, 0, 71, 71, 54, 54,
54, 54, 118, 118, 4, 4, 4, 4, 118, 118, 120, 120, 120, 120,
121, 121, 8, 8, 121, 121, 122, 122, 10, 10, 122, 122, 123, 123,
12, 12, 123, 123, 119, 119, 14, 14, 14, 14, 119, 119, 61, 61,
16, 16, 61, 61, 62, 62, 18, 18, 62, 62, 63, 63, 63, 63,
64, 64, 22, 22, 64, 64, 65, 65, 24, 24, 65, 65, 66, 66,
26, 26, 66, 66, 67, 67, 28, 28, 67, 67, 68, 68, 30, 30,
68, 68, 69, 69, 32, 32, 69, 69, 70, 70, 34, 34, 34, 34,
70, 70, 71, 71, 89, 89, 72, 72, 73, 73, 73, 73, 74, 74,
75, 75, 75, 75, 76, 76, 77, 77, 77, 77, 78, 78, 79, 79,
79, 79, 80, 80, 81, 81, 81, 81, 82, 82, 83, 83, 83, 83,
84, 84, 85, 85, 85, 85, 86, 86, 87, 87, 87, 87, 88, 88,
89, 89, 89, 89, 73, 73, 75, 75, 75, 75, 77, 77, 79, 79,
79, 79, 81, 81, 83, 83, 83, 83, 85, 85, 87, 87, 87, 87,
89, 89, 75, 75, 75, 75, 79, 79, 83, 83, 14, 14, 79, 79,
78, 78, 24, 24, 84, 84, 83, 83, 34, 34, 89, 89, 88, 88,
10, 10, 77, 77, 76, 76, 20, 20, 82, 82, 81, 81, 30, 30,
87, 87, 86, 86, 6, 6, 75, 75, 74, 74, 16, 16, 80, 80,
79, 79, 2, 2, 73, 73, 72, 72, 26, 26, 85, 85, 84, 84,
0, 0, 72, 72, 89, 89, 12, 12, 78, 78, 77, 77, 22, 22,
83, 83, 82, 82, 32, 32, 88, 88, 87, 87, 8, 8, 76, 76,
75, 75, 18, 18, 81, 81, 80, 80, 28, 28, 86, 86, 85, 85,
107, 107, 90, 90, 36, 36, 106, 106, 107, 107, 53, 53, 105, 105,
106, 106, 52, 52, 104, 104, 105, 105, 51, 51, 103, 103, 104, 104,
50, 50, 102, 102, 103, 103, 49, 49, 101, 101, 102, 102, 48, 48,
100, 100, 101, 101, 47, 47, 99, 99, 100, 100, 46, 46, 98, 98,
99, 99, 45, 45, 97, 97, 98, 98, 44, 44, 112, 112, 96, 96,
97, 97, 95, 95, 96, 96, 112, 112, 94, 94, 95, 95, 114, 114,
116, 116, 93, 93, 94, 94, 92, 92, 93, 93, 116, 116, 37, 37,
91, 91, 92, 92, 90, 90, 91, 91, 37, 37, 42, 42, 112, 112,
113, 113, 41, 41, 114, 114, 112, 112, 40, 40, 115, 115, 114, 114,
40, 40, 39, 39, 116, 116, 39, 39, 38, 38, 117, 117, 38, 38,
55, 55, 118, 118, 43, 43, 113, 113, 119, 119, 56, 56, 120, 120,
118, 118, 57, 57, 121, 121, 120, 120, 58, 58, 122, 122, 121, 121,
58, 58, 59, 59, 123, 123, 59, 59, 60, 60, 119, 119, 130, 130,
132, 132, 135, 135, 128, 128, 130, 130, 131, 131, 126, 126, 128, 128,
129, 129, 125, 125, 124, 124, 126, 126, 134, 134, 133, 133, 124, 124,
141, 141, 129, 129, 131, 131, 146, 146, 143, 143, 132, 132, 147, 147,
138, 138, 134, 134, 144, 144, 124, 124, 133, 133, 145, 145, 131, 131,
135, 135, 137, 137, 146, 146, 130, 130, 140, 140, 147, 147, 125, 125,
57, 57, 56, 56, 147, 147, 41, 41, 42, 42, 146, 146, 59, 59,
145, 145, 142, 142, 39, 39, 144, 144, 139, 139, 56, 56, 55, 55,
138, 138, 42, 42, 43, 43, 143, 143, 58, 58, 141, 141, 145, 145,
55, 55, 38, 38, 139, 139, 41, 41, 137, 137, 136, 136, 57, 57,
140, 140, 141, 141, 60, 60, 142, 142, 143, 143, 40, 40, 136, 136,
144, 144},
(vec3[]){{0.9841, -0.1778, -0.0000}, {0.9146, 0.4043, -0.0000},
{0.9247, -0.1778, 0.3366}, {0.7754, 0.5378, 0.3310},
{0.7540, -0.1768, 0.6327}, {0.4942, 0.7357, 0.4632},
{0.4920, -0.1778, 0.8522}, {0.3013, 0.7980, 0.5219},
{0.1709, -0.1778, 0.9691}, {0.1541, 0.7357, 0.6596},
{-0.1709, -0.1778, 0.9691}, {-0.1010, 0.5378, 0.8370},
{-0.4920, -0.1778, 0.8522}, {-0.4573, 0.4043, 0.7921},
{-0.7540, -0.1768, 0.6327}, {-0.6743, 0.5378, 0.5060},
{-0.9247, -0.1778, 0.3366}, {-0.6483, 0.7357, 0.1964},
{-0.9841, -0.1778, -0.0000}, {-0.6026, 0.7980, -0.0000},
{-0.9247, -0.1778, -0.3366}, {-0.6483, 0.7357, -0.1964},
{-0.7538, -0.1778, -0.6325}, {-0.6743, 0.5378, -0.5060},
{-0.4920, -0.1778, -0.8522}, {-0.4573, 0.4043, -0.7921},
{-0.1709, -0.1778, -0.9691}, {-0.1010, 0.5378, -0.8370},
{0.1709, -0.1778, -0.9691}, {0.1541, 0.7357, -0.6596},
{0.4920, -0.1778, -0.8522}, {0.3013, 0.7980, -0.5219},
{0.7538, -0.1778, -0.6325}, {0.4942, 0.7357, -0.4632},
{0.9247, -0.1778, -0.3366}, {0.7754, 0.5378, -0.3310},
{0.9117, 0.4109, -0.0000}, {0.8130, 0.4345, 0.3877},
{0.8011, 0.3907, 0.4535}, {0.5809, 0.7552, 0.3037},
{0.1433, 0.7300, 0.6682}, {-0.1433, 0.7300, 0.6682},
{-0.5809, 0.7552, 0.3037}, {-0.8011, 0.3907, 0.4535},
{-0.8130, 0.4345, 0.3877}, {-0.9117, 0.4109, -0.0000},
{-0.8567, 0.4109, -0.3118}, {-0.6984, 0.4109, -0.5860},
{-0.4558, 0.4109, -0.7895}, {-0.1583, 0.4109, -0.8978},
{0.1583, 0.4109, -0.8978}, {0.4558, 0.4109, -0.7895},
{0.6984, 0.4109, -0.5860}, {0.8567, 0.4109, -0.3118},
{0.9423, 0.0728, 0.3267}, {0.6347, 0.1973, 0.7472},
{0.4225, 0.2699, 0.8652}, {0.1829, 0.1701, 0.9683},
{-0.1829, 0.1701, 0.9683}, {-0.4225, 0.2699, 0.8652},
{-0.6347, 0.1973, 0.7472}, {-0.9423, 0.0728, 0.3267},
{-0.9955, 0.0944, -0.0000}, {-0.9355, 0.0944, -0.3405},
{-0.7626, 0.0944, -0.6399}, {-0.4978, 0.0944, -0.8622},
{-0.1729, 0.0944, -0.9804}, {0.1729, 0.0944, -0.9804},
{0.4978, 0.0944, -0.8622}, {0.7626, 0.0944, -0.6399},
{0.9355, 0.0944, -0.3405}, {0.9955, 0.0944, -0.0000},
{0.6306, -0.7761, -0.0000}, {0.5926, -0.7761, 0.2157},
{0.4830, -0.7761, 0.4053}, {0.3153, -0.7761, 0.5461},
{0.1095, -0.7761, 0.6210}, {-0.1095, -0.7761, 0.6210},
{-0.3153, -0.7761, 0.5461}, {-0.4831, -0.7761, 0.4053},
{-0.5925, -0.7761, 0.2157}, {-0.6306, -0.7761, -0.0000},
{-0.5925, -0.7761, -0.2157}, {-0.4831, -0.7761, -0.4053},
{-0.3153, -0.7761, -0.5461}, {-0.1095, -0.7761, -0.6210},
{0.1095, -0.7761, -0.6210}, {0.3153, -0.7761, -0.5461},
{0.4830, -0.7761, -0.4053}, {0.5925, -0.7761, -0.2157},
{0.7312, 0.6821, -0.0000}, {0.6871, 0.6821, 0.2501},
{0.5656, 0.6745, 0.4746}, {0.3656, 0.6821, 0.6333},
{0.1270, 0.6821, 0.7201}, {-0.1270, 0.6821, 0.7201},
{-0.3656, 0.6821, 0.6333}, {-0.5656, 0.6745, 0.4746},
{-0.6871, 0.6821, 0.2501}, {-0.7312, 0.6821, -0.0000},
{-0.6871, 0.6821, -0.2501}, {-0.5601, 0.6821, -0.4700},
{-0.3656, 0.6821, -0.6333}, {-0.1270, 0.6821, -0.7201},
{0.1270, 0.6821, -0.7201}, {0.3656, 0.6821, -0.6333},
{0.5601, 0.6821, -0.4700}, {0.6871, 0.6821, -0.2501},
{0.9437, 0.3307, -0.0000}, {-0.4719, 0.3307, -0.8173},
{-0.4719, 0.3307, 0.8173}, {-0.0000, 1.0000, -0.0000},
{-0.4787, 0.5258, 0.7031}, {-0.7169, 0.4126, 0.5620},
{-0.1342, 0.4369, 0.8895}, {0.1342, 0.4369, 0.8895},
{0.4787, 0.5258, 0.7031}, {0.7169, 0.4126, 0.5620},
{0.7577, 0.1650, 0.6315}, {-0.7577, 0.1650, 0.6315},
{0.4534, 0.1969, 0.8693}, {0.1663, 0.2444, 0.9553},
{-0.1663, 0.2444, 0.9553}, {-0.4534, 0.1969, 0.8693},
{0.4906, 0.3661, 0.7907}, {0.3424, -0.0553, 0.9379},
{0.1431, 0.4003, 0.9051}, {0.1012, -0.0377, 0.9942},
{-0.1431, 0.4003, 0.9051}, {-0.1012, -0.0377, 0.9942},
{-0.4906, 0.3661, 0.7907}, {-0.3424, -0.0553, 0.9379},
{-0.7362, 0.2805, 0.6159}, {0.7362, 0.2805, 0.6159},
{0.6174, 0.1400, 0.7741}, {-0.6174, 0.1400, 0.7741},
{0.1165, 0.7887, 0.6037}, {-0.1165, 0.7887, 0.6037},
{0.7537, 0.0880, 0.6512}, {0.8090, 0.3734, 0.4541},
{0.1400, -0.2577, 0.9560}, {-0.1400, -0.2577, 0.9560},
{-0.7537, 0.0880, 0.6512}, {-0.8090, 0.3734, 0.4541},
{0.5043, 0.6625, 0.5539}, {-0.4814, -0.2028, 0.8527},
{-0.5043, 0.6625, 0.5539}, {0.4814, -0.2028, 0.8527}},
(vec3[]){{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.8556, 0.8746, 1.0000}, {0.8556, 0.8746, 1.0000},
{0.8556, 0.8746, 1.0000}, {0.8556, 0.8746, 1.0000},
{0.8556, 0.8746, 1.0000}, {0.8556, 0.8746, 1.0000},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.8556, 0.8746, 1.0000},
{0.8556, 0.8746, 1.0000}, {0.8556, 0.8746, 1.0000},
{0.8556, 0.8746, 1.0000}, {0.8556, 0.8746, 1.0000},
{0.8556, 0.8746, 1.0000}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.9696, 1.0000, 0.0000}, {0.9695, 0.9999, 0.0005},
{0.9696, 1.0000, 0.0000}, {0.9696, 1.0000, 0.0000},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.0000, 0.4470, 0.0334}, {0.0000, 0.4470, 0.0334},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900},
{0.1450, 0.1371, 0.1900}, {0.1450, 0.1371, 0.1900}},
148,
1752,
20.f};

View File

@@ -6,11 +6,12 @@
typedef struct {
vec3 *verts;
// vec3 *normals;
// vec3 *color;
int *index;
vec3 *normals;
vec3 *color;
int vert_size;
int index_size;
float shininess;
} model;
typedef struct {
@@ -34,6 +35,8 @@ typedef struct {
// vec3_print(verts[0]);
// return m;
// };
//
const vec3 lightDir = {0.707, 0.707, 0.0f};
void initPoly(polygon *p) {
vec4 **points = p->points;
@@ -88,11 +91,24 @@ void calcBarycentrics(polygon *p, vec2 s, vec3 r) {
r[0] = 1.f - r[1] - r[2];
}
float applyBary(const vec3 bary, float a, float b, float c) {
// float result = 0;
float fApplyBary(const vec3 bary, float a, float b, float c) {
return bary[0] * a + bary[1] * b + bary[2] * c;
}
void vApplyBary(const vec3 bary, const vec3 a, const vec3 b, const vec3 c,
vec3 dst) {
vec3 tmp = {};
dst[0] = 0;
dst[1] = 0;
dst[2] = 0;
vec3_scale(tmp, a, bary[0]);
vec3_add(dst, dst, tmp);
vec3_scale(tmp, b, bary[1]);
vec3_add(dst, dst, tmp);
vec3_scale(tmp, c, bary[2]);
vec3_add(dst, dst, tmp);
}
void render(render_target *target, const model *model, mat4x4 *matrix,
vec4 *buffer) {
@@ -107,66 +123,96 @@ void render(render_target *target, const model *model, mat4x4 *matrix,
mat4x4_mul_vec4(buffer[i], *matrix, tmp);
vec4_scale(buffer[i], buffer[i], 1.0 / buffer[i][3]);
vec4_print(buffer[i]);
// vec4_print(buffer[i]);
}
polygon p = {};
for (int i = 0; i < model->index_size; i += 3) {
vec3 *normals[3] = {};
vec3 *colors[3] = {};
for (int i = 0; i < model->index_size; i += 6) {
p.points[0] = buffer + model->index[i];
p.points[1] = buffer + model->index[i + 1];
p.points[2] = buffer + model->index[i + 2];
p.points[1] = buffer + model->index[i + 2];
p.points[2] = buffer + model->index[i + 4];
initPoly(&p);
if (p.norm[2] > 0)
continue;
printf("baryFac: %f\n", p.baryFactor);
normals[0] = model->normals + model->index[i + 1];
normals[1] = model->normals + model->index[i + 3];
normals[2] = model->normals + model->index[i + 5];
// printf("min x :%f, min y: %f\nmax x: %f, max y: %f \n", p.bounding[0],
// p.bounding[2], p.bounding[1], p.bounding[3]);
colors[0] = model->color + model->index[i];
colors[1] = model->color + model->index[i + 2];
colors[2] = model->color + model->index[i + 4];
// int startX = floor((MAX(-1.0f, p.bounding[0])+1.0)*target->width*0.5);
// int startY = floor((MAX(-1.0f, p.bounding[2])+1.0)*target->height*0.5);
//
// int endX = floor((MIN(1.0f, p.bounding[1])+1.0f)*target->width*0.5);
// int endY = floor((MAX(1.0f, p.bounding[3])+1.0f)*target->height*0.5);
float texWidth = 2.0f / target->width;
float texHight = 2.0f / target->width;
float startX = MAX(-1.0f, p.bounding[0]);
float startY = MAX(-1.0f, p.bounding[2]);
float endX = MIN(1.0f, p.bounding[1]);
float endY = MIN(1.0f, p.bounding[3]);
// float startX = MAX(-1.0f, p.bounding[0]);
// float startY = MAX(-1.0f, p.bounding[2]);
// float endX = MIN(1.0f, p.bounding[1]);
// float endY = MIN(1.0f, p.bounding[3]);
float startX = (MAX(-1.0f, p.bounding[0]) + 1.0f) * 0.5f * target->width;
float endY = (-MAX(-1.0f, p.bounding[2]) + 1.0f) * 0.5f * target->height;
float endX = (MIN(1.0f, p.bounding[1]) + 1.0f) * 0.5f * target->width;
float startY = (-MIN(1.0f, p.bounding[3]) + 1.0f) * 0.5f * target->height;
// printf("min x :%f, min y: %f\n max x: %f, max y: %f \n", startX, startY,
// endX, endY);
//
vec2 sp = {};
vec3 bary = {};
vec4 val = {0};
vec3 nor = {};
vec3 color = {};
for (float y = startY; y < endY; y += texHight) {
for (float x = startX; x < endX; x += texWidth) {
sp[0] = x;
sp[1] = y;
vec3 viewDir = {};
vec3 halfWay = {};
for (int y = startY; y < endY; y += 1) {
for (int x = startX; x < endX; x += 1) {
// for (float y = startY; y < endY; y += texHight) {
// for (float x = startX; x < endX; x += texWidth) {
float fX = ((float)x) * texWidth - 1.0f;
float fY = 1.0f - ((float)y) * texHight;
sp[0] = fX;
sp[1] = fY;
calcBarycentrics(&p, sp, bary);
if (bary[0] >= 0.0f && bary[1] >= 0.0f && bary[2] >= 0.0f) {
uint8_t depth = (uint8_t)(255.0 * applyBary(bary, (*p.points[0])[2],
(*p.points[1])[2],
(*p.points[2])[2]));
int pX = (int)((x + 1.f) * target->width * 0.5f);
int pY = (int)((-y + 1.f) * target->height * 0.5f);
if (depth < getDepth(target, pX, pY)) {
vec3_dup(val, bary);
// val[0] = ((float)depth) / 255.0f;
// val[1] = ((float)depth) / 255.0f;
// val[2] = ((float)depth) / 255.0f;
val[3] = ((float)depth) / 255.0f;
uint8_t depth = (uint8_t)(255.0 * fApplyBary(bary, (*p.points[0])[2],
(*p.points[1])[2],
(*p.points[2])[2]));
// if (depth < getDepth(target, x, y)) {
vApplyBary(bary, (*normals[0]), (*normals[1]), (*normals[2]), nor);
vApplyBary(bary, (*colors[0]), (*colors[1]), (*colors[2]), color);
set(target, pX, pY, val);
}
viewDir[0] = fX;
viewDir[1] = -fY;
viewDir[2] = 2.144663;
vec3_norm(viewDir, viewDir);
vec3_add(halfWay, viewDir, lightDir);
vec3_norm(halfWay, halfWay);
vec3_dup(val, color);
float diffuse = MAX(vec3_dot(nor, lightDir), 0.0f);
// float l = powf(MAX(vec3_dot(halfWay, nor), 0.0), 2.0);
float spec = powf(MAX(vec3_dot(halfWay, nor), 0.0), model->shininess);
spec *= (model->shininess + 8.0) / 25.1327;
// val[0] = nor[0] * 0.5 + 0.5;
// val[1] = nor[1] * 0.5 + 0.5;
// val[2] = nor[2] * 0.5 + 0.5;
// val[0] = ((float)depth) / 255.0f;
// val[1] = ((float)depth) / 255.0f;
// val[2] = ((float)depth) / 255.0f;
vec3_scale(val, val, diffuse * 0.5 + spec + 0.5);
// vec3_set(val, l);
val[3] = ((float)depth) / 255.0f;
set(target, x, y, val);
// }
}
}
}