~shockham/cligen

46b77dd7aac042da5e20a298f6150d2d334910d7 — shockham 1 year, 20 days ago fb5df2c feat/lint-and-fmt
fix: run fmt
2 files changed, 157 insertions(+), 133 deletions(-)

M main.c
M march.c
M main.c => main.c +23 -22
@@ 5,29 5,30 @@
#define HEIGHT 40
#define WIDTH 80

int main(void)
int
main(void)
{
    int i, j, n;
    double x, y, acc;
	int i, j, n;
	double x, y, acc;

    acc = 0.0;
	acc = 0.0;

    while(1) {
        for (i = 1; i < HEIGHT; i++) {
            for (j = 1; j < WIDTH; j++) {
                x = (double)(M_PI * (i / (double)HEIGHT));
                y = (double)(M_PI * (j / (double)WIDTH));
                n = (0.5 + cos(x + acc) * 0.5 + sin(y + acc)) * (231.0 * tan(acc));
                // n = ((0.5 + cos(x)) * (0.5 + sin(y))) * 231;
                // n = ((1.0 + cos(x)) * (1.0 + sin(y))) * 231;
                // n = (((1.0 + cos(x)) * (1.0 + sin(y))) * 16) + 232;
                printf("\033[48;5;%dm ", abs(n));
            }
            printf("\033[0;0m\n");
        }
        acc += 0.001;
        printf("\033[40F\n");
    }
    
    return 0;
	while(1) {
		for(i = 1; i < HEIGHT; i++) {
			for(j = 1; j < WIDTH; j++) {
				x = (double)(M_PI * (i / (double)HEIGHT));
				y = (double)(M_PI * (j / (double)WIDTH));
				n = (0.5 + cos(x + acc) * 0.5 + sin(y + acc)) * (231.0 * tan(acc));
				// n = ((0.5 + cos(x)) * (0.5 + sin(y))) * 231;
				// n = ((1.0 + cos(x)) * (1.0 + sin(y))) * 231;
				// n = (((1.0 + cos(x)) * (1.0 + sin(y))) * 16) + 232;
				printf("\033[48;5;%dm ", abs(n));
			}
			printf("\033[0;0m\n");
		}
		acc += 0.001;
		printf("\033[40F\n");
	}

	return 0;
}

M march.c => march.c +134 -111
@@ 6,155 6,178 @@
#define WIDTH 100

typedef struct vec2 {
    float x;
    float y;
	float x;
	float y;
} vec2;

typedef struct vec3 {
    float x;
    float y;
    float z;
	float x;
	float y;
	float z;
} vec3;

float vec3_length(vec3 a) {
    return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
float
vec3_length(vec3 a)
{
	return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}

vec3 vec3_add(vec3 a, vec3 b) {
    return (vec3){ a.x + b.x, a.y + b.y, a.z + b.z };
vec3
vec3_add(vec3 a, vec3 b)
{
	return (vec3){a.x + b.x, a.y + b.y, a.z + b.z};
}

vec3 vec3_sub(vec3 a, vec3 b) {
    return (vec3){ a.x - b.x, a.y - b.y, a.z - b.z };
vec3
vec3_sub(vec3 a, vec3 b)
{
	return (vec3){a.x - b.x, a.y - b.y, a.z - b.z};
}

vec3 vec3_cross(vec3 a, vec3 b) {
    return (vec3){
        a.y * b.z - a.z * b.y,
        a.z * b.x - a.x * b.z,
        a.x * b.y - a.y * b.x
    };
vec3
vec3_cross(vec3 a, vec3 b)
{
	return (vec3){
		a.y * b.z - a.z * b.y,
		a.z * b.x - a.x * b.z,
		a.x * b.y - a.y * b.x};
}

float vec3_dot(vec3 a, vec3 b) {
    return (a.x * b.x +
            a.y * b.y +
            a.z * b.z);
float
vec3_dot(vec3 a, vec3 b)
{
	return (a.x * b.x +
		a.y * b.y +
		a.z * b.z);
}

vec3 vec3_normalize(vec3 a) {
    float norm, scale;
vec3
vec3_normalize(vec3 a)
{
	float norm, scale;

    norm = sqrt(vec3_dot(a, a));
	norm = sqrt(vec3_dot(a, a));

    if (norm == 0.0f) {
        return (vec3) { 0.0, 0.0, 0.0 };
    }
	if(norm == 0.0f) {
		return (vec3){0.0, 0.0, 0.0};
	}

    scale = 1.0f / norm;
    return (vec3){
        a.x * scale,
        a.y * scale,
        a.z * scale
    };
	scale = 1.0f / norm;
	return (vec3){
		a.x * scale,
		a.y * scale,
		a.z * scale};
}

vec3 vec3_scalar_mul(vec3 a, float b) {
    return (vec3){ a.x * b, a.y * b, a.z * b };
vec3
vec3_scalar_mul(vec3 a, float b)
{
	return (vec3){a.x * b, a.y * b, a.z * b};
}

float sdSphere(vec3 p, float s) {
    return (vec3_length(p) - s);
float
sdSphere(vec3 p, float s)
{
	return (vec3_length(p) - s);
}

float sdPlane(vec3 p, float h) {
    return p.y + h;
float
sdPlane(vec3 p, float h)
{
	return p.y + h;
}

vec2 opUnion(vec2 d1, vec2 d2) {
    return d1.x < d2.x ? d1 : d2;
vec2
opUnion(vec2 d1, vec2 d2)
{
	return d1.x < d2.x ? d1 : d2;
}

float opDisp(vec3 p, float amt) {
    return sin(amt*p.x)*sin(amt*p.y)*sin(amt*p.z);
float
opDisp(vec3 p, float amt)
{
	return sin(amt * p.x) * sin(amt * p.y) * sin(amt * p.z);
}

vec2 scene(vec3 position) {
    vec2 sphere = { sdSphere(position, 0.6f) + opDisp(position, 3.0f), 10.0f };
    vec2 plane = { sdPlane(position, 0.6f), 10.0f };
    return opUnion(plane, sphere);
vec2
scene(vec3 position)
{
	vec2 sphere = {sdSphere(position, 0.6f) + opDisp(position, 3.0f), 10.0f};
	vec2 plane = {sdPlane(position, 0.6f), 10.0f};
	return opUnion(plane, sphere);
}

vec2 traceRay(vec3 origin, vec3 direction)
vec2
traceRay(vec3 origin, vec3 direction)
{
    float material = -1.0f;
	float material = -1.0f;

    float t = 0.02f;
	float t = 0.02f;

    for (int i = 0; i < 1000; i++) {
        vec2 hit = scene(vec3_add(origin, vec3_scalar_mul(direction, t)));
        if (hit.x < 0.002f || t > 20.0f) break;
        t += hit.x;
        material = hit.y;
    }
	for(int i = 0; i < 1000; i++) {
		vec2 hit = scene(vec3_add(origin, vec3_scalar_mul(direction, t)));
		if(hit.x < 0.002f || t > 20.0f) break;
		t += hit.x;
		material = hit.y;
	}

    if (t > 20.0f) {
        material = -1.0f;
    }
	if(t > 20.0f) {
		material = -1.0f;
	}

    return (vec2){ t, material };
	return (vec2){t, material};
}

int main(void)
int
main(void)
{
    int i, j;
    float time;

    // camera stuff
    vec3 origin = { 0.0f, 0.0f, 0.0f };
    vec3 target = { 0.0f, 0.0f, 0.0f };
    //printf("origin: (%f, %f, %f)", origin.x, origin.y, origin.z);

    time = 0.0;

    while(1) {
        printf("frame:%f | cam: (%f, %f, %f)\n", time, origin.x, origin.y, origin.z);
        // cam mat3
        vec3 forward = vec3_normalize(vec3_sub(target, origin));
        vec3 orientation = (vec3){ 0.0f, 1.0f, 0.0f };
        vec3 left = vec3_normalize(vec3_cross(forward, orientation));
        vec3 up = vec3_normalize(vec3_cross(left, forward));

        for (i = HEIGHT; i > 1; i--) {
            for (j = 1; j < WIDTH; j++) {
                vec3 p, direction;
                vec2 out;

                p = vec3_normalize((vec3){ 
                    (float)j / (float)WIDTH - 0.5f, 
                    (float)i / (float)HEIGHT - 0.5f, 
                    2.0f 
                });
                direction = (vec3){
                    left.x * p.x + up.x * p.x + forward.x * p.x,
                    left.y * p.y + up.y * p.y + forward.y * p.y,
                    left.z * p.z + up.z * p.z + forward.z * p.z,
                };
                out = traceRay(origin, direction);
                // output
                if (out.y >= 0.0f) {
                    printf("\033[48;5;%dm ", 231 + (int)out.x);
                } else {
                    printf("\033[48;5;0m ");
                }
            }
            printf("\033[0;0m\n");
        }
        time += 0.002f;
        //origin.x = 2.0f * cos(time);
        origin.z = 5.0 + 2.0f * sin(time);
        printf("\033[%dF\n", HEIGHT + 1);
    }

    return 0;
	int i, j;
	float time;

	// camera stuff
	vec3 origin = {0.0f, 0.0f, 0.0f};
	vec3 target = {0.0f, 0.0f, 0.0f};
	//printf("origin: (%f, %f, %f)", origin.x, origin.y, origin.z);

	time = 0.0;

	while(1) {
		printf("frame:%f | cam: (%f, %f, %f)\n", time, origin.x, origin.y, origin.z);
		// cam mat3
		vec3 forward = vec3_normalize(vec3_sub(target, origin));
		vec3 orientation = (vec3){0.0f, 1.0f, 0.0f};
		vec3 left = vec3_normalize(vec3_cross(forward, orientation));
		vec3 up = vec3_normalize(vec3_cross(left, forward));

		for(i = HEIGHT; i > 1; i--) {
			for(j = 1; j < WIDTH; j++) {
				vec3 p, direction;
				vec2 out;

				p = vec3_normalize((vec3){
					(float)j / (float)WIDTH - 0.5f,
					(float)i / (float)HEIGHT - 0.5f,
					2.0f});
				direction = (vec3){
					left.x * p.x + up.x * p.x + forward.x * p.x,
					left.y * p.y + up.y * p.y + forward.y * p.y,
					left.z * p.z + up.z * p.z + forward.z * p.z,
				};
				out = traceRay(origin, direction);
				// output
				if(out.y >= 0.0f) {
					printf("\033[48;5;%dm ", 231 + (int)out.x);
				} else {
					printf("\033[48;5;0m ");
				}
			}
			printf("\033[0;0m\n");
		}
		time += 0.002f;
		//origin.x = 2.0f * cos(time);
		origin.z = 5.0 + 2.0f * sin(time);
		printf("\033[%dF\n", HEIGHT + 1);
	}

	return 0;
}