Skip to content
Open
Binary file added .DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"files.associations": {
"stdio.h": "c",
"trace.h": "c",
"utils.h": "c",
"structures.h": "c",
"print.h": "c",
"scene.h": "c",
"__hash_table": "c",
"__string": "c",
"array": "c",
"bitset": "c",
"memory": "c",
"optional": "c",
"string_view": "c",
"tuple": "c"
}
}
Binary file added sumsong/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions sumsong/include/print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef PRINT_H
# define PRINT_H

# include <stdio.h>
# include "structures.h"

void write_color(t_color3 pixel_color);

#endif
14 changes: 14 additions & 0 deletions sumsong/include/scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SCENE_H
# define SCENE_H

# include "structures.h"
# include "utils.h"

t_canvas canvas(int width, int height);
t_camera camera(t_canvas *canvas, t_point3 origin);
t_object *object(t_object_type type, void *element, t_color3 albedo);
t_sphere *sphere(t_point3 center, double radius);
t_triangle *triangle(t_point3 dot1, t_point3 dot2, t_point3 dot3);
t_light *light_point(t_point3 light_origin, t_color3 light_color, double bright_ratio);

#endif
119 changes: 119 additions & 0 deletions sumsong/include/structures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#ifndef STRUCTURES_H
# define STRUCTURES_H

typedef struct s_vec3 t_vec3;
typedef struct s_vec3 t_point3;
typedef struct s_vec3 t_color3;

// 1. 레이 구조체
typedef struct s_ray t_ray;

// 2. 장면 구조체
typedef struct s_camera t_camera;
typedef struct s_canvas t_canvas;
typedef struct s_scene t_scene;

// 3. 오브젝트 구조체
typedef struct s_object t_object;
typedef struct s_light t_light;
typedef struct s_sphere t_sphere;
typedef struct s_triangle t_triangle;

// 4. 식별자 매크로
typedef int t_bool;
# define FALSE 0
# define TRUE 1
typedef int t_object_type;
# define LIGHT_POINT 1
# define EPSILON 1e-6 // 0.000001
# define LUMEN 3 // 장면의 밝기
# define SP 0 // 구 sphere
# define TR 4 // 삼각형 triangle

typedef struct s_hit_record t_hit_record;
typedef struct s_object t_object;

struct s_vec3
{
double x;
double y;
double z;
};

struct s_ray
{
t_point3 orig; // 광원 원점
t_vec3 dir; // 광선 단위벡터
};

struct s_camera
{
t_point3 orig; // 카메라 원점(위치)
double viewport_h; // 뷰포트 세로길이
double viewport_w; // 뷰포트 가로길이
t_vec3 horizontal; // 수평길이 벡터
t_vec3 vertical; // 수직길이 벡터
double focal_len; // focal length
t_point3 left_bottom; // 왼쪽 아래 코너점
};

struct s_canvas
{
int width; // canvas width
int height; // canvas height;
double aspect_ratio; // 종횡비
};

struct s_hit_record
{
t_point3 p; // 교점의 좌표
t_vec3 normal; // 교점에서의 법선
double tmin;
double tmax;
double t; // 광원과 교점 사이의 거리
t_bool front_face;
t_color3 albedo; // 반사율
};

struct s_scene
{
t_canvas canvas;
t_camera camera;
t_object *world;
t_object *light;
t_color3 ambient; // 8.4에서 설명할 요소
t_ray ray;
t_hit_record rec;
};

// 오브젝트 구조체
struct s_object
{
t_object_type type; // 도형 타입
void *element; // 도형 데이터 저장할 곳
void *next;
t_color3 albedo;
};

struct s_light
{
t_point3 origin; // 광원의 원점
t_color3 light_color; // 광원의 색
double bright_ratio; // 밝기 정보
};

struct s_sphere
{
t_point3 center; // 구 중심
double radius; // 구 반지름
double radius2; // 구 반지름 제곱
};

struct s_triangle
{
t_point3 dot1;
t_point3 dot2;
t_point3 dot3;
};

#endif
23 changes: 23 additions & 0 deletions sumsong/include/trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TRACE_H
# define TRACE_H

# include "structures.h"
# include "utils.h"

// trace/ray/
t_ray ray(t_point3 orig, t_vec3 dir);
t_point3 ray_at(t_ray *ray, double t);
t_ray ray_primary(t_camera *cam, double u, double v);
t_color3 ray_color(t_scene *scene);
t_color3 phong_lighting(t_scene *scene);
t_color3 point_light_get(t_scene *scene, t_light *light);
t_bool in_shadow(t_object *objs, t_ray light_ray, double light_len);

// trace/hit/
t_bool hit(t_object *obj, t_ray *ray, t_hit_record *rec);
t_bool hit_obj(t_object *obj, t_ray *ray, t_hit_record *rec);
t_bool hit_sphere(t_object *sp_obj, t_ray *ray, t_hit_record *rec);
t_bool hit_triangle(t_object *tr_obj, t_ray *ray, t_hit_record *rec);
void set_face_normal(t_ray *r, t_hit_record *rec);

#endif
32 changes: 32 additions & 0 deletions sumsong/include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef UTILS_H
# define UTILS_H

# include <math.h>
# include <stdlib.h>
# include <stdio.h>
# include "structures.h"

// 벡터 유틸리티
t_vec3 vec3(double x, double y, double z);
t_point3 point3(double x, double y, double z);
t_point3 color3(double r, double g, double b);
void vset(t_vec3 *vec, double x, double y, double z);
double vlength2(t_vec3 vec);
double vlength(t_vec3 vec);
t_vec3 vplus(t_vec3 vec, t_vec3 vec2);
t_vec3 vplus_(t_vec3 vec, double x, double y, double z);
t_vec3 vminus(t_vec3 vec, t_vec3 vec2);
t_vec3 vminus_(t_vec3 vec, double x, double y, double z);
t_vec3 vmult(t_vec3 vec, double t);
t_vec3 vmult_(t_vec3 vec, t_vec3 vec2);
t_vec3 vdivide(t_vec3 vec, double t);
double vdot(t_vec3 vec, t_vec3 vec2);
t_vec3 vcross(t_vec3 vec, t_vec3 vec2);
t_vec3 vunit(t_vec3 vec);
t_vec3 vmin(t_vec3 vec1, t_vec3 vec2);

// 오브젝트 유틸리티
void oadd(t_object **list, t_object *new);
t_object *olast(t_object *list);

#endif
Binary file added sumsong/src/.DS_Store
Binary file not shown.
63 changes: 63 additions & 0 deletions sumsong/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include "../include/structures.h"
#include "../include/utils.h"
#include "../include/print.h"
#include "../include/scene.h"
#include "../include/trace.h"

t_scene *scene_init(void)
{
t_scene *scene;
t_object *world;
t_object *lights;
double ka; // 8.4 에서 설명

// malloc 할당 실패 시, 실습에서는 return NULL로 해두었지만, 적절한 에러 처리가 필요하다.
if(!(scene = (t_scene *)malloc(sizeof(t_scene))))
return (NULL);
scene->canvas = canvas(400, 300);
scene->camera = camera(&scene->canvas, point3(0, 0, 0));
world = object(SP, sphere(point3(0, -1000, 0), 995), color3(0.5, 0, 0.5)); // world 에 구3 추가
oadd(&world, object(SP, sphere(point3(-2, 0, -5), 2), color3(0.5, 0, 0))); // world 에 구1 추가
oadd(&world, object(SP, sphere(point3(2, 0, -5), 2), color3(0, 0.5, 0))); // world 에 구2 추가
oadd(&world, object(TR, triangle(point3(-2, 0, -3), point3(2, 1, -3), point3(1, 1, -1)), color3(0, 0, 0.5)));
scene->world = world;
lights = object(LIGHT_POINT, light_point(point3(0, 20, 0), color3(1, 1, 1), 0.5), color3(0, 0, 0)); // 더미 albedo
scene->light = lights;
ka = 0.1; // 주변광 강도 계수
scene->ambient = vmult(color3(1,1,1), ka); // 8.4 에서 설명
return (scene);
}

int main(void)
{
int i;
int j;
double u;
double v;
t_color3 pixel_color;
t_scene *scene;

// Scene setting
scene = scene_init();
// 랜더링
// P3 는 색상값이 아스키코드라는 뜻, 그리고 다음 줄은 캔버스의 가로, 세로 픽셀 수, 마지막은 사용할 색상값
printf("P3\n%d %d\n255\n", scene->canvas.width, scene->canvas.height);
j = scene->canvas.height - 1;
while (j >= 0)
{
i = 0;
while (i < scene->canvas.width)
{
u = (double)i / (scene->canvas.width - 1);
v = (double)j / (scene->canvas.height - 1);
// ray from camera origin to pixel (u, v)
scene->ray = ray_primary(&scene->camera, u, v);
pixel_color = ray_color(scene);
write_color(pixel_color);
++i;
}
--j;
}
return (0);
}
9 changes: 9 additions & 0 deletions sumsong/src/print/print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "../../include/print.h"

// [0,1] 로 되어있는 rgb 값을 각각 [0,255]에 맵핑 해서 출력.
void write_color(t_color3 pixel_color)
{
printf("%d %d %d\n", (int)(255.999 * pixel_color.x),
(int)(255.999 * pixel_color.y),
(int)(255.999 * pixel_color.z));
}
12 changes: 12 additions & 0 deletions sumsong/src/scene/canvas.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "../../include/scene.h"

// canvas 생성자 함수
t_canvas canvas(int width, int height)
{
t_canvas canvas;

canvas.width = width;
canvas.height = height;
canvas.aspect_ratio = (double)width / (double)height; // 캔버스의 종횡비
return (canvas);
}
54 changes: 54 additions & 0 deletions sumsong/src/scene/object_create.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "../../include/structures.h"
#include "../../include/scene.h"

t_object *object(t_object_type type, void *element, t_color3 albedo)
{
t_object *new;

if (!(new = (t_object *)malloc(sizeof(t_object))))
return (NULL);
new->type = type;
new->element = element;
new->next = NULL;
new->albedo = albedo;
return (new);
}

// 광원 생성자 함수
t_light *light_point(t_point3 light_origin, t_color3 light_color, double bright_ratio)
{
t_light *light;

if(!(light = (t_light *)malloc(sizeof(t_light))))
return (NULL);
light->origin = light_origin;
light->light_color = light_color;
light->bright_ratio = bright_ratio;
return (light);
}

// 구 생성자 함수
t_sphere *sphere(t_point3 center, double radius)
{
t_sphere *sp;

if(!(sp = (t_sphere *)malloc(sizeof(t_sphere))))
return (NULL);
sp->center = center;
sp->radius = radius;
sp->radius2 = radius * radius;
return (sp);
}

// 삼각형 생성자 함수
t_triangle *triangle(t_point3 dot1, t_point3 dot2, t_point3 dot3)
{
t_triangle *tri;

if ((tri = malloc(sizeof(t_triangle))) == NULL)
return (NULL);
tri->dot1 = dot1;
tri->dot2 = dot2;
tri->dot3 = dot3;
return (tri);
}
Loading