~shockham/cligen

39729599bfe804ea8765360a319eb529168203ea — shockham 10 months ago 4273b86 + 46b77dd
Merge branch 'feat/lint-and-fmt' into main and fix conflicts
4 files changed, 221 insertions(+), 167 deletions(-)

A .clang-format
M Makefile
M main.c
M march.c
A .clang-format => .clang-format +22 -0
@@ 0,0 1,22 @@
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AlignOperands: DontAlign
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: TopLevel
BreakBeforeTernaryOperators: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: WebKit
IndentCaseLabels: false
TabWidth: 4
IndentWidth: 4
ContinuationIndentWidth: 4
UseTab: ForContinuationAndIndentation
ColumnLimit: 0
ReflowComments: false
SortIncludes: false
SpaceBeforeParens: false

M Makefile => Makefile +6 -0
@@ 12,5 12,11 @@ compress:
run:
	./$(BIN)

lint:
	cppcheck --enable=all --inconclusive --library=posix --std=c89 --suppress=missingIncludeSystem .

format:
	clang-format -i *.c

clean:
	rm ./$(BIN)

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 +170 -145
@@ 7,194 7,219 @@
#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.5f), 10.0f };
    vec2 plane = { sdPlane(position, 0.8f), 10.0f };
    return opUnion(plane, sphere);
vec2
scene(vec3 position)
{
	vec2 sphere = {sdSphere(position, 0.6f) + opDisp(position, 3.5f), 10.0f};
	vec2 plane = {sdPlane(position, 0.8f), 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};
}

vec3 cam;

void input(void)
void
input(void)
{
    // camera movement
    while (1) {
        char ch = fgetc(stdin);
        switch(ch) {
            case 'w':
                cam.z -= 1.0f;
                printf("\033[%dF\n", 1);
                break;
            case 's':
                cam.z += 1.0f;
                printf("\033[%dF\n", 1);
                break;
            case 'a':
                cam.x -= 1.0f;
                printf("\033[%dF\n", 1);
                break;
            case 'd':
                cam.x += 1.0f;
                printf("\033[%dF\n", 1);
                break;
        }
    }
	// camera movement
	while(1) {
		char ch = fgetc(stdin);
		switch(ch) {
		case 'w':
			cam.z -= 1.0f;
			printf("\033[%dF\n", 1);
			break;
		case 's':
			cam.z += 1.0f;
			printf("\033[%dF\n", 1);
			break;
		case 'a':
			cam.x -= 1.0f;
			printf("\033[%dF\n", 1);
			break;
		case 'd':
			cam.x += 1.0f;
			printf("\033[%dF\n", 1);
			break;
		}
	}
}

int main(void)
int
main(void)
{
    int i, j, n, err;
    float time;
    pthread_t thread;

    // camera stuff
    vec3 origin = { 0.0f, 0.0f, 0.0f };
    vec3 target = { 0.0f, 0.0f, 0.0f };
    cam = (vec3){ 0.0f, 0.0f, 5.0 + 2.0f * sin(time) };
    
    err = pthread_create(&thread, NULL, input, NULL);
    if (err) {
        printf("An error occured: %d", err);
        return 1;
    }

    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 = cam.z + 2.0f * sin(time);
        origin.x = cam.x;

        printf("\033[%dF\n", HEIGHT + 1);
    }

    pthread_join(thread, NULL);
    return 0;
	int i, j, err;
	float time;
	pthread_t thread;

	// camera stuff
    time = 0.0f;
	vec3 origin = {0.0f, 0.0f, 0.0f};
	vec3 target = {0.0f, 0.0f, 0.0f};
	cam = (vec3){0.0f, 0.0f, 5.0 + 2.0f * sin(time)};

	err = pthread_create(&thread, NULL, input, NULL);
	if(err) {
		printf("An error occured: %d", err);
		return 1;
	}

	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 = cam.z + 2.0f * sin(time);
		origin.x = cam.x;

		printf("\033[%dF\n", HEIGHT + 1);
	}

	pthread_join(thread, NULL);
	return 0;
}